WindowsLogSourcesMixin.get_log_sources lists every event channel that is switched on and holds records (Get-WinEvent -ListLog *) as a source of kind eventlog, named by its channel, with its .evtx file, size and last write. Analytic and debug channels are left out. README and CHANGELOG describe the services, host events and log sources readings.
104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
"""The event channels as the host's log sources (NetOrk/netork#2897).
|
|
|
|
A Windows host keeps its logs in event channels. ``Get-WinEvent -ListLog *`` names
|
|
every one with its ``.evtx`` file, size, last write and record count; a channel that
|
|
is switched on and holds records is a log the host writes. Analytic and debug
|
|
channels are trace sessions, not logs a reader can follow, and are left out. The
|
|
answer is the shape of tests/fixtures/synthetic/event_channels.json.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from napalm_windows import WindowsDriver
|
|
from napalm_windows import event_channels as mod
|
|
|
|
FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "event_channels.json"
|
|
|
|
|
|
class FakeTransport:
|
|
def __init__(self, answer: str) -> None:
|
|
self.answer = answer
|
|
self.sent: list[str] = []
|
|
self.is_open = True
|
|
|
|
def run(self, script: str) -> str:
|
|
self.sent.append(script)
|
|
return self.answer
|
|
|
|
|
|
def _reading(answer: str | None = None) -> dict:
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
d._transport = FakeTransport(FIXTURE.read_text() if answer is None else answer)
|
|
return d.get_log_sources()
|
|
|
|
|
|
class TestTheReading:
|
|
def test_read_as_an_administrator_without_a_journal(self):
|
|
reading = _reading()
|
|
assert (reading["attributed"], reading["journald"], reading["forwarding"]) == (
|
|
True,
|
|
False,
|
|
None,
|
|
)
|
|
|
|
def test_every_channel_with_records_sorted_by_name(self):
|
|
assert [s["channel"] for s in _reading()["sources"]] == [
|
|
"Application",
|
|
"Microsoft-Windows-Kernel-PnP/Device Management",
|
|
"Microsoft-Windows-PowerShell/Operational",
|
|
"Microsoft-Windows-Sysmon/Operational",
|
|
"Security",
|
|
"System",
|
|
]
|
|
|
|
def test_a_channel_is_an_eventlog_source(self):
|
|
sysmon = next(s for s in _reading()["sources"] if "Sysmon" in s["channel"])
|
|
assert sysmon == {
|
|
"kind": "eventlog",
|
|
"path": r"C:\Windows\System32\Winevt\Logs\Microsoft-Windows-Sysmon%4Operational.evtx",
|
|
"unit": None,
|
|
"container_id": None,
|
|
"process": None,
|
|
"size": 67113984,
|
|
"modified": "2025-11-30T10:53:19+00:00",
|
|
"channel": "Microsoft-Windows-Sysmon/Operational",
|
|
}
|
|
|
|
def test_what_the_host_did_not_say_is_none(self):
|
|
sources = {s["channel"]: s for s in _reading()["sources"]}
|
|
assert sources["Microsoft-Windows-PowerShell/Operational"]["modified"] is None
|
|
assert sources["Microsoft-Windows-Kernel-PnP/Device Management"]["path"] is None
|
|
|
|
@pytest.mark.parametrize("left_out", ["Debug", "Analytic", "Empty"])
|
|
def test_trace_sessions_and_empty_channels_are_left_out(self, left_out):
|
|
assert not any(left_out in s["channel"] for s in _reading()["sources"])
|
|
|
|
def test_a_single_channel_is_still_a_list(self):
|
|
one = json.loads(FIXTURE.read_text())[2]
|
|
assert [s["channel"] for s in _reading(json.dumps(one))["sources"]] == ["System"]
|
|
|
|
def test_no_channel_is_no_sources(self):
|
|
assert _reading("")["sources"] == []
|
|
|
|
|
|
class TestTheScript:
|
|
def test_lists_every_channel_once(self):
|
|
assert "Get-WinEvent -ListLog *" in mod._PS_EVENT_CHANNELS
|
|
|
|
def test_a_channel_it_may_not_open_costs_only_that_channel(self):
|
|
assert "-ErrorAction SilentlyContinue" in mod._PS_EVENT_CHANNELS
|
|
|
|
def test_only_channels_switched_on_and_holding_records(self):
|
|
assert "$_.IsEnabled -and $_.RecordCount" in mod._PS_EVENT_CHANNELS
|
|
|
|
def test_the_file_is_named_as_it_is_on_disk(self):
|
|
assert "ExpandEnvironmentVariables" in mod._PS_EVENT_CHANNELS
|
|
|
|
def test_a_list_goes_through_input_object(self):
|
|
assert "-InputObject @(" in mod._PS_EVENT_CHANNELS
|