ISSN : 2349-3917
SQL Injection is a major injection technique, which is used to attack data-driven Applications.
Procedures and functions that use dynamic SQL queries by concatenating the text inputs to the dynamic SQL are prone to SQL Injection attack as someone can provide extra commands/malicious text through the input parameter and when executed can result in the unexpected results.
Example
Here, if the user will provide @user_id = ‘105; DROP TABLE SomeTable’, an unexpected DROP table will happen.
SQL Injection is a major injection technique, which is used to attack data-driven Applications.
Procedures and functions that use dynamic SQL queries by concatenating the text inputs to the dynamic SQL are prone to SQL Injection attack as someone can provide extra commands/malicious text through the input parameter and when executed can result in the unexpected results.
Execute Dynamic SQL queries, using SP_EXECUTESQL procedure with the parameters.
While writing dynamic SQL queries and executing them, one needs to be cautious in regards to the following.
1. Avoid concatenating the parameter variables with the query.
Example
2. Avoid executing dynamic SQL queries, using EXEC stored procedure. This approach does not support passing of parameters.
Always use SP_EXECUTESQL procedure with the parameters to execute dynamic SQL queries.
Example
Let @arg1 be the parameter supplied to the procedure, which contains the script, mentioned below.
• 1. The first 2 parameters of SP_EXECUTESQL (@cmd and @parameters should always be of type nvarchar.
• 2. If the dynamic SQL requires multiple string parameters, the parameters can be written separated by commas.
Example
Here, is a complete example, which demonstrates the usage of dynamic SQL in a stored procedure in the correct way.
when we use the parameters supplied to a procedure in a dynamic SQL command and execute it, using EXEC procedure, there is a chance the input parameter can be used to hack into the database object.
Example
This works fine but if I pass something like this as @search_string, the code will be as follows.
This will list out every record from the dbo.My Servers table as the command, which will go to the db. Will be.
SELECT * FROM dbo.MyServers WHERE server_name LIKE '%u' OR 1=1 --%'
Here, the best practice is to embed the parameters (search string) in the dynamic SQL command and execute it, using SP_EXECUTESQL with the parameters, as shown below
If the supplied pattern matches, the query upon execution will generate the appropriate records.
If a malicious pattern is supplied, the execution will result in an empty result set . Please follow the example, stated below.
This will result in an empty resultset and our data will not show up.
Guidelines to use table/column names in dynamic SQL:
While using the table/column names as the parameters in a dynamic SQL command, the system defined function QUOTENAME should be used to enclose the table/column name with in [ and ].
Example
Here, the @tabname variable can be used to manipulate the database in a wrong way. To prevent it, @tabname should be enclosed within [and] as in this case [My Servers; drop table dbo.My Configs] will not be considered as a valid table name.
Invalid Object name ‘MyServers;PRINT ‘HELLO’’.
Here is another example, where both column and table names are used in a dynamic SQL query.
Someone can push something dangerous through the column name.
This will print out all the Server names from your dbo.MyServers table.
This should be rewritten, as stated below.
Invalid column name 'server_name FROM dbo.MyServers;PRINT 'HELLO BRO! U R HACKED