Local File Inclusion (LFI)
Local file inclusion means unauthorized access to files on the system. This vulnerability lets the attacker gain access to sensitive files on the server, and it might also lead to gaining a shell.
How does it work?
The vulnerability stems from unsanitized user-input. LFI is particularly common in php-sites.
Here is an example of php-code vulnerable to LFI. As you can see we just pass in the url-parameter into the require-function without any sanitization. So the user can just add the path to any file.
In this example the user could just enter this string and retrieve the /etc/passwd
file.
Bypassing the added .php and other extra file-endings
It is common to add the file-extension through the php-code. Here is how this would look like:
The php is added to the filename, this will mean that we will not be able to find the files we are looking for. Since the file /etc/passwd.php
does not exist. However, if we add the nullbyte to the end of our attack-string the .php
will not be taken into account. So we add %00
to the end of our attack-string.
This technique is usually called the nullbyte technique since %00
is the nullbyte. The technique only works in versions below php 5.3. So look out for that.
Another way to deal with this problem is just to add a question mark to your attack-string. This way the stuff after gets interpreted as a parameter and therefore excluded. Here is an example:
Bypassing php-execution
So if you have an LFI you can easily read .txt
-files but not .php
files. That is because they get executed by the webserver, since their file-ending says that it contains code. This can be bypassed by using a build-in php-filter.
Here you use a php-filter to convert it all into base64. So in return you get the whole page base64 encoded. Now you only need to decode it. Save the base64-text into a file and then run:
Linux
Tricks
Download config-files in a nice style-format
If you read files straight in the browser the styling can becomes unbearable. Really difficult to read. A way around it is to download the files from the terminal. But that won't work if there is a login that is blocking it. So this is a great workaround:
Sensitive file
This is the default layout of important apache files. https://wiki.apache.org/httpd/DistrosDefaultLayout
Comes from here: https://gist.github.com/sckalath/a8fd4e754a72015aa0b8
Web server files
SSH
Logs
User specific files
Found in the home-directory
Proc files
"Under Linux, /proc includes a directory for each running process, including kernel processes, in directories named /proc/PID, where PID is the process number. Each directory contains information about one process, including: /proc/PID/cmdline, the command that originally started the process."
https://en.wikipedia.org/wiki/Procfs
https://blog.netspi.com/directory-traversal-file-inclusion-proc-file-system/
Bruteforcing SSH known_hosts
https://blog.rootshell.be/2010/11/03/bruteforcing-ssh-known_hosts-files/
LFI to shell
Under the right circumstances you might be able to get a shell from a LFI
Log poisoning
There are some requirements. We need to be able to read log files. In this example we are going to poison the apache log file. You can use either the success.log or the error.log
So once you have found a LFI vuln you have to inject php-code into the log file and then execute it.
Insert php-code into the log file
This can be done with nc or telnet.
You can also add it to the error-log by making a request to a page that doesn't exists
Or in the referer parameter.
Execute it in the browser
Now you can request the log-file through the LFI and see the php-code get executed.
Proc files
If you can read the proc-files on the system you might be able to poison them through the user-agent.
We can also inject code into /proc/self/environ through the user-agent
https://www.exploit-db.com/papers/12992/
https://www.youtube.com/watch?v=ttTVNcPnsJY
Windows
Fingerprinting
Logs
Common path for apache log files on windows:
PHP Session Locations
Retrieving password hashes
In order to retrieve the systems password hashed we need two files: system and SAM. Once you have those two files you can extract the hased using the kali tool pwdump, like this:
The system and SAM files can be found in different locations, so try them all. From a webserver the path might be case-sensitive, even though it is windows. So consider that!
References:
This is the definitive guide to Local File inclusion https://highon.coffee/blog/lfi-cheat-sheet/
And this http://securityidiots.com/Web-Pentest/LFI
And this:
https://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
Last updated