Second-Order LFI
Local File Inclusion (LFI) is typically easy to spot and exploit, unless a WAF needs to be bypassed. However, attackers may overlook more complex forms of LFI that require in-depth understanding of the underlying web application.
Code Review - Identifying the Vulnerability
Application Overview
Updated version with ability to:
Update username
Update filenames
File Storage Mechanism (db.php)
Files are stored locally on the filesystem, in a folder named after the owner:
function fetch_data($id){
global $conn;
$sql = "SELECT * FROM data WHERE id=?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
echo "SQL Error";
exit();
}
// execute query
$id = intval($id);
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$result = mysqli_fetch_assoc($result);
$owner = $result['owner'];
$name = $result['name'];
$path = '/var/www/' . $owner . '/' . $name . '.txt';
return array("name" => $name, "content" => file_get_contents($path));
}Path construction: /var/www/<owner>/<filename>.txt
Analyzing edit_filename.php
Filename is filtered! Rejects .., /, \ β Cannot escape user directory via filename.
Analyzing edit_username.php
No filter on username! Can inject ../ sequences.
update_username Function (db.php)
Critical Bug: Developers forgot to update file paths when username changes!
The Vulnerability
Bug Behavior
User
htb-stdntowns filetest.txtat/var/www/htb-stdnt/test.txtRename file to
HelloWorld.txtβ moved to/var/www/htb-stdnt/HelloWorld.txtChange username to
testβ file path NOT updatedAccess
HelloWorld.txtβ app tries to read/var/www/test/HelloWorld.txtFile not found (wasn't moved)
Escalation to LFI
Since username is not filtered for special characters:
Change filename to match target file name
Change username to traverse directories
App reads from manipulated path β LFI!
Exploit Plan
Target: /tmp/poc.txt
Local Testing Setup
Create PoC File
Database Setup (db.sql)
Start MySQL Container
Start PHP Server
Create Test File
Exploitation
Step 1: Rename File to Target Name
Go to /edit_filename.php and change filename to poc
Step 2: Change Username to Path Traversal
Go to /edit_username.php and set username to ../../tmp
Step 3: Access the File
Select the renamed file poc β Web app loads /var/www/../../tmp/poc.txt
Result: The Exploit Works!
Limitations
Only
.txtfiles can be leaked (extension hardcoded)Still a security issue in real-world complex applications
Requires close analysis of how components interact
Question Walkthrough
Task: Exploit second-order LFI to leak the file flag owned by user admin.
Step 1: Analyze Source
Key findings:
edit_username.phphas no LFI filtersupdate_usernamedoesn't update file paths
Step 2: Login
Credentials: htb-stdnt:Academy_student!
Step 3: Rename File
Change filename of any file (e.g., "Lorem Ipsum") to flag
Step 4: Change Username
Navigate to /edit_username.php and set username to ../www/admin
Step 5: Access Flag
View the "flag" file.
App fetches: /var/www/../www/admin/flag.txt β Admin's flag leaked!
Last updated