Second-Order IDOR (Whitebox)

Now that we have covered background information about second-order vulnerabilities, we will explore methods for identifying, exploiting, and mitigating them within a web application using a whitebox approach.


Code Review - Identifying the Vulnerability

Application Overview

File storage application. After logging in with htb-stdnt, users can see stored files.

Clicking a file results in request to /get_data.php?id=2, displaying the file.

Testing for First-Order IDOR

Accessing /get_data.php?id=1 (another user's file):

  • User is logged out

  • Error message displayed: "Something went wrong. You have been logged out for security reasons."

Result: Not vulnerable to classical first-order IDOR. Let's analyze source code.


Source Code Analysis

get_data.php

<?php
  session_start();
  require_once ('db.php');
  if(!$_SESSION['user']){
    header("Location: index.php");
    exit;
  }
  $_SESSION['id'] = $_GET['id'];  // ← ID set BEFORE access check!
  if(check_access($_SESSION['id'], $_SESSION['user'])){
    header("Location: display_data.php");
    exit;
  } else {
    header("Location: error.php");
    exit;
  }
?>

display_data.php

error.php

The Vulnerability

Key insight: Session variable id is set before access check. It's only cleared in error.php. If we don't follow the redirect, the ID remains set!


Running the Application Locally

Database Setup (db.sql)

Start MySQL Container

Start PHP Server


Exploitation

Step 1: Set Session Variable

Request arbitrary file ID (don't follow redirect):

Response:

Do NOT follow the redirect!

Step 2: Access display_data.php Directly

Navigate to /display_data.php in browser β†’ Admin's file displayed!

This PoC can be scripted to exfiltrate all files on the web application.


Patching

Problem: Session variable id is set before access check.

Fix: Only set session variable after access check passes.

Fixed get_data.php


Question Walkthrough

Task: Exploit the second-order IDOR vulnerability to obtain the flag.

Step 1: Download and Analyze Source

Step 2: Understand the Flow

  1. Login with htb-stdnt:Academy_student!

  2. View files via /get_data.php?id=X

  3. Notice: unauthorized access redirects to error.php

Step 3: Identify Vulnerability

In get_data.php line 10:

  • $_SESSION['id'] is set before check_access()

  • Session only cleared in error.php

Step 4: Exploit

  1. Intercept request to /get_data.php?id=5

  2. Don't follow redirect to error.php

  3. Navigate directly to /display_data.php

  4. Flag displayed!

Last updated