get_host_firewall reads the Windows Firewall: the profiles' settings in force, every enabled rule of the active policy with its filters read in bulk and joined on InstanceID, and the profile of each connected interface. get_windows_features lists the image's optional features with their state from Win32_OptionalFeature. Both scripts are in the harvest.
72 lines
2.9 KiB
Python
72 lines
2.9 KiB
Python
"""Windows optional features: which roles and features a host has on (NetOrk/netork#2898).
|
|
|
|
:class:`WindowsFeaturesMixin` implements napalm-device-types'
|
|
:class:`~napalm_device_types.windows_features.WindowsFeaturesMixin`: the facts a CVE that
|
|
needs a role or feature -- the DNS Server, Hyper-V, IIS, SMB 1.0, Message Queuing --
|
|
is held against.
|
|
|
|
**One CIM query** (``Win32_OptionalFeature``), not ``Get-WindowsOptionalFeature -Online``,
|
|
which loads the DISM module and takes far longer, and not Server Manager's
|
|
``Get-WindowsFeature``, which a client has not. The class lists every feature the image
|
|
knows, on a client and a server, under the names DISM gives them (``SMB1Protocol``,
|
|
``MSMQ-Server``, ``IIS-WebServerRole``); a server's roles are among them under those
|
|
names. ``InstallState`` is 1 enabled, 2 disabled, 3 absent (the payload removed),
|
|
4 unknown. A pending change shows as the state the feature has now.
|
|
|
|
No feature at all is a reading that went wrong -- every image has features -- and raises
|
|
rather than reading as a host with none.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from napalm_device_types import WindowsFeaturesMixin as _Contract
|
|
from napalm_device_types.windows_features import WindowsFeatureDict, WindowsFeaturesDict
|
|
|
|
from napalm_windows._powershell import _as_list
|
|
|
|
_PS_WINDOWS_FEATURES = r"""
|
|
$ErrorActionPreference = 'Stop'
|
|
ConvertTo-Json -Compress -Depth 3 -InputObject @(
|
|
Get-CimInstance -ClassName Win32_OptionalFeature | ForEach-Object {
|
|
[pscustomobject]@{ name = [string]$_.Name; state = [int]$_.InstallState }
|
|
}
|
|
)
|
|
"""
|
|
|
|
#: Win32_OptionalFeature's InstallState.
|
|
_STATES = {1: "enabled", 2: "disabled", 3: "absent", 4: "unknown"}
|
|
|
|
|
|
def parse_windows_features(raw: Any) -> WindowsFeaturesDict:
|
|
"""What ``_PS_WINDOWS_FEATURES`` emitted, as names and states.
|
|
|
|
:raises ValueError: when no feature came back.
|
|
"""
|
|
features: list[WindowsFeatureDict] = []
|
|
for entry in _as_list(raw):
|
|
name = str(entry.get("name") or "").strip() if isinstance(entry, dict) else ""
|
|
if name:
|
|
features.append({"name": name, "state": _STATES.get(entry.get("state"), "unknown")})
|
|
if not features:
|
|
raise ValueError("the Windows features reading came back without features")
|
|
return {"features": features}
|
|
|
|
|
|
class WindowsFeaturesMixin(_Contract):
|
|
"""``get_windows_features`` for a Windows host; the driver carries the script."""
|
|
|
|
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
|
|
|
|
def _run_raw(self, script: str) -> str: ...
|
|
|
|
def get_windows_features(self) -> WindowsFeaturesDict:
|
|
"""Every optional feature of the host's image with its state; see the module.
|
|
|
|
:raises ValueError: when no feature came back.
|
|
"""
|
|
out = self._run_raw(_PS_WINDOWS_FEATURES).strip()
|
|
return parse_windows_features(json.loads(out) if out else None)
|