1

任何正则表达式向导能够提供帮助?

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

到目前为止,我可以获得时间、日期、源 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

蟒蛇文件:

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
***********

谢谢!

4

1 回答 1

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')]
于 2021-03-22T11:10:09.350 回答