Ir al contenido

📕 FIELD MANUAL 15.1: SQL TRUNCATION (THE DOPPELGÄNGER ATTACK)

Objective: We will hijack privileged accounts (Admin) by exploiting database column length limits, without using quotes or...

0views

Classification: RED TEAM EYES ONLY

Objective: We will hijack privileged accounts (Admin) by exploiting database column length limits, without using quotes or classic SQL syntax.

MITRE ATT&CK: T1078 (Valid Accounts).

Databases define limits. E.g.: username VARCHAR(20). MySQL (in default configurations) has a critical peculiarity: It will truncate excess characters without throwing an error.

  1. There is a user admin.
  2. We want to log in as them, but we do not have their password.
  3. We will create a new user that, for MySQL, is identical to the admin.

The Space Trick: For MySQL, the string "admin" is equal to "admin " (ignores trailing spaces in comparisons).

We will try to register with a very long username to see if it gets truncated or throws an error. Let's assume we discover that the limit is 20 characters.

We will register with the following username: admin x(Note: “admin” + 14 spaces + “x” = 20 characters).

  1. The App will receive: admin x (Length 20).
  2. The App will check: “Does this user exist?” -> No. (Because admin...x is not equal to admin).
  3. The App will insert: INSERT INTO users (user, pass) VALUES ('admin...x', 'pass123').
  4. MySQL Truncates: Since the field is VARCHAR(20), it will cut off the final x if it exceeds the limit (or if we adjust the spaces perfectly to the limit).
    • What will actually be saved: admin
  1. We will go to the Login.
  2. Username: admin x (Our long username).
  3. Password: pass123 (Our password).
  4. The App will look for: SELECT * FROM users WHERE user = 'admin...x'.
  5. Match: MySQL will compare our input with the stored value (admin...). They will match!
  6. The App will log us in as a user.
  7. Impact: Since the system will see that the user is named admin (spaces will be ignored), it will grant us the privileges of the original Administrator.
  • Defense: We will enable STRICT_TRANS_TABLES in MySQL (strict mode) so that it throws an error instead of silently truncating.
  • Validation: We will verify the input length in the code (Backend) before sending it to the database.