Path traversal vulnerability: from discovery to flag part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
PHP Wrappers and LFI Vulnerability: Reading the Source Code

If it works on a CTF task with a PHP backend include()This isn't just path traversal—it's an LFI vulnerability with access to PHP wrappers. The difference is critical: path traversal allows file reading, while LFI can allow file execution as PHP code.
php://filter — source code via base64

Problem: Including PHP file via includewill execute it on the server and return the HTML result, not the source code. Wrapper php://filterencodes the content to base64 before execution. Request ?file=php://filter/convert.base64-encode/resource=indexwill return a base64 string. Decode it using echo "PD9waH..." | base64 -d— and you see the full source code.

In CTFs, this means access to application logic, hardcoded keys, and flag paths. Sometimes the flag is right there in a PHP file comment (yes, task authors do that). A nuance from HackTricks: php://filtercase-insensitive - query with PhP://FiLtErThis will work too. It helps bypass blacklist filters that check for lowercase strings.

Other filters are also available: convert.base64-decode, string.rot13, convert.iconv.*On tasks of increased difficulty, the authors block base64, but they let it through rot13or chains convert.iconv.*— PHP filter chains technique (Synacktiv, 2022) allows to build RCE through php://filtereven without allow_url_include.
data:// — instant RCE

If allow_url_include = On(rare in production, but CTF task authors sometimes include it intentionally), wrapper data://Allows you to execute PHP code directly from a URL: ?page=data:text/plain,<?php system('id'); ?>Instant RCE without any extra steps. Check for the setup via phpinfo(): if disabled, proceed to log poisoning.
From Server File Reading to RCE: Log Poisoning

A typical situation on CTF: traversal works, php://filtershows the source code, but allow_url_includeturned off and data://It doesn't work. We need a different way to execute the code. Log poisoning is one of the most reliable, and I would even say one of the most beautiful.
The essence of the technique

Apache and NGINX write the header User-Agentin the access.log without sanitization. If you send an HTTP request with PHP code in this header, the server will write the line to the log as is. When the log file is connected via include()With path traversal, the PHP engine will execute all the code it finds, including the embedded webshell.

In Burp Repeater, you only need to change the header of one request:

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

After this request ?file=....//....//....//var/log/apache2/access.log&c=idwill connect the "poisoned" log, and PHP will execute system('id')The command output will appear in the response among the log garbage.
Preconditions and limitations

Without any of these conditions, log poisoning is useless:

Backend uses include() or require(), not file_get_contents() or readfile()Only the first two execute the PHP code in the file's contents.
The path to the log file is known. Standard paths: /var/log/apache2/access.log(Debian/Ubuntu), /var/log/httpd/access_log (CentOS/RHEL), /var/log/nginx/access.log.
The web server user (www-data, apache, nginx) has read access to the log file. On hardened systems, this is not guaranteed.

Step-by-step exploitation: four requests to RCE

Step 1 - Confirm include()If the PHP file was executed via traversal (HTML result was returned, not the source code), then include()or its equivalent is used in the backend. The source code has returned— file_get_contents(), log poisoning won't work.

Step 2 : Poison the logs. In Burp Repeater: change the User-Agent to <?php system($_GET['c']); ?>, you send a GET request to any page on the server. One request is enough.

Step 3 : Connect the log via traversal. Request ?file=....//....//....//....//var/log/apache2/access.log&c=idConnect the log with the webshell. The response will contain the output among the garbage logs. id.

Step 4 - Take the flag. Replace it. idon cat /flag.txtor you are executing a suid binary /readflag, if direct reading is prohibited by file system permissions. In one of the CTF tasks (Stratagem on MetaCTF), this very vector turned directory traversal into full-fledged command execution: the flag file was accessible only to root, and the suid binary /readflagrequired a call via system call.

The entire chain consists of four HTTP requests in Burp. Two to three minutes, if you know the order.
Real CVEs with path traversal payload

CTF web challenges are often inspired by real-world vulnerabilities. The three examples below demonstrate how path traversal exploitation works in production and the severity ratings assigned to such findings.

CVE-2024-40348 — Bazarr v1.4.3. Vulnerability in /api/swaggerui/staticAllows unauthenticated attackers to perform directory traversal. 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 access, low complexity, no privileges required. CWE-22. EPSS 0.0822 — Top 5% for 30-day exploitability. According to CISA-ADP: SSVC decision track* (Exploitation: poc, Technical Impact: partial, Automatable: yes). A ready-made script is available on GitHub. bigb0x/CVE-2024-40348(31 stars) for reading /etc/passwdfrom the target server. A typical example: an API endpoint turns out to be more vulnerable than the main interface.

CVE-2023-32315 — Openfire (CISA KEV). Path traversal in the Openfire Administrative Console. An unauthenticated user could access Admin Console pages via Setup Environment. CVSS 8.6 (HIGH), vector CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:L/A:L. Affected versions org.igniterealtime.openfire:xmppserverFrom 3.10.0 to 4.6.7, fixed in 4.6.8. Included in the CISA KEV (Known Exploited Vulnerabilities) catalog since 2023-08-24 — actively exploited in real attacks. EPSS 1.0000 — extremely high probability, Top 1%. CISA-ADP: Exploitation: active, Automatable: yes, Technical Impact: total. Payload used Unicode encoding: setup/setup-s/%u002e%u002e/%u002e%u002e/log.jsp.

CVE-2021-45967 — Pascom Cloud Phone System. NGINX/Tomcat configuration error. CVSS 9.8 (CRITICAL) is almost the maximum. Vector CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H — complete compromise of confidentiality, integrity, and availability. Payload with ..;/passes through NGINX undetected, Tomcat resolves it as a traversal. A clear example: confusion in the interpretation of paths between two components creates a vulnerability that is invisible to either component individually.
Target files when crawling web application directories

Reading server files is not /etc/passwdAnd that's it. This file is needed for exactly one thing: to confirm that traversal is working. After that, the choice of targets depends on the OS and environment.
Linux и Unix

Priority goals: /etc/shadow(password hashes, requires root rights), /home/<user>/.ssh/id_rsa(private SSH key), /home/<user>/.bash_history(command history - sometimes with cleartext passwords). The file system is especially interesting. /proc: /proc/self/environcontains environment variables of the current process (often include tokens and secrets), /proc/self/cwd/app.py or /proc/self/cwd/index.phpprovide the source code via symlink of the current directory, /proc/self/cmdlineshows the launch arguments (may contain passwords passed via the command line).
Windows

Both separators / And \acceptable. Proof-of-concept: C:\Windows\win.ini And C:\windows\system32\license.rtf— are present on all modern Windows systems. For escalation: C:\inetpub\wwwroot\web.config(IIS configuration with connection strings), C:\windows\repair\sam(SAM file with password hashes). According to PayloadsAllTheThings, Windows also allows UNC paths ( \\localhost\c$\windows\win.ini), which corresponds to CWE-40 (Path Traversal: '\UNC\share\name\') - the server can attempt to authenticate to the remote resource by sending an NTLM hash.
Cloud and containers

According to YesWeHack, Go and serverless frameworks load files from cloud-mapped paths. On AWS Lambda, the function source code is located in /var/task/— if the endpoint renders templates on a custom path, traversal to /var/task/index.jsReveals all code. In Kubernetes: /run/secrets/kubernetes.io/serviceaccount/token— service account token for moving around the cluster, /run/secrets/kubernetes.io/serviceaccount/namespace— namespace name.
Web-specific files

.env(API keys, database passwords, JWT secrets), config.php / database.yml / settings.py(application configuration), .git/config (If .gitavailable - you can download the entire repository), composer.json / package.json(dependencies and versions pointing to known CVEs in packages).

I've come across CTF write-ups where the author randomly tried 50 payloads from a cheat sheet and was surprised that none of them passed. Meanwhile, one request through php://filterwould show the source code of the vulnerable endpoint, from which it is clear: it is worth str_replace('../', '', $input), And ....//Solves the problem. The problem isn't the lack of the right payload in your arsenal—it's the lack of habit of reading the code before breaking it. One read source code is worth fifty random payloads.

Path traversal on an easy task is a copy-paste from the cheat sheet. Path traversal on a 500-point task is the ability to build a chain: traversal → source code leak → credential extraction → log poisoning → RCE. This chain is the difference between a player who takes three out of five web tasks and one who consistently takes five out of five. It should be practiced not on cheat sheets, but on tasks where you assemble each link yourself.

And the second point, which is almost never discussed in Russian-language analyses: the choice of files to read. During pentests, credentials from .envare pulled out in every second project. CTF authors copy this pattern. I read /etc/passwdand stopped - lost 80% of the information that the same vulnerability provides.
 
Top Bottom