1

參數化的靜態/代碼SQL語句是否受SQL注入攻擊?代碼/靜態SQL(T-SQL)中的SQL注入

例如,假設我有以下簡化存儲過程: 請問,我讓輸入@PSeries_desc意味着我必須遵守注入攻擊,如果它是參數化的事實呢? 以前,這是一個動態的SQL語句,代碼是使用exec而不是sp_executesql因此,它絕對是受到攻擊的。

CREATE procedure get_product_by_title 
    @PSearchType int = NULL 
    , @Pseries_desc varchar(40) = NULL 
as 
begin 

declare 
    @whereLikeBeg varchar(1) 
, @whereLikeEnd varchar(1) 

set @whereLikeBeg = '' 
set @whereLikeEnd = '' 

if @search_code = 'contains' 
begin 
    set @whereLikeBeg = '%' 
    set @whereLikeEnd = '%' 
end 

if @search_code = 'starts_with' 
begin 
    set @whereLikeEnd = '%' 
end 

select 
    distinct B.parent_product_id 
    , B.parent_product_id 
from 
    tableA 

where 
    parent_product_id = child_product_id 
    and product_title like @whereLikeBeg + @Pseries_desc + @whereLikeEnd 
end 
+0

您確實應該從DAL中顯示調用代碼(C#或VB)以查看其安全性。 – m4tt1mus 2011-03-29 18:26:59

+0

不幸的是,現有的代碼基於經典的ASP。傳統應用程序的萬歲! – Abe 2011-03-29 18:43:18

回答

1

此代碼看起來安全,我...

參數化查詢不保護自己免受SQL注入攻擊的唯一方法,但它可能做的最簡單,最安全的方式。

即使您忘記了sql注入攻擊,動態構建查詢也不是一個好習慣,尤其是當您使用字符串時,因爲它們可能包含會影響查詢的SQL保留字/字符。

+0

我同意。我只有在必須時纔會使用動態SQL。即使使用sp_executesql並傳入參數化數據,調試仍然非常困難。 – Abe 2011-03-29 18:23:13

1

如果您在訪問代碼中使用參數化查詢,則無需擔心。在存儲過程中檢查它是不合適的。