使用pyparsing模塊解析Snort日誌時出現問題。使用PyParsing解析Snort日誌
問題在於將Snort日誌(其中有多行條目,以空行分隔)和獲取pyparsing來將每個條目解析爲一個整塊,而不是一行一行地閱讀並期望該語法與每行(顯然,它不)
我已經嘗試將每個塊轉換爲臨時字符串,剝離每個塊內的換行符,但它拒絕正確處理。我可能完全在錯誤的軌道上,但我不這麼認爲(類似的形式完美適用於系統日誌類型的日誌,但這些是單行條目,因此適用於您的基本文件迭代器/行處理)
這裏的日誌和代碼示例我到目前爲止有:
[**] [1:486:4] ICMP Destination Unreachable Communication with Destination Host is Administratively Prohibited [**]
[Classification: Misc activity] [Priority: 3]
08/03-07:30:02.233350 172.143.241.86 -> 63.44.2.33
ICMP TTL:61 TOS:0xC0 ID:49461 IpLen:20 DgmLen:88
Type:3 Code:10 DESTINATION UNREACHABLE: ADMINISTRATIVELY PROHIBITED HOST FILTERED
** ORIGINAL DATAGRAM DUMP:
63.44.2.33:41235 -> 172.143.241.86:4949
TCP TTL:61 TOS:0x0 ID:36212 IpLen:20 DgmLen:60 DF
Seq: 0xF74E606
(32 more bytes of original packet)
** END OF DUMP
[**] ...more like this [**]
和更新的代碼:
def snort_parse(logfile):
header = Suppress("[**] [") + Combine(integer + ":" + integer + ":" + integer) + Suppress("]") + Regex(".*") + Suppress("[**]")
cls = Optional(Suppress("[Classification:") + Regex(".*") + Suppress("]"))
pri = Suppress("[Priority:") + integer + Suppress("]")
date = integer + "/" + integer + "-" + integer + ":" + integer + "." + Suppress(integer)
src_ip = ip_addr + Suppress("->")
dest_ip = ip_addr
extra = Regex(".*")
bnf = header + cls + pri + date + src_ip + dest_ip + extra
def logreader(logfile):
chunk = []
with open(logfile) as snort_logfile:
for line in snort_logfile:
if line !='\n':
line = line[:-1]
chunk.append(line)
continue
else:
print chunk
yield " ".join(chunk)
chunk = []
string_to_parse = "".join(logreader(logfile).next())
fields = bnf.parseString(string_to_parse)
print fields
任何幫助,指針,RTFMs,你這樣做是非,等。, 不勝感激。
你是上帝。這是超出我的專業知識的解決方案,但只要我讓自己瞭解所有工作部件,即將實施。謝謝! – 2010-08-04 15:48:57
+1 - 尼斯回答〜unutbu,打我一拳! (你的groupby代碼看起來很瘋狂,當我得到幾分鐘時,我將不得不對其進行排序。) – PaulMcG 2010-08-04 15:55:03
+許多隻是爲了可愛和優雅地使用'groupby'。 – katrielalex 2010-08-04 20:20:03