Ir al contenido

📕 FIELD MANUAL 12.1: SECOND-ORDER SQL INJECTION (THE DELAYED INJECTION)

Objective: We will exploit logical vulnerabilities where the payload is injected in one phase (Seeding), but detonates in a completely different function...

0views

Classification: RED TEAM EYES ONLY

Objective: We will exploit logical vulnerabilities where the payload is injected in one phase (Sowing), but detonated in a completely different function (Detonation).

MITRE ATT&CK: T1190 (Exploit Public-Facing Applications).

Most pentesters fail here because they expect an immediate error. In Second-Order, the attack is asynchronous.

  • Classic/Reflected: Input -> Query -> Boom (Immediate).
  • Stored: Input -> DB -> Vista de Admin -> Boom (XSS - Client).
  • Second-Order: Input (Seguro) -> DB -> Proceso de Backend -> Boom (SQLi - Server).
  1. Registration (Seeding): We register as a user.
    • Input: admin' --
    • Backend: The app escapes the quotes (\') when inserting.
    • DB: The user is literally saved as the string admin' --.
  2. Operation (Detonation): We will go to “Change Password”.
    • Vulnerable Logic: The app retrieves our user from the session or DB to build the update query.
    • Constructed Query: UPDATE users SET password='123' WHERE username='admin' --'
    • Result: The query closes the quote after admin and comments out the rest (-). We have just changed the password of the real user admin, not ours.

We must find a field that is saved in the DB and that we know will be used in another query later without being re-sanitized.

  • Common Vectors: Username, Shipping address, Email, Profile comments.
  • Seeding Payload (MySQL): admin' #
  • Seeding Payload (Generic): admin' --

We will execute the secondary function that uses the seeded data.

  • Action: We will go to “Change Password”, “Update Profile”, or “View Order History”.
  • Mechanism: If the application blindly trusts the data because it “comes from the database” (trusted zone) and concatenates it, the injection occurs at that moment.

Detecting this automatically is difficult because the scanner needs to understand the business logic (Register -> Then go to Profile).

  • Burp Suite (Manual):
    1. We will intercept the Registration.
    2. We will modify the username to test'.
    3. We will navigate through the app while logged in. If we see a SQL error on any other page, it is Second-Order.
  • Defense: The golden rule is “Trust No One”.
  • Validation: We will never trust data, even if it comes from our own database.
  • Mitigation: We will always use Prepared Statements, both when inserting data (Input) and when retrieving and using it in a new query (Processing).