程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> ASP安全檢測與過濾函數SafeCheck

ASP安全檢測與過濾函數SafeCheck

編輯:ASP技巧

'作用:安全字符串檢測函數
'名字:SafeCheck
'參數:CheckString,CheckType,CheckLength
'說明:
'Checkstring待檢測字符串:任意字符.
'CheckType檢測類型0正常短字符1數字2日期3金錢4編碼Html5解碼Html6登錄字符串7防攻擊檢測
'CheckLength檢測類型長度:類型為int,當為金錢時為小數點的位置
'返回值:如果通過檢測,返回正確字符串,
'如果未通過則返回錯誤代碼SYSTEM_ERROR|ERROR_CODE
'Script Writen by :SnowDu(杜雪.Net)
'Web:http://www.snsites.com/
'Web:http://www.knowsky.com/
'-------------------------------------------
function  SafeCheck(CheckString,CheckType,CheckLength)
On Error Resume Next
ErrorRoot="SYSTEM_ERROR|"
if checkString="" then
 SafeCheck=ErrorRoot&"00001"
 exit function
end if

CheckString=Replace(CheckString,"'","&#39")
select case CheckType
case 0
 CheckString=trim(CheckString)
 SafeCheck=Left(CheckString,CheckLength)
case 1
 if not isnumberic(CheckString) then
  SafeCheck=ErrorRoot&"00002"
  exit function
 else
  SafeCheck=Left(CheckString,CheckLength)
 end if
case 2
 tempVar=IsDate(CheckString)
 if Not TempVar then
  SafeCheck=ErrorRoot&"00003"
  exit function
 else
  select case CheckLength
  case 0
   SafeCheck=FormatDateTime(CheckString,vbShortDate)
  case 1
   SafeCheck=FormatDateTime(CheckString,vbLongDate)
  case 2
   SafeCheck=CheckString
  end select
 end if
case 3
 tempVar=FormatCurrency(CheckString,0)
 if Err then
  SafeCheck=ErrorRoot&"00004"
  exit function
 else
  SafeCheck=FormatCurrency(CheckString,CheckLength)
 end if
case 4
 sTemp = CheckString
 If IsNull(sTemp) = True Then
  SafeCheck=ErrorRoot&"00005"
  Exit Function
 End If
 sTemp = Replace(sTemp, "&", "&")
 sTemp = Replace(sTemp, "<", "&lt;")
 sTemp = Replace(sTemp, ">", "&gt;")
 sTemp = Replace(sTemp, Chr(34), "&quot;")
 sTemp = Replace(sTemp, Chr(10), "<br>")
 SafeCheck = Left(sTemp,CheckLength)
case 5
 sTemp = CheckString
 If IsNull(sTemp) = True Then
  SafeCheck=ErrorRoot&"00006"
  Exit Function
 End If
 sTemp = Replace(sTemp, "&amp;", "&")
 sTemp = Replace(sTemp, "&lt;", "<")
 sTemp = Replace(sTemp, "&gt;", ">")
 sTemp = Replace(sTemp, "&quot;", Chr(34))
 sTemp = Replace(sTemp, "<br>",Chr(10))
 SafeCheck = Left(sTemp,CheckLength)
case 6
 s_BadStr = "'  &<>?%,;:()`~!@#$^*{}[]|+-=" & Chr(34) & Chr(9) & Chr(32)
 n = Len(s_BadStr)
 IsSafeStr = True
 For i = 1 To n
  If Instr(CheckString, Mid(s_BadStr, i, 1)) > 0 Then
   IsSafeStr = False
  End If
 Next
 if IsSafeStr then
  SafeCheck=left(CheckString,CheckLength)
 else
  SafeCheck=ErrorRoot&"00007"
  Exit Function
 end if
case 7
 s_Filter="net user|xp_cmdshell|/add|select|count|asc|char|mid|'|""|"
 S_Filter=S_Filter&"insert|delete|drop|truncate|from|%|declare|-"
 S_Filters=split(S_Filter,"|")
 isFound=false
 for i=0 to ubound(S_Filters)-1
  if Instr(lcase(CheckString),lcase(S_Filters(i)))<>0 then
   isFound=true
   exit for
  end if
 next
 if isFound then
  SafeCheck=ErrorRoot&"00008"
  Exit Function
 else
  SafeCheck=left(CheckString,CheckLength)
 end if
end select
end function

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved