SQL Truncation
Full command arsenal in the cheatsheetSQL truncation is an account takeover attack that exploits a mismatch between the length the application accepts and the length the database stores. It is not an SQL injection: no query is broken. The flaw is that the database silently truncates values exceeding the column length and, on top of that, strips trailing spaces.
The mechanics. Assume an email VARCHAR(20) column with a unique index, and a registration that validates "this email does not exist yet". The attacker wants to seize admin@book.htb:
- They submit
admin@book.htb+ spaces + padding, e.g.admin@book.htb 123(over 20 characters). - The application validation compares that string with the existing ones: it differs from
admin@book.htb, so it passes the duplicate check. - The database truncates to 20 chars (
admin@book.htb) and trims trailing spaces, leavingadmin@book.htb. - Since that email already exists, depending on configuration the row is overwritten (or a second indistinguishable "admin" is created), leaving the password under the attacker's control.
Detection hint. Any field that truncates input visibly (a name cut to N characters) suggests strict length limits in the schema. If the email or username behaves the same, truncation is a candidate. Confirm the limit by registering a value that, once truncated, collides with an existing account (the app returns a duplicate error).
Practical details. Panels usually reject spaces on the frontend; bypass it by sending the request with Burp and URL-encoded spaces (%20). Behaviour depends on the DBMS and its mode (MySQL in non-strict mode truncates without error; in strict mode it aborts).
Example payload#
The target email, followed by spaces (which the database trims) and a filler character to exceed the column length (for example VARCHAR(20)):
curl -s -X POST http://<host>/register \
--data-urlencode 'email=admin@book.htb a' \
--data-urlencode 'password=hacked123'
# check the account takeover
curl -s -X POST http://<host>/login --data 'email=admin@book.htb&password=hacked123'How to detect it. On the server side the attack leaves a trail in the DBMS's own warnings: MySQL and MariaDB emit Warning 1265 Data truncated for column on the malicious insert (or 1062 Duplicate entry if the unique index fires), and in strict mode that becomes an error. In the data, the collision is found with a query: SELECT TRIM(email), COUNT(*) FROM users GROUP BY TRIM(email) HAVING COUNT(*) > 1, or simply the values with trailing spaces. And in the application log the full sequence is a registration whose submitted field is longer than the column followed, shortly after, by a password change on the privileged account.
Defence. Validate length before reaching the database and reject (do not trim) anything exceeding it; enable the DBMS strict mode so truncation is an error, not a silence; normalise (trim) consistently in the application layer; use robust uniqueness constraints. Related to username enumeration (knowing which account to attack) and account takeover in general.
Machines where it's used
- BookHackTheBox