Skip to content

armgit / bbot

Public Imported from GitHub
0

The recursive internet scanner for hackers. 🧡

Name CPE-derived technologies by product and retain the CPE (#3535)

Paul Muellere89bd2c7652f
diff --git a/bbot/core/event/base.py b/bbot/core/event/base.py
index b0a73ebc3..c87b7b838 100644
--- a/bbot/core/event/base.py
+++ b/bbot/core/event/base.py
@@ -2040,6 +2040,7 @@ class TECHNOLOGY(DictHostEvent):
host: str
technology: str
url: Optional[str] = None
+ cpe: Optional[str] = None
_validate_url = field_validator("url")(validators.validate_url)
_validate_host = field_validator("host")(validators.validate_host)
diff --git a/bbot/core/helpers/misc.py b/bbot/core/helpers/misc.py
index 1baf3da42..7143dd25f 100644
--- a/bbot/core/helpers/misc.py
+++ b/bbot/core/helpers/misc.py
@@ -2613,6 +2613,36 @@ def weighted_shuffle(items, weights):
return [item for _, item in keyed]
+cpe_field_regex = re.compile(r"(?<!\\):")
+
+
+def cpe_product(cpe):
+ """
+ Get the product name from a CPE, either a 2.2 URI or a 2.3 formatted string.
+
+ Returns None if the string isn't a CPE or doesn't name a product.
+
+ Examples:
+ >>> cpe_product("cpe:/a:jquery:jquery:3.5.1")
+ 'jquery'
+ >>> cpe_product("cpe:2.3:a:apache:tomcat:9.0.50:*:*:*:*:*:*:*")
+ 'tomcat'
+ """
+ if cpe.startswith("cpe:2.3:"):
+ # 2.3 escapes literal colons with a backslash
+ fields = [f.replace("\\", "") for f in cpe_field_regex.split(cpe[len("cpe:2.3:") :])]
+ elif cpe.startswith("cpe:/"):
+ fields = cpe[len("cpe:/") :].split(":")
+ else:
+ return None
+ # fields are part, vendor, product, version, ...
+ product = fields[2].lower() if len(fields) > 2 else ""
+ # "*" and "-" mean ANY and N/A
+ if product in ("", "*", "-"):
+ return None
+ return product
+
+
def parse_port_string(port_string):
"""
Parses a string containing ports and port ranges into a list of individual ports.
diff --git a/bbot/modules/censys_ip.py b/bbot/modules/censys_ip.py
index 078dce65f..8e6bab22a 100644
--- a/bbot/modules/censys_ip.py
+++ b/bbot/modules/censys_ip.py
@@ -152,10 +152,14 @@ class censys_ip(censys):
# Extract software/technologies
for software in service.get("software", []):
- product = software.get("uniform_resource_identifier", software.get("product", ""))
- if product:
+ cpe = software.get("uniform_resource_identifier", "")
+ technology = (self.helpers.cpe_product(cpe) if cpe else None) or software.get("product", "")
+ if technology:
+ tech = {"technology": technology, "host": str(event.host)}
+ if cpe:
+ tech["cpe"] = cpe
await self.emit_event(
- {"technology": product, "host": str(event.host)},
+ tech,
"TECHNOLOGY",
parent=event,
context="{module} found {event.type}: {event.data[technology]} on {event.parent.data}",
diff --git a/bbot/modules/shodan_enterprise.py b/bbot/modules/shodan_enterprise.py
index b87442816..86e98b5ce 100644
--- a/bbot/modules/shodan_enterprise.py
+++ b/bbot/modules/shodan_enterprise.py
@@ -63,40 +63,21 @@ class shodan_enterprise(BaseModule):
for data in host["data"]:
# TECHNOLOGY Extraction
- ## TECHNOLOGY CPE Formats
- for technology in data.get("cpe", []):
- tech = {"technology": technology, "host": data.get("ip_str"), "port": data.get("port")}
+ # name technologies by their CPE product, falling back to Shodan's product name when there's no CPE
+ technologies = []
+ for cpe in data.get("cpe23") or data.get("cpe") or []:
+ technology = self.helpers.cpe_product(cpe)
+ if technology is not None:
+ technologies.append({"technology": technology, "cpe": cpe})
+ if not technologies and "product" in data:
+ technologies.append({"technology": data["product"]})
+ for tech in technologies:
await self.emit_event(
- tech,
+ {**tech, "host": data.get("ip_str")},
"TECHNOLOGY",
parent=event,
tags=data.get("tags") or [],
- context=f"{{module}} queried Shodan API for {ip} and found TECHNOLOGY: {technology}",
- )
-
- for technology in data.get("cpe23", []):
- tech = {"technology": technology, "host": data.get("ip_str"), "port": data.get("port")}
- await self.emit_event(
- tech,
- "TECHNOLOGY",
- parent=event,
- tags=data.get("tags") or [],
- context=f"{{module}} queried Shodan API for {ip} and found TECHNOLOGY: {technology}",
- )
-
- # TECHNOLOGY Additional Formats
- if "product" in data:
- tech = {
- "technology": data.get("product"),
- "host": data.get("ip_str"),
- "port": data.get("port"),
- }
- await self.emit_event(
- tech,
- "TECHNOLOGY",
- parent=event,
- tags=data.get("tags") or [],
- context=f"{{module}} queried Shodan API for {ip} and found TECHNOLOGY: {data['product']}",
+ context=f"{{module}} queried Shodan API for {ip} and found TECHNOLOGY: {tech['technology']}",
)
if "http" in data:
diff --git a/bbot/modules/shodan_idb.py b/bbot/modules/shodan_idb.py
index 4519c0863..9615fd7ad 100644
--- a/bbot/modules/shodan_idb.py
+++ b/bbot/modules/shodan_idb.py
@@ -129,8 +129,12 @@ class shodan_idb(BaseModule):
context=f'{{module}} queried Shodan\'s InternetDB API for "{query_host}" and found {{event.type}}: {{event.pretty_string}}',
)
for cpe in data.get("cpes", []):
+ technology = self.helpers.cpe_product(cpe)
+ if technology is None:
+ self.debug(f"Skipping CPE without a product: {cpe}")
+ continue
await self.emit_event(
- {"technology": cpe, "host": str(event.host)},
+ {"technology": technology, "cpe": cpe, "host": str(event.host)},
"TECHNOLOGY",
parent=event,
context=f'{{module}} queried Shodan\'s InternetDB API for "{query_host}" and found {{event.type}}: {{event.pretty_string}}',
diff --git a/bbot/test/test_step_1/test_helpers.py b/bbot/test/test_step_1/test_helpers.py
index a8f7ae8b6..4146463b4 100644
--- a/bbot/test/test_step_1/test_helpers.py
+++ b/bbot/test/test_step_1/test_helpers.py
@@ -773,6 +773,23 @@ async def test_word_cloud(helpers, bbot_scanner):
await scan1._cleanup()
+def test_cpe_product(helpers):
+ assert helpers.cpe_product("cpe:/a:jquery:jquery:3.5.1") == "jquery"
+ assert helpers.cpe_product("cpe:/o:microsoft:windows") == "windows"
+ assert (
+ helpers.cpe_product("cpe:/a:Microsoft:Internet_Information_Services:10.0") == "internet_information_services"
+ )
+ assert helpers.cpe_product("cpe:2.3:a:apache:tomcat:9.0.50:*:*:*:*:*:*:*") == "tomcat"
+ # 2.3 escapes literal colons
+ assert helpers.cpe_product("cpe:2.3:a:vendor:foo\\:bar:1.0:*:*:*:*:*:*:*") == "foo:bar"
+ # no product
+ assert helpers.cpe_product("cpe:/a:vendor") is None
+ assert helpers.cpe_product("cpe:2.3:a:vendor:*:1.0:*:*:*:*:*:*:*") is None
+ assert helpers.cpe_product("cpe:2.3:a:vendor:-:1.0:*:*:*:*:*:*:*") is None
+ # not a CPE
+ assert helpers.cpe_product("nginx") is None
+
+
def test_names(helpers):
assert helpers.names == sorted(helpers.names)
assert helpers.adjectives == sorted(helpers.adjectives)
diff --git a/bbot/test/test_step_2/module_tests/test_module_censys_ip.py b/bbot/test/test_step_2/module_tests/test_module_censys_ip.py
index 6c94a2de7..071b41a27 100644
--- a/bbot/test/test_step_2/module_tests/test_module_censys_ip.py
+++ b/bbot/test/test_step_2/module_tests/test_module_censys_ip.py
@@ -215,7 +215,9 @@ class TestCensys_IP(ModuleTestBase):
# Check TECHNOLOGY events from software
assert any(
- e.type == "TECHNOLOGY" and e.data["technology"] == "cpe:2.3:a:apache:tomcat:9.0.50:*:*:*:*:*:*:*"
+ e.type == "TECHNOLOGY"
+ and e.data["technology"] == "tomcat"
+ and e.data["cpe"] == "cpe:2.3:a:apache:tomcat:9.0.50:*:*:*:*:*:*:*"
for e in events
), "Failed to detect Apache Tomcat technology with CPE"
assert any(e.type == "TECHNOLOGY" and e.data["technology"] == "java" for e in events), (
diff --git a/bbot/test/test_step_2/module_tests/test_module_shodan_enterprise.py b/bbot/test/test_step_2/module_tests/test_module_shodan_enterprise.py
index 0860c95da..030af7e4c 100644
--- a/bbot/test/test_step_2/module_tests/test_module_shodan_enterprise.py
+++ b/bbot/test/test_step_2/module_tests/test_module_shodan_enterprise.py
@@ -63,8 +63,12 @@ class TestShodan_Enterprise(ModuleTestBase):
assert finding_map["CVE-2020-00001"] == "LOW"
tech_events = [e for e in events if e.type == "TECHNOLOGY"]
tech_names = {e.data.get("technology") for e in tech_events}
- assert "cpe:/a:google:dns" in tech_names
+ # named by CPE product, with the CPE retained
+ assert "dns" in tech_names
+ assert any(e.data.get("cpe") == "cpe:2.3:a:google:dns:1.0:*:*:*:*:*:*:*" for e in tech_events)
+ # the udp service has no CPE, so it falls back to Shodan's product name
assert "google public dns" in tech_names
+ assert not any(name.startswith("cpe:") for name in tech_names)
assert "openssl" in tech_names
assert "nginx" in tech_names
diff --git a/bbot/test/test_step_2/module_tests/test_module_shodan_idb.py b/bbot/test/test_step_2/module_tests/test_module_shodan_idb.py
index e1dc470d3..3e3108e43 100644
--- a/bbot/test/test_step_2/module_tests/test_module_shodan_idb.py
+++ b/bbot/test/test_step_2/module_tests/test_module_shodan_idb.py
@@ -70,7 +70,15 @@ class TestShodan_IDB(ModuleTestBase):
assert description.index("CVE-2021-26855") < description.index("CVE-2021-26857")
assert "\n" not in description
assert finding_events[0].data["severity"] == "INFO"
- assert 2 == len([e for e in events if e.type == "TECHNOLOGY" and str(e.module) == "shodan_idb"])
+ technologies = {
+ e.data["technology"]: e.data["cpe"]
+ for e in events
+ if e.type == "TECHNOLOGY" and str(e.module) == "shodan_idb"
+ }
+ assert technologies == {
+ "internet_information_services": "cpe:/a:microsoft:internet_information_services",
+ "outlook_web_access": "cpe:/a:microsoft:outlook_web_access:15.0.1367",
+ }
assert set(module_test.module._cve_cache.keys()) == {"CVE-2021-26857", "CVE-2021-26855"}
assert module_test.cvedb_request_counts == {"CVE-2021-26857": 1, "CVE-2021-26855": 1}