LFI
Local File Inclusion
Writable directories to target
/tmp/
/var/tmp/
/dev/shm/
/var/www/html/uploads/Session File Inclusion
Session files can be leveraged for LFI to RCE by poisoning session data.
Alternative: Create Your Own Session
Some applications allow you to set arbitrary session variables
Session File Format
Session files use a serialized format:
Custom Session Path
If you control session.save_path:
The %00 Null Terminator
This only works on PHP < 5.3.4 due to the Magic Quotes fix
Is often used to terminate a string prematurely, effectively allowing attackers to manipulate the filename or file extension.
In C-based languages (which PHP is written in), strings are null-terminated. The \0 character signals the end of a string.
Filter Bypasses
Encoding Bypasses
Extension Appending Bypasses
Many applications append .php automatically.
Zip/Compress Tricks
Keyword Filter Bypasses
Wrapper Filter Bypasses
Blacklist Bypasses
Chaining
If it's possible to access logs try
Log Poisoning
LFI to RCE via pearcmd.php
PHP PEAR is a package manager that often comes installed by default on Linux systems.
It includes a script (pearcmd.php) designed to run command-line tasks.
We can pass arguments to the script via the URL query string (?+argument), allowing us to write arbitrary content to files.
Requires: register_argc_argv=On (enabled by default in most PHP configs)
Find the path to PEAR
Create a "Config" file containing your payload
Use the config-create command. The script takes two arguments: the "content" (which we pretend is a directory path) and the output filename.
Sometimes you need
.phpin the path:/tmp/shell.phpSometimes app adds it automatically, use:
/tmp/shell
Other PEAR commands
Insomnia Attack
This technique, published by Insomnia Security (Brett Moore, 2011), exploits a race condition to achieve Remote Code Execution through LFI by leveraging PHP's file upload mechanism and phpinfo() output.
When PHP processes a file upload, it temporarily stores the file in the system's temp directory (usually /tmp/) with a random name like /tmp/phpXXXXXX. This temporary file exists only while PHP is processing the request and is deleted immediately after.
The phpinfo() function displays all PHP variables, including uploaded file information with the temporary filename. By winning a race condition, we can:
Upload a malicious file via
phpinfo()Extract the temporary filename from
phpinfo()outputInclude that file via
LFIbeforePHPdeletes itAchieve code execution
Requirements
LFIVulnerabilityAny local file inclusion vulnerability
Must be able to include files from
/tmp/
PHPInfo()PageAny accessible page that outputs
phpinfo()Common locations:
/phpinfo.php,/info.php,/test.php
file_uploads = OnPHPconfiguration must allow file uploadsCheck in
phpinfooutput under "Core" sectionThis is enabled by default in most
PHPinstallations
Optional But Helpful
Optional But HelpfulMultiple threads - Increases success rate
Low latency - Local network or fast connection improves odds
No rate limiting - More attempts = higher success
PHP uses chunked transfer encoding for large responses. When phpinfo() output exceeds the buffer size (default 4096 bytes), we can receive partial content while PHP is still processing and the temporary file still exists.
The Padding Trick
To ensure phpinfo() exceeds the buffer threshold and slow down processing slightly:
Add large
HTTPheadersAdd
URLquery parametersAdd cookie values
Total padding:
~5000+bytes per field
This increases processing time from microseconds to milliseconds, giving us a window to win the race.
Identifying Vulnerable Systems
Identifying Vulnerable SystemsStep 1: Find PHPInfo Page
Step 2: Verify LFI Vulnerability
Step 3: Check PHP Configuration
Manual Exploitation
Manual ExploitationUnderstanding the Payload
This creates a persistent webshell at /tmp/g that we can access repeatedly without winning the race again.
Step 1: Craft the Upload Request
Step 2: Send Request and Extract Temp Filename
Step 3: Race to Include the File
Automated Exploitation
Automated ExploitationDownload the exploit from here
Custom Payload Variations
Custom Payload VariationsReverse Shell
Persistent Backdoor
Memory Resident (No File)
Increasing Success Rate
Increasing Success Rate1. Increase Thread Count
2. Reduce Network Latency
Run from a
VPSin the same region/datacenterUse
localhostif you have local access
3. Monitor Temp Directory
If you have some access to the system:
4. Increase Padding
Modify the script to add even more padding:
Alternative Temp Locations
Alternative Temp LocationsLast updated