网络编程
位置:首页>> 网络编程>> Asp编程>> 怎么样才能让ASP避免被SQL注入啊?

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

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

标签:注入,sql,安全

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

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

 

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com