LFI Vulnerability Exploitation: From Passwd to RCE part 1

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
LFI and File Retrieval: How to Distinguish Local File Inclusion from Read-Only


The first thing you need to figure out at a CTF is the type of vulnerability. Not every file read through ../— this is LFI. As described in dr34mhacks' research, the difference between Local File Inclusion and Arbitrary File Retrieval determines the entire subsequent exploitation vector. For more details, see our detailed analysis of creating CTF tasks .





Local File Inclusion occurs when using PHP's include functions - include(), require(), include_once(), require_once()The key point is that the included file is executed by the interpreter. config.php— PHP will run the script. You'll see the execution result or a blank page, but not the source code.





Arbitrary File Retrieval - the application reads a file and returns the contents as text. This is the responsibility of file_get_contents(), readfile(), fopen() + fread()The PHP script will be displayed as plain text—the source code in the palm of your hand.





Quick test: Substitute the path to a known PHP file of the application (e.g. index.php). See the source code with tags <?php— File Retrieval. If you see the execution result or a blank page, it's LFI. For File Retrieval, look for credentials in the config files. For LFI, build a chain to remote code execution via LFI.





The attack logic: the attacker uses local file inclusion to read configurations (database passwords, API keys), extracts the application's source code to search for further holes, and ultimately obtains an RCE to gain persistence on the server. In the MITRE ATT&CK classification, this technique is T1190 — Exploit Public-Facing Application (Initial Access tactic). According to Mandiant M-Trends 2025, exploits are the leading initial access vector, accounting for 38% of incidents. In the OWASP Top 10, LFI falls under A03:2021 — Injection. The root weakness according to MITRE CWE is CWE-98: Improper Control of Filename for Include/Require Statement in PHP Program (likelihood: High).






Path traversal vulnerability: directory and filter traversal


Directory traversal attack is the foundation of LFI exploitation. Sequence ../Allows you to jump out of the directory where the application restricts access and access any file on the server.





Basic payload for reading /etc/passwd vulnerability: ../../../../etc/passwd. Quantity ../depends on the depth of the current directory. If the application includes files from /var/www/html/pages/, you need four levels to the root of the file system. In practice, it's easier to add 8-10 sequences—the extra ones ../The OS simply ignores the root.





For Windows the goals are different: ..\..\..\..\windows\win.ini or ..\..\..\..\windows\system32\drivers\etc\hosts.





Typical filters and bypasses:





Filter Bypass Example
Substitution ../to an empty line Double investment ....//....//etc/passwd
Blocking ../at the beginning Absolute path /etc/passwd
Simple URL-decode Double URL encoding %252e%252e%252f
Keyword Blocking ( passwd) Alternative files /etc/hosts, /proc/self/cmdline


According to Invicti's recommendations, basic sanitization and blocklist approaches never save the day—attackers bypass them through encryption, truncation tricks, and alternative path representations. A blocklist is an illusion of protection.






Null byte injection LFI и path truncation


If an application concatenates an extension to user input - include($file . ".php")— the attacker needs to "trim" the added suffix. Two working methods.





Null byte ( %00) - works on PHP versions below 5.3.4. Null byte is the string terminator character in C, which PHP is built on. The file system sees %00and cuts everything after: payload page=../../../../etc/passwd%00forms a string /etc/passwd\0.php, but the OS opens /etc/passwdThis vector is dead on modern PHP versions, but CTF tasks with older PHP regularly pop up. The version can be determined from the header. X-Powered-Byin the HTTP response or output phpinfo().





Path truncation is an alternative for PHP 5.3+. In older versions of PHP, the maximum path length was 256 characters (Windows) or 4096 (Linux). If the path exceeds the limit, PHP truncates the tail. Payload: ../../../../etc/passwd+ several hundred characters /.— suffix .phpgoes beyond the limit and is discarded.





Technique PHP version Prerequisite Result
Null byte %00 < 5.3.4 include($file.".php") Bypassing expansion concatenation
Path truncation < 5.3 (with reservations) Path length > OS limit Suffix truncation
Double investment Any Substitution ../on "" Bypassing string replacement
URL double-encode Any WAF before the application Bypassing WAF rules



PHP wrapper LFI: from reading source code to code execution


PHP wrappers (stream wrappers) are built-in protocols for accessing various data sources. Exploiting the LFI vulnerability requires three: php://filter, php://input And data://Each player solves their own problem in the CTF web exploitation chain.






php://filter for exploiting PHP's include functions


include()executes the PHP file instead of displaying its code. Wrapper php://filterPasses the file contents through a filter (base64 encoding) before passing them to the interpreter. The encoded string does not contain tags. <?php— and is displayed as plain text.





Payload: page=php://filter/convert.base64-encode/resource=config.phpThe response contains a base64 string. Decoding: echo "строка" | base64 -d— and here's the source code for config.php with passwords and keys. A standard php://filter exploitation technique for CTFs.





What to read through php://filterat competitions:





config.php, db.php, database.php— credentials for the database
index.php, login.php— authentication logic and input filtering
.htpasswd— password hashes for HTTP Basic Auth
flag.php— sometimes the flag is stored as a PHP variable


An advanced option is PHP filter chain LFI. Researchers have shown that by applying filters sequentially convert.iconvYou can generate arbitrary text, including PHP code, without writing it to disk. This turns php://filterFrom a reading tool to an RCE tool. Such tasks are already appearing in hard/insane CTFs (HackTheBox, TryHackMe)—and their number is growing.






php://input and data:// are the direct path to LFI to RCE


If in php.ini costs allow_url_include = On, wrappers are opened for direct code execution without writing to disk.





php://inputreads the raw body of the POST request. Payload: GET request with page=php://input, and in the POST body there is PHP code <?php system('id'); ?>The interpreter "includes" the contents of body and executes it.





data://passes data directly to the URL: page=data://text/plain,<?php system('id'); ?>or in base64: page=data://text/plain;base64,PD9waHAgc3lzdGVtKCdpZCcpOyA/Pg==On DVWA, this vector operates at the Low level out of the box—you can check it in a minute.





Limitation: allow_url_includeIt's been disabled by default since PHP 5.2, and it's almost never enabled on production servers. But on CTF rigs, it's a different story. You can check via phpinfo()or indirectly: if php://inputreturns the execution result instead of an error - the option is enabled.
 
Top Bottom