怎么样才能让ASP避免被SQL注入啊?

来源:asp之家 时间:2008-08-08 12:27:00 

某人使用如下过滤代码,发现经常被黑:

n=request.form("username")
p=request.form("userchr")
if instr(n,"'")<>0 then response.end
if instr(n,"=")<>0 then response.end
if instr(n,"%")<>0 then response.end
if n<>"" and p<>"" then
set rs1 = Server.CreateObject("ADODB.Recordset")
sql="select * from Character where AccountID='"&n&"' and Name='"&p&"' and pklevel<>3"
rs1.open sql,conn,1,3

下面是网友的一些建议:

只对username进行了过滤,不够严密饿.:)

没有对userchr进行过滤,导致SQL注入..

用这句就可以把数据库改了:
XX';update character set clevel=200 where accountID='YOUID';--

我总结了被黑的经验:

1.禁止远程提交:

server_v1=Cstr(Request.ServerVariables("HTTP_REFERER"))
server_v2=Cstr(Request.ServerVariables("SERVER_NAME"))
if mid(server_v1,8,len(server_v2))<>server_v2 then
response.write "<br><br><center><table border=1 cellpadding=20 bordercolor=black bgcolor=#EEEEEE width=450>"
response.write "<tr><td style='font:9pt Verdana'>"
response.write "你提交的路径有误,禁止从站点外部提交数据!"
response.write "</td></tr></table></center>"
response.end
end if

常用的非法字串提交,尤其有有查询的页面,或写入数据库,尽量不要GET方式提交

过滤大部分敏感字符函数:

 

sub checkdata(datavalue)
if instr(datavalue,";")<>0 or instr(datavalue,">")<>0 or instr(datavalue,"<")<>0 or instr(datavalue,")")<>0 or instr(datavalue,"(")<>0 or instr(datavalue,"'")<>0 or instr(datavalue,"/")<>0 or instr(datavalue,"\")<>0 or instr(datavalue,",")<>0 or instr(datavalue," ")<>0 or instr(datavalue," ")<>0 or instr(datavalue,chr(13))<>0 or instr(datavalue,"&")<>0 or instr(datavalue,"%")<>0 or instr(datavalue,"=")<>0 then
response.write "<script language=javascript>alert('对不起!您所输入的数据包含非法字符');history.back()</script>"
response.end
end if
end sub

过滤一部分:

sub mincheckdata(datavalue)
if instr(datavalue,";")<>0  or instr(datavalue,"(")<>0 or instr(datavalue,"'")<>0 or instr(datavalue,"=")<>0 or instr(datavalue,"%")<>0 or instr(datavalue,"&")<>0 then
response.write "<script language=javascript>alert('对不起!您所输入的数据包含非法字符');history.back()</script>"
response.end
end if
end sub

 

标签:注入,sql,安全
0
投稿

猜你喜欢

  • 长文章自动分页asp实例-支持HTML

    2007-10-10 21:29:00
  • 影响SEO的页面制作细节

    2008-10-18 16:06:00
  • 巧用Dreamweaver制作复杂图像

    2010-09-02 12:34:00
  • 一场关于YUI3/jQuery的精彩辩论

    2010-11-11 12:50:00
  • SQL离前端有多远?

    2009-05-21 10:37:00
  • MySQL查询不含周末的五天前的日期

    2008-11-11 12:28:00
  • CSS实现HTML元素透明的那些事

    2010-02-01 12:34:00
  • asp使用ServerVariables集合

    2008-02-27 13:22:00
  • sqlserver中去除字符串中连续的分隔符的sql语句

    2012-06-06 20:07:39
  • CSS结合js实现动态更换皮肤

    2007-07-14 10:01:00
  • 巧用Dreamweaver MX控制页面元素

    2009-09-13 18:38:00
  • ASP访问SQL Server内置对象

    2008-04-05 06:49:00
  • INSERT INTO SELECT语句与SELECT INTO FROM语句的一些区别

    2012-06-06 19:38:28
  • 重温Javascript继承机制

    2011-07-04 12:17:23
  • asp文章上一篇,下一篇实现代码

    2008-03-24 20:15:00
  • 教程:打造SQL Server2000的安全策略

    2008-12-23 15:52:00
  • 网页设计:把导航系统做薄

    2007-12-28 12:02:00
  • Adobe发布Flash Player 10正式版

    2008-10-15 17:15:00
  • asp如何删除数据库中的表或索引?

    2010-06-26 12:23:00
  • 如何清除Vbscript惹出来的中文乱码?

    2010-01-18 20:50:00
  • asp之家 网络编程 m.aspxhome.com