【问题标题】:python, regex, matching strings with repeating characterspython,正则表达式,匹配带有重复字符的字符串
【发布时间】:2012-10-04 16:12:14
【问题描述】:

我正在尝试在 Apache 日志文件中搜索与特定漏洞扫描相关的特定条目。我需要将单独文件中的字符串与博客中的 URI 内容进行匹配。我试图找到的一些字符串包含重复的特殊字符,例如“?”。

例如,我需要能够匹配只包含字符串 '????????' 的攻击但我不想在字符串'??????????????????'上收到警报因为每次攻击都与特定的攻击 ID 号相关联。因此,使用:

if attack_string in log_file_line:
    alert_me()

...不会工作。因此,我决定将字符串放入正则表达式:

if re.findall(r'\%s' % re.escape(attack_string),log_file_line):
    alert_me()

...这也不起作用,因为任何日志文件行都包含字符串'????????'即使有超过 8 个 '?' 也匹配在日志文件行中。

然后我尝试向正则表达式添加边界:

if re.findall(r'\\B\%s\\B' % re.escape(attack_string),log_file_line):
    alert_me()

...在这两种情况下都停止匹配。我需要能够动态分配我正在寻找的字符串,但我不想只匹配包含该字符串的任何行。我怎样才能做到这一点?

【问题讨论】:

  • 攻击字符串后面有空格吗?
  • 如果你使用原始字符串,你不应该把\加倍。检查是否是这个问题。
  • r'\?\?\?\?\?\?\?\?(?!\?)' 不太清楚你在问什么。
  • 如果成功找到的字符串后面有空格,这个正则表达式将把它剪掉 re.findall(r'\%s\s' % re.escape(attack_string),log_file_line)
  • @Mozoby 和 Tadgh - 字符串后面并不总是有空格字符。此外,有时在发出警报之前,可能需要在特定日志文件行上匹配多个字符串。每个签名包含一个或多个必须按顺序匹配的字符串。在每次迭代中,来自签名堆栈的一个新字符串被放置在 attack_string 中并与 log_file_line 进行比较。由于“GET /vulnerable.page??????????”和 "GET /vulnerable.page/????????/gotya.exploit" 和 "GET /vulnerable.page" 都是不同的签名,我不能指望空格。

标签: python regex


【解决方案1】:

怎么样:

(?:[^?]|^)\?{8}(?:[^?]|$)

说明:

(?-imsx:(?:[^?]|^)\?{8}(?:[^?]|$))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [^?]                     any character except: '?'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ^                        the beginning of the string
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  \?{8}                    '?' (8 times)
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [^?]                     any character except: '?'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2020-11-29
    • 2011-11-28
    • 2018-07-26
    • 1970-01-01
    相关资源
    最近更新 更多