PHP安全新闻早8点特别篇-----ASP代码安全审计

这个还是值得深入学习一下的

<%

'正则表达式相关的变量
Dim regEx, Match, Match2, Matches, Matches2,ScriptName,UserTrueIP,sqltype,tupfile
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
regEx.MultiLine = True

ScriptName = Trim(Request.ServerVariables("SCRIPT_NAME"))
UserTrueIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If UserTrueIP = "" Then UserTrueIP = Request.ServerVariables("REMOTE_ADDR")
UserTrueIP = ReplaceBadChar(UserTrueIP)

Const EnableStopInjection = True '是否启用防SQL注入功能,True为启用,False为禁用
If EnableStopInjection = True Then
If Request.QueryString <> "" Then
Call StopInjection(Request.QueryString)
sqltype="QueryString"
End If
If Request.Cookies <> "" Then
Call StopInjection(Request.Cookies)
sqltype="Cookies"
End If
tupfile=LCase(Mid(ScriptName, InStrRev(ScriptName, "/") + 1))
If Request.Form<>"" Then
Call StopInjection2(Request.Form)
sqltype="Form"
End If

End If

'**************************************************
'函数名:ReplaceBadChar
'作 用:过滤非法的SQL字符
'参 数:strChar-----要过滤的字符
'返回值:过滤后的字符
'**************************************************
Function ReplaceBadChar(strChar)
If strChar = "" Or IsNull(strChar) Then
ReplaceBadChar = ""
Exit Function
End If
Dim strBadChar, arrBadChar, tempChar, i
strBadChar = "+,',%,^,&,?,(,),<,>,[,],{,},/,,;,:," & Chr(34) & "," & Chr(0) & ",--"
arrBadChar = Split(strBadChar, ",")
tempChar = strChar
For i = 0 To UBound(arrBadChar)
tempChar = Replace(tempChar, arrBadChar(i), "")
Next
tempChar = Replace(tempChar, "@@", "@")
ReplaceBadChar = tempChar
End Function

Sub StopInjection(Values)
Dim FoundInjection
regEx.Pattern = "'|;|#|([\s\b+()]+(select|insert|delete|declare|alter|create|backup|exists|daxia123|jxmmtv|<script|/script>)[\s\b+]*)"
Dim sItem, sValue
For Each sItem In Values
sValue = Values(sItem)
If regEx.Test(sValue) Then
FoundInjection = True
Response.Write "很抱歉,由于您提交的内容中含有危险的SQL注入代码,致使本次操作无效! "
Response.Write "<br>字段名:" & sItem
Response.Write "<br>字段值:" & sValue
Response.Write "<br>关键字:"
Set Matches = regEx.Execute(sValue)
For Each Match In Matches
Response.Write FilterJS(Match.value)
FilterJStb=FilterJStb&FilterJS(Match.value)
Next
Call LogtoTxt(sqltype,sItem,sValue,FilterJStb)
'Response.Write "<br><br>如果您是正常提交仍出现上面的提示,请联系站长暂时禁用掉防SQL注入功能,操作完成后再打开。"

    End If
Next
If FoundInjection = True Then
    Response.End
End If

End Sub

Sub StopInjection2(Values)
Dim FoundInjection
regEx.Pattern = "[';#()][\s+()](select|insert|delete|declare|exec|alter|create|backup|exists|daxia123|jxmmtv|<script|/script>)[\s+]"
Dim sItem, sValue
For Each sItem In Values
sValue = Values(sItem)
If regEx.Test(sValue) Then
FoundInjection = True
Response.Write "很抱歉,由于您提交的内容中含有危险的SQL注入代码,致使本次操作无效! "
Response.Write "<br>字段名:" & sItem
Response.Write "<br>字段值:" & sValue
Response.Write "<br>关键字:"
Set Matches = regEx.Execute(sValue)
For Each Match In Matches
Response.Write FilterJS(Match.value)
Next
'Response.Write "<br><br><br><br>如果您是正常提交仍出现上面的提示,请联系站长暂时禁用掉防SQL注入功能,操作完成后再打开。"

    End If
Next
If FoundInjection = True Then
    Response.End
End If

End Sub

Sub LogtoTxt(sqltype,sItem,sValue,FilterJStb)
dim fso99,LogFilePath99,Sql_act
set fso99 = createobject("scripting.filesystemobject")
LogFilePath99=server.mappath("")+"\ll\SqlinToFileDvbbs.txt"
set Sql_act = fso99.OpenTextFile(LogFilePath99,8,true)
Sql_act.write("操作IP:")
Sql_act.Write(UserTrueIP)
Sql_act.write(" 操作时间:")
Sql_act.write(Now)
Sql_act.write(" 操作页面:")
Sql_act.write(ScriptName)
Sql_act.write(" 提交方式:")
Sql_act.write(sqltype)
Sql_act.write(" 字段名:")
Sql_act.write(sItem)
Sql_act.write(" 字段值:")
Sql_act.write(sValue)
Sql_act.write(" 关键字:")
Sql_act.write(FilterJStb)
Sql_act.writeline
Sql_act.writeline
Sql_act.Close
'response.Write(LogFilePath99)
End sub
'=================================================
'函数名:FilterJS()
'作 用:过滤非法JS字符
'参 数:strInput 需要过滤的内容
'=================================================
Function FilterJS(ByVal strInput)
If IsNull(strInput) Or Trim(strInput) = "" Then
FilterJS = ""
Exit Function
End If
Dim reContent

' 替换掉HTML字符实体(Character Entities)名字和分号之间的空白字符,比如:ä    ;替换成ä
regEx.Pattern = "(&#*\w+)[\x00-\x20]+;"
strInput = regEx.Replace(strInput, "$1;")

' 将无分号结束符的数字编码实体规范成带分号的标准形式
regEx.Pattern = "(&#x*[0-9A-F]+);*"
strInput = regEx.Replace(strInput, "$1;")

' 将  &lt; &gt; & "字符实体中的 & 替换成 & 以便在进行HtmlDecode时保留这些字符实体
'RegEx.Pattern = "&(amp|lt|gt|nbsp|quot);"
'strInput = RegEx.Replace(strInput, "&$1;")

' 将HTML字符实体进行解码,以消除编码字符对后续过滤的影响
'strInput = HtmlDecode(strInput);

' 将ASCII码表中前32个字符中的非打印字符替换成空字符串,保留 9、10、13、32,它们分别代表 制表符、换行符、回车符和空格。
regEx.Pattern = "[\x00-\x08\x0b-\x0c\x0e-\x19]"
strInput = regEx.Replace(strInput, "")  
   
oldhtmlString = ""
Do While oldhtmlString &lt;&gt; strInput
    oldhtmlString = strInput
    regEx.Pattern = "(&lt;[^&gt;]+src[\x00-\x20]*=[\x00-\x20]*[^&gt;]*?)&#([^&gt;]*&gt;)"  '过虑掉 src 里的 &#
    strInput = regEx.Replace(strInput, "$1&#$2")
    regEx.Pattern = "(&lt;[^&gt;]+style[\x00-\x20]*=[\x00-\x20]*[^&gt;]*?)&#([^&gt;]*&gt;)"  '过虑掉style 里的 &#
    strInput = regEx.Replace(strInput, "$1&#$2")
    regEx.Pattern = "(&lt;[^&gt;]+style[\x00-\x20]*=[\x00-\x20]*[^&gt;]*?)\\([^&gt;]*&gt;)"   '替换掉style中的 "\" 
    strInput = regEx.Replace(strInput, "$1/$2")  
Loop
' 替换以on和xmlns开头的属性
regEx.Pattern = "on(load\s*=\s*""*'*resizepic\(this\)'*""*)"
strInput = regEx.Replace(strInput, "off$1")
regEx.Pattern = "on(mousewheel\s*=\s*""*'*return\s*bbimg\(this\)'*""*)"
strInput = regEx.Replace(strInput, "off$1")

regEx.Pattern = "(&lt;[^&gt;]+[\x00-\x20""'/])(on|xmlns)([^&gt;]*)&gt;"
strInput = regEx.Replace(strInput, "$1pe$3&gt;")

regEx.Pattern = "off(load\s*=\s*""*'*resizepic\(this\)'*""*)"
strInput = regEx.Replace(strInput, "on$1")
regEx.Pattern = "off(mousewheel\s*=\s*""*'*return\s*bbimg\(this\)'*""*)"
strInput = regEx.Replace(strInput, "on$1")


' 替换javascript
regEx.Pattern = "([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`'""]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:"
strInput = regEx.Replace(strInput, "$1=$2nojavascript...")

' 替换vbscript
regEx.Pattern = "([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`'""]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:"
strInput = regEx.Replace(strInput, "$1=$2novbscript...")

'替换style中的注释部分,比如:&lt;div style="xss:expres/*comment*/sion(alert(x))"&gt;
regEx.Pattern = "(&lt;[^&gt;]+style[\x00-\x20]*=[\x00-\x20]*[^&gt;]*?)/\*[^&gt;]*\*/([^&gt;]*&gt;)"
strInput = regEx.Replace(strInput, "$1$2")
' 替换expression
regEx.Pattern = "(&lt;[^&gt;]+)style[\x00-\x20]*=[\x00-\x20]*([`'""]*).*[eeE][xxX][ppP][rrR][eeE][ssS][ssS][iiI][ooO][nnN][\x00-\x20]*[\(\(][^&gt;]*&gt;"
strInput = regEx.Replace(strInput, "$1&gt;")

' 替换behaviour
regEx.Pattern = "(&lt;[^&gt;]+)style[\x00-\x20]*=[\x00-\x20]*([`'""]*).*behaviour[^&gt;]*&gt;&gt;"
strInput = regEx.Replace(strInput, "$1&gt;")
' 替换behavior
regEx.Pattern = "(&lt;[^&gt;]+)style[\x00-\x20]*=[\x00-\x20]*([`'""]*).*behavior[^&gt;]*&gt;&gt;"
strInput = regEx.Replace(strInput, "$1&gt;")

' 替换script
regEx.Pattern = "(&lt;[^&gt;]+)style[\x00-\x20]*=[\x00-\x20]*([`'""]*).*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^&gt;]*&gt;"
strInput = regEx.Replace(strInput, "$1&gt;")

' 替换namespaced elements 不需要
regEx.Pattern = "&lt;/*\w+:\w[^&gt;]*&gt;"
strInput = regEx.Replace(strInput, " ")

Dim oldhtmlString
oldhtmlString = ""
Do While oldhtmlString &lt;&gt; strInput
    oldhtmlString = strInput
    '实行严格过滤
    regEx.Pattern = "&lt;/*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^&gt;]*&gt;?"
    strInput = regEx.Replace(strInput, " ")
    '过滤掉SHTML的Include包含文件漏洞
    regEx.Pattern = "&lt;!--\s*#include[^&gt;]*&gt;"
    strInput = regEx.Replace(strInput, "noshtml")
    'If FilterLevel &gt; 0 Then
    '   '实行严格过滤
    '   RegEx.Pattern = "&lt;/*(embed|object)[^&gt;]*&gt;"
    '   strInput = RegEx.Replace(strInput, "")
    'End If
Loop
FilterJS = strInput

End Function
%>

这个能防你的COOKIE了吧

总结的很好,回复学习。

找到了阿哈 收集整理一下

支持了

我发的那个代码,有能过的不啊?

look~~~~~~~~~~

没人能过啊????

asp缘董也搞出来了 哈哈 听所很全的哦

支持猿洞牛{:4_60:}

感谢~好漂亮~收藏了~

http://www.90sec.org/thread-1927-1-1.html

看你的早八点 享受啊

我来学习下!!!!!

又是8点{:4_80:}

趁现在还早,看看先

来学习之 回复看看

要好好的学习下 挖洞洞

回复一下,好好学学

{:4_30:}