Sql server 代码/静态SQL(T-SQL)中的SQL注入
参数化静态/代码SQL语句是否受到SQL注入攻击 例如,假设我有以下简化的存储过程: 我正在传递输入@PSeries\u desc这一事实是否意味着如果它被参数化,我会受到注入攻击? 以前,这是一个动态SQL语句,代码是使用Sql server 代码/静态SQL(T-SQL)中的SQL注入,sql-server,tsql,stored-procedures,sql-injection,parameterized,Sql Server,Tsql,Stored Procedures,Sql Injection,Parameterized,参数化静态/代码SQL语句是否受到SQL注入攻击 例如,假设我有以下简化的存储过程: 我正在传递输入@PSeries\u desc这一事实是否意味着如果它被参数化,我会受到注入攻击? 以前,这是一个动态SQL语句,代码是使用exec执行的,而不是sp_executesql,因此,它肯定会受到攻击 CREATE procedure get_product_by_title @PSearchType int = NULL , @Pseries_desc varchar(40) = NULL
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
这个密码对我来说很安全 参数化查询不是防止SQL注入攻击的唯一方法,但它可能是最简单、最安全的方法
即使您忘记了sql注入攻击,动态构建查询也不是一个好的做法,尤其是在处理字符串时,因为字符串可能包含sql保留字/字符,这将对您的查询产生影响。如果您在访问代码中使用参数化查询,您无需担心。在存储过程中检查它是不正确的。我同意。只有在必要时,我才会使用动态SQL。即使您使用sp_executesql并传入参数化数据,调试仍然非常困难。您确实应该显示DAL中的调用代码(C#或VB)以查看其是否安全。不幸的是,现有的代码库是经典ASP。旧式应用万岁!