Study guide · SQL injection and template injection
Data stays data: SQL injection and template injection in TYPO3
Injection is one of the oldest and most dangerous holes on the web. On this page you learn how SQL injection and server-side template injection arise, how an attacker finds and exploits them, and how to prevent them cleanly in TYPO3. At the end you also know their relatives, command injection and object injection.
The core: keep data and code apart
Every injection has the same cause. Input ends up in a place where a program reads it not as a value but as an instruction. In SQL injection that place is the SQL string of a database query. In template injection it is the source of a template. In command injection it is a shell command.
That is why there is one shared principle: input goes in as data, never as code. In SQL you achieve this with bound parameters. In a template you pass user values as variables. Both separate the data from the executable part.
Everything the client sends counts as unsafe. That includes GET and POST parameters, cookies and HTTP headers. Constants from your own versioned code and fixed values from the extension configuration, on the other hand, are controlled by you as the operator.
The example on this page is an extension for events. It has a search, a sortable list, and it sends confirmation mails to visitors who register.
How SQL injection arises
SQL injection arises when you concatenate input into the SQL string. Then an attacker can close the text literal with a single quote and append SQL of their own.
$sql = "SELECT * FROM tx_myevents_event WHERE title LIKE '%" . $q . "%'";A prepared statement works differently. The database first receives the structure of the query with placeholders. The values arrive separately afterwards. So a value can no longer change the structure. A quote then simply stays a character in the search term.
Neither addslashes nor htmlspecialchars is reliable protection. The first function does not know the character set of the connection. The second one encodes for HTML, not for SQL.
How an attacker proceeds
The first test is often a single quote in the search field. If the query aborts with an SQL error, the input is evidently being inserted into the SQL string. If the page even shows the error message, it reveals tables and queries. With that the attacker works out the number of columns, for example. That is why SQL errors on the live site belong in the log and not in the output.
If the results appear on the page, for example in a search, the attacker uses UNION SELECT. UNION combines two result sets. This lets them append rows of a foreign table to the visible list. The appended query must return the same number of columns as the original, and the types must match. Otherwise the database aborts.
' UNION SELECT username, password FROM be_users --The two dashes at the end start an SQL comment. So the rest of the original query no longer gets in the way, for example a closing quote. The injected query stays valid. Bound parameters prevent this attack. The input then stays a value and cannot append a second query.
A popular target is the table be_users. It contains the user names and password hashes of the backend logins. Hashes cannot be reversed, but weak passwords can be cracked offline.
If the page shows no results, the attacker turns to blind SQL injection. They ask yes or no questions and conclude the answer from the behaviour of the page. In the time-based variant they delay the query only when a condition is true. If a search page always shows the same message but responds measurably slower to certain inputs, that is a clear warning sign. The input influences the query. Data can be read this way bit by bit, more slowly, but just as dangerously.
What an injection can do in the worst case depends on the query and on the rights of the database user. In a SELECT the attacker can usually only read. With further options they can change and delete data, create admin accounts or access files.