Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like clause and sql injection

Tags:

sql

tsql

I am having a doubt about this situation.

I've a query like this within a Stored Procedure:

SELECT column1, column2
FROM table1
WHERE column1 like '%' + @column1 + '%'

My question is, this is vulnerable to SQL Injection? Do I need to change this to something like this: (?)

declare @column1Like nvarchar(200);

@column1Like = '%' + @column1 + '%'

SELECT column1, column2
FROM table1
WHERE column1 like @column1Like

Regards

like image 415
Bruno Costa Avatar asked Jan 26 '11 13:01

Bruno Costa


2 Answers

The quick answer is no. To be vulnerable to SQL injection one must be using dynamic SQL execution.

This would be vulnerable:

EXECUTE ('SELECT column1, column2 FROM table1 WHERE column1 like ' + @column1Like);

That also means there is no real difference between both of your examples (from a security standpoint at least).

like image 165
rsenna Avatar answered Oct 20 '22 23:10

rsenna


I think it is vulnerable, for example : '%' or 1=1-- will show all registers of the database if you don´t format it like @column1Like.

In this case, I think it´s the same than (@column1Like= '' or @column1Like is null) but you must think another examples like

'%' union select SELECT `column11`, `column22`
FROM table2 where `colum11` -- is the same type than `column1` 
--and `column22` is the same type than `column22`.
like image 27
user1411821 Avatar answered Oct 20 '22 21:10

user1411821