【问题标题】:Matching entries using python使用python匹配条目
【发布时间】:2013-03-12 10:33:11
【问题描述】:

我已经解析了两个日志,因此只有 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

如果有更好的方法,请告诉我,因为我认为我可能过于复杂了。

【问题讨论】:

  • 您是否正在寻找同时显示为 NeXposeSnort 的 CVE?
  • 从 NeXpose 扫描报告中提取 nexpose cve,从 snort 扫描日志中提取 snort cve。我想使用他们的 CVE 编号将 snort 发现的任何攻击与 nexpose 漏洞相关联。
  • 那么,将您的问题翻译成专业术语:您有两个 CVE 集合,您想知道哪些 CVE 出现在两个集合中?

标签: python parsing correlation


【解决方案1】:

你考虑过 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))

【讨论】:

  • 你绝对的传奇兄弟。为之欢呼,真的很感激。
  • 很高兴为您提供帮助。如果您打算继续使用 Python 进行开发,您可能会发现列表理解是一个有用且有趣的概念:docs.python.org/2/tutorial/…
【解决方案2】:

我建议使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2020-08-06
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多