2013-03-12 84 views
3

我已经解析了两个日志,以便只需将CVE编号写入文本文件,以便它们显示为一个列表。这里是输出的一部分;使用python匹配条目

NeXpose Results: CVE-2007-6519 
NeXpose Results: CVE-1999-0559 
NeXpose Results: CVE-1999-1382 
Snort results: CVE-1999-0016 
Snort results: CVE-1999-0016 
Snort results: CVE-1999-0016 
Snort results: CVE-1999-0016 
Snort results: CVE-1999-0016 

它就像这样一直通过该文件。现在我想让我的代码通过CVE编号进行编码,并查看是否有任何NeXpose CVE匹配snort CVE,因为我正在关注这两个CVE。这是我的代码;

#!/usr/bin/env python 
nexpose = {} 
snort = {} 

CVE = open("CVE.txt","r") 
cveWarning = open("Warning","w") 

for line in CVE.readlines(): 
     list_of_line = line.split(' ') 

    if "NeXpose" in list_of_line[0]: 
     nexResults = list_of_line[2] 
     #print 'NeXpose: ', nexResults 



    if "Snort" in list_of_line[0]: 
     cveResults = list_of_line[2] 
     #print 'Snort: ', cveResults 


a_match = [True for match in nexResults if match in cveResults] 
print a_match 

如果有更好的方法做到这一点,请让我知道,因为我认为我可能过于复杂的事情。

+0

您是否在寻找同时出现'NeXpose'和'Snort'的CVE? – 2013-03-12 10:36:31

+0

nexpose cve从NeXpose扫描报告中提取,snort cve从snort扫描日志中提取。我想使用CVE编号将任何攻击嗅探与nexpose漏洞关联起来。 – user2099445 2013-03-12 10:40:35

+0

因此,将你的问题翻译成专业术语:你有两个CVE集合,你想知道哪个CVE出现在两个集合中? – 2013-03-12 10:46:04

回答

4

您是否考虑过Python集?

#!/usr/bin/python 

lines = open('CVE.txt').readlines() 
nexpose = set([l.split(':')[1].strip() for l in lines if l.startswith('NeXpose')]) 
snort = set([l.split(':')[1].strip() for l in lines if l.startswith('Snort')]) 

# print 'Nexpose: ', ', '.join(nexpose) 
# print 'Snort : ', ', '.join(snort) 

print 'CVEs both in Nexpose and Snort : ', ', '.join(snort.intersection(nexpose)) 
+0

你绝对的传奇兄弟。为此欢呼,真的很感激它。 – user2099445 2013-03-12 10:47:46

+0

乐意协助。如果您打算继续使用Python开发,您可能会发现列表理解是一个有用且有趣的概念:http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions – 2013-03-12 10:53:15

0

我建议使用python集合。

nex = set() 
sno = set() 
for line in CVE.readlines(): 
    list_of_line = line.split(' ') 

    if(list_of_line[0]=="NeXpose"): 
     nex.add(list_of_line[2]) 
    if(list_of_line[0]=="Snort"): 
     sno.add(list_of_line[2]) 

inboth = sno.intersection(nex)