发新话题
打印

过滤跨站脚本与HTML标签的几个代码

过滤跨站脚本与HTML标签的几个代码

复制内容到剪贴板
代码:
Function NoCSSHackAdmin(Str,StrTittle) '过滤跨站脚本和HTML标签
Dim regEx
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Pattern = "<|>|\t"
If regEx.Test(LCase(Str)) Then
  Response.Write "<script>alert('"& StrTittle &"含有非法字符(<,>,tab)');history.back();</script>"
  Response.End
End If
Set regEx = Nothing
NoCSSHackAdmin = Str
End Function
复制内容到剪贴板
代码:
Function NoCSSHackInput(Str) '过滤跨站脚本和HTML标签
Dim regEx
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Pattern = "<|>|(script)|on(mouseover|mouseon|mouseout|click|dblclick|blur|focus|change)|url|eval|\t"
If regEx.Test(LCase(Str)) Then
  Response.Write "<script>alert('你的输入含有非法字符(<,>,tab,script等),请检查后再提交!');history.back();</script>"
  Response.End
End If
Set regEx = Nothing
NoCSSHackInput = Str
End Function
复制内容到剪贴板
代码:

Function NoCSSHackContent(Str) '过滤跨站脚本,只过滤脚本,对HTML不过滤
Dim regEx
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Pattern = "iframe|object|(script)|on(mouseover|mouseon|mouseout|click|dblclick|blur|focus|change)|(url\()|eval"
If regEx.Test(LCase(Str)) Then
  Response.Write "<script>alert('你提交的内容含有非法字符(不能包含脚本),请检查后再提交!');history.back();</script>"
  Response.End
End If
Set regEx = Nothing
NoCSSHackContent = Str
End Function
上面大家可能都见过这些代码,上面只能检测出内容中有非法的代码,并不能过滤,我们要的是过滤,那这个代码就不行了,并且其中的JS事件并不全,不能完全过滤掉JS事件,所以我修改了一个可以替换掉JS事件和HTML代码的函数
复制内容到剪贴板
代码:
Function RegReplace(RegStr,Str) '正则表达式过滤
Dim regEx,Match,Matches
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = RegStr
'进行匹配
Set Matches = regEx.Execute(Str)
' 遍历匹配集合,并替换掉匹配的项目
For Each Match in Matches
Str=Replace(Str,Match.Value,"")
Next
RegReplace=Str
Set regEx = Nothing
End Function
复制内容到剪贴板
代码:
Function RemoveHTML(Str)'去除HTML
Dim RegStr
RegStr = "<.+?>"
RemoveHTML = RegReplace(RegStr,Str)
End Function
复制内容到剪贴板
代码:
Function NoJSEvent(Str)'修改的过滤跨站脚本
Dim RegStr
RegStr = "on\w+="".+?"""
NoJSEvent = RegReplace(RegStr,Str)
End Function
复制内容到剪贴板
代码:
Function NoCSSHackInput(Str) '过滤跨站脚本和HTML标签
NoCSSHackInput = RemoveHTML(NoJSEvent(Str))
End Function
其中第一个函数是自定义正则表达式过滤函数,你可以自己写正则表达式来过滤其它的一些代码。
注意:下面两个函数要用到第一个函数,所以这三个函数最好放到一个文件里
网络无限,生活无限

TOP

发新话题