Page cover
githubEdit

file-importLFI

Local File Inclusion

circle-info

Writable directories to target

/tmp/
/var/tmp/
/dev/shm/
/var/www/html/uploads/
chevron-rightPath Traversalhashtag
chevron-rightFile Signatureshashtag
chevron-rightSession File Inclusionhashtag

Session files can be leveraged for LFI to RCE by poisoning session data.

circle-info
circle-info

Alternative: Create Your Own Session

Some applications allow you to set arbitrary session variables

circle-info

Session File Format

Session files use a serialized format:

circle-info

Custom Session Path

If you control session.save_path:

chevron-rightThe %00 Null Terminatorhashtag

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.

chevron-rightFilter Bypasseshashtag
circle-info

Encoding Bypasses

circle-info

Extension Appending Bypasses

Many applications append .php automatically.

circle-info

Zip/Compress Tricks

circle-info

Keyword Filter Bypasses

circle-info

Wrapper Filter Bypasses

circle-info

Blacklist Bypasses

chevron-rightChaininghashtag
chevron-rightLFI to RCE via pearcmd.phphashtag
circle-info

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)

circle-info

Find the path to PEAR

circle-info

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 .php in the path: /tmp/shell.php

  • Sometimes app adds it automatically, use: /tmp/shell

circle-info

Other PEAR commands

chevron-rightInsomnia Attackhashtag

This technique, published by Insomnia Securityarrow-up-right (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:

  1. Upload a malicious file via phpinfo()

  2. Extract the temporary filename from phpinfo() output

  3. Include that file via LFI before PHP deletes it

  4. Achieve code execution

circle-info

Requirements

  1. LFI Vulnerability

    • Any local file inclusion vulnerability

    • Must be able to include files from /tmp/

  2. PHPInfo() Page

    • Any accessible page that outputs phpinfo()

    • Common locations: /phpinfo.php, /info.php, /test.php

  3. file_uploads = On

    • PHP configuration must allow file uploads

    • Check in phpinfo output under "Core" section

    • This is enabled by default in most PHP installations

Optional But Helpful

  • Multiple threads - Increases success rate

  • Low latency - Local network or fast connection improves odds

  • No rate limiting - More attempts = higher success

circle-info

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.

circle-info

The Padding Trick

To ensure phpinfo() exceeds the buffer threshold and slow down processing slightly:

  • Add large HTTP headers

  • Add URL query parameters

  • Add 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

circle-info

Step 1: Find PHPInfo Page

circle-info

Step 2: Verify LFI Vulnerability

circle-info

Step 3: Check PHP Configuration

Manual Exploitation

circle-info

Understanding the Payload

This creates a persistent webshell at /tmp/g that we can access repeatedly without winning the race again.

circle-info

Step 1: Craft the Upload Request

circle-info

Step 2: Send Request and Extract Temp Filename

circle-info

Step 3: Race to Include the File

Automated Exploitation

circle-info

Download the exploit from herearrow-up-right

Custom Payload Variations

circle-info

Reverse Shell

circle-info

Persistent Backdoor

circle-info

Memory Resident (No File)

Increasing Success Rate

circle-info

1. Increase Thread Count

circle-info

2. Reduce Network Latency

  • Run from a VPS in the same region/datacenter

  • Use localhost if you have local access

circle-info

3. Monitor Temp Directory

If you have some access to the system:

circle-info

4. Increase Padding

Modify the script to add even more padding:

Alternative Temp Locations

circle-info

Last updated