Merge remote-tracking branch 'origin/feature/windows-logins-2895' into chore/merge-train-windows-depth
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 22s
driver / lowest (pull_request) Successful in 42s
driver / types (pull_request) Successful in 1m24s
driver / test (3.12) (pull_request) Successful in 1m23s
driver / test (3.13) (pull_request) Successful in 1m16s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Successful in 1m23s
CI / driver (pull_request) Successful in 2m58s
driver / publish (pull_request) Skipped

This commit is contained in:
Christian Manivong committed 2026-12-01 23:21:04 +01:00
commit 52286e4009
2 files changed
+36 -13

No files matched your search

+14 -4
View File
@@ -208,19 +208,29 @@ def _local(sid: str | None, domain: str | None, computer: str, machine: str | No
return None if domain is None else domain.upper() == computer.upper()
def _member_name(parts: list[str], sid: str | None) -> tuple[str, str | None]:
"""The member's name as ``DOMAIN\\name`` and its domain; a bare SID has none."""
if len(parts) >= 2:
return f"{parts[-2]}\\{parts[-1]}", parts[-2]
return (parts[0] if parts else str(sid or "")), None
def _is_group(raw: dict[str, Any]) -> bool | None:
kind = str(raw.get("class") or "").lower()
return {"group": True, "user": False}.get(kind)
def _member(raw: dict[str, Any], computer: str, machine: str | None) -> LoginGroupMemberDict:
"""A ``WinNT:`` member: ``WinNT://DOMAIN/name``, ``WinNT://WORKGROUP/HOST/name`` for
the host's own, ``WinNT://S-1-5-…`` for a SID the host can no longer resolve."""
parts = [p for p in str(raw.get("path") or "").removeprefix("WinNT://").split("/") if p]
sid = str(raw.get("sid") or "") or None
domain = parts[-2] if len(parts) >= 2 else None
name = f"{domain}\\{parts[-1]}" if domain else (parts[0] if parts else str(sid or ""))
kind = str(raw.get("class") or "").lower()
name, domain = _member_name(parts, sid)
return {
"name": name,
"sid": sid,
"local": _local(sid, domain, computer, machine),
"group": True if kind == "group" else False if kind == "user" else None,
"group": _is_group(raw),
}
+22 -9
View File
@@ -209,24 +209,37 @@ def _session(event: dict[str, Any], computer: str, ends: dict[int, list[int]]) -
}
def parse_login_sessions(raw: dict[str, Any]) -> LoginSessionsDict:
"""What :data:`_PS_LOGIN_SESSIONS` emitted, as napalm-device-types' reading.
:raises LoginSessionsUnavailable: when the host does not audit logons.
"""
def _audited_logons(raw: dict[str, Any]) -> list[dict[str, Any]]:
"""The logon events read; raises when the log holds none, not even this reading's own."""
logons = [e for e in _as_list(raw.get("logons")) if isinstance(e, dict)]
if raw.get("canary") is False and not logons:
raise LoginSessionsUnavailable(
"the Security log holds no logon, not even this reading's own: logons are not audited"
)
own, computer, ends = _own_address(raw), str(raw.get("computer") or ""), _ends(raw)
timed = [e for e in logons if isinstance(e.get("t"), int)]
sessions = [_session(e, computer, ends) for e in _logons(timed)]
return [e for e in logons if isinstance(e.get("t"), int)]
def _sessions(
logons: list[dict[str, Any]], raw: dict[str, Any], own: str | None
) -> list[LoginSessionDict]:
"""The sessions newest first, those from *own* left out, at most SESSIONS_READ."""
computer, ends = str(raw.get("computer") or ""), _ends(raw)
sessions = [_session(e, computer, ends) for e in _logons(logons)]
sessions = [s for s in sessions if own is None or s["address"] != own]
sessions.sort(key=lambda s: s["started"], reverse=True)
return sessions[:SESSIONS_READ]
def parse_login_sessions(raw: dict[str, Any]) -> LoginSessionsDict:
"""What :data:`_PS_LOGIN_SESSIONS` emitted, as napalm-device-types' reading.
:raises LoginSessionsUnavailable: when the host does not audit logons.
"""
logons = _audited_logons(raw)
own = _own_address(raw)
return {
"own_address": own,
"sessions": sessions[:SESSIONS_READ],
"sessions": _sessions(logons, raw, own),
"records_since": _iso(raw.get("oldest")),
}