如此高效通用的分页存储过程是带有sql注入漏洞的

简介: 原文:如此高效通用的分页存储过程是带有sql注入漏洞的在google中搜索“分页存储过程”会出来好多结果,是大家常用的分页存储过程,今天我却要说它是有漏洞的,而且漏洞无法通过修改存储过程进行补救,如果你觉得我错了,请读下去也许你会改变看法。
原文: 如此高效通用的分页存储过程是带有sql注入漏洞的

google中搜索“分页存储过程”会出来好多结果,是大家常用的分页存储过程,今天我却要说它是有漏洞的,而且漏洞无法通过修改存储过程进行补救,如果你觉得我错了,请读下去也许你会改变看法。

 

通常大家都会认为存储过程可以避免sql注入的漏洞,这适用于一般的存储过程,而对于通用分页存储过程是不适合的,请看下面的代码和分析!

 

一般的通用的分页存储过程代码如下:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 通用分页存储过程
img_a6339ee3e57d1d52bc7d02b338e15a60.gifCREATE PROCEDURE pagination
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@tblName varchar(255), -- 表名 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@strGetFields varchar(1000= '*'-- 需要返回的列 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@fldName varchar(255)=''-- 排序的字段名 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@PageSize int = 10-- 页尺寸 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@PageIndex int = 1-- 页码 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@doCount bit = 0-- 返回记录总数, 非 0 值则返回 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@OrderType bit = 0-- 设置排序类型, 非 0 值则降序 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
@strWhere varchar(1500= '' -- 查询条件 (注意: 不要加 where) 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
AS 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
declare @strSQL varchar(5000-- 主语句 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
declare @strTmp varchar(110-- 临时变量 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
declare @strOrder varchar(400-- 排序类型 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
if @doCount != 0 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
begin 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
if @strWhere !='' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where '+@strWhere 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
else 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strSQL = 'select count(*) as Total from [' + @tblName + ']' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
end 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
else 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
begin 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
if @OrderType != 0 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
begin 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strTmp = '<(select min' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strOrder = ' order by [' + @fldName +'] desc' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
--如果@OrderType不是0,就执行降序,这句很重要! 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
end 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
else 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
begin 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strTmp = '>(select max' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strOrder = ' order by [' + @fldName +'] asc' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
end 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
if @PageIndex = 1 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
begin 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
if @strWhere != '' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
else 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from ['+ @tblName + ''+ @strOrder 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
--如果是第一页就执行以上代码,这样会加快执行速度 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
end 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
else 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
begin 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
--以下代码赋予了@strSQL以真正执行的SQL代码 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from [' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' ['+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'+ @strOrder 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
if @strWhere != '' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set @strSQL = 'select top ' + str(@PageSize+' '+@strGetFields+ ' from [' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '([' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' [' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' ' 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
end 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
end 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
exec (@strSQL)
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
GO
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

 

大家可以看到上面的存储过程中是通过一些步骤最终拼接成一个sql字符串,然后通过exec执行这个串得到分页的结果。

 

我们假定要做一个这样的查询,通过用户名UserName模糊查询用户,为了叙述方便,便于理解我们只考虑取第一页的情况,取出存储过程中取第一页的拼串行如下:

img_a6339ee3e57d1d52bc7d02b338e15a60.gif set   @strSQL   =   ' SELECT TOP  '   +   str ( @PageSize +   '   '   +   @strGetFields   +   '  from [ '   +   @tblName   +   ' ]  where  '   +   @strWhere   +   '   '   +   @strOrder

 

为了便于说明问题,我们可以假定

@pageSize 20 @strGetFields ‘*’ @tblName UserAccount,@strOrder ’ ORDER  BY  ID DESC’ 那么上面一行可以写成如下形式:

img_a6339ee3e57d1d52bc7d02b338e15a60.gif set   @strSQL   =   ' SELECT TOP 20 *  from [UserAccount]  where  '   +   @strWhere    +   '  ORDER BY ID DESC’

 

我们可以假定用户输入的模糊用户名是: Jim’s dog

我们用SqlParameter传递参数给分页存储过程@strWhere 的值是:’UserName LIKE ‘’%Jim’’ dog%’’’(注意LIKE后边的字符串中的单引号已经全部变成两个单引号了),我们将这个值代入上面的@strSQL赋值语句中,如下:

 

img_a6339ee3e57d1d52bc7d02b338e15a60.gif set   @strSQL   =   ' SELECT TOP 20 *  from [UserAccount]  where  UserName LIKE  '' %Jim ''  dog% ''  ORDER BY ID DESC’

 

让我们写上声明变量的部分执行在查询分析器中测试一下,代码如下:

 

img_a6339ee3e57d1d52bc7d02b338e15a60.gif DECLARE   @strSQL   varchar ( 8000 )
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
DECLARE   @strWhere   varchar ( 1000 )
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
SET   @strWhere   =   ' UserName LIKE  '' %Jim ''  dog% '''
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
set   @strSQL   =   ' SELECT TOP 20 *  from [UserAccount]  where  '   +   @strWhere   +   '  ORDER BY ID DESC '
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
print   @strSQL
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
exec  ( @strSQL )
img_a6339ee3e57d1d52bc7d02b338e15a60.gif


大家可以把上面几行代码粘贴到查询分析器中执行一下,就可以看到下面的画面:

img_220a628374d75293afd1f9f921116a41.jpg
在消息的第一行,打印出了要执行的sql语句,很显然此语句的 LIKE ‘%Jim’ 后面的部分全部被截断了,也就是说如果用户输入的不是Jim’s dog而是Jim’ delete from UserAccount那么会正确的执行删除操作,传说中的sql注入就会出现了。

 

问题出现了,我们应该怎么解决问题?

1.  很显然我们使用SqlParameter传递参数已经将单引号替换成了连个单引号了,可是因为我们在数据库中拼串导致替换并不能解决问题。

2.  根据我的实验证明如果使用存储过程不可能解决这个问题,我们只有将这个存储过程要执行的拼串操作放到数据访问层去做,才可以避免这个问题。

 

如果大家有在存储过程中解决这个问题的办法,请不吝赐教。

备注:本文说的是MS SQL Server2000 的数据库,而非使用SQL 2005的新特性分页。

目录
相关文章
|
1月前
|
SQL 监控 安全
SQL注入的实现原理以及防止
SQL注入的实现原理以及防止
|
2月前
|
SQL 数据库
20、绕过去除and、or、union select、空格的sql注入
20、绕过去除and、or、union select、空格的sql注入
32 0
|
2月前
|
SQL 数据库
小课堂 -- 绕过去除特殊字符的sql注入
小课堂 -- 绕过去除特殊字符的sql注入
22 0
|
2月前
|
SQL Java 数据库连接
[SQL]SQL注入与SQL执行过程(基于JDBC)
[SQL]SQL注入与SQL执行过程(基于JDBC)
50 0
|
1月前
|
SQL Java 应用服务中间件
Java项目防止SQL注入的四种方案
Java项目防止SQL注入的四种方案
40 0
|
2月前
|
SQL 安全 关系型数据库
接上篇文章,在测试宝塔 WAF 的未授权访问漏洞时无意间还发现了一个 SQL 注入漏洞
接上篇文章,在测试宝塔 WAF 的未授权访问漏洞时无意间还发现了一个 SQL 注入漏洞,品相还不错,可执行任意 SQL 语句。 总之,吃了一惊,一个防 SQL 注入的工具居然也有 SQL 注入漏洞。 请看这段代码
416 1
|
1月前
|
存储 SQL 数据库
sql serve存储过程
sql serve存储过程
14 0
|
8天前
|
SQL 安全 Go
如何在 Python 中进行 Web 应用程序的安全性管理,例如防止 SQL 注入?
在Python Web开发中,确保应用安全至关重要,主要防范SQL注入、XSS和CSRF攻击。措施包括:使用参数化查询或ORM防止SQL注入;过滤与转义用户输入抵御XSS;添加CSRF令牌抵挡CSRF;启用HTTPS保障数据传输安全;实现强身份验证和授权系统;智能处理错误信息;定期更新及审计以修复漏洞;严格输入验证;并培训开发者提升安全意识。持续关注和改进是保证安全的关键。
17 0
|
16天前
|
SQL 安全 PHP
CTF--Web安全--SQL注入之Post-Union注入
CTF--Web安全--SQL注入之Post-Union注入
|
1月前
|
SQL 安全 测试技术
如何在 Python 中进行 Web 应用程序的安全性管理,例如防止 SQL 注入?
如何在 Python 中进行 Web 应用程序的安全性管理,例如防止 SQL 注入?
15 0