3

我已经解析了两个日志,因此只有 CVE 编号写入文本文件,因此它们显示为一个列表。这是输出的一部分;

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 匹配,因为我正在研究将两者关联起来。这是我的代码;

#!/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

如果有更好的方法可以做到这一点,请告诉我,因为我认为我可能会使事情过于复杂。

4

2 回答 2

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))
于 2013-03-12T10:42:06.790 回答
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)
于 2013-03-12T10:41:22.377 回答