Files
napalm-zyxel/tests/test_fingerprint.py
T
Christian Manivong 6f9b91ae9b
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m18s
driver / test (3.13) (pull_request) Successful in 4m9s
driver / test (3.12) (pull_request) Successful in 4m28s
driver / lowest (pull_request) Successful in 3m41s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m18s
CI / driver (pull_request) Successful in 6m0s
driver / publish (pull_request) Skipped
fix(fingerprint): tell a DAL gateway from other Zyxel devices
The driver's evidence was all vendor-wide: Zyxel's enterprise OID, Zyxel
OUIs and "zyxel" on the web page. Every managed Zyxel device has all
three, and they were the driver's whole maximum, so discovery named any
Zyxel switch this gateway with high confidence.

Discovery now also fetches /getRSAPublickKey on ports 80 and 443, the
DAL key exchange the driver logs in with, without counting the port.
Its "RSAPublicKey" answer weighs 24: vendor-wide evidence alone is 28 of
52 (medium), a gateway 52 of 52, or 37 of 52 without SNMP.

Closes #13
2026-11-02 08:47:46 +01:00

110 lines
4.2 KiB
Python

"""Discovery tells a DAL gateway from any other Zyxel device (#13).
The driver's evidence was all vendor-wide: Zyxel's enterprise OID, Zyxel OUIs and the
word "zyxel" on the web page. Every managed Zyxel device (switch, firewall, NAS, access
point) has all three, and under discovery's normalisation they made up the driver's
whole maximum: 28 of 28, confidence "high".
What only a gateway this driver can talk to has is the DAL's key exchange: an
unauthenticated ``GET /getRSAPublickKey`` that answers ``{"RSAPublicKey": ...}``. The
driver cannot log in without it (``ZyxelVMGDriver.open``).
``_netork_confidence`` mirrors how netOrk scores a driver
(``netork/jobs/tasks/discovery_fingerprint.py``: ``_score_driver``,
``_max_possible_score``, ``norm = score / max``, "high" from 0.7, "medium" from 0.4).
"""
from __future__ import annotations
import re
import pytest
from napalm_device_types import rules_max_weight
from napalm_zyxel import ZyxelVMGDriver
#: What netOrk adds for a matching sysObjectID prefix and for a matching OUI.
OID_WEIGHT = 15.0
OUI_WEIGHT = 6.0
ZYXEL_OID = "1.3.6.1.4.1.890.1.5.8.20"
ZYXEL_MAC = "00:13:49:aa:bb:cc"
OTHER_MAC = "00:00:5e:00:53:01"
SWITCH_PAGE = "<title>ZyXEL GS1920-24HP</title> zyxel web configurator"
#: The gateway's landing page and its answer to ``GET /getRSAPublickKey``, as the
#: probe accumulates them (it fetches every path of a scheme into one body).
GATEWAY_PAGE = (
"<title>Zyxel VMG4005-B50A</title> zyxel "
'{"RSAPublicKey": "-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkq...\\n-----END PUBLIC KEY-----\\n"}'
)
def _netork_score(cls: type[ZyxelVMGDriver], oid: str | None, mac: str | None, page: str) -> float:
score = 0.0
if mac and mac[:8].upper() in (p.upper() for p in cls.OUI_PREFIXES):
score += OUI_WEIGHT
if oid:
if not oid.startswith(cls.SNMP_OBJECT_ID_PREFIX):
return 0.0
score += OID_WEIGHT
combined = re.sub(r"\s+", " ", page.lower())
for rule in cls.HTTP_FINGERPRINT:
matched = rule.pattern in combined
if rule.mandatory and not matched:
return 0.0
if matched:
score += -rule.weight if rule.negative else rule.weight
return score
def _netork_max(cls: type[ZyxelVMGDriver]) -> float:
total = sum(spec.weight for spec in cls.PORT_SPECS or [])
total += OID_WEIGHT if cls.SNMP_OBJECT_ID_PREFIX else 0.0
total += OUI_WEIGHT if cls.OUI_PREFIXES else 0.0
rules = (*cls.SNMP_FINGERPRINT, *cls.SSH_FINGERPRINT, *cls.HTTP_FINGERPRINT)
return total + rules_max_weight(rules) or 1.0
def _netork_confidence(oid: str | None, mac: str | None, page: str) -> tuple[float, str]:
norm = _netork_score(ZyxelVMGDriver, oid, mac, page) / _netork_max(ZyxelVMGDriver)
return norm, "high" if norm >= 0.7 else "medium" if norm >= 0.4 else "low"
def test_vendor_wide_evidence_alone_stays_below_high():
"""A Zyxel switch: Zyxel OID, Zyxel OUI, "zyxel" on its page."""
norm, confidence = _netork_confidence(ZYXEL_OID, ZYXEL_MAC, SWITCH_PAGE)
assert norm < 0.7
assert confidence != "high"
def test_a_gateway_with_snmp_scores_the_maximum():
assert _netork_confidence(ZYXEL_OID, ZYXEL_MAC, GATEWAY_PAGE) == (1.0, "high")
def test_a_gateway_without_snmp_is_still_high():
"""Residential gateways often answer no SNMP; the DAL answer carries them."""
assert _netork_confidence(None, ZYXEL_MAC, GATEWAY_PAGE)[1] == "high"
def test_the_dal_answer_outweighs_snmp_and_oui_together():
"""A gateway behind a router (no OUI) without SNMP beats a switch with both."""
gateway, _ = _netork_confidence(None, OTHER_MAC, GATEWAY_PAGE)
switch, _ = _netork_confidence(ZYXEL_OID, ZYXEL_MAC, SWITCH_PAGE)
assert gateway > switch
@pytest.mark.parametrize(("scheme", "port"), [("http", 80), ("https", 443)])
def test_discovery_fetches_the_dal_key_exchange_without_counting_the_port(scheme, port):
specs = [s for s in ZyxelVMGDriver.PORT_SPECS or [] if (s.scheme, s.port) == (scheme, port)]
assert [(s.paths, s.weight, s.mandatory) for s in specs] == [
(("/getRSAPublickKey",), 0.0, False)
]
def test_the_probed_path_is_the_one_the_driver_logs_in_with():
assert ZyxelVMGDriver._RSA_KEY_PATH == "getRSAPublickKey"