| Sql Injection |
Article Index for Sql |
Website Links For Sql |
Information AboutSql Injection |
| CATEGORIES ABOUT SQL INJECTION | |
| data management | |
| security exploits | |
| injection exploits | |
| sql | |
|
FORMS OF SQL INJECTION VULNERABILITIES Incorrectly filtered escape characters This form of SQL injection occurs when user input is not filtered for Escape Characters and is then passed into a SQL statement. This results in the potential manipulation of the statements performed on the database by the end user of the application. The following line of code illustrates this vulnerability:
If the "userName" variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the "userName" variable as a' or 't'='t renders this SQL statement by the parent language:
If this code were to be used in an authentication procedure then this example could be used to force the selection of a valid username because the evaluation of 't'='t' is always true. On MS SQL Server any valid SQL command may be injected via this method, including the execution of multiple statements. The following value of "userName" in the statement below would cause the deletion of the "users" table as well as the selection of all data from the "data" table:
This input renders the final SQL statement as follows:
Incorrect type handling This form of SQL injection occurs when a user supplied field is not Strongly Typed or is not checked for Type constraints. This could take place when a numeric field is to be used in a SQL statement, but the programmer makes no checks to validate that the user supplied input is numeric. For example:
It is clear from this statement that the author intended a_variable to be a number correlating to the "id" field. However, if it is in fact a String then the end user may manipulate the statement as they choose, thereby bypassing the need for escape characters. For example, setting a_variable to 1;DROP TABLE users will delete the "users" table from the database as the rendered SQL would be rendered as follows:
=== Vulnerabilities ins SECURING APPLICATIONS AGAINST SQL INJECTION Application remediation SQL injection is easy to work around in most Programming Languages that target web applications or offer functionality. In Perl DBI , the DBI::quote method escapes special characters (assuming the variable holds a reference to a DBI object): = ->prepare (
. ->quote() ); However, this is generally not the best way to approach the issue. DBI allows the use of placeholders, which let you bind data to a statement separately to defining the SQL statement. For databases that do not natively support placeholders, DBI emulates them by automatically applying the DBI::quote function to the values. Many databases do support binding values separately via their APIs; DBI will use the native placeholder support in this case. For example:
->execute(); The advantage is that you do not have to remember to apply DBI::quote to every value. It is either bound separately, or quoted appropriately, depending on the support offered by the particular DBMS you are using. You then avoid the basic issue of SQL injection where values are interpreted as SQL. For databases that support placeholders natively, there are often significant performance advantages to using placeholders, as the database can cache the compiled representation of a statement and reuse it between executions with different bound values. Placeholders are also sometimes referred to as 'bind variables' or simply 'parameters'. In PHP , there are different built-in functions to use for different DBMSes for escaping values suitable for embedding in literal SQL statements. For MySQL , the equivalent is the built-in function mysql_real_escape_string: = mysql_query (
. mysql_real_escape_string(, Resource id #7) . "'" ); The native interface to a particular DBMS may also offer a method of binding placeholders separately, for example, mysql_stmt_bind_param , or oci_bind_by_name . Alternatively, a database abstraction library can be used to emulate placeholders in a similar way to Perl's DBI. One example out of several available libraries is ADOdb . In the Java programming language, the equivalent is the PreparedStatement class. Instead of Connection con = (acquire Connection) Statement stmt = con.createStatement();
use the following Connection con = (acquire Connection)
pstmt.setString(1, userName); ResultSet rset = pstmt.executeQuery(); In the .NET or Mono programming language " C# ", the equivalent are the ADO.NET SqlCommand (for Microsoft SQL Server) or OracleCommand (for Oracle's database server) objects. The example below shows how to prevent injection attacks using the SqlCommand object. The code for other ADO.NET providers is very similar, but may vary slightly depending on the specific implementation by that provider vendor. Instead of using( SqlConnection con = (acquire connection) ) { con.Open();
using( SqlDataReader rdr = cmd.ExecuteReader() ){ ... } } } use the following using( SqlConnection con = (acquire connection) ) { con.Open();
cmd.Parameters.AddWithValue("@userName", userName); using( SqlDataReader rdr = cmd.ExecuteReader() ){ ... } } } Database remediation Security privileges Setting security privileges on the database to the least-required is a simple remediation. Few applications will require that the application user has delete rights to a table or database. This strategy does not solve the SQL injection problem, but it may limit the potential damage. Stored procedures Most databases also offer the capability of preparing SQL statements at the database layer via Stored Procedure s. Rather than using an application layer to construct SQL dynamically, stored procedures encapsulate reusable database procedures that are called with Typed parameters. This provides several security advantages: by parameterizing input parameters and type-enforcing them, user input is effectively filtered. In addition, most databases allow stored procedures to execute under different security privileges from the database user. For instance, an application would have execute access to a stored procedure, but no access to the base tables. This restricts the ability of the application to do anything beyond the actions specified in the stored procedures. However, using stored procedures does not solve the code injection problem, if user input is not parameterized or filtered, as in the following (constructed) example: Given two stored procedures GET_PASSWORD(userName) and GET_USER(userName, password), an attacker could still inject code into a GET_USER call if the password is not correctly escaped: |
|
|