like语句是否吸引SQL注入

时间:2019-02-04 07:58:07

标签: sql sql-server

执行如下所述的like语句会导致任何安全问题或SQL注入。 而且,如果可以,如何减轻风险?

以下脚本将在存储过程中执行;使用like查询的推荐方法是什么?

此代码段是从参数化存储过程中复制的。

declare @args varchar(100) ='SID%' -- start with
-- set @args ='SID01' -- exact match
-- set @args ='%%' -- all
-- set @args = '%VV%' -- contains
-- set @args ='SID%' -- start with
-- set @args ='%JUN' -- end with
select * from Employee e where e.LastName like @args

1 个答案:

答案 0 :(得分:2)

如果在SQL中使用类型变量作为参数,则可以免受SQL Injection攻击。 当攻击者可以访问动态SQL时,SQL注入才起作用。

这是不安全的:

DECLARE @sql nvarchar(50)=N'select * from Employee e where e.LastName like '''+@args+''''
EXEC sys.sp_executesql  @sql

这很安全:

select * from Employee e where e.LastName like @args
相关问题