SQL Injection

Overview
SQL Injection attacks are another instantiation of injection attack, in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands.

Consequences

 * Confidentiality: Since SQL databases generally hold sensitive data, loss of confidentiality is a frequent problem with SQL Injection vulnerabilities.


 * Authentication: If poor SQL commands are used to check user names and passwords, it may be possible to connect to a system as another user with no previous knowledge of the password.


 * Authorization: If authorization information is held in an SQL database, it may be possible to change this information through the successful exploitation of an SQL Injection vulnerability.


 * Integrity: Just as it may be possible to read sensitive information, it is also possible to make changes or even delete this information with an SQL Injection attack.

Exposure period

 * Requirements specification: A non-SQL style database which is not subject to this flaw may be chosen.


 * Implementation: If SQL is used, all flaws resulting in SQL Injection problems must be mitigated at the implementation level.

Platform

 * Language: SQL


 * Platform: Any (requires interaction with an SQL database)

Required resources
Any

Severity
Medium to High

Likelihood of exploit
Very High

Avoidance and mitigation

 * Requirements specification: A non-SQL style database which is not subject to this flaw may be chosen.


 * Implementation: Use vigorous white-list style checking on any user input that may be used in an SQL command. Rather than escape meta-characters, it is safest to disallow them entirely. Reason: Later use of data that has been entered in the database may neglect to escape meta-characters before use.

Discussion
SQL Injection has become a common issue with database-driven web sites. The flaw is easily detected, and easily exploited, and as such, any site or software package with even a minimal user base is likely to be subject to an attempted attack of this kind.

Essentially, the attack is accomplished by placing a meta character into data input to then place SQL commands in the control plane, which did not exist there before. This flaw depends on the fact that SQL makes no real distinction between the control and data planes.

Examples
In SQL:

select id, firstname, lastname from writers

If one provided:

Firstname: evil'ex Lastname: Newman

the query string becomes:

select id, firstname, lastname from authors where forename = 'evil'ex' and surname ='newman' which the database attempts to run as

Incorrect syntax near al' as the database tried to execute evil.

The above SQL statement could be Coded in Java as:

String firstName = requests.getParameters("firstName"); String lasttName = requests.getParameters("firstName"); // FIXME: query string doesn't use parameter and shouldn't be a PreparedStatement PreparedStatement writersAdd = conn.prepareStatement("SELECT id FROM writers WHERE firstname=firstName");

In which some of the same problems exist.

Related problems

 * Injection problems