Authentication

Case Study Overview

Practical whitebox pentesting exercise demonstrating advanced code injection that requires source code access to identify and exploit.

Requirements

  • VSCode installed

  • Node.js installed

  • Source code archive


Data Gathering

Scenario

Given code base in archive without further details (minimum requirement for whitebox pentest).

Setup

# Download and extract
wget <archive_url> && unzip intro_to_whitebox_pentesting.zip
cd intro_to_whitebox_pentesting

# Open in VSCode
code .

Project Structure


Code Analysis

app.js - Entry Point

Key Points:

  • Express server on port 5000

  • JSON body parser β†’ endpoints expect JSON body

  • Two route groups: /api/auth and /api/service

Server Startup


Authentication Routes

auth-routes.js

Endpoint: POST /api/auth/authenticate


Authentication Controllers

auth-controllers.js Functions

Function
Purpose
Exported

validateEmail

Validate email format

No (local)

getUserToken

Generate JWT token

Yes

verifyToken

Verify JWT token

Yes

validateEmail

Simple regex validation for email format.

getUserToken

Flow:

  1. Get email from POST body (JSON)

  2. Validate email format

  3. Sign JWT with email and role

  4. Return token

verifyToken

Flow:

  1. Get token from Authorization header

  2. If no token β†’ 403 Unauthorized

  3. Verify JWT signature

  4. Add decoded data to req.user

  5. Call next() to proceed

Usage: Middleware for authenticated endpoints


Using AI for Code Understanding

VSCode Copilot

Can use AI to:

  • βœ… Confirm your understanding

  • βœ… Clarify unclear code

  • ⚠️ Don't overly rely on it

  • ⚠️ Always double-check responses

Example Query

"What does this function do?"

AI explains JWT token generation with email and role claims.


Identifying Vulnerable Function

Service Controllers Analysis

Looking at service-controllers.js:

Why It's Vulnerable

Issue
Description

eval() usage

Executes arbitrary code

User-controlled input

onError parameter

Blacklist filtering

Can be bypassed

Answer

Most likely vulnerable function: validateString

The use of eval() in validateString creates a security vulnerability that could potentially be exploited to execute arbitrary code.


Code Review Findings

Authentication (auth-controllers.js)

Function
Status
Notes

validateEmail

βœ… Safe

Regex validation

getUserToken

βœ… Safe

Proper JWT signing

verifyToken

βœ… Safe

Proper JWT verification

Service (service-controllers.js)

Function
Status
Notes

validateString

🚨 VULNERABLE

eval() with user input


Next Steps

  1. Analyze validateString - Understand full context

  2. Trace input flow - How does user input reach eval()?

  3. Check filters - Can blacklist be bypassed?

  4. Local Testing - Set up environment and test

  5. Exploitation - Develop PoC


Key Takeaways

  1. Start with entry points - app.js shows route structure

  2. Follow the code - CMD/CTRL + click in VSCode

  3. Look for dangerous functions - eval(), exec(), etc.

  4. Understand the flow - How data moves through app

  5. Use AI wisely - Confirm, don't rely blindly

Last updated