使用REGEX查找关键字

时间:2016-02-02 15:53:27

标签: regex vb.net keyword

嘿,我有以下 badwords ,我想查看它们是否在我传递的字符串中:

Private Function injectionCheck(queryString As String) As Integer
    Dim badWords() As String = {"EXEC", "EXECUTE", ";", "-", "*", "--", "@",
                                "UNION", "DROP", "DELETE", "UPDATE", "INSERT", "MASTER",
                                "TABLE", "XP_CMDSHELL", "CREATE", "XP_FIXEDDRIVES", 
                                "SYSCOLUMNS", "SYSOBJECTS"}
    Dim pattern As String = "\b(" + Regex.Escape(badWords(0))

    For Each key In badWords.Skip(1)
        pattern += "|" + Regex.Escape(key)
    Next

    pattern += ")\b"

    Return Regex.Matches(queryString, pattern, RegexOptions.IgnoreCase).Count
End Function

对于模式,我得到以下内容:

\b(EXEC|EXECUTE|;|-|\*|--|@|UNION|DROP|DELETE|UPDATE|INSERT|MASTER|TABLE|XP_CMDSHELL|
   CREATE|XP_FIXEDDRIVES|SYSCOLUMNS|SYSOBJECTS)\b

哪个看起来对我不对。但每当我打电话给它时,我会得到 0 作为对此的回应:

Dim blah As Integer = injectionCheck("select * from bob where something = 'you'")

所以我要忽略的是因为上面不应该返回 0 - 它应该返回 2 因为*和'使用不应该使用。

1 个答案:

答案 0 :(得分:2)

如果您计划将单词作为整个单词进行匹配,但关键字可能以非单词字符开头/结尾,则可能会遇到类似的问题。单词边界含义取决于上下文:\b--\b将匹配X--X但不会匹配, --,

您需要明确的边界匹配。使用lookarounds (?<!\w)作为前导,(?!\w)作为尾随单词边界。

实施如下所示的更改:

Dim pattern As String = "(?<!\w)(" + Regex.Escape(badWords(0)) ' <== HERE

For Each key In badWords.Skip(1)
    pattern += "|" + Regex.Escape(key)
Next

pattern += ")(?!\w)"  ' <== AND HERE