ASP 防SQL注入的两种函数

本文介绍了一种在ASP中防止SQL注入的方法,通过定义函数SafeRequest来验证输入参数的类型,并提供了一段代码用于检查HTTP请求中的潜在危险字符串,确保应用安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Function SafeRequest(ParaName,ParaType) 
'--- 传入参数 --- 
'
ParaName:参数名称-字符型 
'
ParaType:参数类型-数字型(1表示以上参数是数字,0表示以上参数为字符) 

Dim ParaValue 
ParaValue
=Request(ParaName) 
If ParaType=1 then 
If not isNumeric(ParaValue) then 
Response.write 
"参数" & ParaName & "必须为数字型!" 
Response.end 
End if 
Else 
ParaValue
=replace(ParaValue,"'","''"
End if 
SafeRequest
=ParaValue 
End function 

用SafeRequest(ParaName,ParaType)代替request.form("")和request..querystring("")

   *********************************************************************************************************

-------------------------------------------------------------------------------------------------------------------------------------------

   *********************************************************************************************************

<%
dim sql_injdata
sql_injdata
="'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare"
sql_injHint
=replace(sql_injdata,"|"," ")
sql_injHint
=replace(sql_injHint,"'","'")
sql_inj
=split(sql_injdata,"|")

if request.querystring<>"" then
    
for each getData in request.querystring
    
for i=0 to ubound(sql_inj)
        
if instr(lcase(request.querystring(getData)),sql_inj(i))>0 then
        hint
="alert('为了保证用户的信息安全,请不要使用非法注入字符。如下字符为非法的: @sql_injHint@');"
        hint
=replace(hint,"@sql_injHint@",sql_injHint)
        response.write 
"<script language=javascript>"
        response.write hint
        response.write 
"history.back()"
        response.write 
"</script>"
        response.end
      
end if
    
next
    
next
end if

if request.form<>"" then
    
for each getData in request.querystring
    
for i=0 to ubound(sql_inj)
        
if instr(lcase(request.form(getData)),sql_inj(i))>0 then
        hint
="alert('为了保证用户的信息安全,请不要使用非法注入字符。如下字符为非法的: @sql_injHint@');"
        hint
=replace(hint,"@sql_injHint@",sql_injHint)
        response.write 
"<script language=javascript>"
        response.write hint
        response.write 
"history.back()"
        response.write 
"</script>"
        response.end
      
end if
    
next
    
next
end if
%>
将此段代码形成一个文件 (如:defanj.asp),将所有要用到数据库的文件头部加入<!--#include file=defanj.asp-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值