156 lines
7.3 KiB
Python
156 lines
7.3 KiB
Python
from __future__ import annotations
|
|
|
|
from vuln_mcp.matching import make_asset
|
|
from vuln_mcp.models import AssetFacts, Doc, Issue, Status
|
|
from vuln_mcp.search import Claim, Group, Observation, _base_checks, _claim_coverage, _query, _render
|
|
|
|
CVE = 'CVE-2025-43715'
|
|
OTHER = 'CVE-2025-43999'
|
|
GHSA = 'GHSA-abcd-1234-wxyz'
|
|
EDB = 'https://www.exploit-db.com/exploits/51001'
|
|
MSF = 'https://github.com/rapid7/metasploit-framework/blob/master/modules/exploits/linux/http/cve_2025_43715.rb'
|
|
POC = 'https://github.com/example/poc-cve-2025-43715'
|
|
TEMPLATE = 'https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/main/http/cves/2025/CVE-2025-43715.yaml'
|
|
|
|
|
|
def _claim(source: str, native: str, facts: dict, *, url: str | None = None, state: str = 'published') -> Claim:
|
|
doc = Doc(source=source, native_id=native, publisher=source, url=url or f'https://example.test/{native}',
|
|
title=native, state=state, issues=[Issue(id=native, facts=facts)])
|
|
return Claim(Observation(doc, 'live'), doc.issues[0])
|
|
|
|
|
|
def _group(*claims: Claim) -> Group:
|
|
group = Group(CVE)
|
|
group.claims.extend(claims)
|
|
return group
|
|
|
|
|
|
def _index() -> dict[str, Status]:
|
|
return {source: Status(source=source, status='initializing') for source in
|
|
('kev', 'exploitdb', 'msrc', 'pocs', 'nuclei', 'metasploit')}
|
|
|
|
|
|
def _hit(group: Group, asset: AssetFacts | None = None):
|
|
query = _query(CVE, None)
|
|
target = make_asset('id', CVE, asset)
|
|
return _render(group, query, target, [], _index())
|
|
|
|
|
|
def test_refs_are_ordered_by_type() -> None:
|
|
group = _group(
|
|
_claim('pocs', CVE, {'exploit_refs': [{'type': 'poc_repo', 'url': POC}]}),
|
|
_claim('nuclei', CVE, {'exploit_refs': [{'type': 'detection_template', 'url': TEMPLATE}]}),
|
|
_claim('metasploit', CVE, {'exploit_refs': [{'type': 'metasploit_module', 'url': MSF}]}),
|
|
_claim('exploitdb', CVE, {'exploit_refs': [{'type': 'exploit_db', 'url': EDB, 'edb_id': '51001'}]}),
|
|
)
|
|
assert [(ref['type'], ref['url']) for ref in _hit(group).exploit_refs] == [
|
|
('exploit_db', EDB), ('metasploit_module', MSF), ('poc_repo', POC), ('detection_template', TEMPLATE),
|
|
]
|
|
|
|
|
|
def test_verified_refs_precede_unverified_ones_of_the_same_type() -> None:
|
|
group = _group(_claim('exploitdb', CVE, {'exploit_refs': [
|
|
{'type': 'exploit_db', 'url': EDB + '9'},
|
|
{'type': 'exploit_db', 'url': EDB + '1', 'verified': True},
|
|
{'type': 'exploit_db', 'url': EDB + '5'},
|
|
]}))
|
|
assert [ref['url'] for ref in _hit(group).exploit_refs] == [EDB + '1', EDB + '9', EDB + '5']
|
|
|
|
|
|
def test_refs_dedup_by_type_and_url_and_keep_extra_fields() -> None:
|
|
group = _group(
|
|
_claim('exploitdb', CVE, {'exploit_refs': [
|
|
{'type': 'exploit_db', 'url': EDB, 'edb_id': '51001', 'verified': True},
|
|
{'type': 'exploit_db', 'url': EDB, 'edb_id': '51001'},
|
|
]}),
|
|
_claim('pocs', CVE, {'exploit_refs': [
|
|
{'type': 'poc_repo', 'url': POC},
|
|
{'type': 'poc_repo', 'url': POC},
|
|
]}),
|
|
)
|
|
refs = _hit(group).exploit_refs
|
|
assert [ref['url'] for ref in refs] == [EDB, POC]
|
|
assert refs[0]['edb_id'] == '51001' and refs[0]['verified'] is True
|
|
|
|
|
|
def test_refs_are_capped() -> None:
|
|
urls = [f'https://www.exploit-db.com/exploits/{51000 + number}' for number in range(30)]
|
|
group = _group(_claim('exploitdb', CVE, {'exploit_refs': [{'type': 'exploit_db', 'url': url} for url in urls]}))
|
|
refs = _hit(group).exploit_refs
|
|
assert len(refs) == 20
|
|
assert refs[0]['url'] == urls[0]
|
|
|
|
|
|
def test_unsafe_refs_are_dropped() -> None:
|
|
unsafe = [
|
|
{'type': 'exploit_db', 'url': EDB}, # exploit-db path would fail the source-ref filter
|
|
{'type': 'poc_repo', 'url': 'http://github.com/example/insecure'},
|
|
{'type': 'poc_repo', 'url': 'https://user:secret@github.com/example/userinfo'},
|
|
{'type': 'poc_repo', 'url': 'https://github.com:8443/example/port'},
|
|
{'type': 'poc_repo', 'url': 'not-a-url'},
|
|
{'type': 'poc_repo', 'url': 'https:///no-host'},
|
|
{'type': 'writeup', 'url': POC},
|
|
{'type': 'poc_repo'},
|
|
{'url': POC},
|
|
'string-entry',
|
|
]
|
|
hit = _hit(_group(_claim('pocs', CVE, {'exploit_refs': unsafe})))
|
|
assert hit.exploit_refs == [{'type': 'exploit_db', 'url': EDB}]
|
|
assert hit.source_refs == []
|
|
|
|
|
|
def test_exploitation_passthrough() -> None:
|
|
values = {'automatable': 'yes', 'technical_impact': 'remote code execution', 'cvss_av': 'N',
|
|
'cvss_pr': 'N', 'cvss_ui': 'N', 'component_role': 'management interface'}
|
|
hit = _hit(_group(_claim('metasploit', CVE, {'exploitation': values})))
|
|
assert hit.exploitation == values
|
|
|
|
|
|
def test_exploitation_fills_missing_keys_and_skips_foreign_values() -> None:
|
|
group = _group(
|
|
_claim('metasploit', CVE, {'exploitation': {'automatable': 'yes', 'cvss_av': 'N', 'cvss_pr': 7}}),
|
|
_claim('pocs', CVE, {'exploitation': {'technical_impact': 'denial of service', 'cvss_av': 'L',
|
|
'component_role': None}}),
|
|
)
|
|
hit = _hit(group)
|
|
assert hit.exploitation == {
|
|
'automatable': 'yes', 'technical_impact': 'denial of service', 'cvss_av': 'N',
|
|
'cvss_pr': None, 'cvss_ui': None, 'component_role': None,
|
|
}
|
|
assert _hit(_group(_claim('pocs', CVE, {}))).exploitation is None
|
|
|
|
|
|
def test_verdict_and_fixes_are_unchanged() -> None:
|
|
product = {'publisher': 'Siemens', 'name': 'Ctrl', 'model': 'M1', 'status': 'affected',
|
|
'versions': ['1.3.0'], 'events': [{'introduced': '0'}, {'fixed': '1.3.1'}]}
|
|
plain = _group(_claim('cvelist', CVE, {}, url='https://www.cve.org/CVERecord?id=' + CVE))
|
|
plain.claims[0].issue.products.append(product)
|
|
enriched = _group(
|
|
_claim('cvelist', CVE, {}, url='https://www.cve.org/CVERecord?id=' + CVE),
|
|
_claim('pocs', CVE, {'exploit_refs': [{'type': 'poc_repo', 'url': POC}],
|
|
'exploitation': {'automatable': 'yes'}}),
|
|
)
|
|
enriched.claims[0].issue.products.append(product)
|
|
asset = AssetFacts(product='M1', version='1.3.0')
|
|
left, right = _hit(plain, asset), _hit(enriched, asset)
|
|
assert (left.applicability, left.fixed_versions, left.match_reason) == ('affected', ['1.3.1'], 'exact_id')
|
|
assert (right.applicability, right.fixed_versions, right.match_reason) == \
|
|
(left.applicability, left.fixed_versions, left.match_reason)
|
|
assert right.source_refs == left.source_refs
|
|
assert right.exploit_refs == [{'type': 'poc_repo', 'url': POC}]
|
|
|
|
|
|
def test_exploit_checks_are_scheduled_for_cves_only() -> None:
|
|
planned = {(check.source, check.native_id) for check in _base_checks(CVE)}
|
|
assert {('pocs', CVE), ('nuclei', CVE), ('metasploit', CVE)} <= planned
|
|
assert all(check.native_id == CVE for check in _base_checks(CVE) if check.source in {'pocs', 'nuclei', 'metasploit'})
|
|
assert {check.source for check in _base_checks(GHSA)} == {'osv', 'circl'}
|
|
assert {check.source for check in _base_checks(OTHER)} >= {'pocs', 'nuclei', 'metasploit'}
|
|
|
|
|
|
def test_unrun_exploit_checks_expose_coverage() -> None:
|
|
hit = _hit(_group(_claim('cvelist', CVE, {})))
|
|
assert hit.coverage == []
|
|
statuses = _claim_coverage(_group(_claim('cvelist', CVE, {})), {}, _index())
|
|
for source in ('pocs', 'nuclei', 'metasploit'):
|
|
assert any(status.source == source and status.status == 'initializing' for status in statuses) |