LFI & RFI
General Information
Template Engines
The common place we usually find LFI within is template engines. These keep the static parts of the web application the same when navigating between different pages. For example, /index.php?page=about'
:
/index.php
sets the static content?page=about
pulls the dynamic content, e.g.about.php
Suggestive Parameters
A suggestive parameter is a parameter that hints at what it does via its name and it makes a good candidate for directory traversal flaws.
Files
On Windows hosts the
C:\
should be ommited, as if not it would end up searching for//C:/
.
/etc/passwd
/windows/win.ini
/etc/hosts
/windows/system32/drivers/etc/hosts
LFI
This can lead to the exposure of sensitive information, execution of malicious scripts, and potentially complete server compromise.
Validate and sanitize user inputs, restrict file paths to trusted directories, and use proper access controls to prevent unauthorized file access.
Relative Path
Absolute Path
The absolute path can be used when the application's code use the whole input to read a file. This allows the attacker, given that no LFI filters are implemented, to read a file directly.
RFI
This can result in remote code execution, data theft, and complete control over the compromised server.
Disable remote file includes, validate and sanitize user inputs, and configure the server to only allow the inclusion of local files from trusted directories.
In the example below, it is possible to access remote files using the application's filename
parameter (Figure 5).
Filters
File Traversal Filters
Once common defense against FI attacks is by using Search and Replace filters which remove all instances of path traversal sequences (Figure 6.1). If these filters are non-recursive, i.e., run just a single time on the input string, it can be bypassed by doubling up the traversal sequence (Figure 6.2).
Approved Paths
A common protective technique against LFI is the use of regular expressions to ensure that the file being included is under a defined directory. This can be easily bypassed by including the required directory within the payload (Figure 7).
Filename Prefix
Using /../
(the directory may not exist and may break some FI techniques, such as PHP wrappers/filters).
Appended Extensions
Path Truncation (ealier PHP versions)
Max strings length is
4096
chars -> everything longer is truncated.Trailing
/
and.
in path names are truncated (/etc/passwd/.
->/etc/passwd
).Both PHP and Linux disregard multiple
/
in the path (////etc/passwd
=/etc/passwd
).Similarly, a current directory shortcut (
.
) in the middle of the path would also be disregarded.
Based on the those characteristics, we can create very long strings that evaluate to the target path.
For this technique to work, we need to start the path with a non-existing directory.
Null Byte (PHP < 5.3.4)
Appended extensions can be also bypassed with the use of a null byte (%00
or 0x00
).
Other Techniques
PHP Filters
Read the source code of PHP files.
Second-Order Attacks
The goal here is to poison a database entry, such as the registration name, so another web application functionality gets tricked. This type of attack is often overlooked by the developers, as values pulled from the application's database is generally trusted. For example, if a web application provides the option to download our avatar via /profile/$username/avatar.png
, we could try crafting a malicious LFI-based username, such as ../../../etc/passwd
.
Last updated
Was this helpful?