LFI to RCE
Log Poisoning
LFI to RCE via Apache Log File Poisoning (PHP)
LFI to RCE via SSH Log File Poisoning (PHP)
LFI to RCE via SMTP Log File Poisoning (PHP)
Log Files
Proc Environ Injection
Our main target is to inject the /proc/self/environ
file from the HTTP Header: User-Agent
. This file hosts the initial environment of the Apache process. Thus, the environmental variable User-Agent
is likely to appear there.
Remote File Inclusion
In php this is disable by default because allow_url_include
is Off. It must be On for it to work, and in that case you could include a PHP file from your server and get RCE:
If for some reason allow_url_include
is On, but PHP is filtering access to external webpages, according to this post, you could use for example the data protocol with base64 to decode a b64 PHP code and egt RCE:
Copy
In the previous code, the final +.txt
was added because the attacker needed a string that ended in .txt
, so the string ends with it and after the b64 decode that part will return just junk and the real PHP code will be included (and therefore, executed).
Another example not using the php://
protocol would be:
Copy
Via Email
Send a mail to a internal account (user@localhost) containing your PHP payload like <?php echo system($_REQUEST["cmd"]); ?>
and try to include to the mail of the user with a path like /var/mail/<USERNAME>
or /var/spool/mail/<USERNAME>
Via /proc/*/fd/*
Upload a lot of shells (for example : 100)
Include http://example.com/index.php?page=/proc/$PID/fd/$FD, with $PID = PID of the process (can be brute forced) and $FD the file descriptor (can be brute forced too)
Via /proc/self/environ
Like a log file, send the payload in the User-Agent, it will be reflected inside the /proc/self/environ file
Via upload
If you can upload a file, just inject the shell payload in it (e.g : <?php system($_GET['c']); ?>
).
In order to keep the file readable it is best to inject into the metadata of the pictures/doc/pdf
Via Zip fie upload
Upload a ZIP file containing a PHP shell compressed and access:
Via PHP sessions
Check if the website use PHP Session (PHPSESSID)
In PHP these sessions are stored into /var/lib/php5/sess\[PHPSESSID]_ files
Set the cookie to <?php system('cat /etc/passwd');?>
Use the LFI to include the PHP session file
Via ssh
If ssh is active check which user is being used (/proc/self/status & /etc/passwd) and try to access <HOME>/.ssh/id_rsa
Via vsftpd logs
The logs for the FTP server vsftpd are located at /var/log/vsftpd.log. In the scenario where a Local File Inclusion (LFI) vulnerability exists, and access to an exposed vsftpd server is possible, the following steps can be considered:
Inject a PHP payload into the username field during the login process.
Post injection, utilize the LFI to retrieve the server logs from /var/log/vsftpd.log.
Via php base64 filter (using base64)
As shown in this article, PHP base64 filter just ignore Non-base64.You can use that to bypass the file extension check: if you supply base64 that ends with ".php", and it would just ignore the "." and append "php" to the base64. Here is an example payload
Via php filters (no file needed)
This writeup explains that you can use php filters to generate arbitrary content as output. Which basically means that you can generate arbitrary php code for the include without needing to write it into a file.
Via segmentation fault
Upload a file that will be stored as temporary in /tmp
, then in the same request, trigger a segmentation fault, and then the temporary file won't be deleted and you can search for it.
LFI2RCE via Segmentation Fault
Via Nginx temp file storage
If you found a Local File Inclusion and Nginx is running in front of PHP you might be able to obtain RCE with the following technique:
Via PHP_SESSION_UPLOAD_PROGRESS
If you found a Local File Inclusion even if you don't have a session and session.auto_start
is Off
. If you provide the PHP_SESSION_UPLOAD_PROGRESS
in multipart POST data, PHP will enable the session for you. You could abuse this to get RCE:
LFI2RCE via PHP_SESSION_UPLOAD_PROGRESS
Via temp file uploads in Windows
If you found a Local File Inclusion and and the server is running in Windows you might get RCE:
Via pearcmd.php
+ URL args
pearcmd.php
+ URL argsAs explained in this post, the script /usr/local/lib/phppearcmd.php
exists by default in php docker images. Moreover, it's possible to pass arguments to the script via the URL because it's indicated that if a URL param doesn't have an =
, it should be used as an argument.
The following request create a file in /tmp/hello.php
with the content <?=phpinfo()?>
:
The following abuses a CRLF vuln to get RCE (from here):
Via phpinfo() (file_uploads = on)
If you found a Local File Inclusion and a file exposing phpinfo() with file_uploads = on you can get RCE:
Via compress.zlib + PHP_STREAM_PREFER_STUDIO
+ Path Disclosure
PHP_STREAM_PREFER_STUDIO
+ Path DisclosureIf you found a Local File Inclusion and you can exfiltrate the path of the temp file BUT the server is checking if the file to be included has PHP marks, you can try to bypass that check with this Race Condition:
LFI2RCE Via compress.zlib + PHP_STREAM_PREFER_STUDIO + Path Disclosure
Via eternal waiting + bruteforce
If you can abuse the LFI to upload temporary files and make the server hang the PHP execution, you could then brute force filenames during hours to find the temporary file:
To Fatal Error
If you include any of the files /usr/bin/phar
, /usr/bin/phar7
, /usr/bin/phar.phar7
, /usr/bin/phar.phar
. (You need to include the same one 2 time to throw that error).
I don't know how is this useful but it might be. Even if you cause a PHP Fatal Error, PHP temporary files uploaded are deleted.
![](https://book.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F129538173-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-L_2uGJGU7AVNRcqRvEi%252Fuploads%252FJmw2mcXj7BplxfReaLxL%252Fimage.png%3Falt%3Dmedia%26token%3D898f2793-e077-44ee-88ef-6051474e49af&width=768&dpr=4&quality=100&sign=308cc3b6&sv=1)
Last updated
Was this helpful?