Publish EventLens v1.1.0 (#1)

This commit was merged in pull request #1.
This commit is contained in:
Jens committed 2026-11-20 12:20:18 +00:00
1 parent c0ade21029
commit 87432f3283
63 files changed
+7381 -1420

No files matched your search

+90 -16
View File
@@ -3,9 +3,12 @@ import json
from datetime import UTC, datetime
from typing import Any
from .models import Category, NormalizedEvent, TimestampSource
from .models import AuditOutcome, Category, NormalizedEvent, TimestampSource
AUTH_EVENT_IDS = {4624, 4625, 4634, 4647, 4648, 4672, 4768, 4769, 4771, 4776}
AUTH_EVENT_IDS = {4624, 4625, 4634, 4647, 4648, 4672, 4740, 4768, 4769, 4771, 4776}
ACCOUNT_CHANGE_EVENT_IDS = {4720, 4728, 4732, 4756}
AUDIT_FAILURE_KEYWORD = 0x0010_0000_0000_0000
AUDIT_SUCCESS_KEYWORD = 0x0020_0000_0000_0000
LEVELS = {0: "information", 1: "critical", 2: "error", 3: "warning", 4: "information", 5: "verbose"}
MAX_EVENT_ID = 4_294_967_295
MAX_RECORD_ID = 9_223_372_036_854_775_807
@@ -86,6 +89,21 @@ def _provider(system: dict[str, Any]) -> str:
return _bounded_text(value, MAX_PROVIDER_CHARS, "Unknown provider")
def _outcome(system: dict[str, Any]) -> AuditOutcome | None:
raw = _scalar(system.get("Keywords"))
if raw is None or isinstance(raw, bool):
return None
try:
value = int(raw, 0) if isinstance(raw, str) else int(raw)
except (TypeError, ValueError, OverflowError):
return None
if value & AUDIT_FAILURE_KEYWORD:
return "failure"
if value & AUDIT_SUCCESS_KEYWORD:
return "success"
return None
def _store_field(output: dict[str, str], key: str, value: str, limit: int) -> None:
bounded_key = _bounded_text(key or "value", MAX_FIELD_KEY_CHARS)
if bounded_key not in output:
@@ -127,47 +145,78 @@ def _flatten(
if len(output) >= limit:
return
return
_store_field(output, prefix or "value", _render_scalar(value)[:MAX_FIELD_VALUE_CHARS], limit)
def _render_scalar(value: Any) -> str:
"""Render a scalar exactly like ``json.dumps`` would, with fast paths for common types."""
if isinstance(value, str):
return value
if value is None:
return "null"
if value is True:
return "true"
if value is False:
return "false"
if type(value) is int:
return str(value)
try:
rendered = json.dumps(value, ensure_ascii=False) if not isinstance(value, str) else value
return json.dumps(value, ensure_ascii=False)
except (TypeError, ValueError):
rendered = "[unrenderable value]"
_store_field(output, prefix or "value", rendered[:MAX_FIELD_VALUE_CHARS], limit)
return "[unrenderable value]"
def _fold(value: str) -> str:
return value.strip().casefold()
EVENTLOG_PROVIDERS = frozenset({"eventlog", "microsoft-windows-eventlog"})
def _channel_matches(channel: str, *accepted: str) -> bool:
return channel.strip().casefold() in {value.casefold() for value in accepted}
folded = _fold(channel)
return any(folded == value.casefold() for value in accepted)
def is_security_audit(provider: str, channel: str) -> bool:
return provider.strip().casefold() == "microsoft-windows-security-auditing" and _channel_matches(channel, "Security")
return _fold(provider) == "microsoft-windows-security-auditing" and _fold(channel) == "security"
def is_service_control_manager(provider: str, channel: str) -> bool:
return provider.strip().casefold() == "service control manager" and _channel_matches(channel, "System")
return _fold(provider) == "service control manager" and _fold(channel) == "system"
def is_dns_client(provider: str, channel: str) -> bool:
return provider.strip().casefold() == "microsoft-windows-dns-client" and _channel_matches(channel, "System")
return _fold(provider) == "microsoft-windows-dns-client" and _fold(channel) == "system"
def is_application_error(provider: str, channel: str, event_id: int | None) -> bool:
return provider.casefold().strip() == "application error" and event_id == 1000 and _channel_matches(channel, "Application")
return event_id == 1000 and _fold(provider) == "application error" and _fold(channel) == "application"
def is_windows_error_reporting(provider: str, channel: str, event_id: int | None) -> bool:
return provider.casefold().strip() == "windows error reporting" and event_id == 1001 and _channel_matches(channel, "Application")
return event_id == 1001 and _fold(provider) == "windows error reporting" and _fold(channel) == "application"
def is_event_log(provider: str, channel: str) -> bool:
return provider.casefold().strip() in {"eventlog", "microsoft-windows-eventlog"} and _channel_matches(channel, "System")
return _fold(provider) in EVENTLOG_PROVIDERS and _fold(channel) == "system"
def is_log_cleared(provider: str, channel: str, event_id: int | None) -> bool:
"""Security 1102 (audit log cleared) or System 104 (an event log was cleared)."""
if event_id not in {1102, 104} or _fold(provider) not in EVENTLOG_PROVIDERS:
return False
return (event_id == 1102 and _channel_matches(channel, "Security")) or (
event_id == 104 and _channel_matches(channel, "System")
)
def is_kernel_general(provider: str, channel: str) -> bool:
return provider.strip().casefold() == "microsoft-windows-kernel-general" and _channel_matches(channel, "System")
return _fold(provider) == "microsoft-windows-kernel-general" and _fold(channel) == "system"
def is_kernel_power(provider: str, channel: str) -> bool:
return provider.strip().casefold() == "microsoft-windows-kernel-power" and _channel_matches(channel, "System")
return _fold(provider) == "microsoft-windows-kernel-power" and _fold(channel) == "system"
def classify(provider: str, event_id: int | None, channel: str) -> Category:
@@ -181,6 +230,10 @@ def classify(provider: str, event_id: int | None, channel: str) -> Category:
return "network"
if is_security_audit(provider, channel) and event_id in AUTH_EVENT_IDS:
return "authentication"
if is_log_cleared(provider, channel, event_id) or (
is_security_audit(provider, channel) and event_id in ACCOUNT_CHANGE_EVENT_IDS
):
return "security"
if is_application_error(provider, channel, event_id) or is_windows_error_reporting(provider, channel, event_id):
return "crash"
if (
@@ -195,6 +248,23 @@ def classify(provider: str, event_id: int | None, channel: str) -> Category:
def summarize(provider: str, event_id: int | None, category: Category, fields: dict[str, str]) -> str:
if category == "service" and event_id == 7045:
service = fields.get("EventData.ServiceName") or fields.get("service")
return f"Service installed: {service}" if service else "Service installed"
if category == "security" and event_id == 1102:
return "Security audit log cleared"
if category == "security" and event_id == 104:
cleared = fields.get("UserData.LogFileCleared.Channel")
return f"Event log cleared: {cleared}" if cleared else "Event log cleared"
if category == "security" and event_id == 4720:
account = fields.get("EventData.TargetUserName")
return f"User account created: {account}" if account else "User account created"
if category == "security" and event_id in {4728, 4732, 4756}:
group = fields.get("EventData.TargetUserName")
return f"Member added to security group {group}" if group else "Member added to a security group"
if category == "authentication" and event_id == 4740:
account = fields.get("EventData.TargetUserName")
return f"Account locked out: {account}" if account else "Account locked out"
if category == "service" and event_id in {7023, 7024, 7031, 7034}:
service = fields.get("EventData.param1") or fields.get("UserData.param1") or fields.get("service")
return f"{service} terminated unexpectedly" if service else "Service failure recorded"
@@ -245,11 +315,14 @@ def normalize_record(
stable_id = f"event-{hashlib.sha256(seed).hexdigest()[:16]}"
timestamp, timestamp_source = _timestamp(system, record_timestamp)
summary = summarize(provider, event_id, category, fields)[:MAX_SUMMARY_CHARS]
return NormalizedEvent(
# Every value above is already bounded and typed exactly as NormalizedEvent
# requires (see tests/test_performance.py, which re-validates the output),
# so skip per-event pydantic validation on this hot path.
return NormalizedEvent.model_construct(
id=stable_id,
timestamp=timestamp,
timestamp_source=timestamp_source,
timestamp_valid=timestamp is not None,
timestamp_valid=timestamp is not None and timestamp_source in {"event", "record"},
provider=provider,
event_id=event_id,
level=level[:32],
@@ -257,5 +330,6 @@ def normalize_record(
computer=computer,
category=category,
summary=summary,
outcome=_outcome(system),
fields=fields,
)