From a124993c1c25b96e9118785f8c8c7058432d9baf Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 1 Dec 2026 12:44:02 +0100 Subject: [PATCH] feat(hardening): a host's security posture as optional HardeningDict keys (NetOrk/netork#2893) Secure Boot, the TPM, disk encryption per volume, the antimalware product, the firewall's profiles and virtualization-based security, in terms no OS owns, so a Windows host's protection fits the reading Linux fills. The new keys are NotRequired; every key Linux fills stays required and unchanged. --- CHANGELOG.md | 10 +++ README.md | 9 ++ napalm_device_types/__init__.py | 12 +++ napalm_device_types/hardening.py | 105 ++++++++++++++++++++++- tests/test_hardening_posture.py | 141 +++++++++++++++++++++++++++++++ 5 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 tests/test_hardening_posture.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 847c85a..3eb6ae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- A host's security posture in `HardeningDict` (NetOrk/netork#2893), as optional keys a + driver fills where it reads them, so a Windows host's protection fits the same reading as + a Linux host's hardening: `secure_boot` (`SecureBootState`: `on`, `off`, `unsupported`), + `tpm` (`TpmDict`), `disk_encryption` (`DiskEncryptionDict` with an `EncryptedVolumeDict` + per volume and its `VolumeProtection`), `antimalware` (`AntimalwareDict`, its + `AntimalwareMode` telling whether another product protects the host), `firewall_profiles` + (`FirewallProfileDict`, on or off) and `vbs` (`VbsDict`, `VbsStatus`). Everything Linux + fills stays required and unchanged; `parse_hardening` fills none of the new keys. + ## [4.55.0] - 2026-12-01 ### Added diff --git a/README.md b/README.md index 8248567..54d4c5a 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,15 @@ command line, `ID`/`ID_LIKE` from `/etc/os-release`, whether the host runs virtu (`systemd-detect-virt`, else the `hypervisor` CPU flag) and the CPU's `vendor_id`. Read as root first and, where root is refused, as the login user without the profile counts. A driver supplies `_run_hardening_command(command, privileged=)`; what was not read is `None`, never "off". +`HardeningDict` also carries a host's security posture in terms no OS owns, as optional keys +a driver fills where it reads them (NetOrk/netork#2893): `secure_boot` (`on`, `off`, or +`unsupported` on a BIOS boot), `tpm` (`TpmDict`), `disk_encryption` (`DiskEncryptionDict`: +the provider, whether it is installed, and each volume's protection, status and cipher), +`antimalware` (`AntimalwareDict`: enabled, real-time protection, the mode -- `passive` and +`edr_block` when another product protects the host -- and the signatures' date and age), +`firewall_profiles` (each profile on or off; the rules are the host firewall reading's) and +`vbs` (virtualization-based security and the services it runs). The Linux reading fills +none of them yet. They carry state, never a secret: no recovery password or key protector. `LoginAccountsMixin` (`get_login_accounts`) is mixed in the same way: every account on the host a person can log in with -- root and UID 1000 on, with a shell that is not `nologin` or diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index b6ff305..7cca34f 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -202,8 +202,14 @@ from napalm_device_types.fixes import ( ) from napalm_device_types.hardening import ( HARDENING_COMMAND, + AntimalwareDict, + DiskEncryptionDict, + EncryptedVolumeDict, + FirewallProfileDict, HardeningDict, HardeningMixin, + TpmDict, + VbsDict, cpu_vulnerability_state, parse_hardening, ) @@ -654,6 +660,12 @@ __all__ = [ "firmware_component", "microcode_package", "HardeningDict", + "AntimalwareDict", + "DiskEncryptionDict", + "EncryptedVolumeDict", + "FirewallProfileDict", + "TpmDict", + "VbsDict", "cpu_vulnerability_state", "parse_hardening", "LOGIN_ACCOUNTS_COMMAND", diff --git a/napalm_device_types/hardening.py b/napalm_device_types/hardening.py index 4431252..b545d89 100644 --- a/napalm_device_types/hardening.py +++ b/napalm_device_types/hardening.py @@ -28,14 +28,22 @@ here once and a driver only carries the command across. **Not read is None, neve empty or off:** a section the command did not print -- a file missing, a tool not installed, root refused -- comes back ``None``, so a consumer never reads "not looked" as "clean". + +**The security posture, on any OS** (NetOrk/netork#2893). Six optional keys answer the +first questions about a host's protection in terms no OS owns: ``secure_boot``, ``tpm``, +``disk_encryption`` (per volume), ``antimalware``, ``firewall_profiles`` and ``vbs`` +(virtualization-based security). A driver that reads them fills them; one that does not +leaves them out, which reads the same as ``None``: not read. ``parse_hardening`` (Linux) +fills none of them yet. They carry state only, never a secret: no recovery password, no +key protector, no key. """ -from __future__ import annotations - +# No ``from __future__ import annotations``: TypedDict reads ``NotRequired`` off the +# evaluated annotations, and HardeningDict's posture keys are optional. import json import re from shlex import quote -from typing import TYPE_CHECKING, Literal, TypedDict +from typing import TYPE_CHECKING, Literal, NotRequired, TypedDict from napalm_device_types.terminal import strip_terminal_codes @@ -98,6 +106,86 @@ class AppArmorDict(TypedDict): profiles: dict[str, int] | None +#: Secure Boot: "on" or "off" on a UEFI host; "unsupported" where the firmware has none +#: (a BIOS boot), which is no finding. +SecureBootState = Literal["on", "off", "unsupported"] +#: Whether a volume's encryption protects it now: "off" too where it is encrypted but its +#: protection is suspended, or its key sits in the clear on the volume. +VolumeProtection = Literal["on", "off", "unknown"] +#: Who protects the host: "active" the product itself; "passive" and "sxs_passive" another +#: product does, and this one only scans on demand; "edr_block" another product does, and +#: this one's EDR blocks what it detects after the fact. +AntimalwareMode = Literal["active", "passive", "edr_block", "sxs_passive"] +#: Virtualization-based security: "configured" is switched on but not running (yet). +VbsStatus = Literal["off", "configured", "running"] + + +class TpmDict(TypedDict): + """The host's Trusted Platform Module. ``None`` in a field: could not be read.""" + + present: bool # False: the host has no TPM, or the firmware hides it + enabled: bool | None + activated: bool | None + owned: bool | None # taken into use by the OS + version: str | None # the specification's version: "2.0", "1.2" + manufacturer: str | None # the vendor's ID as the TPM gives it: "INTC", "AMD", "MSFT" + + +class EncryptedVolumeDict(TypedDict): + """One volume the host's disk encryption knows.""" + + volume: str # where it is mounted: "C:", "/", "/home" + system: bool # the volume the OS runs from + protection: VolumeProtection + #: "fully_encrypted", "fully_decrypted", "encrypting", "decrypting", + #: "encryption_paused", "decryption_paused"; None where the tool gives none. + status: str | None + method: str | None # the cipher, in snake case: "xts_aes_128", "aes_256", ... + + +class DiskEncryptionDict(TypedDict): + """The host's own disk encryption: BitLocker on Windows, LUKS on Linux.""" + + provider: str # "bitlocker", "luks" + #: False where the host does not have it (BitLocker is a feature a Windows Server + #: installs first): no volume can be encrypted with it, ``volumes`` is empty. + installed: bool + volumes: list[EncryptedVolumeDict] + + +class AntimalwareDict(TypedDict): + """The host's own antimalware product, as it reports itself. ``None``: not read.""" + + product: str # "Microsoft Defender Antivirus" + enabled: bool | None # its scanning engine is on + real_time: bool | None # it scans files as they are opened + mode: AntimalwareMode | None + signatures_updated: int | None # when its signatures were last updated, unix seconds + signature_age_days: int | None # their age in days, as the product counts it + signature_version: str | None + tamper_protected: bool | None # its settings are locked against local changes + + +class FirewallProfileDict(TypedDict): + """One profile of the host's own firewall: whether it filters at all. + + Its rules are not here: what they let through is the host firewall reading's. + """ + + name: str # lowercase: "domain", "private", "public" + enabled: bool | None + + +class VbsDict(TypedDict): + """Virtualization-based security: the hypervisor-isolated part of the OS.""" + + status: VbsStatus | None + #: The services it runs, in snake case: "credential_guard", "hvci" (memory + #: integrity), "system_guard", "smm_firmware_measurement", "kernel_shadow_stacks", + #: "kernel_shadow_stacks_audit", "hypervisor_paging_translation"; None where not read. + running: list[str] | None + + class HardeningDict(TypedDict): """What :meth:`HardeningMixin.get_hardening` read. ``None``: not read, never "off".""" @@ -117,6 +205,13 @@ class HardeningDict(TypedDict): #: ``hypervisor`` CPU flag tells it. virtualization: str | None cpu_vendor: str | None # the first vendor_id of /proc/cpuinfo: "GenuineIntel", ... + # The security posture (NetOrk/netork#2893); left out where the driver does not read it. + secure_boot: NotRequired[SecureBootState | None] + tpm: NotRequired[TpmDict | None] + disk_encryption: NotRequired[DiskEncryptionDict | None] + antimalware: NotRequired[AntimalwareDict | None] # None also where it reports nothing + firewall_profiles: NotRequired[list[FirewallProfileDict] | None] + vbs: NotRequired[VbsDict | None] def cpu_vulnerability_state(text: str) -> CpuVulnerabilityState: @@ -350,6 +445,10 @@ class HardeningMixin: own hardware; "vm" where only the ``hypervisor`` CPU flag tells it * cpu_vendor (str or None) - ``vendor_id`` of ``/proc/cpuinfo`` + A driver may add the security posture (``secure_boot``, ``tpm``, + ``disk_encryption``, ``antimalware``, ``firewall_profiles``, ``vbs``); this Linux + reading does not yet. + ``None`` means not read. Read as root first, for AppArmor's profiles; where root is refused, as the login user, without them. diff --git a/tests/test_hardening_posture.py b/tests/test_hardening_posture.py new file mode 100644 index 0000000..a392c35 --- /dev/null +++ b/tests/test_hardening_posture.py @@ -0,0 +1,141 @@ +"""The OS-neutral security posture in HardeningDict (NetOrk/netork#2893). + +Secure Boot, the TPM, disk encryption per volume, the antimalware product, the host +firewall's profiles and virtualization-based security: optional keys beside what a Linux +host reports, so a Windows host's reading fits the same shape and Linux's stays as it is. +""" + +from __future__ import annotations + +from typing import get_args, get_type_hints + +from napalm_device_types import ( + AntimalwareDict, + DiskEncryptionDict, + EncryptedVolumeDict, + FirewallProfileDict, + HardeningDict, + TpmDict, + VbsDict, +) +from napalm_device_types.hardening import ( + AntimalwareMode, + SecureBootState, + VbsStatus, + VolumeProtection, + parse_hardening, +) + +LINUX_KEYS = frozenset( + { + "lsm", + "selinux", + "apparmor", + "crypto_policy", + "fips", + "cpu_vulnerabilities", + "cmdline", + "mitigations", + "nosmt", + "os_ids", + "virtualization", + "cpu_vendor", + } +) +POSTURE_KEYS = frozenset( + {"secure_boot", "tpm", "disk_encryption", "antimalware", "firewall_profiles", "vbs"} +) + + +def test_what_linux_fills_stays_required(): + assert HardeningDict.__required_keys__ == LINUX_KEYS + + +def test_the_posture_keys_are_optional(): + assert HardeningDict.__optional_keys__ == POSTURE_KEYS + + +def test_a_linux_reading_carries_none_of_them(): + reading = parse_hardening("HARDENING_BEGIN\n[fips]\n0\nHARDENING_END\n") + + assert set(reading) == LINUX_KEYS + + +def test_the_words_each_state_takes(): + assert set(get_args(SecureBootState)) == {"on", "off", "unsupported"} + assert set(get_args(VolumeProtection)) == {"on", "off", "unknown"} + assert set(get_args(AntimalwareMode)) == {"active", "passive", "edr_block", "sxs_passive"} + assert set(get_args(VbsStatus)) == {"off", "configured", "running"} + + +def test_the_shapes(): + assert set(get_type_hints(TpmDict)) == { + "present", + "enabled", + "activated", + "owned", + "version", + "manufacturer", + } + assert set(get_type_hints(EncryptedVolumeDict)) == { + "volume", + "system", + "protection", + "status", + "method", + } + assert set(get_type_hints(DiskEncryptionDict)) == {"provider", "installed", "volumes"} + assert set(get_type_hints(AntimalwareDict)) == { + "product", + "enabled", + "real_time", + "mode", + "signatures_updated", + "signature_age_days", + "signature_version", + "tamper_protected", + } + assert set(get_type_hints(FirewallProfileDict)) == {"name", "enabled"} + assert set(get_type_hints(VbsDict)) == {"status", "running"} + + +def test_a_windows_reading_fits_the_shape(): + reading: HardeningDict = { + **dict.fromkeys(LINUX_KEYS), # type: ignore[typeddict-item] + "secure_boot": "on", + "tpm": { + "present": True, + "enabled": True, + "activated": True, + "owned": True, + "version": "2.0", + "manufacturer": "INTC", + }, + "disk_encryption": { + "provider": "bitlocker", + "installed": True, + "volumes": [ + { + "volume": "C:", + "system": True, + "protection": "on", + "status": "fully_encrypted", + "method": "xts_aes_128", + } + ], + }, + "antimalware": { + "product": "Microsoft Defender Antivirus", + "enabled": True, + "real_time": True, + "mode": "active", + "signatures_updated": 1764547200, + "signature_age_days": 0, + "signature_version": "1.443.512.0", + "tamper_protected": True, + }, + "firewall_profiles": [{"name": "domain", "enabled": True}], + "vbs": {"status": "running", "running": ["credential_guard", "hvci"]}, + } + + assert set(reading) == LINUX_KEYS | POSTURE_KEYS