5

I heard that SQL Injection can still be possible when using ADO.NET SQLParameter (paramterised query) for SQL Server.

I am looking for real examples in C#/VB code as proof.

EDIT: I am looking for SPECIFIC working examples. Not introductions to sql injection or how to prevent it.

5 Answers 5

3

If you're creating a statement in a stored proc and using sp_executesql, a parameterized query is a false safety net.

Sign up to request clarification or add additional context in comments.

2 Comments

I searched briefly months ago, and this is the only vulnerability I could find. If you have to use sp_executesql, you got larger problems anyway.
I guess the same can be said for executing dynamic queries "exec 'select * from tab where f1 = ' + @param1"
3

There is another question here on SO with some great answers on SQL Injection...

Are Parameters really enough to prevent Sql injections?

This example comes directly from the link above from Steven A. Lowe.

An example, where parameter @p1 is intended to be a table name

create procedure dbo.uspBeAfraidBeVeryAfraid ( @p1 varchar(64) ) 
AS
    SET NOCOUNT ON
    declare @sql varchar(512)
    set @sql = 'select * from ' + @p1
    exec(@sql)
GO

Here is some further reading...

2 Comments

Thanks but none of these links have working examples. I am already familiar of the fact to use paramterised queries. Hence my question on how to bypass it.
@Tony: Did you look at the link for the other answer here on SO? There is a great example in there in ansewr #2 from Mr. Lowe.
2

A specific example...

create procedure dbo.spVulnerable
@firstname varchar(200)
as
exec ('select id from tblPerson where firstname = ''' + @firstname + '''')
go

I might have gotten the syntax wrong but no matter how you parameterize @firstname it is still vulnerable to something like the following:

"Joe' or 1=1"

As long as you use dynamic SQL you are potentially vulnerable to SQL injection. Other than using parameterized SQL, the only solution is to whitelist your input (or, if you're feeling brave, try to strip the input of dangerous characters).

Comments

0

Try searching for vulnerabilities in ADO.net, there can be a security flaw.

1 Comment

I am looking for specific examples.
0

Say you have a products catalog for your web size and your search page allows to search by product name, description, color and size (say you sell bras):

create table [products] (
    product_id int identity(1,1) not null primary key
    , name varchar(256)
    , description varchar(max)
    , color varchar(256)
    , size varchar(256));
GO  

create procedure usp_dynamicSearch
    @product varchar(256) = NULL
    , @description varchar(256) = NULL
    , @color varchar(256) = NULL
    , @size varchar(256) = NULL
as
begin
    set nocount on;
    declare @sql nvarchar(max)
        , @and nvarchar(5);
    set @sql = N'SELECT 
        product_id, name, description, color, size 
        FROM products
        WHERE ';
    set @and = N'';
    if (@product is not null) 
    begin
        set @sql = @sql + N'name LIKE ''' + @product + N'''';
        set @and = N' AND ';
    end
    if (@description is not null) 
    begin
        set @sql = @sql + @and + N'description LIKE ''' + @description + N'''';
        set @and = N' AND ';
    end
    if (@color is not null) 
    begin
        set @sql = @sql + @and + N'color = ''' + @color + N'''';
        set @and = N' AND ';
    end
    if (@size is not null) 
    begin
        set @sql = @sql + @and + N'size = ''' + @size + N'''';
    end 
    exec sp_executesql @sql;
end
GO

You use a stored procedure that dynamically constructs a SQL appropiate for the search. You invoke it by passing parameters:

exec usp_dynamicSearch @color = N'Red', @size = N'58-DD';

Since the procedure constructs dynamic SQL in a careless fashion, is still open to SQL injection:

exec usp_dynamicSearch @color = N'Red', @size = N''';
INSERT INTO products (name, description) 
values (''31337'', ''haxorz!''); 
--';

The unwanted product was sinserted in the catalog (to make this a bening attack...). In this case, the appropiate fix is to use parameters in the dynamic SQL as well and further pass the parameters to the sp_executesql invocation:

alter procedure usp_dynamicSearch
    @product varchar(256) = NULL
    , @description varchar(256) = NULL
    , @color varchar(256) = NULL
    , @size varchar(256) = NULL
as
begin
    set nocount on;
    declare @sql nvarchar(max)
        , @and nvarchar(5);
    set @sql = N'SELECT 
        product_id, name, description, color, size 
        FROM products
        WHERE ';
    set @and = N'';
    if (@product is not null) 
    begin
        set @sql = @sql + N'name LIKE @product';
        set @and = N' AND ';
    end
    if (@description is not null) 
    begin
        set @sql = @sql + @and + N'description LIKE @description';
        set @and = N' AND ';
    end
    if (@color is not null) 
    begin
        set @sql = @sql + @and + N'color = @color';
        set @and = N' AND ';
    end
    if (@size is not null) 
    begin
        set @sql = @sql + @and + N'size = @size';
    end 
    exec sp_executesql @sql , N'@product varchar(256)
        , @description varchar(256)
        , @color varchar(256)
        , @size varchar(256)'
        , @product, @description, @color, @size;
end
GO

So sp_executesql and dynamic SQL is the main concern. Other than that, there are also various system procedures that build dynamic SQL under the covers and historically some were proven to be vulnerable, specially on SQL 2000.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.