enodia-sentinal/enodia_sentinel/detectors/persistence.py
Luna 586f74b929 Relicense under GPL-3.0-or-later
Replace MIT with the full GNU GPLv3 text, update license metadata in
pyproject.toml (+ trove classifiers) and PKGBUILD, and add
SPDX-License-Identifier headers to all Python modules and shell scripts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 06:52:34 -07:00

43 lines
1.4 KiB
Python

# SPDX-License-Identifier: GPL-3.0-or-later
"""persistence — modification of a watched persistence-relevant file.
Persistence has to land somewhere that survives a reboot: cron, systemd units,
``authorized_keys``, shell rc files. We watch a configurable set and alert on
anything modified since the previous scan (``state.persist_since``).
"""
from __future__ import annotations
import os
from collections.abc import Iterator
from ..alert import Alert, Severity
from ..config import Config
from ..system import SystemState
def _iter_files(roots) -> Iterator[str]:
for root in roots:
if os.path.isdir(root):
for dirpath, _dirs, files in os.walk(root, onerror=lambda e: None):
for f in files:
yield os.path.join(dirpath, f)
elif os.path.lexists(root):
yield root
def detect(state: SystemState, cfg: Config) -> Iterator[Alert]:
since = state.persist_since
if since is None:
return
for path in _iter_files(cfg.watch_persistence):
try:
mtime = os.lstat(path).st_mtime
except OSError:
continue
if mtime > since:
yield Alert(
severity=Severity.HIGH,
signature="persistence",
key=f"persist:{path}",
detail=f"persistence file modified: {path}",
)