【问题标题】:Regex to find specific word正则表达式查找特定单词
【发布时间】:2022-08-18 15:09:53
【问题描述】:

我有一个大文件,其中包含多个条目,如下所示:

{\"author\":[\"frack113\"],\"description\":\"Detects a Sysmon configuration change, which could be the result of a legitimate reconfiguration or someone trying manipulate the configuration\",\"ruleId\":\"8ac03a65-6c84-4116-acad-dc1558ff7a77\",\"falsePositives\":[\"Legitimate administrative action\"],\"from\":\"now-360s\",\"immutable\":false,\"outputIndex\":\".siem-signals-default\",\"meta\":{\"from\":\"1m\"},\"maxSignals\":100,\"riskScore\":35,\"riskScoreMapping\":[],\"severity\":\"medium\",\"severityMapping\":[],\"threat\":[{\"tactic\":{\"id\":\"TA0005\",\"reference\":\"https://attack.mitre.org/tactics/TA0005\",\"name\":\"Defense Evasion\"},\"framework\":\"MITRE ATT&CK®\",\"technique\":[]}],\"to\":\"now\",\"references\":[\"https://docs.microsoft.com/en-us/sysinternals/downloads/sysmon\"],\"version\":1,\"exceptionsList\":[],\"index\":[\"winlogbeat-*\"],\"query\":\"(winlog.channel:\\\"Microsoft\\\\-Windows\\\\-Sysmon\\\\/Operational\\\" AND winlog.event_id:\\\"16\\\")\",\"language\":\"lucene\",\"filters\":[],\"type\":\"query\"},\"schedule\":{\"interval\":\"5m\"}}

我正在开发一个 python 程序来检测单词 \"query\" 之后的字符串,例如

\"query\":\"(winlog.channel:\\\"Microsoft\\\\-Windows\\\\-Sysmon\\\\/Operational\\\" AND winlog.event_id:\\\"16\\\")\"

我正在尝试检测(winlog.channel:\\\"Microsoft\\\\-Windows\\\\-Sysmon\\\\/Operational\\\" AND winlog.event_id:\\\"16\\\"),我要检测其中的多个,然后使用它与另一个文件中的“查询”进行比较,以查找是否有任何相似之处。

我尝试使用此正则表达式,但根本无法检测到“查询”。

(?<=^\\\"query\\\":\\W)(\\w.*)$ 

(?<=\'{\\\"query\\\"}\':\\s)\'?([^\'}},]+)

如果有人能提供一些指点,我将不胜感激,因为我被困了几个小时!

  • 你确定不能只使用json.load
  • “似乎不起作用”是什么意思?它究竟以哪种方式不起作用?
  • 如果您共享文件,我们可以提供帮助。如前所述,如果它是 json 文件,甚至可能需要正则表达式。
  • 您正在尝试匹配不存在的字符,例如 \'{ 如果这是 json,您可以考虑使用解析器。对于当前字符串:(?&lt;=\"query\":\")\\([^()]+\\) regex101.com/r/3va8gP/1
  • @chitown88 不确定这是否是在这里共享文件的方式,但这里是文件的链接,文件相当长:link

标签: python json


【解决方案1】:

您的问题中也有 python 标签 - 所以我假设涉及 python 脚本的解决方案应该没问题。

假设您有一个带有条目的文件 data.txt 作为给定示例:

{"author":["frack113"],"description":"Detects a Sysmon configuration change, which could be the result of a legitimate reconfiguration or someone trying manipulate the configuration","ruleId":"8ac03a65-6c84-4116-acad-dc1558ff7a77","falsePositives":["Legitimate administrative action"],"from":"now-360s","immutable":false,"outputIndex":".siem-signals-default","meta":{"from":"1m"},"maxSignals":100,"riskScore":35,"riskScoreMapping":[],"severity":"medium","severityMapping":[],"threat":[{"tactic":{"id":"TA0005","reference":"https://attack.mitre.org/tactics/TA0005","name":"Defense Evasion"},"framework":"MITRE ATT&CK®","technique":[]}],"to":"now","references":["https://docs.microsoft.com/en-us/sysinternals/downloads/sysmon"],"version":1,"exceptionsList":[],"index":["winlogbeat-*"],"query":"(winlog.channel:\"Microsoft\\-Windows\\-Sysmon\\/Operational\" AND winlog.event_id:\"16\")","language":"lucene","filters":[],"type":"query"},"schedule":{"interval":"5m"}}

然后,您可以运行以下脚本来打印所需的字符串。

def main():
    with open('data.txt') as f:
        for line in f:
            
            line = line.split("query")
            result = line[1]
            result = result.split(")")
            result = result[0][2:]

            print(result)

main()                      

对于您提供的示例字符串,此脚本将打印:

"(winlog.channel:\"Microsoft\\-Windows\\-Sysmon\\/Operational\" AND winlog.event_id:\"16\"

希望能帮助到你!

【讨论】:

    【解决方案2】:

    2种方法来做到这一点:

    1. 以 json 格式读入,然后遍历字典。 2) 以 str 形式读入并对其进行正则表达式。

      1. 以 json 格式读入:

      import json
      
      file = 'exportedSignal.ndjson'
      with open(file, 'r', encoding = 'cp850') as f:
          jsonData = json.load(f)
      
      queries = []
      hits = jsonData['hits']['hits']
      for hit in hits:
          if 'query' in hit['_source']['alert']['params'].keys():
              query = hit['_source']['alert']['params']['query']
              queries.append(query)
      print(queries)
      

      2.使用正则表达式:

      import re
      
      file = 'exportedSignal.ndjson'
      with open(file, 'r', encoding = 'cp850') as f:
          data = f.read()
      
      queries = re.findall('\"query\":\"(.*?)\"', data)
      print(queries)
      

      输出:

      两者都从 "query" 键生成 2006 个值的列表。

    【讨论】:

      猜你喜欢
      • 2022-07-14
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多