這是輸出格式,基于“CVE_data_meta”,我需要對匹配的 ID 進行重復資料洗掉。
#pull references
for ref in item["cve"]["references"]["reference_data"]:
references = ref["url"]
cleanData.append({"CVE_data_meta": cve_data_meta_id,
"description": description,
"baseScore": baseScore,
"vectorSring": vectorString,
"cweID": cweValue,
"cweID URL": ("https://cwe.mitre.org/data/definitions/"
str(cweValue) ".html"),
"references": references,
"publishedDate": pub_date,
"lastModifiedDate": last_mod_date
})
這是我從 API 的清理回應中提取資料并輸出到 JSON 檔案的迭代:
# # ==========================================================================================
# # narrow response with additional 'keywords'
# # ==========================================================================================
myResults = open("2-cleanData.json", "r")
scope = json.load(myResults)
output_json=[]
results = []
for k in keywords:
counter = 0
items = [x for x in scope if k in x['description']]
for item in items:
output_json.append(item)
counter = 1
results.append(counter)
with open("3-Final CVEs.json", "w ") as outFile2:
outFile2.write(json.dumps(output_json, indent=2,))
該keywords變數可由用戶更改;但希望任何人都能夠添加關鍵字并且不會在輸出檔案中獲得重復的條目。
完整代碼在這里。
示例輸出:(3 個 CVE 條目)
{
"CVE_data_meta": "CVE-2021-0924",
"description": "In xhci_vendor_get_ops of xhci.c, there is a possible out of bounds read due to a missing bounds check. This could lead to local escalation of privilege with no additional execution privileges needed. User interaction is not needed for exploitation.Product: AndroidVersions: Android kernelAndroid ID: A-194461020References: Upstream kernel",
"baseScore": 7.8,
"vectorSring": "CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H",
"cweID": "CWE-125",
"cweID URL": "https://cwe.mitre.org/data/definitions/CWE-125.html",
"references": "https://source.android.com/security/bulletin/2021-11-01",
"publishedDate": "2021-12-15T19:15Z",
"lastModifiedDate": "2021-12-17T18:12Z"
},
{
"CVE_data_meta": "CVE-2021-0981",
"description": "In enqueueNotificationInternal of NotificationManagerService.java, there is a possible way to run a foreground service without showing a notification due to improper input validation. This could lead to local escalation of privilege with no additional execution privileges needed. User interaction is not needed for exploitation.Product: AndroidVersions: Android-12Android ID: A-191981182",
"baseScore": 7.8,
"vectorSring": "CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H",
"cweID": "CWE-269",
"cweID URL": "https://cwe.mitre.org/data/definitions/CWE-269.html",
"references": "https://source.android.com/security/bulletin/pixel/2021-12-01",
"publishedDate": "2021-12-15T19:15Z",
"lastModifiedDate": "2021-12-17T18:09Z"
...several entries later...
"CVE_data_meta": "CVE-2021-0924",
"description": "In xhci_vendor_get_ops of xhci.c, there is a possible out of bounds read due to a missing bounds check. This could lead to local escalation of privilege with no additional execution privileges needed. User interaction is not needed for exploitation.Product: AndroidVersions: Android kernelAndroid ID: A-194461020References: Upstream kernel",
"baseScore": 7.8,
"vectorSring": "CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H",
"cweID": "CWE-125",
"cweID URL": "https://cwe.mitre.org/data/definitions/CWE-125.html",
"references": "https://source.android.com/security/bulletin/2021-11-01",
"publishedDate": "2021-12-15T19:15Z",
"lastModifiedDate": "2021-12-17T18:12Z"
},
現在,只需要擺脫重復...
uj5u.com熱心網友回復:
您可以通過使用set跟蹤'CVE_data_meta'已經看到的條目并跳過已經看到的條目來輕松地對結果進行重復資料洗掉,如下所示。set成員資格測驗非常快,所以這會很快。
使用此有限的測驗資料進行測驗:
myResults = [
{'CVE_data_meta': 'CVE-2021-0924',
'description': 'In xhci_vendor_get_ops of xhci.c, there is a possible out of '
'bounds read due to a missing bounds check. This could lead '
'to local escalation of privilege with no additional '
'execution privileges needed. User interaction is not needed '
'for exploitation.Product: AndroidVersions: Android '
'kernelAndroid ID: A-194461020References: Upstream kernel',
'baseScore': 7.8,
'vectorSring': 'CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H',
'cweID': 'CWE-125',
'cweID URL': 'https://cwe.mitre.org/data/definitions/CWE-125.html',
'references': 'https://source.android.com/security/bulletin/2021-11-01',
'publishedDate': '2021-12-15T19:15Z',
'lastModifiedDate': '2021-12-17T18:12Z'},
{'CVE_data_meta': 'CVE-2021-0981',
'description': 'In enqueueNotificationInternal of '
'NotificationManagerService.java, there is a possible way to '
'run a foreground service without showing a notification due '
'to improper input validation. This could lead to local '
'escalation of privilege with no additional execution '
'privileges needed. User interaction is not needed for '
'exploitation.Product: AndroidVersions: Android-12Android ID: '
'A-191981182',
'baseScore': 7.8,
'vectorSring': 'CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H',
'cweID': 'CWE-269',
'cweID URL': 'https://cwe.mitre.org/data/definitions/CWE-269.html',
'references': 'https://source.android.com/security/bulletin/pixel/2021-12-01',
'publishedDate': '2021-12-15T19:15Z',
'lastModifiedDate': '2021-12-17T18:09Z'},
{'CVE_data_meta': 'CVE-2021-0924',
'description': 'In xhci_vendor_get_ops of xhci.c, there is a possible out of '
'bounds read due to a missing bounds check. This could lead '
'to local escalation of privilege with no additional '
'execution privileges needed. User interaction is not needed '
'for exploitation.Product: AndroidVersions: Android '
'kernelAndroid ID: A-194461020References: Upstream kernel',
'baseScore': 7.8,
'vectorSring': 'CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H',
'cweID': 'CWE-125',
'cweID URL': 'https://cwe.mitre.org/data/definitions/CWE-125.html',
'references': 'https://source.android.com/security/bulletin/2021-11-01',
'publishedDate': '2021-12-15T19:15Z',
'lastModifiedDate': '2021-12-17T18:12Z'}
]
代碼:
from pprint import pprint
# Deduplicate results
cleaned = []
seen = set()
for obj in myResults:
key = obj['CVE_data_meta']
if key not in seen:
cleaned.append(obj)
seen.add(key)
pprint(cleaned)
uj5u.com熱心網友回復:
查看您的代碼后,我相信您可以執行以下操作以避免重復字典:
results = []
cve_ids = []
for k in keywords:
counter = 0
items = [x for x in scope if k in x['description']]
for item in items if item['cweID'] not in cwe_ids:
output_json.append(item)
cwe_ids.append(item['cweID'])
counter = 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/384521.html
