Sql注入脚本

时间:2011-09-27 12:20:17

标签: asp-classic sql-injection

这个问题的标题似乎可能先前被问及回答,但对我来说却是不同的情景。我用这个脚本在我的ASP站点停止sql注入。根据我的知识或注入脚本,我已经尝试了一切。是否仍然可以突破这个代码或者你觉得这很好。

这是脚本

<%
Function IsInject(strCheck, boolForm)
    IsInject = False
    If Not boolForm And Len(strCheck) > 50 Then IsInject = True
'   Dim sCmdList, arrCmds, i
    If boolForm Then
        sCmdList = "declare,varchar,convert,delete,create,is_srvrolemember,ar(,cast("
    Else
        sCmdList = "update,union,select,drop,declare,varchar,convert,delete,create,is_srvrolemember,ar(,cast(,char("
    End If
    arrCmds = Split(sCmdList, ",")
    For i = 0 To UBound(arrCmds)
        If Instr(UCase(CStr(strCheck)), UCase(arrCmds(i))) > 0 Then
            IsInject = True
            Exit For
        End If
    Next
    Erase arrCmds
End Function
Function CleanInject(strClean, boolInt)
    If boolInt Then CleanInject = CInt(strClean) Else CleanInject = Replace(strClean, "'", "''")
End Function

'-----------------------------------------------------------
'redirect user if specific IP
'Dim ipaddress, bFBIRedirect, sInjectType
bFBIRedirect = True
ipaddress = Request.ServerVariables("REMOTE_ADDR")
Select Case ipaddress
    Case "90.120.206.10"
    Case Else
        bFBIRedirect = False
End Select
If bFBIRedirect Then Response.Redirect "http://www.fbi.gov"
'-----------------------------------------------------------

'Dim bIsInject, sHackString
bIsInject = False

If Not bInject Then
'   Dim qsItm
    For Each qsItm In Request.QueryString
        If IsInject(Request.QueryString(qsItm), False) Then
            bIsInject = True
            sHackString = qsItm & "=" & Request.QueryString(qsItm)
            sHackType = "QueryString"
            sInjectType = "qs-" & Request.QueryString(qsItm)
            Exit For
        End If
    Next
End If
If Not bInject Then
'   Dim frmItm
'   For Each frmItm In Request.Form
'       If IsInject(Request.Form(frmItm), True) Then
'           bIsInject = True
'           sHackString = Request.Form(frmItm)
'           sHackString = frmItm & "=" & Request.Form(frmItm)
'           sHackType = "Form"
'           Exit For
'       End If
'   Next
End If

If bIsInject Then
    Session("hacktype") = sHackType
    Session("hackstr") = sHackString
    Session("thepagefrom") = Request.ServerVariables("PATH_INFO")
    Session("theip") = Request.ServerVariables("REMOTE_ADDR")

'   Dim arrWhereAt, iWhereAt, sRedirect

    arrWhereAt = Split(Request.ServerVariables("PATH_INFO"), "/")
    iWhereAt = UBound(arrWhereAt)

    sRedirect = "unknownerror.asp?ip=" & Request.ServerVariables("REMOTE_ADDR") & "&err=" & sInjectType & "&pg=" & Request.ServerVariables("PATH_INFO")
    If iWhereAt = 1 Then sRedirect = "../" & sRedirect
    If iWhereAt = 2 Then sRedirect = "../../" & sRedirect
    If iWhereAt = 3 Then sRedirect = "../../../" & sRedirect

    Response.Redirect sRedirect
End If
%>

2 个答案:

答案 0 :(得分:1)

而不是这样做我认为我会在创建SQL查询时使用ADO Parameter对象,第二个最好的事情是对动态SQL查询的输入字段进行类型转换,例如将字符串转换为SQL字符串(用两个替换任何' ''),确保号码是一个数字等。

答案 1 :(得分:1)

使用黑名单删除命令并不是一个好主意。你必须确保你涵盖所有可能的命令,但仍有人可能会偷偷摸摸过去。如果您从不是攻击但仍包含攻击字符串的用户获取数据,这也可能会失败。例子“回到苏联时代”。

正如尼古拉所建议的那样,看看你是否能找到某种准备好的陈述来使用。或者找一个非常好的库来为你正确地转义数据。