SQL InjectionSQL injection is a technique where an attacker exploits flaws in application code responsible for building dynamic SQL queries. The attacker can gain access to privileged sections of the application, retrieve all information from the database, tamper with existing data, or even execute dangerous system-level commands on the database host. The vulnerability occurs when developers concatenate or interpolate arbitrary input in their SQL statements.
Example #1 Splitting the result set into pages ... and making superusers (PostgreSQL) In the following example, user input is directly interpolated into the SQL query allowing the attacker to gain a superuser account in the database.
0; insert into pg_shadow(usename,usesysid,usesuper,usecatupd,passwd) select 'crack', usesysid, 't','t','crack' from pg_shadow where usename='postgres'; -- 0; is to supply a valid offset to the
original query and to terminate it.
A feasible way to gain passwords is to circumvent your search result pages.
The only thing the attacker needs to do is to see if there are any submitted variables
used in SQL statements which are not handled properly. These filters can be set
commonly in a preceding form to customize Example #2 Listing out articles ... and some passwords (any database server)
SELECT statement which reveals all passwords:
' union select '1', concat(uname||'-'||passwd) as name, '1971-01-01', '0' from usertable; --
Example #3 From resetting a password ... to gaining more privileges (any database server)
' or uid like'%admin% to $uid to
change the admin's password, or simply sets $pwd to
hehehe', trusted=100, admin='yes to gain more
privileges, then the query will be twisted:
While it remains obvious that an attacker must possess at least some knowledge of the database architecture to conduct a successful attack, obtaining this information is often very simple. For example, the code may be part of an open-source software and be publicly available. This information may also be divulged by closed-source code - even if it's encoded, obfuscated, or compiled - and even by your own code through the display of error messages. Other methods include the use of typical table and column names. For example, a login form that uses a 'users' table with column names 'id', 'username', and 'password'.
Example #4 Attacking the database host operating system (MSSQL Server) A frightening example of how operating system-level commands can be accessed on some database hosts.
a%' exec master..xp_cmdshell 'net user test testpass /ADD' --
to $prod, then the $query will be:
sa and the MSSQLSERVER service was
running with sufficient privileges, the attacker would now have an
account with which to access this machine.
Image courtesy of » xkcd Avoidance Techniques
The recommended way to avoid SQL injection is by binding all data via
prepared statements. Using parameterized queries isn't enough to entirely
avoid SQL injection, but it is the easiest and safest way to provide input
to SQL statements. All dynamic data literals in Parameter binding can only be used for data. Other dynamic parts of the SQL query must be filtered against a known list of allowed values.
Example #5 Avoiding SQL injection by using PDO prepared statements
Prepared statements are provided by PDO, by MySQLi, and by other database libraries. SQL injection attacks are mainly based on exploiting the code not being written with security in mind. Never trust any input, especially from the client side, even though it comes from a select box, a hidden input field, or a cookie. The first example shows that such a simple query can cause disasters. A defense-in-depth strategy involves several good coding practices:
Besides these, you benefit from logging queries either within your script or by the database itself, if it supports logging. Obviously, the logging is unable to prevent any harmful attempt, but it can be helpful to trace back which application has been circumvented. The log is not useful by itself but through the information it contains. More detail is generally better than less. |