Exploitation - Local and remote file inclusions
The File Inclusion vulnerability allows for the include of a file locally present on the server or remotely hosted on a webserver, usually exploiting a "dynamic file inclusion" mechanisms implemented in the target application.
This issue is caused when an application builds a path to executable code using an attacker-controlled variable in a way that allows the attacker to control which file is executed at run time. The vulnerability occurs due to the use of user-supplied input without proper validation.
A file include vulnerability is distinct from a generic Directory Traversal Attack, in that directory traversal is a way of gaining unauthorized file system access, and a file inclusion vulnerability subverts how an application loads code for execution.
File Inclusion vulnerability detection
In black box testing, look for scripts which take filenames as parameters, such as the following examples:
Try to access freely accessible files, such as:
File access
Basic
Typical proof-of-concept for basic LFI would be:
Files of interest
The following Linux files can be of interest:
Null Byte
In some cases, the server will append an extension at the end of the requested file, such as ".php”.
Such mechanisms can be bypassed using the line terminator char null byte:
Double encoding
Encoding parameters twice in hexadecimal format can be leveraged to bypass security controls such as blacklist filters. For example, the '.' (dot) character represent %2E in hexadecimal representation. When the % symbol is encoded again, its representation in hexadecimal code is %25. The result from the double encoding process is %252E.
Filter bypass
Some blacklist filter may be implemented and restrict the usage of "../". Those kind of filter may be passed using the following payloads:
PHP wrappers
php://filter This PHP wrapper can be used to access the source code of the page:
Remote Code Execution
Basic remote file inclusion
If a File Inclusion vulnerability can be used to include remote files, the vulnerability could be leveraged to make the web application retrieve and execute a remote script.
The included script must be in a language supported by the web application. Refer to the [General] Shell
note for web and reverse shell payloads.
File upload
If the web application exposes an upload functionality, the uploaded files could be leveraged through a local file inclusion to execute code on the target.
If no filter restrict the uploaded files type, the uploaded files should be in a language supported by the web application to achieve code execution. Refer to the [General] Shell
note for web and reverse shell payloads.
Multiples filters can however limit the files type accepted by the web application
client-side restrictions using
JavaScript
, etc.HTTP
Content-Type
verificationfile extension black or white listing
Knowledge of the uploaded files path could be needed in order to include the files through the local file inclusion vulnerability if the upload directory is different from the file inclusion vulnerability working directory. The upload directory path may be leaked by the web application, usually in error messages or in settings / configuration web pages.
Refer to the [WebApps] File upload
note for more information on how to bypass upload filters and retrieve the uploaded file path.
PHP wrappers
data
The PHP data:// wrapper allows for the injection and execution of PHP code given in parameter:
expect
The PHP expect:// wrapper allows execution of system commands but is not enabled by default.
input
The PHP input:// wrapper allows for the injection and execution of PHP code given in the POST request body:
zip / phar
The zip:// and phar:// wrappers can be used to achive code execution if a zip or phar archive can be upload to the server:
Log files contamination
Code can be injected into log files, principle known as log files contamination, in order to be executed whenever accessing the log files through a LFI.
On Linux, the log files of the Apache, SSH, FTP or SMTP services can be targeted.
Apache
The server access log records all requests processed by the server and the server error log contains any errors encountered while processing requests.
The access log may record the request GET parameters as well as the User-Agent and Referer HTTP headers.
Depending on the configured level of error logging enabled on the server, the error log files may record the URI for missing requested resources (HTTP error 404).
Possible httpd.conf
locations, in which access, error or custom log files locations may be defined:
Additionally, the following locations could be used for the Apache access / error log files:
SSH / FTP
Every connection attempts to the SSH or FTP services may be logged. As the username specified for the connection attempt is recorded, it can be used as an injection point.
Default configuration files locations that define the SSH / FTP services log files location:
rsyslog
, an open-source software utility used on UNIX and Unix-like computer systems, may be deployed on the targeted system to centralize logs. The utility configuration is defined in the /etc/rsyslog.conf
configuration file and the authentication log policies are specified in the sub-system auth.*
SMTP Log Poisioning
Information about the emails sent through the service may be logged. By default Postfix notably includes email addresses of the sender and recipients.
Refer to the L7 SMTP - Methodology
note for more information on how to manually send emails through a SMTP service.
Proc environ or files descriptor injection
The /proc/self/environ
file contains the environment variables of the current process and notably, for an Apache process, the variable HTTP_USER_AGENT
set from the User-Agent requests. Making requests containing code in the HTTP User-Agent header and accessing the /proc/self/environ
file through a LFI could result in code execution. Note that on modern Linux systems this file is not accessible to unprivileged users.
Environment variables of others processes can also be accessed by privileged users using /proc/<PID>/environ
and could also be exploited, given the web service is running under elevated privileges and the environment variables of an other process can be populated through user controlled parameter.
The /proc/self/fd/<FD_ID>
and /proc/<PID>/fd/<FD_ID>
files references the pipes or sockets opened respectively by the current and the specified process. Theses files are used for inter process communication and may include content populated through user controlled parameters. If such content can be identified, for example the logging of requests processed or active session cookies defined, a LFI vulnerability could be leveraged into code execution.
PHP sessions
By default, PHP stores information about the active sessions in /var/lib/php/sess_<PHPSESSID>
or /var/lib/php5/sess_<PHPSESSID>
(PHP5). For instance the session PHPSESSID=uv06hljg4igcfkf3nsk9tsjle5
would be stored in /var/lib/php5/sess_uv06hljg4igcfkf3nsk9tsjle5
.
The default session files locations can be modified using the session.save_path
configuration directive in the php.ini
configuration file or at runtime using ini_set('session.save_path', '/path/sessions/folder')
.
phpinfo()
If available, a page executing and returning the PHP's phpinfo()
function could be used to leverage a LFI vulnerability into remote code execution.
The phpinfo()
function returns the currently defined variables in its scope, notably including the values sent through the request GET
or POST
parameters and the uploaded file _FILES
, which contains the temporary file upload location on the file system tmp_name
. The temporary uploaded file only exists while the PHP processor is operating on the requested .php file, and is deleted at the end of processing. By making multiple large upload POST
requests to the phpinfo
script with a number of large HTTP headers, it is possible to increase the processing time in order to retrieve the name of a temporary file and access it through the LFI vulnerability before deletion.
More information about this exploit technique can be found here: https://insomniasec.com/downloads/publications/LFI%20With%20PHPInfo%20Assistance.pdf
Additionally, the LFISuite
tool presented below can be used to automatically exploit this technique.
Automatic exploit tools
Kadimus
LFISuite offers a more complete approach
Kadimus
is CLI tool to automatically scan and exploit Local File Inclusion vulnerabilities using the following methods of attack:
/var/log/auth.log RCE
/proc/self/environ RCE
php://input RCE
data://text RCE
LFISuite
LFISuite
is CLI multi-platforms (Windows, Linux and OS X) tool to automatically scan and exploit Local File Inclusion vulnerabilities. It provides a more exhaustive list of possible log files locations and also offers more attacks methods that Kadimus
:
/proc/self/environ
php://filter
php://input
/proc/self/fd
access log
phpinfo
data://
expect://
All methods above can be tried automatically by specifying the Auto-Hack
modality. If a LFI vulnerability is detected, the reverseshell
command can be used to start a reverse shell.
References
https://www.rcesecurity.com/2017/08/from-lfi-to-rce-via-php-sessions/ https://www.hackingarticles.in/smtp-log-poisioning-through-lfi-to-remote-code-exceution/ https://liberty-shell.com/sec/2018/05/19/poisoning/ https://medium.com/bugbountywriteup/bugbounty-journey-from-lfi-to-rce-how-a69afe5a0899 https://insomniasec.com/downloads/publications/LFI%20With%20PHPInfo%20Assistance.pdf
Last updated