【问题标题】:Rewriting a string and removing unwanted elements in python重写字符串并删除 python 中不需要的元素
【发布时间】:2022-11-17 16:03:12
【问题描述】:

我使用 python 库 Nvdlib,它旨在从 Nist 中提取信息。在这些信息中,我对 CPE 尤其是 api 输出很感兴趣。 这是我的代码:

import nvdlib
r = nvdlib.searchCVE(cveId='CVE-2019-19781')[0]

conf = r.configurations #list in ouput

for x in conf:
     txt = ', '.join(str(x) for x in x.nodes) #transforme list to string
     print(x)

输出 :

{'operator': 'AND', 'negate': False, 'nodes': [{'operator': 'OR', 'negate': False, 'cpeMatch': [{'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:application_delivery_controller_firmware:10.5:*:*:*:*:*:*:*', 'matchCriteriaId': 'D56F2AAF-4658-484C-9A3A-D8A52BA5B10C'}, {'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:application_delivery_controller_firmware:11.1:*:*:*:*:*:*:*', 'matchCriteriaId': '8CE9E655-0D97-4DCF-AC2F-79DCD12770E5'}, {'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:application_delivery_controller_firmware:12.0:*:*:*:*:*:*:*', 'matchCriteriaId': '49454F7D-77B5-46DF-B95C-312AF2E68EAD'}, {'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:application_delivery_controller_firmware:12.1:*:*:*:*:*:*:*', 'matchCriteriaId': '201246D4-1E22-4F28-9683-D6A9FD0F7A6B'}, {'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:application_delivery_controller_firmware:13.0:*:*:*:*:*:*:*', 'matchCriteriaId': 'A3A50966-5554-4919-B6CE-BD8F6FF991D8'}]}, {'operator': 'OR', 'negate': False, 'cpeMatch': [{'vulnerable': False, 'criteria': 'cpe:2.3:h:citrix:application_delivery_controller:-:*:*:*:*:*:*:*', 'matchCriteriaId': '80E69E10-6F40-4FE4-9D84-F6C25EAB79D8'}]}]}

{'operator': 'AND', 'negate': False, 'nodes': [{'operator': 'OR', 'negate': False, 'cpeMatch': [{'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:netscaler_gateway_firmware:10.5:*:*:*:*:*:*:*', 'matchCriteriaId': '7E0FA8E2-3E8F-481E-8C39-FB00A9739DFC'}, {'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:netscaler_gateway_firmware:11.1:*:*:*:*:*:*:*', 'matchCriteriaId': 'A5D73B9A-59AA-4A38-AEAF-7EAB0965CD7E'}, {'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:netscaler_gateway_firmware:12.0:*:*:*:*:*:*:*', 'matchCriteriaId': 'B9F3ED0E-7F3D-477B-B645-77DA5FC7F502'}, {'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:netscaler_gateway_firmware:12.1:*:*:*:*:*:*:*', 'matchCriteriaId': '58349F8E-3177-413A-9CBE-BB454DCD31E4'}]}, {'operator': 'OR', 'negate': False, 'cpeMatch': [{'vulnerable': False, 'criteria': 'cpe:2.3:h:citrix:netscaler_gateway:-:*:*:*:*:*:*:*', 'matchCriteriaId': 'DEBB9B6A-1CAD-4D82-9B1E-939921986053'}]}]}

{'operator': 'AND', 'negate': False, 'nodes': [{'operator': 'OR', 'negate': False, 'cpeMatch': [{'vulnerable': True, 'criteria': 'cpe:2.3:o:citrix:gateway_firmware:13.0:*:*:*:*:*:*:*', 'matchCriteriaId': 'A80EAFB1-82DA-49BE-815D-D248624B442C'}]}, {'operator': 'OR', 'negate': False, 'cpeMatch': [{'vulnerable': False, 'criteria': 'cpe:2.3:h:citrix:gateway:-:*:*:*:*:*:*:*', 'matchCriteriaId': '3EF98B43-71DB-4230-B7AC-76EC2B1F0533'}]}]}

我的程序:我得到信息,我用上面的代码将输出从“列表”转移到字符串(我不知道这是否是最好的方法)。

然后我用变量 "to_delet_char = ["''", '""', "{" ,"}", "vulnerable", ": True, 'criteria': ", ", : ", "'", "]", ",", "OR negate:", "operator:", "False", "cpeMatch:", "[", "]", ]

我的目标是删除输出中除“cpe”之外的所有信息,以得到“列表”或“字典”形式的结果,我只能在其中找到这种元素:

“cpe:2.3:o:citrix:netscaler_gateway_firmware:12.0::::::

我设法毫不费力地删除了所有内容,但是每次我无法定位时,匹配序列 ID 都是不同的。

是否有通过另一个库或不“仅恢复”cpe 或删除除“cpe”之外的所有内容然后将它们转换为列表或字典以用于数据库条目的解决方案

【问题讨论】:

  • 如果每个节点都是一个数据结构,那么将其字符串化会适得其反。您可以使用每个元素中的键“nodes”循环遍历列表,并为每个条目使用键“cpeMatch”循环遍历列表。然后你只需要用键“criteria”获取字典条目。
  • 最后,您没有删除任何内容,而是构建了一个新的 list[dict],其中仅包含您感兴趣的条目。
  • @Matthias 谢谢你的回答。我还是个初学者,我想我已经很好地理解了理论,但是在实践中,您是否有关于该主题的示例或文档,因为我不知道如何使用键在列表中“导航”或检索字典条目关键

标签: python python-3.x


【解决方案1】:

我认为很难删除字符串中的所有内容,因为您无法预见将来字符串中会包含什么。但是随后您可以发现要为 cpe 子字符串查找的模式。

只需添加它,对于每个循环,您都会查找子字符串,然后进行一些切片、拆分和修剪以获得最终输出。

nIndex = x.find('cpe')

print ((x[nIndex:].split())[0][:-2])

只是一行的例子,它会给你下面的输出,

我在上面向您展示的内容仅适用于一次迭代中的单个 cpe 子字符串。可能您将不得不在该迭代中找到一些。您可以参考这个关于如何检索多个索引的好例子 > https://stackoverflow.com/a/3873422/12128167

对于数据的存储,最简单的方法是使用一个列表,只需声明一个空的list=[],然后将其附加在list.append("your output")的末尾即可。如果你打算使用它们,你可以探索其他 python 集合 > https://www.w3schools.com/python/python_dictionaries.asp

【讨论】:

  • 感谢您的帮助,我根据我的目的稍微调整了您的代码,但是我注意到它只恢复输出中存在的第一个“cpe”,有时可能存在多个 cpe。代码: for x in conf: txt = ', '.join(str(x) for x in x.nodes) nIndex = txt.find('cpe:2.3:') print ((txt[nIndex:].split ())[0][:-2])
  • @Vilguax 如果有效,您可以使用它,我在答案中添加了一个链接,因为我也注意到有多个“cpe”。你可以看看它是否对你有帮助。
猜你喜欢
  • 1970-01-01
  • 2021-01-13
  • 2015-08-06
  • 2011-02-16
  • 2016-06-29
  • 1970-01-01
  • 2018-03-24
  • 2011-12-24
相关资源
最近更新 更多