Ir al contenido

📕 FIELD MANUAL 13.1: HQL & JPQL INJECTION (ENTERPRISE JAVA)

Objective: We will inject into common ORM (Hibernate/JPA) abstraction layers in large banking and corporate environments.

0views

Classification: RED TEAM EYES ONLY

Objective: We will inject into ORM abstraction layers (Hibernate/JPA) common in large banking and corporate environments.

MITRE ATT&CK: T1190.

In Java Enterprise, developers do not write direct SQL. We write HQL (Hibernate Query Language) or JPQL.

  • Pure SQL: SELECT * FROM tbl_users WHERE name = 'x'
  • HQL/JPQL: FROM User WHERE name = 'x'
  1. Objects, not Tables: We do not select tables (tbl_users), we select Java Classes (User).
  2. No UNION: Standard HQL does not support UNION. This kills most fast extraction payloads.
  3. Case Sensitive: Class (table) names are case-sensitive.

2. WEAPONIZATION: ATTACKING THE ABSTRACTION

Sección titulada «2. WEAPONIZATION: ATAQUE A LA ABSTRACCIÓN»

Since there is no UNION, we will use errors or inference to map the Java object.

Payload to discover attributes (columns): If we attempt to access a property that does not exist in the mapped Java Class, Hibernate will throw a specific exception.

  • Payload: ' AND x=1
    • Error: Could not resolve property: x of: com.bank.User (We confirm that 'x' does not exist).
  • Payload: ' AND password=1
    • Error: Incompatible types or Data type mismatch (This means that password DOES exist, but we are attempting to compare it to a number).

Sometimes we can trick the Hibernate translator into passing native functions to the underlying database (Oracle/MySQL).

  • Payload: ' OR 1=1 --
    • This usually works because Hibernate passes the WHERE part quite directly to the final SQL engine.
  • SQLMap: Supports HQL Injection but often requires manual assistance.
  • Flags: -dbms="HQL" (though sometimes it is better to let it detect the actual backend).
  • Defense: We will use “Named Parameters” in JPA/Hibernate.
    • Insecure: entityManager.createQuery("FROM User WHERE name = '" + input + "'")
    • Secure: entityManager.createQuery("FROM User WHERE name = :username").setParameter("username", input)
  • Detection: Java exception logs (Stack Traces) showing org.hibernate.QueryException.