【问题标题】:Challenging regex clause in Python - Suricata / fast.logPython 中具有挑战性的正则表达式子句 - Suricata / fast.log
【发布时间】:2021-03-22 10:56:09
【问题描述】:

任何正则表达式向导可以提供帮助?

我正在尝试让正则表达式解析 Suricata 快速日志。到目前为止,我发现了一个旧帖子 here 但想从日志中获取所有数据。

到目前为止,我可以获得时间、日期、源 ip、源端口、目标 ip 和目标端口,但还想获得警报标题、分类和优先级。

日志文件:

03/21/2021-20:24:02.524057  [**] [1:2006380:14] ET POLICY Outgoing Basic Auth Base64 HTTP Password detected unencrypted [**] [Classification: Potential Corporate Privacy Violation] [Priority: 1] {TCP} 192.168.10.14:48820 -> 192.168.10.18:8086
03/21/2021-20:24:23.567546  [**] [1:2014939:5] ET POLICY DNS Query for TOR Hidden Domain .onion Accessible Via TOR [**] [Classification: Potential Corporate Privacy Violation] [Priority: 1] {UDP} 192.168.10.14:49405 -> 192.168.10.1:53

Python 文件:

import re

log_file = open('fast.log','r')
for line in log_file:
    r_search = re.search('([0-9/]+)-([0-9:.]+)\s+.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})\s+->\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})', line)
    print(f'Date - {r_search.group(1)}')
    print(f'Time - {r_search.group(2)}')
    print(f'Scr IP - {r_search.group(3)}')
    print(f'Scr Port - {r_search.group(4)}')
    print(f'Dess IP - {r_search.group(5)}')
    print(f'Dess Port - {r_search.group(6)}')
    print('***********')

log_file.close()

当前输出:

Date - 03/21/2021
Time - 20:24:02.524057
Scr IP - 192.168.10.14
Scr Port - 48820
Dess IP - 192.168.10.18
Dess Port - 8086
***********

想要的输出:

Date - 03/21/2021
Time - 20:24:02.524057
Alert Rule - ET POLICY Outgoing Basic Auth Base64 HTTP Password detected unencrypted
Classification - Potential Corporate Privacy Violation
Priority - 1
Scr IP - 192.168.10.14
Scr Port - 48820
Dess IP - 192.168.10.18
Dess Port - 8086
***********

谢谢!

【问题讨论】:

    标签: python regex suricata


    【解决方案1】:

    以下正则表达式模式似乎在这里工作:

    logs = ['03/21/2021-20:24:02.524057  [**] [1:2006380:14] ET POLICY Outgoing Basic Auth Base64 HTTP Password detected unencrypted [**] [Classification: Potential Corporate Privacy Violation] [Priority: 1] {TCP} 192.168.10.14:48820 -> 192.168.10.18:8086', '03/21/2021-20:24:23.567546  [**] [1:2014939:5] ET POLICY DNS Query for TOR Hidden Domain .onion Accessible Via TOR [**] [Classification: Potential Corporate Privacy Violation] [Priority: 1] {UDP} 192.168.10.14:49405 -> 192.168.10.1:53']
    for log in logs:
        matches = re.findall(r'^(.*?)-(\S+)\s+\[.*?\]\s+\[.*?\]\s+(.*?)\s+\[.*?\]\s+\[(.*?)\]\s+\[(.*?)\].*?(\d+(?:\.\d+)*):(\d+)\s+->\s+(\d+(?:\.\d+)*):(\d+).*$', log)
        print(matches)
    

    打印出来:

    [('03/21/2021',
      '20:24:02.524057',
      'ET POLICY Outgoing Basic Auth Base64 HTTP Password detected unencrypted',
      'Classification: Potential Corporate Privacy Violation',
      'Priority: 1',
      '192.168.10.14',
      '48820',
      '192.168.10.18',
      '8086')]
    [('03/21/2021',
      '20:24:23.567546',
      'ET POLICY DNS Query for TOR Hidden Domain .onion Accessible Via TOR',
      'Classification: Potential Corporate Privacy Violation',
      'Priority: 1',
      '192.168.10.14',
      '49405',
      '192.168.10.1',
      '53')]
    

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      相关资源
      最近更新 更多