January 10, 2024

SQL Injection

 SQL Injection 


SQL injection is an attack in which malicious code is inserted into strings that are later passed to an instance of SQL Server for parsing and execution. Any procedure that constructs SQL statements should be reviewed for injection vulnerabilities because SQL Server will execute all syntactically valid queries that it receives. Even parameterized data can be manipulated by a skilled and determined attacker.



How SQL Injection Works

The primary form of SQL injection consists of direct insertion of code into user-input variables that are concatenated with SQL commands and executed. A less direct attack injects malicious code into strings that are destined for storage in a table or as metadata. When the stored strings are subsequently concatenated into a dynamic SQL command, the malicious code is executed.

The injection process works by prematurely terminating a text string and appending a new command. Because the inserted command may have additional strings appended to it before it is executed, the malefactor terminates the injected string with a comment mark "--". Subsequent text is ignored at execution time.



The following script shows a simple SQL injection. The script builds an SQL query by concatenating hard-coded strings together with a string entered by the user:


C#



var vcustname;  

custname= Request.form ("vcustname");  

var sql = "select * from OrdersTable where custname= '" + custname+ "'";  

The user is prompted to enter the name of a Customer. If she enters “Robert”, the query assembled by the script looks similar to the following:


SQL


Copy

SELECT * FROM OrdersTable WHERE custname= 'Robert'  


However, assume that the user enters the following:


Robert'; drop table OrdersTable--  


In this case, the following query is assembled by the script:


SQL


Copy

SELECT * FROM OrdersTable WHERE custname= 'Robert';drop table OrdersTable--'  



The semicolon (;) denotes the end of one query and the start of another. The double hyphen (--) indicates that the rest of the current line is a comment and should be ignored. If the modified code is syntactically correct, it will be executed by the server. When SQL Server processes this statement, SQL Server will first select all records in OrdersTable where custname= 'Robert'. Then, SQL Server will drop OrdersTable.


How to prevent SQL Injection?


You must validate all user input and carefully review code that executes constructed SQL commands in the server that you are using. 

Follow Coding best practices 


  • Validate the size, type, or content of the data that is received by your application

  • Never concatenate user input that is not validated. String concatenation is the primary point of entry for script injection.

  • Never build Transact-SQL statements directly from user input.

  • Stored procedures may be susceptible to SQL injection if they use unfiltered input. If you use stored procedures, you should use parameters as their input.

  • Use the Parameters Collection with Dynamic SQL

  • If you are using a LIKE clause, wildcard characters still must be escaped.

  • Filtering input may also be helpful in protecting against SQL injection by removing escape characters.

  • How will your application behave if an errant or malicious user enters a 10-megabyte MPEG file where your application expects a postal code?

  • How will your application behave if a DROP TABLE, DELETE FROM  statement is embedded in a text field?

  • Test the size and data type of input and enforce appropriate limits. This can help prevent deliberate buffer overruns.

  • Test the content of string variables and accept only expected values. Reject entries that contain binary data, escape sequences, and comment characters. This can help prevent script injection and can protect against some buffer overrun exploits.

  • When you are working with XML documents, validate all data against its schema as it is entered.

  • In multitiered environments, all data should be validated before admission to the trusted zone. Data that does not pass the validation process should be rejected and an error should be returned to the previous tier.

  • Implement multiple layers of validation. Precautions you take against casually malicious users may be ineffective against determined attackers. A better practice is to validate input in the user interface and at all subsequent points where it crosses a trust boundary. For example, data validation in a client-side application can prevent simple script injection. However, if the next tier assumes that its input has already been validated, any malicious user who can bypass a client can have unrestricted access to a system.







No comments:

Creating DataFrames from CSV in Apache Spark

 from pyspark.sql import SparkSession spark = SparkSession.builder.appName("CSV Example").getOrCreate() sc = spark.sparkContext Sp...