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

  1. User htb-stdnt owns file test.txt at /var/www/htb-stdnt/test.txt

  2. Rename file to HelloWorld.txt β†’ moved to /var/www/htb-stdnt/HelloWorld.txt

  3. Change username to test β†’ file path NOT updated

  4. Access HelloWorld.txt β†’ app tries to read /var/www/test/HelloWorld.txt

  5. File not found (wasn't moved)

Escalation to LFI

Since username is not filtered for special characters:

  1. Change filename to match target file name

  2. Change username to traverse directories

  3. 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 .txt files 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.php has no LFI filters

  • update_username doesn'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