2009-09-15 36 views

回答

3

如果要在存儲過程中創建語句並使用sp_executesql,則參數化查詢是錯誤的安全網。

+0

我在*幾個月前搜索了*,這是我能找到的唯一漏洞。如果您必須使用sp_executesql,則無論如何您都會遇到更大的問題。 – mxmissile 2009-09-15 22:27:38

+0

我想同樣可以說執行動態查詢「exec」選擇*從選項卡其中f1 ='+ @ param1「 – pauloya 2009-09-15 22:46:28

+1

我不使用sp_executesql。 – 2009-09-16 04:05:13

3

這裏有對SO與SQL注入一些偉大的答案另一個問題...

Are Parameters really enough to prevent Sql injections?

這個例子從史蒂芬答洛韋上面的鏈接直接來了。

一個例子,在參數@ P1旨在成爲一個表名

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

下面是一些進一步閱讀...

+0

謝謝但這些鏈接都沒有工作示例。我已經熟悉使用參數化查詢的事實。因此我的問題是如何繞過它。 – 2009-09-15 22:23:32

+0

@Tony:你看過這裏的其他答案的鏈接嗎? Lowe先生在第二張專輯中有一個很好的例子。 – RSolberg 2009-09-15 22:24:23

0

嘗試搜索ADO.net中的漏洞,可能存在安全漏洞。

+0

我正在尋找具體的例子。 – 2009-09-15 22:18:17

2

一個具體的例子...

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

我可能已經得到了語法錯誤,但不管你怎麼參數@firstName仍然容易受到類似以下內容:

"Joe' or 1=1" 

由於只要您使用動態SQL,您就可能會受到SQL注入的攻擊。除了使用參數化的SQL之外,唯一的解決方案是將您的輸入列入白名單(或者,如果您感覺很勇敢,請嘗試去除危險字符的輸入)。

0

假設你有一個產品目錄,爲您的網絡規模和搜索頁允許按產品名稱,描述,顏色和大小進行搜索(說你賣胸罩):

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 

您可以使用一個存儲過程,動態地構造一個適用於搜索的SQL。您可以通過傳遞參數來調用它:

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

由於程序構建動態SQL中一個不小心的方式,仍然是開放的SQL注入:

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

不需要的產品目錄中的sinserted(使這是一個攻擊...)。在這種情況下,此時,相應的解決辦法是在動態SQL中使用參數以及進一步傳遞參數給sp_executesql調用:

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 

因此sp_executesql的動態SQL是主要關注的問題。除此之外,還有各種系統過程構建動態SQL,並且歷史上有些被證明是易受攻擊的,特別是在SQL 2000上。

相關問題