Most cases webapps will protect against file inclusion.
Still, unless webapp is propertly secure against malicious LFI user input we can bypass protections
Non-Recursive path traversal filters
Most basic filter against LFI is a search and replace filter - simply deleting substrings of ../ to avoid path traversal
$language = str_replace('../', '', $_GET['language']);
This filter is insecure - It's not recursively removing the ../ substring, it runs a single time on the input string and does not apply the filter on the output string. for example, if we try ....// as payload, filter would remove ../ and output string remains ../
So, we can still get LFI with something like ....//....//....//....//etc/passwd
we may use ..././ or ....\/ and several other recursive LFI payloads.
- In some cases escaping the forward slash character may also work to avoid path traversal filters (
....\/) or adding extra forward slashes (....////)
Encoding payloads for lfi
Webfilters may implement input filters that include certain lfi related characters, like . or /
- Just URL encode your input
Core PHP filters on v5.3.4 and earlier were specifically vulnerable to this
Even on newer versions we may find custom filters that may be bypassed through URL encoding.
../ becomes %2e%2e%2f - Use burpsuite encoding tool
Also possible to use burp decoder to encode the encoded string again to double encode, and also bypass other types of filters.
Refer to command injection module for more about bypassing blacklisted characters - this can be used in conjunction with lfi
Approved paths
Webapps may use RegExps to ensure file being included is under a specific path.
if(preg_match('/^\.\/languages\/.+$/', $_GET['language'])) {
include($_GET['language']);
} else {
echo 'Illegal path specified!';
}
This code ensures we're looking at the ./languages directory
To find approved path we can examine requests sent by existing forms and see what path they use for normal web functionality , and we can fuzz web directories under the same path / try different ones until we get a match.
To bypass approved paths we can use path traversal but starting with the approved path and use ../ to return to the root directory
i.e. ./languages/../../../../etc/passwd
Some webapps may apply this filter along with the other one seen in previous notes so we can combine techniques, and url encode the payload or use recursive payload
All techniques mentioned so far should work with any LFI vulnerability regardless of backend dev lang or framework
Appended extensions, LFI
Some webapps append an extension to input string. .php for example to ensure the file we include is in the expected extension
Modern PHP we cannot bypass this and only restricted to files with that extension - which may be useful for reading source code
The below techniques are OBSOLETE with modern PHP and only work with >5.3/5.4
- Path Truncation
Defined strings have max length of 4096 chars - likely due to 32 bit system limitations
If a longer string is passed it will be simply truncated and any chars after max is ignored
PHP also used to remove trailing slashes and single dots in path names so if we call /etc/passwd/. then /. is truncated, then php calls /etc/passwd.
PHP, linux in general, disregard multiple slashes in path ////etc/passwd is the same as /etc/passwd
Current directory shortcut in middle of path would also be disregarded
/etc/./passwd
Combining these limitations we can create very long strings that evaluate to a correct path. Whenever we reach 4096 char limitation, appended extension would be truncated and we have a path without an appended extension. It's important to note we would need to START PATH WITH NONEXISTING DIRECTORY for this technique to work.
?language=non_existing_directory/../../../etc/passwd/./././.[./ REPEATED ~2048 times]
bash script to automate creation of the string
echo -n "non_existing_directory/../../../etc/passwd/" && for i in {1..2048}; do echo -n "./"; done
Null byte injection
PHP > 5.5 vulnerable to null byte injection
- adding
%00(null byte) at the end of a string would terminate the string and not consider anythingafter it. - This is due to how strings are handled in lowlevel memory - strings in memory must use a null byte to indicate the end of a string as seen in ASM, C, and C++ langs
- To exploit this vulnerability end the payload with a nullbyte
/etc/passwd%00such that final path passed to include is:/etc/passwd%00.php- This way even though .php is appended - null byte terminates ending strings appended