#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""procmon - Linux 进程异常监控 + 服务器登录检测（白名单基线 + 预警）

原理: 首次采集当前进程为基线白名单, 之后每轮巡检, 凡是不在白名单、
或特征异常的进程即输出告警。同一进程(按 exe/脚本标识)只提醒一次,
超过 alert_repeat_hours 再次出现才重复提醒, 避免刷屏。
登录检测基于 /var/log/auth.log(或 secure) 增量解析 + 账号基线比对。

部署(root):
    sudo mkdir -p /opt/procmon && sudo cp procmon.py /opt/procmon/
    sudo python3 /opt/procmon/procmon.py init          # 采集基线
    sudo cp procmon.service /etc/systemd/system/
    sudo systemctl daemon-reload
    sudo systemctl enable --now procmon
    journalctl -u procmon -f

常用:
    procmon.py check               # 单次巡检, 有高危/注意项时退出码 1
    procmon.py check --json        # 机器可读输出
    procmon.py learn               # 查看所有未登记进程(只读)
    procmon.py learn --commit      # 确认无害后补录进程白名单
    procmon.py accounts            # 查看系统账号与基线的差异(只读)
    procmon.py accounts --commit   # 确认无误后重建账号基线
    procmon.py refresh-hash        # 系统升级后刷新 exe 哈希
    procmon.py show                # 查看白名单

路径:
    配置 /etc/procmon/config.json
    数据 /var/lib/procmon/{whitelist,state,hashcache}.json
    告警 /var/log/procmon/alerts.jsonl
"""

import argparse
import fnmatch
import hashlib
import ipaddress
import json
import os
import pwd
import queue
import re
import socket
import struct
import sys
import threading
import time
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple

try:
    HZ = int(os.sysconf("SC_CLK_TCK"))
except (ValueError, OSError):
    HZ = 100

DEF_HOME = "/var/lib/procmon"
DEF_CONFIG = "/etc/procmon/config.json"
DEF_LOG = "/var/log/procmon/alerts.jsonl"
DEF_API_URL = "http://api.linksports.com.cn:8180/api/v1/earlyWarning/send"

MAX_HASH_BYTES = 64 * 1024 * 1024

SEV_RANK = {"high": 0, "medium": 1, "info": 2}
SEV_CN = {"high": "高危", "medium": "注意", "info": "提示"}
SEV_COLOR = {"high": "\033[1;31m", "medium": "\033[1;33m", "info": "\033[1;36m"}
RESET = "\033[0m"

HIGH_PREFIX = ("/tmp/", "/var/tmp/", "/dev/shm/", "/run/shm/", "/run/user/")
MED_PREFIX = ("/home/", "/root/", "/var/www/", "/usr/local/nginx/html/",
              "/srv/", "/var/spool/", "/opt/")

# 进程名 -> 合法的 exe 前缀, 用于识别"改名的木马"
FAKE_MAP = {
    "sshd": ("/usr/sbin/sshd",),
    "cron": ("/usr/sbin/cron",),
    "crond": ("/usr/sbin/crond",),
    "nginx": ("/usr/", "/opt/"),
    "mysqld": ("/usr/sbin/mysqld", "/usr/local/mysql", "/usr/libexec/mysqld"),
    "dbus-daemon": ("/usr/bin/dbus-daemon",),
    "systemd": ("/lib/systemd/", "/usr/lib/systemd/", "/sbin/init", "/usr/sbin/init"),
    "ss": ("/usr/sbin/ss", "/usr/bin/ss", "/bin/ss"),
    "netstat": ("/usr/bin/netstat", "/bin/netstat"),
    "ps": ("/usr/bin/ps", "/bin/ps"),
}

INTERP_NAMES = {"python", "perl", "ruby", "node", "nodejs", "java", "php",
                "bash", "sh", "dash", "zsh", "lua", "awk", "pwsh"}

DEFAULT_CONFIG = {
    "interval": 30,
    "host_name": "",                      # 告警中的主机名, 留空取 socket.gethostname()
    "monitor_ports": True,
    "whitelist_hash": True,
    # 订阅内核 cn_proc 事件, 捕获轮询采不到的短命进程(需 root)
    "monitor_short_proc": True,
    # 短命进程告警下限: all(默认全部) / high(只报高危, 用于压制噪声)
    "short_proc_level": "all",
    "cpu_threshold": 90.0,
    "rss_threshold_mb": 4096,
    "keep_days": 7,
    "alert_repeat_hours": 24,
    # 服务器登录检测: auth.log/secure 增量解析 + /etc/passwd 账号基线
    "monitor_login": True,
    "login_success_alert": True,          # 普通账号每次登录成功 -> 注意
    "auth_log": "",                       # 留空自动探测 auth.log / secure
    "login_whitelist": [],                # 受信登录来源 IP/CIDR
    "failed_login_threshold": 5,          # 连续失败(中途无成功登录)≥ 该值 -> 高危
    "failed_login_window_hours": 72,      # 连续失败计数窗口(小时), 超时或成功登录清零
    # 低等级来源 IP 聚合(撞库/账号枚举): 窗口内失败多且涉及多个账号
    "ip_alert_threshold": 5,              # 同一来源 IP 失败次数阈值
    "ip_alert_window_hours": 72,          # 来源 IP 聚合窗口(小时)
    "ip_alert_level": "info",             # off/info(提示)/medium(注意)
    "monitor_priv_esc": True,             # sudo/su 提权到 root -> 注意
    "info_digest_enable": True,           # 提示级不实时推送, 每日汇总
    "info_digest_hour": 9,                # 每日汇总时刻(0-23)
    # 提权命令含以下子串时不告警(自身运维, 如 sudo python3 .../procmon.py refresh-hash)
    "priv_esc_ignore_cmd_substr": ["procmon.py"],
    # 预警接口(预警信息发送接口) —— api_code 为空则不推送
    "api_url": DEF_API_URL,
    "api_app_id": 10101,
    "api_code": "ProcAnomaly_papa",
    "api_timeout": 10,
    "api_batch_size": 20,
    "api_field_map": {},
    # 登录事件单独通道(留空则复用 api_code / api_field_map)
    "login_api_code": "LoginAnomaly_papa",
    "login_api_field_map": {},
    # 推送结果日志(JSONL, 每批一条); 留空则不写
    "notify_log": "/var/log/procmon/notify.log",
    # 通用 webhook(可选, 与预警接口并行)
    "webhook": "",
    "webhook_type": "dingtalk",
    "ignore_cmd_substr": ["procmon.py"],
    "ignore_comm": [
        "ps", "top", "htop", "pgrep", "pstree", "grep", "egrep", "fgrep",
        "awk", "sed", "cut", "sort", "uniq", "wc", "tr", "head", "tail",
        "cat", "ls", "find", "xargs", "less", "more", "vi", "vim", "nano",
        "systemctl", "journalctl", "dmesg", "who", "w", "last", "uptime",
        "df", "du", "free", "lsblk", "lsof", "netstat", "ss", "ping",
        "dig", "nslookup", "curl", "wget", "scp", "rsync", "tar", "gzip",
        "unzip", "git", "which", "env", "uname", "date", "sleep",
    ],
}

_BOOT_TIME = None
_USER_CACHE: Dict[int, str] = {}


# --------------------------------------------------------------------------- #
# 基础工具
# --------------------------------------------------------------------------- #
def now_iso() -> str:
    return time.strftime("%Y-%m-%d %H:%M:%S")


def host_name(cfg: Optional[dict] = None) -> str:
    """告警里的主机名: 优先配置 host_name, 否则取系统 hostname。"""
    if cfg:
        name = str(cfg.get("host_name") or "").strip()
        if name:
            return name
    return socket.gethostname()


def boot_time() -> int:
    global _BOOT_TIME
    if _BOOT_TIME is None:
        _BOOT_TIME = int(time.time())
        try:
            with open("/proc/stat") as f:
                for line in f:
                    if line.startswith("btime"):
                        _BOOT_TIME = int(line.split()[1])
                        break
        except OSError:
            pass
    return _BOOT_TIME


def load_json(path: str, default=None):
    try:
        with open(path, "r", encoding="utf-8") as f:
            return json.load(f)
    except (OSError, ValueError):
        return default


def save_json(path: str, obj) -> None:
    d = os.path.dirname(path)
    if d:
        os.makedirs(d, exist_ok=True)
    tmp = path + ".tmp"
    with open(tmp, "w", encoding="utf-8") as f:
        json.dump(obj, f, ensure_ascii=False, indent=2, sort_keys=True)
    os.replace(tmp, path)


def user_name(uid: int) -> str:
    if uid in _USER_CACHE:
        return _USER_CACHE[uid]
    try:
        name = pwd.getpwuid(uid).pw_name
    except KeyError:
        name = str(uid)
    _USER_CACHE[uid] = name
    return name


def file_sha256(path: str, cache: Dict[str, dict]) -> Optional[str]:
    try:
        st = os.stat(path)
    except OSError:
        return None
    if st.st_size > MAX_HASH_BYTES:
        return None
    rec = cache.get(path)
    if rec and rec.get("mtime") == st.st_mtime and rec.get("size") == st.st_size:
        return rec.get("sha256")
    h = hashlib.sha256()
    try:
        with open(path, "rb") as f:
            for chunk in iter(lambda: f.read(1 << 20), b""):
                h.update(chunk)
    except OSError:
        return None
    digest = h.hexdigest()
    cache[path] = {"mtime": st.st_mtime, "size": st.st_size, "sha256": digest}
    return digest


# --------------------------------------------------------------------------- #
# 进程采集
# --------------------------------------------------------------------------- #
@dataclass
class Proc:
    pid: int
    ppid: int
    comm: str
    state: str
    uid: int
    user: str
    argv: List[str]
    exe: Optional[str]
    cwd: Optional[str]
    rss_kb: int
    threads: int
    cpu_time: float
    starttime: int
    deleted: bool = False
    exe_ino: int = 0                    # 运行中 exe 的 inode, 用于 deleted 去重

    @property
    def cmdline(self) -> str:
        return " ".join(self.argv) if self.argv else "[" + self.comm + "]"

    @property
    def started(self) -> str:
        return time.strftime("%Y-%m-%d %H:%M:%S",
                             time.localtime(boot_time() + self.starttime / float(HZ)))


def read_proc(pid: int) -> Optional[Proc]:
    base = "/proc/%d" % pid
    try:
        with open(base + "/stat", "rb") as f:
            raw = f.read().decode("utf-8", "replace")
    except OSError:
        return None

    lp = raw.find("(")
    rp = raw.rfind(")")
    if lp < 0 or rp < 0 or rp + 2 > len(raw):
        return None
    comm = raw[lp + 1:rp]
    rest = raw[rp + 2:].split()
    if len(rest) < 20:
        return None
    try:
        state = rest[0]
        ppid = int(rest[1])
        cpu_time = (int(rest[11]) + int(rest[12])) / float(HZ)
        starttime = int(rest[19])
    except (ValueError, IndexError):
        return None

    argv: List[str] = []
    try:
        with open(base + "/cmdline", "rb") as f:
            raw_cmd = f.read()
        if raw_cmd:
            argv = [a.decode("utf-8", "replace") for a in raw_cmd.split(b"\0") if a]
    except OSError:
        pass

    exe = None
    deleted = False
    exe_ino = 0
    try:
        exe = os.readlink(base + "/exe")
        if exe.endswith(" (deleted)"):
            deleted = True
            exe = exe[:-10]
    except OSError:
        exe = None
    if deleted:
        # 进程仍在映射已被删除的 inode, 记录它用于按 inode 去重
        try:
            exe_ino = int(os.stat(base + "/exe").st_ino)
        except OSError:
            exe_ino = 0

    cwd = None
    try:
        cwd = os.readlink(base + "/cwd")
    except OSError:
        pass

    uid, rss_kb, threads = -1, 0, 0
    try:
        with open(base + "/status", "r", encoding="utf-8", errors="replace") as f:
            for line in f:
                if line.startswith("Uid:"):
                    uid = int(line.split()[1])
                elif line.startswith("VmRSS:"):
                    rss_kb = int(line.split()[1])
                elif line.startswith("Threads:"):
                    threads = int(line.split()[1])
    except (OSError, ValueError):
        pass

    return Proc(pid=pid, ppid=ppid, comm=comm, state=state, uid=uid,
                user=user_name(uid) if uid >= 0 else "?", argv=argv, exe=exe,
                cwd=cwd, rss_kb=rss_kb, threads=threads,
                cpu_time=cpu_time, starttime=starttime, deleted=deleted,
                exe_ino=exe_ino)


def collect_procs() -> Dict[int, Proc]:
    out = {}
    try:
        names = os.listdir("/proc")
    except OSError:
        return out
    for name in names:
        if not name.isdigit():
            continue
        p = read_proc(int(name))
        if p is not None:
            out[p.pid] = p
    return out


def take_snapshot() -> Tuple[Dict[int, Proc], float]:
    return collect_procs(), time.monotonic()


def cpu_percent(prev: Dict[int, Proc], cur: Dict[int, Proc],
                elapsed: float) -> Dict[int, float]:
    out = {}
    for pid, p in cur.items():
        q = prev.get(pid)
        if q is not None and q.starttime == p.starttime:
            delta = p.cpu_time - q.cpu_time
            if delta >= 0:
                out[pid] = delta / elapsed * 100.0
    return out


# --------------------------------------------------------------------------- #
# 白名单 key
# --------------------------------------------------------------------------- #
def is_interpreter(exe: Optional[str]) -> bool:
    if not exe:
        return False
    b = os.path.basename(exe).lower()
    b = re.sub(r"[\d.]+$", "", b)
    return b in INTERP_NAMES or b.startswith("python")


def norm_path(path: str, cwd: Optional[str]) -> str:
    if not os.path.isabs(path):
        path = os.path.join(cwd or "/", path)
    return os.path.normpath(path)


def proc_key(p: Proc) -> str:
    """白名单主键: 优先 exe 路径; 解释器类附加脚本/命令标识, 防止用白名单
    解释器执行任意载荷时被放行。"""
    exe = p.exe
    if exe:
        if not is_interpreter(exe):
            return "exe:" + exe
        for i, a in enumerate(p.argv):
            if a in ("-c", "-e", "-r", "--eval", "-m") and i + 1 < len(p.argv):
                h = hashlib.sha1(p.argv[i + 1].encode("utf-8", "replace")).hexdigest()[:12]
                return "exe:%s|cmd:%s" % (exe, h)
        for a in p.argv[1:]:
            if a.startswith("-"):
                continue
            if "/" in a or "." in a:
                return "exe:%s|arg:%s" % (exe, norm_path(a, p.cwd))
        return "exe:" + exe
    if p.argv:
        a0 = p.argv[0]
        if "/" in a0:
            return "path:" + norm_path(a0, p.cwd)
        return "name:" + p.comm
    return "kernel:" + p.comm


GLOB_CHARS = ("*", "?", "[")


def is_glob_key(key: str) -> bool:
    return any(c in key for c in GLOB_CHARS)


def lookup_entry(entries: Dict[str, dict], key: str) -> Optional[Tuple[str, dict]]:
    """按主键查白名单: 精确匹配优先, 未命中再按 glob 模式兜底。

    返回 (命中的白名单键, 条目); 未命中返回 None。glob 条目用于收敛
    "同一批路径反复出现"的短命脚本(如 /data/scripts/backup_*.sh),
    避免逐条 learn。命中 glob 时不做 sha256 校验(不同脚本内容本就不同)。
    """
    e = entries.get(key)
    if e is not None:
        return key, e
    for pat, item in entries.items():
        if is_glob_key(pat) and fnmatch.fnmatchcase(key, pat):
            return pat, item
    return None


# --------------------------------------------------------------------------- #
# 监听端口
# --------------------------------------------------------------------------- #
def hex_ip(h: str) -> str:
    try:
        if len(h) == 8:
            return ".".join(str(int(h[i:i + 2], 16)) for i in (6, 4, 2, 0))
        b = bytes.fromhex(h)
        b = b"".join(b[i:i + 4][::-1] for i in range(0, 16, 4))
        return socket.inet_ntop(socket.AF_INET6, b)
    except (ValueError, OSError):
        return h


def read_listen_ports() -> Dict[str, dict]:
    """inode -> {port, addr}"""
    ports: Dict[str, dict] = {}
    for fn in ("/proc/net/tcp", "/proc/net/tcp6"):
        try:
            with open(fn, "r", encoding="utf-8", errors="replace") as f:
                lines = f.read().splitlines()[1:]
        except OSError:
            continue
        for line in lines:
            parts = line.split()
            if len(parts) < 10 or parts[3] != "0A":
                continue
            try:
                addr_hex, port_hex = parts[1].split(":")
                ports[parts[9]] = {"port": int(port_hex, 16), "addr": hex_ip(addr_hex)}
            except (ValueError, IndexError):
                continue
    return ports


def map_pid_ports(procs: Dict[int, Proc], ports: Dict[str, dict]) -> Dict[int, List[dict]]:
    out: Dict[int, List[dict]] = {}
    if not ports:
        return out
    for pid in procs:
        fd_dir = "/proc/%d/fd" % pid
        try:
            fds = os.listdir(fd_dir)
        except OSError:
            continue
        for fd in fds:
            try:
                link = os.readlink(os.path.join(fd_dir, fd))
            except OSError:
                continue
            if link.startswith("socket:["):
                info = ports.get(link[8:-1])
                if info:
                    out.setdefault(pid, []).append(info)
    return out


def is_loopback(ip: str) -> bool:
    return (ip.startswith("127.") or ip in ("::1", "0:0:0:0:0:0:0:1")
            or ip.startswith("::ffff:127."))


# --------------------------------------------------------------------------- #
# 短命进程: 内核 cn_proc 事件(轮询采不到毫秒级的进程)
# --------------------------------------------------------------------------- #
NETLINK_CONNECTOR = 11
CN_IDX_PROC = 1
CN_VAL_PROC = 1
PROC_CN_MCAST_LISTEN = 1
PROC_CN_MCAST_IGNORE = 2
PROC_EVENT_EXEC = 2
NLMSG_DONE = 3
CN_MSG_LEN = 20          # cn_msg: idx/val/seq/ack/len/flags


def cn_proc_msg(op: int) -> bytes:
    """订阅/退订报文: nlmsghdr(16) + cn_msg(20) + op(4)。"""
    payload = struct.pack("=IIIIHH", CN_IDX_PROC, CN_VAL_PROC, 0, 0, 4, 0)
    payload += struct.pack("=I", op)
    hdr = struct.pack("=IHHII", 16 + len(payload), NLMSG_DONE, 0, 0, 0)
    return hdr + payload


def parse_exec_pids(buf: bytes) -> List[int]:
    """从 netlink 数据报里取出 exec 事件的 tgid。

    nlmsghdr: len/type/flags/seq/pid; cn_msg 20 字节; 之后是 proc_event:
    what(4)+cpu(4)+timestamp_ns(8), 联合体从偏移 16 开始, exec 分支前两个
    字段是 process_pid / process_tgid。
    """
    out: List[int] = []
    off = 0
    while off + 16 <= len(buf):
        nlen, _ntype, _flags, _seq, _pid = struct.unpack_from("=IHHII", buf, off)
        if nlen < 16 + CN_MSG_LEN or off + nlen > len(buf):
            break
        ev = off + 16 + CN_MSG_LEN
        if ev + 32 <= off + nlen:
            what = struct.unpack_from("=I", buf, ev)[0]
            if what == PROC_EVENT_EXEC:
                _ppid, tgid = struct.unpack_from("=II", buf, ev + 16)
                if tgid > 0:
                    out.append(tgid)
        off += (nlen + 3) & ~3         # netlink 消息按 4 字节对齐
    return out


def cn_proc_worker(events: "queue.Queue", stop: threading.Event,
                   warn=None) -> None:
    """常驻线程: 订阅 cn_proc, 每次 exec 立刻快照该进程投入队列。

    轮询只能覆盖"截面存活"的进程, fork/exec 后立刻退出的进程必然漏采,
    只有事件源才看得到。快照在事件到达时立即做, 尽量赶在进程退出前读到
    cmdline/exe; 读不到的交给下一轮轮询兜底。
    """
    if not hasattr(socket, "AF_NETLINK"):
        if warn:
            warn("当前平台无 netlink, 短命进程监听已跳过")
        return
    while not stop.is_set():
        sock = None
        try:
            sock = socket.socket(socket.AF_NETLINK, socket.SOCK_DGRAM,
                                 NETLINK_CONNECTOR)
            sock.bind((os.getpid(), CN_IDX_PROC))
            sock.send(cn_proc_msg(PROC_CN_MCAST_LISTEN))
            sock.settimeout(1.0)
            while not stop.is_set():
                try:
                    data = sock.recv(8192)
                except socket.timeout:
                    continue
                if events.qsize() >= 500:
                    # 队列积压(高频 fork)时不追加快照, 宁可漏采也不要拖慢被监控机
                    continue
                for pid in parse_exec_pids(data):
                    p = read_proc(pid)
                    if p is None:
                        continue
                    try:
                        events.put_nowait(p)
                    except queue.Full:
                        try:            # 丢最旧的, 保证新事件进得来
                            events.get_nowait()
                            events.put_nowait(p)
                        except (queue.Empty, queue.Full):
                            pass
        except OSError as e:
            if warn:
                warn("cn_proc 订阅失败(%s), 5 秒后重试" % e)
            stop.wait(5)
        finally:
            if sock is not None:
                try:
                    sock.send(cn_proc_msg(PROC_CN_MCAST_IGNORE))
                except OSError:
                    pass
                sock.close()


# --------------------------------------------------------------------------- #
# 告警判定
# --------------------------------------------------------------------------- #
@dataclass
class Alert:
    sev: str = "info"
    pid: int = 0
    ppid: int = 0
    user: str = "-"
    comm: str = "-"
    key: str = ""
    exe: Optional[str] = None
    cmd: str = ""
    cwd: Optional[str] = None
    reasons: List[str] = field(default_factory=list)
    ports: List[int] = field(default_factory=list)
    started: str = ""
    kind: str = "proc"                  # proc / login
    extra: Dict[str, str] = field(default_factory=dict)


def make_alert(p: Proc, key: str, sev: str, reasons: List[str],
               ports: List[int]) -> Alert:
    return Alert(sev=sev, pid=p.pid, ppid=p.ppid, user=p.user, comm=p.comm,
                 key=key, exe=p.exe, cmd=p.cmdline[:300], cwd=p.cwd,
                 reasons=reasons, ports=ports, started=p.started)


def fmt_local(ts: float) -> str:
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))


def suspicious_maps(pid: int) -> Optional[bool]:
    """检查进程内存映射是否带内存马特征(memfd / 已删除的动态库)。

    True=发现可疑映射, False=干净, None=无权限读取(需 root 复核)。
    """
    try:
        with open("/proc/%d/maps" % pid, "r", encoding="utf-8", errors="replace") as f:
            lines = f.read().splitlines()
    except OSError:
        return None
    for line in lines:
        if "memfd:" in line:
            return True
        parts = line.split(maxsplit=5)
        if len(parts) < 6:
            continue
        path = parts[5]
        if "(deleted)" in path and ".so" in path:
            return True
    return False


def judge_deleted(p: Proc) -> Tuple[str, List[str]]:
    """判定"运行中的 exe 已被删除"的真实风险。

    最常见的误报: 解释器/二进制被包升级替换, 老进程仍持有旧 inode,
    /proc/<pid>/exe 显示 (deleted)。特征是"同路径文件存在, 且其 mtime
    晚于进程启动时间"。仍判高危的情形: 原路径在临时目录、同路径文件
    已不存在、内存映射里出现 memfd 或已删除的 .so(内存马)。
    """
    exe = p.exe
    if not exe:
        return "high", ["可执行文件已被删除(exe deleted), 且无法获取原路径"]
    if exe.startswith(HIGH_PREFIX):
        return "high", ["可执行文件已被删除(exe deleted)",
                        "原件位于临时/共享目录: " + exe,
                        "疑似挖矿/后门驻留"]
    try:
        st = os.stat(exe)
    except OSError:
        st = None
    if st is None:
        return "high", ["可执行文件已被删除(exe deleted)",
                        "同路径文件已不存在: " + exe,
                        "疑似挖矿/后门驻留, 建议立即取证"]
    maps = suspicious_maps(p.pid)
    if maps is None:
        return "high", ["可执行文件已被删除(exe deleted)",
                        "无法读取内存映射复核(需 root)"]
    if maps:
        return "high", ["可执行文件已被删除(exe deleted)",
                        "内存映射中存在 memfd 或已删除的动态库, 疑似内存马",
                        "建议立即取证"]

    # dpkg 会保留包内 mtime, 重装同版本时 mtime 可能早于进程启动, 所以同时看
    # ctime(文件在磁盘上被创建/替换的时间), 两者取较晚的与启动时间比较
    start_ts = boot_time() + p.starttime / float(HZ)
    file_ts = max(st.st_mtime, st.st_ctime)
    if file_ts > start_ts + 1:
        return "medium", ["运行中的可执行文件已被删除(exe deleted)",
                          "同路径文件于 %s 被替换, 晚于进程启动时间 %s"
                          % (fmt_local(file_ts), p.started),
                          "判定为程序升级替换, 进程仍运行旧 inode, 建议重启该进程"]
    return "high", ["可执行文件已被删除(exe deleted)",
                    "同路径文件存在但未晚于进程启动时间更新, 疑似挖矿/后门驻留"]


def judge_unregistered(p: Proc) -> Tuple[str, List[str]]:
    """不在白名单的判定: 目录特征 / exe 不可读 / 相对路径启动 / 进程名伪装。"""
    sev = "medium"
    reasons: List[str] = ["进程不在白名单"]
    exe = p.exe
    if exe:
        if exe.startswith(HIGH_PREFIX):
            sev = "high"
            reasons.append("可执行文件位于临时/共享目录: " + exe)
        elif exe.startswith(MED_PREFIX):
            reasons.append("可执行文件位于可写业务目录: " + exe)
    else:
        if os.geteuid() == 0:
            sev = "high"
            reasons.append("无法读取可执行文件(可能被隐藏或映射在内存)")
        else:
            reasons.append("无法读取可执行文件(需要 root 权限复核)")
    if exe is None and p.argv and "/" not in p.argv[0]:
        reasons.append("以相对路径启动: " + p.argv[0])
    target = FAKE_MAP.get(p.comm)
    if target and exe and not exe.startswith(target):
        sev = "high"
        reasons.append("进程名疑似伪装(%s 实际来自 %s)" % (p.comm, exe))
    return sev, reasons


def collect_short_alerts(cfg: dict, wl: dict, events: "queue.Queue",
                         max_items: int = 500) -> List[Alert]:
    """把 cn_proc 队列里"已退出"的短命进程转成告警。

    仍存活的进程跳过, 交由本轮轮询处理, 避免同一进程报两条。队列在
    事件到达时已快照过 cmdline/exe, 进程退出后照样能判定。
    """
    entries = wl.get("entries") or {}
    ignore_comm = set(cfg.get("ignore_comm") or [])
    ignore_sub = cfg.get("ignore_cmd_substr") or []
    level = str(cfg.get("short_proc_level") or "all").strip().lower()
    min_rank = SEV_RANK["high"] if level == "high" else SEV_RANK["info"]
    out: List[Alert] = []
    drained = 0
    while drained < max_items:
        try:
            p = events.get_nowait()
        except queue.Empty:
            break
        drained += 1
        if not p.argv and p.exe is None:      # 内核线程
            continue
        if p.comm in ignore_comm:             # 短命进程必然 < 60s
            continue
        if any(s and s in p.cmdline for s in ignore_sub):
            continue
        if p.state == "Z":
            continue
        cur = read_proc(p.pid)
        if cur is not None and cur.starttime == p.starttime:
            continue                          # 还活着, 轮询会处理
        key = proc_key(p)
        if lookup_entry(entries, key) is not None:
            continue
        sev, reasons = judge_unregistered(p)
        reasons.append("短命进程, 轮询未采集到(由内核 exec 事件捕获)")
        if p.deleted:
            dsev, dreasons = judge_deleted(p)
            if SEV_RANK.get(dsev, 9) < SEV_RANK.get(sev, 9):
                sev = dsev
            reasons.extend(dreasons)
        if SEV_RANK.get(sev, 9) > min_rank:
            continue
        out.append(make_alert(p, key, sev, reasons, []))
    return out


def gather_alerts(cfg: dict, wl: dict, hash_cache: Dict[str, dict],
                  prev_snapshot=None) -> Tuple[List[Alert], tuple]:
    cur, t_cur = take_snapshot()
    if prev_snapshot is None:
        time.sleep(1.0)
        prev, t_prev = cur, t_cur
        cur, t_cur = take_snapshot()
    else:
        prev, t_prev = prev_snapshot
    elapsed = max(0.001, t_cur - t_prev)
    cpu = cpu_percent(prev, cur, elapsed)

    entries = wl.get("entries") or {}
    ignore_comm = set(cfg.get("ignore_comm") or [])
    ignore_sub = cfg.get("ignore_cmd_substr") or []
    cpu_thr = float(cfg.get("cpu_threshold") or 90.0)
    rss_thr = float(cfg.get("rss_threshold_mb") or 4096)
    now = time.time()
    boot = boot_time()

    listen = read_listen_ports() if cfg.get("monitor_ports") else {}
    pid_ports = map_pid_ports(cur, listen) if listen else {}

    alerts: List[Alert] = []
    for p in cur.values():
        # 内核线程: 无 cmdline 且无 exe
        if not p.argv and p.exe is None:
            continue

        age = now - (boot + p.starttime / float(HZ))
        if p.comm in ignore_comm and age < 60:
            continue
        if any(s and s in p.cmdline for s in ignore_sub):
            continue

        key = proc_key(p)
        # deleted 场景按 inode 去重: 进程重启后加载新文件即不再命中, 老 inode 的
        # 告警记录不会牵连新进程
        akey = "%s|ino:%d" % (key, p.exe_ino) if (p.deleted and p.exe_ino) else key
        ext_ports = [x["port"] for x in pid_ports.get(p.pid, []) if not is_loopback(x["addr"])]
        ext_ports = sorted(set(ext_ports))

        if p.state == "Z":
            alerts.append(make_alert(p, key, "info", ["僵尸进程, 父进程未回收"], ext_ports))
            continue

        hit = lookup_entry(entries, key)
        reasons: List[str] = []
        sev: Optional[str] = None

        if hit is None:
            sev, reasons = judge_unregistered(p)
            if ext_ports:
                sev = "high"
                reasons.append("监听对外端口: " + ",".join(str(x) for x in ext_ports))
            if p.deleted:
                dsev, dreasons = judge_deleted(p)
                if SEV_RANK.get(dsev, 9) < SEV_RANK.get(sev, 9):
                    sev = dsev
                reasons.extend(dreasons)
        else:
            mkey, entry = hit
            if p.deleted:
                sev, reasons = judge_deleted(p)
            elif (mkey == key and cfg.get("whitelist_hash")
                  and entry.get("sha256") and p.exe):
                # glob 命中的条目不对内容做哈希校验: 不同脚本内容本就不同
                digest = file_sha256(p.exe, hash_cache)
                if digest and digest != entry["sha256"]:
                    sev = "high"
                    reasons.append("可执行文件内容与基线不一致, 疑似被替换")

        c = cpu.get(p.pid, 0.0)
        if c >= cpu_thr:
            reasons.append("CPU 占用 %.0f%%" % c)
            sev = sev or "info"
        if rss_thr and p.rss_kb / 1024.0 >= rss_thr:
            reasons.append("内存占用 %d MB" % (p.rss_kb // 1024))
            sev = sev or "info"

        if sev is None:
            continue
        alerts.append(make_alert(p, akey, sev, reasons, ext_ports))

    alerts.sort(key=lambda a: (SEV_RANK.get(a.sev, 9), a.pid))
    return alerts, (cur, t_cur)


def dedupe(alerts: List[Alert], state: dict, cfg: dict, now: float) -> List[Alert]:
    seen = state.setdefault("seen", {})
    repeat = float(cfg.get("alert_repeat_hours") or 24) * 3600.0
    out: List[Alert] = []
    for a in alerts:
        rec = seen.get(a.key)
        if rec is None:
            seen[a.key] = {"pid": a.pid, "user": a.user, "sev": a.sev,
                           "first": now, "last": now, "count": 1,
                           "notified": now, "reasons": a.reasons[:3]}
            out.append(a)
            continue
        rec["last"] = now
        rec["pid"] = a.pid
        rec["count"] = int(rec.get("count") or 1) + 1
        if now - float(rec.get("notified") or 0) >= repeat:
            rec["notified"] = now
            a.reasons.append("此前已告警, 累计出现 %d 次" % rec["count"])
            out.append(a)

    keep = float(cfg.get("keep_days") or 7) * 86400.0
    for k in [k for k, v in seen.items() if now - float(v.get("last") or 0) > keep]:
        del seen[k]
    return out


# --------------------------------------------------------------------------- #
# 服务器登录检测
# --------------------------------------------------------------------------- #
AUTH_LOG_CANDIDATES = ("/var/log/auth.log", "/var/log/secure")
PRIV_GROUPS = ("sudo", "wheel", "admin", "docker", "lxd")
DENY_SHELLS = ("nologin", "false", "sync", "halt", "shutdown")
MONTH_NUM = {m: i + 1 for i, m in enumerate(
    ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))}

SYSLOG_TS = re.compile(r"^([A-Z][a-z]{2})\s+(\d{1,2})\s+(\d{2}):(\d{2}):(\d{2})")
RE_SSH_OK = re.compile(
    r"sshd\[(\d+)\]:\s+Accepted\s+(\S+)\s+for\s+(\S+)\s+from\s+(\S+)\s+port\s+(\d+)")
RE_SSH_FAIL = re.compile(
    r"sshd\[(\d+)\]:\s+Failed\s+(\S+)\s+for\s+(invalid user\s+)?(\S+)\s+from\s+(\S+)\s+port\s+(\d+)")
RE_SSH_INVALID = re.compile(
    r"sshd\[(\d+)\]:\s+Invalid user\s+(\S+)\s+from\s+(\S+)")
RE_SUDO = re.compile(r"sudo:\s+(\S+)\s+:\s+.*USER=(\S+)\s*;\s*COMMAND=(.*)$")
RE_SU = re.compile(
    r"su(?:\[\d+\])?:\s+pam_unix\(su(?:-l)?:session\):\s+session opened for user\s+(\S+)\s+by\s+(\S+)\(")


def parse_syslog_ts(line: str, now: Optional[float] = None) -> float:
    """syslog 时间戳无年份, 按当前年份推断; 出现"未来时间"时回退一年(跨年)。"""
    now = time.time() if now is None else now
    m = SYSLOG_TS.match(line)
    if not m:
        return now
    try:
        mon = MONTH_NUM[m.group(1)]
        day, hh, mm, ss = (int(m.group(i)) for i in (2, 3, 4, 5))
    except (KeyError, ValueError):
        return now
    lt = time.localtime(now)
    for year in (lt.tm_year, lt.tm_year - 1):
        try:
            ts = time.mktime((year, mon, day, hh, mm, ss, 0, 0, -1))
        except (OverflowError, ValueError):
            return now
        if ts <= now + 86400:
            return ts
    return now


def read_accounts() -> Dict[str, dict]:
    out: Dict[str, dict] = {}
    try:
        with open("/etc/passwd", "r", encoding="utf-8", errors="replace") as f:
            lines = f.read().splitlines()
    except OSError:
        return out
    for line in lines:
        parts = line.split(":")
        if len(parts) < 7 or not parts[0]:
            continue
        try:
            uid, gid = int(parts[2]), int(parts[3])
        except ValueError:
            continue
        out[parts[0]] = {"uid": uid, "gid": gid, "home": parts[5], "shell": parts[6]}
    return out


def loginable_shell(shell: Optional[str]) -> bool:
    if not shell:
        return False
    return os.path.basename(shell) not in DENY_SHELLS


def read_priv_groups() -> Dict[str, List[str]]:
    out: Dict[str, List[str]] = {}
    try:
        with open("/etc/group", "r", encoding="utf-8", errors="replace") as f:
            lines = f.read().splitlines()
    except OSError:
        return out
    for line in lines:
        parts = line.split(":")
        if len(parts) < 4 or parts[0] not in PRIV_GROUPS:
            continue
        out[parts[0]] = sorted(x for x in parts[3].split(",") if x)
    return out


def shadow_empty_accounts() -> List[str]:
    """空密码账号(需 root 才能读 /etc/shadow)。"""
    out: List[str] = []
    try:
        with open("/etc/shadow", "r", encoding="utf-8", errors="replace") as f:
            lines = f.read().splitlines()
    except OSError:
        return out
    for line in lines:
        parts = line.split(":")
        if len(parts) >= 2 and parts[0] and parts[1] == "":
            out.append(parts[0])
    return out


def resolve_auth_log(cfg: dict) -> Optional[str]:
    p = (cfg.get("auth_log") or "").strip()
    if p:
        return p if os.path.exists(p) else None
    for cand in AUTH_LOG_CANDIDATES:
        if os.path.exists(cand):
            return cand
    return None


def read_log_delta(path: str, rec: dict) -> Tuple[List[str], dict]:
    """续读认证日志。首次(rec 为空)直接跳到文件尾, 不回放历史避免刷屏。"""
    try:
        st = os.stat(path)
    except OSError:
        return [], rec
    offset = int(rec.get("offset") or 0)
    if rec.get("dev") != st.st_dev or rec.get("ino") != st.st_ino or st.st_size < offset:
        offset = 0  # 日志轮转或被截断
    new = {"dev": st.st_dev, "ino": st.st_ino, "offset": offset, "size": st.st_size}
    if not rec:
        new["offset"] = st.st_size
        return [], new
    if st.st_size <= offset:
        return [], new
    try:
        with open(path, "rb") as f:
            f.seek(offset)
            raw = f.read(st.st_size - offset)
    except OSError:
        return [], new
    end = raw.rfind(b"\n")
    if end < 0:  # 只有半行, 等下次补全
        return [], new
    raw = raw[:end + 1]
    new["offset"] = offset + len(raw)
    return raw.decode("utf-8", "replace").splitlines(), new


def is_trusted_ip(ip: str, cfg: dict) -> bool:
    if is_loopback(ip):
        return True
    try:
        addr = ipaddress.ip_address(ip)
    except ValueError:
        return False
    if getattr(addr, "ipv4_mapped", None):
        addr = addr.ipv4_mapped
    for item in cfg.get("login_whitelist") or []:
        try:
            if "/" in str(item):
                if addr in ipaddress.ip_network(str(item), strict=False):
                    return True
            elif addr == ipaddress.ip_address(str(item)):
                return True
        except ValueError:
            continue
    return False


def login_alert(sev: str, key: str, event: str, account: str, reasons: List[str],
                cmd: str = "", src_ip: str = "", method: str = "", tty: str = "",
                extra: Optional[Dict[str, str]] = None,
                ts: Optional[float] = None) -> Alert:
    if ts is None:
        ts = time.time()
    ex = {"event": event}
    if account and account != "-":
        ex["account"] = account
    if src_ip:
        ex["src_ip"] = src_ip
    if method:
        ex["method"] = method
    if tty:
        ex["tty"] = tty
    if extra:
        ex.update({k: str(v) for k, v in extra.items()})
    return Alert(sev=sev, pid=0, ppid=0, user=account or "-", comm=event, key=key,
                 exe=None, cmd=cmd or event, cwd=None, reasons=reasons, ports=[],
                 started=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts)),
                 kind="login", extra=ex)


def parse_auth_lines(lines: List[str]) -> Tuple[List[dict], List[dict], List[dict]]:
    ok: List[dict] = []
    fails: List[dict] = []
    priv: List[dict] = []
    failed_pairs = set()
    for line in lines:
        ts = parse_syslog_ts(line)
        m = RE_SSH_OK.search(line)
        if m:
            ok.append({"ts": ts, "pid": m.group(1), "method": m.group(2),
                       "account": m.group(3), "ip": m.group(4)})
            continue
        m = RE_SSH_FAIL.search(line)
        if m:
            fails.append({"ts": ts, "method": m.group(2), "account": m.group(4),
                          "ip": m.group(5), "invalid": bool(m.group(3))})
            failed_pairs.add((m.group(5), m.group(4)))
            continue
        m = RE_SSH_INVALID.search(line)
        if m:
            fails.append({"ts": ts, "method": "invalid", "account": m.group(2),
                          "ip": m.group(3), "invalid": True})
            continue
        m = RE_SUDO.search(line)
        if m:
            priv.append({"ts": ts, "tool": "sudo", "by": m.group(1),
                         "to": m.group(2), "cmd": m.group(3).strip()})
            continue
        m = RE_SU.search(line)
        if m:
            priv.append({"ts": ts, "tool": "su", "by": m.group(2),
                         "to": m.group(1), "cmd": ""})
    # "Invalid user" 与 "Failed ... for invalid user" 是同一批尝试的两行, 去重
    fails = [f for f in fails
             if not (f["method"] == "invalid" and (f["ip"], f["account"]) in failed_pairs)]
    return ok, fails, priv


def _num(value, default):
    try:
        return type(default)(value)
    except (TypeError, ValueError):
        return default


def window_desc(seconds: float) -> str:
    """把窗口秒数转成可读文案: 整小时按小时, 否则按分钟。"""
    if seconds >= 3600 and seconds % 3600 == 0:
        return "%d 小时" % int(seconds // 3600)
    return "%d 分钟" % max(1, int(seconds // 60))


def gather_login_alerts(cfg: dict, state: dict, wl: dict) -> List[Alert]:
    """登录成功/失败/爆破/提权 检测, 并推进 auth.log 偏移。"""
    if not cfg.get("monitor_login"):
        return []
    path = resolve_auth_log(cfg)
    if not path:
        return []
    lg = state.setdefault("login", {})
    # 旧版 failures 以 IP 为键(带 users 字段), 新版以账号为键, 旧记录直接丢弃
    for k in [k for k, r in (lg.get("failures") or {}).items() if "users" in r]:
        del lg["failures"][k]
    files = lg.setdefault("files", {})
    try:
        lines, newrec = read_log_delta(path, files.get(path) or {})
    except OSError:
        return []
    files[path] = newrec
    if not lines:
        return []

    ok_events, fail_events, priv_events = parse_auth_lines(lines)
    now = time.time()
    window = max(60.0, _num(cfg.get("failed_login_window_hours") or 72, 72.0) * 3600.0)
    thr = max(1, _num(cfg.get("failed_login_threshold") or 5, 5))
    ip_thr = max(1, _num(cfg.get("ip_alert_threshold") or 5, 5))
    ip_window = max(60.0, _num(cfg.get("ip_alert_window_hours") or 72, 72.0) * 3600.0)
    ip_level = str(cfg.get("ip_alert_level") or "info").strip().lower()
    if ip_level not in SEV_RANK:
        ip_level = "off"

    failures = lg.setdefault("failures", {})
    ip_failures = lg.setdefault("ip_failures", {})
    touched = set()
    touched_ips = set()
    for ev in fail_events:
        acct_name = ev["account"]
        ip = ev["ip"]
        r = failures.get(acct_name)
        if not r or ev["ts"] - float(r.get("first") or 0) > window:
            r = {"count": 0, "first": ev["ts"], "last": ev["ts"],
                 "ips": {}, "invalid": 0}
            failures[acct_name] = r
        r["count"] = int(r.get("count") or 0) + 1
        r["last"] = ev["ts"]
        srcs = r.setdefault("ips", {})
        srcs[ip] = int(srcs.get(ip) or 0) + 1
        if ev.get("invalid"):
            r["invalid"] = int(r.get("invalid") or 0) + 1
        touched.add(acct_name)

        # 同一来源 IP 的失败聚合(撞库/账号枚举), 独立于账号计数
        ir = ip_failures.get(ip)
        if not ir or ev["ts"] - float(ir.get("first") or 0) > ip_window:
            ir = {"count": 0, "first": ev["ts"], "last": ev["ts"],
                  "accounts": {}, "invalid": 0}
            ip_failures[ip] = ir
        ir["count"] = int(ir.get("count") or 0) + 1
        ir["last"] = ev["ts"]
        accs = ir.setdefault("accounts", {})
        accs[acct_name] = int(accs.get(acct_name) or 0) + 1
        if ev.get("invalid"):
            ir["invalid"] = int(ir.get("invalid") or 0) + 1
        touched_ips.add(ip)

    alerts: List[Alert] = []
    for acct_name in sorted(touched):
        r = failures[acct_name]
        count = int(r.get("count") or 0)
        if count < thr:
            continue
        srcs = r.get("ips") or {}
        last = float(r.get("last") or now)
        win = window_desc(window)
        reasons = ["%s内账号 %s 连续登录失败 %d 次" % (win, acct_name, count)]
        if len(srcs) > 1:
            reasons.append("来自 %d 个来源 IP" % len(srcs))
        if r.get("invalid"):
            reasons.append("其中 %d 次为不存在的账号(疑似账号枚举)" % int(r["invalid"]))
        reasons.append("连续失败达到告警阈值(%d 次/%s)" % (thr, win))
        top = sorted(srcs.items(), key=lambda kv: -kv[1])[:3]
        # 未登录成功的失败侧只报提示, 由每日汇总推送; 爆破成功仍由成功登录侧报高危
        alerts.append(login_alert(
            "info", "login:brute:%s" % acct_name, "登录爆破",
            acct_name, reasons,
            cmd=",".join("%s×%d" % (ip, n) for ip, n in top) or "-",
            src_ip=",".join(ip for ip, _ in top),
            extra={"failed_count": str(count)}, ts=last))

    # 低等级: 同一来源 IP 在窗口内多次失败且涉及多个账号(撞库/扫描)。
    # 只针对单账号的攻击已由上面的账号高危规则覆盖, 这里不重复报。
    if ip_level in SEV_RANK:
        for ip in sorted(touched_ips):
            ir = ip_failures[ip]
            cnt = int(ir.get("count") or 0)
            accs = ir.get("accounts") or {}
            if cnt < ip_thr or len(accs) < 2:
                continue
            win = window_desc(ip_window)
            reasons = ["%s内来自 %s 的登录失败 %d 次, 涉及 %d 个账号"
                       % (win, ip, cnt, len(accs))]
            if ir.get("invalid"):
                reasons.append("其中 %d 次为不存在的账号(疑似账号枚举)"
                               % int(ir["invalid"]))
            reasons.append("达到来源 IP 聚合阈值(%d 次/%s)" % (ip_thr, win))
            top = sorted(accs.items(), key=lambda kv: -kv[1])[:5]
            alerts.append(login_alert(
                ip_level, "login:ip:%s" % ip, "来源扫描", "-", reasons,
                cmd=",".join("%s×%d" % (a, n) for a, n in top) or "-",
                src_ip=ip,
                extra={"failed_count": str(cnt),
                       "account": ",".join(a for a, _ in top)},
                ts=float(ir.get("last") or now)))

    accounts = read_accounts()
    base_accounts = wl.get("accounts") or {}
    ips = lg.setdefault("ips", {})

    for ev in ok_events:
        ip, user, method = ev["ip"], ev["account"], ev["method"]
        trusted = is_trusted_ip(ip, cfg)
        acct = accounts.get(user)
        recent = failures.get(user) or {}
        recent_n = int(recent.get("count") or 0)
        if ev["ts"] - float(recent.get("last") or 0) > window:
            recent_n = 0
        first_ip = ip not in ips
        # 成功登录中断该账号的"连续失败"计数
        failures.pop(user, None)

        reasons: List[str] = []
        sev: Optional[str] = None
        if base_accounts and user not in base_accounts:
            sev = "high"
            reasons.append("登录账号不在账号基线, 可能为新增或后门账号")
        if acct and not loginable_shell(acct.get("shell")):
            sev = "high"
            reasons.append("账号 shell 为 %s, 却成功登录(疑似 PAM/配置被篡改)"
                           % acct.get("shell"))
        if recent_n >= thr:
            sev = "high"
            reasons.append("该账号 %s内失败 %d 次后登录成功, 疑似爆破成功"
                           % (window_desc(window), recent_n))
        is_root = bool(acct and int(acct.get("uid", -1)) == 0)
        if is_root:
            if method.startswith("password"):
                if trusted:
                    sev = sev or "medium"
                    reasons.append("root 使用密码方式登录(来源在受信白名单)")
                else:
                    sev = "high"
                    reasons.append("root 使用密码方式远程登录")
            elif trusted:
                sev = sev or "medium"
                reasons.append("root 远程登录(来源在受信白名单)")
            else:
                sev = sev or "high"
                reasons.append("root 从非受信来源远程登录")
        else:
            # 普通账号: 每次登录成功都提示注意, 不按 IP/账号做 24h 抑制
            if cfg.get("login_success_alert", True):
                sev = sev or "medium"
                reasons.append("普通账号登录成功")
                if first_ip and not trusted:
                    reasons.append("首次从该来源 IP 登录")

        # 记录来源 IP 历史(仅用于原因判定)
        rec_ip = ips.get(ip)
        if rec_ip:
            rec_ip["last"] = ev["ts"]
            rec_ip["count"] = int(rec_ip.get("count") or 0) + 1
            rec_ip["user"] = user
        else:
            ips[ip] = {"first": ev["ts"], "last": ev["ts"], "count": 1,
                       "user": user}
        if sev is None:
            continue
        # 所有账号(含 root)每次登录单独成条, 不做 24h 去重
        key = "login:ssh:%s@%s:%s:%d" % (user, ip, ev.get("pid") or "-",
                                         int(ev["ts"]))
        alerts.append(login_alert(
            sev, key, "SSH 登录", user, reasons,
            cmd="%s from %s (%s)" % (user, ip, method),
            src_ip=ip, method=method, ts=ev["ts"]))

    if cfg.get("monitor_priv_esc", True):
        priv_ignore = cfg.get("priv_esc_ignore_cmd_substr") or []
        for ev in priv_events:
            if ev["to"] != "root":
                continue
            tool, by, cmd = ev["tool"], ev["by"], ev["cmd"]
            # 自身运维命令(如 sudo python3 .../procmon.py refresh-hash)不告警
            if any(s and s in cmd for s in priv_ignore):
                continue
            digest = hashlib.sha1(cmd.encode("utf-8", "replace")).hexdigest()[:8]
            reason = "用户 %s 通过 %s 提权到 root" % (by, tool)
            if cmd:
                reason += ": " + cmd[:120]
            alerts.append(login_alert(
                "medium", "login:priv:%s:%s:%s" % (tool, by, digest),
                "%s 提权" % tool, by, [reason], cmd=cmd[:200] or tool, ts=ev["ts"]))

    # 清理过期统计(超过窗口即不可再累计), 避免 state.json 无限增长
    for name in [k for k, r in failures.items()
                 if now - float(r.get("last") or 0) > window]:
        del failures[name]
    for ip in [k for k, r in ip_failures.items()
               if now - float(r.get("last") or 0) > ip_window]:
        del ip_failures[ip]
    keep = float(cfg.get("keep_days") or 7) * 86400.0
    for ip in [k for k, r in ips.items() if now - float(r.get("last") or 0) > keep]:
        del ips[ip]
    return alerts


def gather_identity_alerts(cfg: dict, wl: dict,
                           state: dict) -> Tuple[List[Alert], bool]:
    """账号基线比对: 新增/删除/变更账号、高权限组、空密码账号。

    返回 (告警, 基线是否被初始化需要落盘)。首次运行静默建立基线。
    同一差异(如某账号持续留在 sudo 组)只在首次出现时告警, 差异消失后再出现会重新告警。
    """
    if not cfg.get("monitor_login"):
        return [], False
    accounts = read_accounts()
    if not accounts:
        return [], False
    base = wl.get("accounts")
    if not isinstance(base, dict) or not base:
        wl["accounts"] = {n: {"uid": a["uid"], "shell": a["shell"],
                              "added": now_iso()} for n, a in accounts.items()}
        wl["priv_groups"] = read_priv_groups()
        wl["accounts_updated"] = now_iso()
        return [], True

    seen = state.setdefault("ident_seen", {})
    cur: Dict[str, bool] = {}

    def fresh(sig: str) -> bool:
        """登记当前差异, 仅当该差异此前未告警过时返回 True。"""
        cur[sig] = True
        return sig not in seen

    alerts: List[Alert] = []
    for name in sorted(accounts):
        a = accounts[name]
        b = base.get(name)
        if b is None:
            if int(a["uid"]) == 0:
                sev, reasons = "high", ["新增账号 uid=0, 具备 root 等价权限"]
            elif loginable_shell(a["shell"]):
                sev, reasons = "high", ["新增可登录账号 shell=%s" % a["shell"]]
            else:
                sev, reasons = "medium", ["新增系统账号 shell=%s" % a["shell"]]
            reasons.insert(0, "账号不在基线")
            if fresh("acct:add:%s:%s:%s" % (name, a["uid"], a["shell"])):
                alerts.append(login_alert(
                    sev, "login:acct:add:" + name, "账号新增", name, reasons,
                    cmd="uid=%s shell=%s home=%s"
                        % (a["uid"], a["shell"], a["home"])))
            continue

        reasons = []
        sev: Optional[str] = None
        if int(a["uid"]) != int(b.get("uid") if b.get("uid") is not None else -1):
            sev = "high" if int(a["uid"]) == 0 else "medium"
            reasons.append("UID 由 %s 变为 %s" % (b.get("uid"), a["uid"]))
        if a["shell"] != b.get("shell"):
            if loginable_shell(a["shell"]) and not loginable_shell(b.get("shell")):
                sev = "high"
                reasons.append("shell 由 %s 改为 %s, 账号变为可登录"
                               % (b.get("shell"), a["shell"]))
            else:
                sev = sev or "medium"
                reasons.append("shell 由 %s 改为 %s" % (b.get("shell"), a["shell"]))
        if sev and reasons and fresh("acct:chg:%s:%s:%s"
                                     % (name, a["uid"], a["shell"])):
            alerts.append(login_alert(
                sev, "login:acct:chg:" + name, "账号变更", name, reasons,
                cmd="uid=%s shell=%s" % (a["uid"], a["shell"])))

    for name in sorted(base):
        if name not in accounts and fresh("acct:del:" + name):
            alerts.append(login_alert(
                "medium", "login:acct:del:" + name, "账号删除", name,
                ["账号已从 /etc/passwd 移除, 请确认操作来源"]))

    dirty = False
    groups = read_priv_groups()
    bgroups = wl.get("priv_groups")
    if isinstance(bgroups, dict) and bgroups:
        for g in sorted(groups):
            added = [m for m in sorted(set(groups[g]) - set(bgroups.get(g) or []))
                     if fresh("grp:%s:%s" % (g, m))]
            if added:
                alerts.append(login_alert(
                    "high", "login:grp:%s:%s" % (g, ",".join(added)), "权限变更",
                    added[0],
                    ["用户 %s 被加入高权限组 %s, 具备 root 等价权限"
                     % (",".join(added), g)],
                    cmd="group=%s members=%s" % (g, ",".join(groups[g]))))
    elif groups:
        wl["priv_groups"] = groups
        wl["accounts_updated"] = now_iso()
        dirty = True

    for name in shadow_empty_accounts():
        if fresh("empty:" + name):
            alerts.append(login_alert(
                "high", "login:empty:" + name, "空密码账号", name,
                ["账号密码为空, 可免密登录, 需立即处理"]))
    # 差异消失的签名自动丢弃, 该差异再次出现时会重新告警
    state["ident_seen"] = cur
    return alerts, dirty


# --------------------------------------------------------------------------- #
# 输出 / 通知
# --------------------------------------------------------------------------- #
def format_alert(a: Alert, color: bool) -> str:
    head = "[%s]" % SEV_CN.get(a.sev, a.sev)
    if color:
        head = SEV_COLOR.get(a.sev, "") + head + RESET
    if a.kind == "login":
        lines = ["%s %s %s" % (head, a.comm, a.started)]
        if a.user and a.user != "-":
            lines.append("  账号: " + a.user)
        if a.extra.get("src_ip"):
            lines.append("  来源: " + a.extra["src_ip"])
        if a.extra.get("method"):
            lines.append("  方式: " + a.extra["method"])
        lines.append("  详情: " + a.cmd)
        lines.append("  原因: " + "; ".join(a.reasons))
        return "\n".join(lines)
    lines = ["%s pid=%d ppid=%d user=%s start=%s" % (head, a.pid, a.ppid, a.user, a.started),
             "  命令: " + a.cmd]
    if a.exe:
        lines.append("  路径: " + a.exe)
    if a.cwd:
        lines.append("  目录: " + a.cwd)
    lines.append("  原因: " + "; ".join(a.reasons))
    if a.ports:
        lines.append("  端口: " + ",".join(str(x) for x in a.ports))
    return "\n".join(lines)


def alert_to_dict(a: Alert) -> dict:
    d = {"severity": a.sev, "kind": a.kind, "pid": a.pid, "ppid": a.ppid,
         "user": a.user, "comm": a.comm, "key": a.key, "exe": a.exe,
         "cmd": a.cmd, "cwd": a.cwd, "reasons": a.reasons, "ports": a.ports,
         "started": a.started}
    if a.extra:
        d["extra"] = a.extra
    return d


def write_log(path: str, alerts: List[Alert]) -> None:
    if not path or not alerts:
        return
    try:
        d = os.path.dirname(path)
        if d:
            os.makedirs(d, exist_ok=True)
        ts = now_iso()
        with open(path, "a", encoding="utf-8") as f:
            for a in alerts:
                rec = alert_to_dict(a)
                rec["time"] = ts
                f.write(json.dumps(rec, ensure_ascii=False) + "\n")
    except OSError as e:
        sys.stderr.write("写日志失败: %s\n" % e)


def alert_vars(a: Alert, cfg: Optional[dict] = None) -> Dict[str, str]:
    """预警接口 info_json 元素的默认字段。

    键名即 warning_conf.parameter 中可用的变量名, 顺序由 parameter 决定,
    同名字段在 temp 中按 %s 顺序占位。登录类告警额外带
    event/account/src_ip/method/tty/failed_count。
    """
    v = {
        "host": host_name(cfg),
        "time": now_iso(),
        "severity": SEV_CN.get(a.sev, a.sev),
        "severity_code": a.sev,
        "pid": str(a.pid),
        "ppid": str(a.ppid),
        "user": a.user,
        "comm": a.comm,
        "cmd": a.cmd,
        "exe": a.exe or "",
        "cwd": a.cwd or "",
        "reasons": "; ".join(a.reasons),
        "ports": ",".join(str(x) for x in a.ports),
        "key": a.key,
        "started": a.started,
    }
    for k, val in a.extra.items():
        v.setdefault(k, str(val))
    return v


def notify_log_write(cfg: dict, rec: dict) -> None:
    """把一次推送结果追加到 notify.log(JSONL), 失败不影响巡检。"""
    path = (cfg.get("notify_log") or "").strip()
    if not path:
        return
    item = dict(rec)
    item.setdefault("time", now_iso())
    item.setdefault("host", host_name(cfg))
    try:
        d = os.path.dirname(path)
        if d:
            os.makedirs(d, exist_ok=True)
        with open(path, "a", encoding="utf-8") as f:
            f.write(json.dumps(item, ensure_ascii=False) + "\n")
    except OSError as e:
        sys.stderr.write("写通知日志失败(%s): %s\n" % (path, e))


def notify_api(cfg: dict, alerts: List[Alert], kind: str = "proc") -> bool:
    """调用「预警信息发送接口」POST {api_url}, form-data。

    参数: app_id / code / info_json(JSON 数组字符串, 批量模式)。
    服务端按 info_json 每个元素渲染一条预警文本, 逐条真实推送钉钉/短信。
    HTTP 状态码恒为 200, 业务结果以响应体 code == 200 为准。
    每次调用(含跳过/成功/失败)写入 notify.log。
    """
    code = (cfg.get("api_code") or "").strip()
    if not code or not alerts:
        notify_log_write(cfg, {"channel": "warn_api", "kind": kind,
                               "code": code, "count": len(alerts),
                               "ok": False, "skipped": True,
                               "reason": "api_code 为空" if not code else "无告警"})
        return False
    url = (cfg.get("api_url") or "").strip() or DEF_API_URL
    try:
        app_id = int(cfg.get("api_app_id") or 0)
    except (TypeError, ValueError):
        app_id = 0
    try:
        timeout = float(cfg.get("api_timeout") or 10)
    except (TypeError, ValueError):
        timeout = 10.0
    try:
        batch = max(1, int(cfg.get("api_batch_size") or 20))
    except (TypeError, ValueError):
        batch = 20

    fmap = cfg.get("api_field_map") or {}
    ok = True
    for i in range(0, len(alerts), batch):
        chunk = alerts[i:i + batch]
        items = []
        for a in chunk:
            v = alert_vars(a, cfg)
            if isinstance(fmap, dict) and fmap:
                items.append({str(k): v.get(str(src), "") for k, src in fmap.items()})
            else:
                items.append(v)
        form = {
            "app_id": str(app_id),
            "code": code,
            "info_json": json.dumps(items, ensure_ascii=False),
        }
        body = urllib.parse.urlencode(form).encode("utf-8")
        req = urllib.request.Request(
            url, data=body,
            headers={"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"})
        try:
            with urllib.request.urlopen(req, timeout=timeout) as resp:
                text = resp.read().decode("utf-8", "replace")
            res = json.loads(text)
            # 响应体含 to_user(钉钉 token) 等敏感配置, 只取 code/message, 不落日志
            if int(res.get("code") or 0) != 200:
                ok = False
                sys.stderr.write("预警接口返回失败: code=%s message=%s\n"
                                 % (res.get("code"), res.get("message")))
                notify_log_write(cfg, {
                    "channel": "warn_api", "kind": kind, "code": code,
                    "count": len(chunk), "ok": False,
                    "error": ("code=%s message=%s" % (res.get("code"),
                                                      res.get("message")))[:200]})
            else:
                # 成功也留痕(走 stderr, 避免污染 check --json 的 stdout)
                sys.stderr.write("预警接口成功: code=%s 条数=%d\n"
                                 % (code, len(chunk)))
                notify_log_write(cfg, {
                    "channel": "warn_api", "kind": kind, "code": code,
                    "count": len(chunk), "ok": True})
        except Exception as e:  # noqa: BLE001 通知失败不影响巡检
            ok = False
            sys.stderr.write("预警接口调用失败(%s): %s\n" % (url, e))
            notify_log_write(cfg, {
                "channel": "warn_api", "kind": kind, "code": code,
                "count": len(chunk), "ok": False, "error": str(e)[:200]})
    return ok


def notify_webhook(cfg: dict, alerts: List[Alert]) -> None:
    url = (cfg.get("webhook") or "").strip()
    if not url or not alerts:
        return
    host = host_name(cfg)
    lines = ["【procmon】%s 发现 %d 个异常" % (host, len(alerts))]
    for a in alerts[:10]:
        if a.kind == "login":
            lines.append("- [%s] %s 账号=%s 来源=%s" % (
                SEV_CN.get(a.sev, a.sev), a.comm, a.user,
                a.extra.get("src_ip") or "-"))
        else:
            lines.append("- [%s] pid=%d user=%s %s" % (
                SEV_CN.get(a.sev, a.sev), a.pid, a.user, a.cmd[:100]))
        lines.append("  " + "; ".join(a.reasons[:3]))
    kind = cfg.get("webhook_type") or "dingtalk"
    if kind in ("dingtalk", "wecom"):
        payload = {"msgtype": "text", "text": {"content": "\n".join(lines)}}
    else:
        payload = {"source": "procmon", "host": host,
                   "alerts": [alert_to_dict(a) for a in alerts]}
    data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
    req = urllib.request.Request(url, data=data,
                                 headers={"Content-Type": "application/json; charset=utf-8"})
    try:
        urllib.request.urlopen(req, timeout=10).read()
        notify_log_write(cfg, {"channel": "webhook", "kind": "all",
                               "count": len(alerts), "ok": True})
    except Exception as e:  # noqa: BLE001  通知失败不影响巡检
        sys.stderr.write("webhook 通知失败: %s\n" % e)
        notify_log_write(cfg, {"channel": "webhook", "kind": "all",
                               "count": len(alerts), "ok": False,
                               "error": str(e)[:200]})


# --------------------------------------------------------------------------- #
# 提示级每日汇总
# --------------------------------------------------------------------------- #
INFO_PENDING_LIMIT = 1000


def split_info_digest(cfg: dict,
                      fresh: List[Alert]) -> Tuple[List[Alert], List[Alert]]:
    """把提示级告警从实时推送中分流出来。"""
    if not cfg.get("info_digest_enable", True):
        return fresh, []
    live: List[Alert] = []
    deferred: List[Alert] = []
    for a in fresh:
        (deferred if a.sev == "info" else live).append(a)
    return live, deferred


def append_pending(pending: List[dict], add: List[Alert],
                   limit: int = INFO_PENDING_LIMIT) -> List[dict]:
    """把新提示并入缓存, 同 key 覆盖, 超出上限丢最旧。"""
    idx = {p.get("key"): i for i, p in enumerate(pending)}
    for a in add:
        d = alert_to_dict(a)
        k = d.get("key")
        if k in idx:
            pending[idx[k]] = d
        else:
            idx[k] = len(pending)
            pending.append(d)
    if limit and len(pending) > limit:
        del pending[:len(pending) - limit]
    return pending


def digest_due(cfg: dict, state: dict, now: float) -> bool:
    """是否已过当日汇总时刻且今天还没发过。"""
    if not cfg.get("info_digest_enable", True) or not state.get("pending_info"):
        return False
    try:
        hour = int(cfg.get("info_digest_hour", 9))
    except (TypeError, ValueError):
        hour = 9
    if hour < 0 or hour > 23:
        hour = 9
    lt = time.localtime(now)
    return (lt.tm_hour >= hour
            and state.get("last_digest") != time.strftime("%Y-%m-%d", lt))


def top_counts(m: Dict[str, int], n: int) -> str:
    arr = sorted(m.items(), key=lambda kv: (-kv[1], kv[0]))[:n]
    return ", ".join("%s(%d)" % (k, v) for k, v in arr)


def build_info_digest(pending: List[dict], now: float) -> Alert:
    """把缓存的提示级告警合并成一条汇总告警。"""
    brute: Dict[str, int] = {}
    scan: Dict[str, int] = {}
    other: Dict[str, int] = {}
    kind = "proc"
    for p in pending:
        if p.get("kind") == "login":
            kind = "login"
        key = p.get("key") or ""
        if key.startswith("login:brute:"):
            u = p.get("user") or "-"
            brute[u] = brute.get(u, 0) + 1
        elif key.startswith("login:ip:"):
            ip = key[len("login:ip:"):]
            scan[ip] = scan.get(ip, 0) + 1
        else:
            c = p.get("comm") or "-"
            other[c] = other.get(c, 0) + 1
    reasons = ["累计 %d 项提示级告警(不实时推送, 每日汇总)" % len(pending)]
    if brute:
        reasons.append("账号登录失败/枚举: " + top_counts(brute, 5))
    if scan:
        reasons.append("来源 IP 扫描: " + top_counts(scan, 5))
    if other:
        reasons.append("其他提示: " + top_counts(other, 5))
    last = pending[-1]
    last_reasons = last.get("reasons") or []
    if last_reasons:
        reasons.append("最近一项: %s - %s"
                       % (last.get("comm") or "-", last_reasons[0]))
    lt = time.localtime(now)
    return Alert(sev="info", user="-", comm="提示汇总",
                 key="digest:info:" + time.strftime("%Y-%m-%d", lt),
                 cmd="%d 项提示级告警汇总" % len(pending),
                 reasons=reasons, kind=kind,
                 started=time.strftime("%Y-%m-%d %H:%M:%S", lt))


def flush_info_digest(cfg: dict, state: dict, now: float) -> List[Alert]:
    """到达汇总时刻时返回一条汇总告警并清空缓存。"""
    if not digest_due(cfg, state, now):
        return []
    alert = build_info_digest(state.get("pending_info") or [], now)
    state["pending_info"] = []
    state["last_digest"] = time.strftime("%Y-%m-%d", time.localtime(now))
    return [alert]


def notify(cfg: dict, alerts: List[Alert]) -> None:
    """主通道: 预警信息发送接口(配置了 api_code 时); 可选并行通用 webhook。

    配置 login_api_code 后, 登录类告警走独立 code/字段映射, 便于服务端
    使用"服务器登录异常"模板, 与进程告警文案区分。
    """
    if not alerts:
        return
    proc_alerts = [a for a in alerts if a.kind != "login"]
    login_alerts = [a for a in alerts if a.kind == "login"]
    if proc_alerts:
        notify_api(cfg, proc_alerts, kind="proc")
    if login_alerts:
        lcfg = cfg
        if (cfg.get("login_api_code") or "").strip():
            lcfg = dict(cfg)
            lcfg["api_code"] = cfg["login_api_code"]
            lcfg["api_field_map"] = (cfg.get("login_api_field_map")
                                     or cfg.get("api_field_map") or {})
        notify_api(lcfg, login_alerts, kind="login")
    if (cfg.get("webhook") or "").strip():
        notify_webhook(cfg, alerts)


# --------------------------------------------------------------------------- #
# 配置与数据
# --------------------------------------------------------------------------- #
class Paths:
    def __init__(self, home: str, config: str, log: str):
        self.home = home
        self.config = config
        self.log = log

    @property
    def whitelist(self) -> str:
        return os.path.join(self.home, "whitelist.json")

    @property
    def state(self) -> str:
        return os.path.join(self.home, "state.json")

    @property
    def hashcache(self) -> str:
        return os.path.join(self.home, "hashcache.json")

    def ensure(self) -> None:
        for p in (self.home, os.path.dirname(self.config), os.path.dirname(self.log)):
            if p:
                os.makedirs(p, exist_ok=True)


def load_config(path: str) -> dict:
    cfg = dict(DEFAULT_CONFIG)
    data = load_json(path)
    if isinstance(data, dict):
        for k, v in data.items():
            if not k.startswith("_"):
                cfg[k] = v
        # 旧配置迁移: 旧schema用 threshold(默认15)+factor 做分级,
        # 新版为"连续失败达到 threshold(默认5)即高危"。
        # 仅当仍是旧默认值 15 时迁移, 用户自定义过的值保持不变。
        if "brute_force_high_factor" in data:
            if str(cfg.get("failed_login_threshold")) == "15":
                cfg["failed_login_threshold"] = DEFAULT_CONFIG["failed_login_threshold"]
            cfg.pop("brute_force_high_factor", None)
        # 失败窗口单位由分钟改为小时: 旧默认 10 分钟 -> 新默认 72 小时
        if "failed_login_window_min" in data:
            if "failed_login_window_hours" not in data:
                old_min = _num(data.get("failed_login_window_min"), 10.0)
                cfg["failed_login_window_hours"] = (
                    72.0 if float(old_min) == 10.0 else round(float(old_min) / 60.0, 2))
            cfg.pop("failed_login_window_min", None)
        # 来源 IP 聚合窗口由分钟改为小时: 旧默认 10 分钟/10 次 -> 72 小时/5 次
        if "ip_alert_window_min" in data:
            if "ip_alert_window_hours" not in data:
                old_ip_min = _num(data.get("ip_alert_window_min"), 10.0)
                cfg["ip_alert_window_hours"] = (
                    72.0 if float(old_ip_min) == 10.0 else round(float(old_ip_min) / 60.0, 2))
                if str(data.get("ip_alert_threshold")) == "10":
                    cfg["ip_alert_threshold"] = DEFAULT_CONFIG["ip_alert_threshold"]
            cfg.pop("ip_alert_window_min", None)
    return cfg


def load_wl(path: str) -> dict:
    data = load_json(path)
    if not isinstance(data, dict):
        data = {}
    if not isinstance(data.get("entries"), dict):
        data["entries"] = {}
    data.setdefault("version", 1)
    data.setdefault("created", now_iso())
    return data


def load_state(path: str) -> dict:
    data = load_json(path)
    if not isinstance(data, dict):
        data = {}
    if not isinstance(data.get("seen"), dict):
        data["seen"] = {}
    data.setdefault("version", 1)
    return data


def add_key_entry(wl: dict, key: str, user: str = "", example: str = "",
                  sha256: Optional[str] = None) -> bool:
    """按主键直接写入白名单, 返回是否新增。"""
    existed = key in wl["entries"]
    e = wl["entries"].setdefault(key, {})
    e.setdefault("added", now_iso())
    if user:
        e["user"] = user
    if example:
        e["example"] = example[:200]
    if sha256:
        e["sha256"] = sha256
    return not existed


def add_entry(wl: dict, p: Proc, cfg: dict, hash_cache: Dict[str, dict],
              refresh_hash: bool = False) -> Tuple[str, bool]:
    key = proc_key(p)
    existed = key in wl["entries"]
    digest = None
    if cfg.get("whitelist_hash") and p.exe:
        if refresh_hash or not (wl["entries"].get(key) or {}).get("sha256"):
            digest = file_sha256(p.exe, hash_cache)
    add_key_entry(wl, key, p.user, p.cmdline, digest)
    return key, not existed


ALERT_TS_FMT = "%Y-%m-%d %H:%M:%S"


def learn_rows_from_alerts(entries: Dict[str, dict], log_path: str,
                           hours: float) -> List[dict]:
    """从告警流水里收集"未登记"的进程主键。

    短命进程存活时间极短, learn 的单次 /proc 快照永远采不到; 但巡检报警时
    已经把主键和命令行写进 alerts.jsonl, 直接复用这份记录即可补录。
    deleted 场景的去重键带 |ino:N 后缀, 写白名单前要剥掉。
    """
    since = time.time() - max(0.1, hours) * 3600.0
    rows: Dict[str, dict] = {}
    try:
        with open(log_path, "r", encoding="utf-8", errors="replace") as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                try:
                    rec = json.loads(line)
                except ValueError:
                    continue
                if rec.get("kind") != "proc":
                    continue
                key = re.sub(r"\|ino:\d+$", "", str(rec.get("key") or ""))
                if not key or lookup_entry(entries, key) is not None:
                    continue
                ts_txt = str(rec.get("time") or "")
                try:
                    ts = time.mktime(time.strptime(ts_txt, ALERT_TS_FMT))
                except (ValueError, OverflowError):
                    ts = time.time()
                if ts < since:
                    continue
                row = rows.get(key)
                if row is None:
                    rows[key] = {"key": key, "cmd": str(rec.get("cmd") or ""),
                                 "user": str(rec.get("user") or ""),
                                 "sev": str(rec.get("severity") or ""),
                                 "count": 1, "last": ts_txt}
                else:
                    row["count"] += 1
                    if ts_txt > row["last"]:
                        row["last"] = ts_txt
    except OSError:
        return []
    return sorted(rows.values(), key=lambda r: r["last"], reverse=True)


# --------------------------------------------------------------------------- #
# 子命令
# --------------------------------------------------------------------------- #
def cmd_init(args, cfg: dict, paths: Paths) -> int:
    paths.ensure()
    if not os.path.exists(paths.config):
        save_json(paths.config, DEFAULT_CONFIG)
        print("已生成配置: " + paths.config)
    hash_cache = load_json(paths.hashcache, {}) or {}
    wl = load_wl(paths.whitelist)
    if wl["entries"] and not args.force:
        print("白名单已存在 %d 条 (使用 --force 追加当前进程)" % len(wl["entries"]))
        return 0
    added = 0
    deleted: List[Tuple[Proc, str, List[str]]] = []
    for p in collect_procs().values():
        if not p.argv and p.exe is None:
            continue
        if p.state == "Z":
            continue
        _, new = add_entry(wl, p, cfg, hash_cache)
        added += 1 if new else 0
        if p.deleted:
            deleted.append((p,) + judge_deleted(p))
    if cfg.get("monitor_login") and not isinstance(wl.get("accounts"), dict):
        accounts = read_accounts()
        if accounts:
            wl["accounts"] = {n: {"uid": a["uid"], "shell": a["shell"],
                                  "added": now_iso()}
                              for n, a in accounts.items()}
            wl["priv_groups"] = read_priv_groups()
            wl["accounts_updated"] = now_iso()
    wl["updated"] = now_iso()
    save_json(paths.whitelist, wl)
    save_json(paths.hashcache, hash_cache)
    print("基线采集完成: 新增 %d 条, 白名单共 %d 条" % (added, len(wl["entries"])))
    print("账号基线: %d 个" % len(wl.get("accounts") or {}))
    print("白名单: " + paths.whitelist)
    if deleted:
        print("\n注意: 采集中有 %d 个进程运行的可执行文件已被删除:" % len(deleted))
        for p, dsev, dreasons in deleted:
            print("  [%s] pid=%-7d user=%s exe=%s" % (SEV_CN.get(dsev, dsev),
                                                    p.pid, p.user, p.exe))
            print("      " + p.cmdline[:110])
            print("      " + "; ".join(dreasons))
        print("  其中判定为「程序升级替换」的建议重启对应进程, 否则会持续告警;")
        print("  判定为高危的必须先取证清理, 不要直接建基线放行。")
    return 0


def print_learn_rows(rows: List[dict], numbered: bool = False) -> None:
    for i, row in enumerate(rows, 1):
        prefix = "  %2d) " % i if numbered else "  "
        pad = " " * (len(prefix) - 2)
        p = row.get("proc")
        if p is not None:
            print("%spid=%-7d user=%-10s %s"
                  % (prefix, p.pid, p.user, row["cmd"][:110]))
        else:
            print("%s[%s] %s" % (prefix, SEV_CN.get(row["sev"], row["sev"] or "-"),
                                 row["cmd"][:100]))
            print("%s出现 %d 次, 最近 %s, 用户 %s"
                  % (pad + "    ", row["count"], row["last"], row["user"] or "-"))
        print("%skey: %s" % (pad + "    ", row["key"]))


def stdin_is_tty() -> bool:
    try:
        return bool(sys.stdin.isatty())
    except (AttributeError, ValueError):
        return False


def parse_selection(raw: str, total: int) -> Optional[List[int]]:
    """解析 1,3,5 / 2-4 / all / none 形式的编号, 非法返回 None。"""
    raw = raw.strip().lower()
    if raw in ("", "none", "n", "q", "quit"):
        return []
    if raw in ("all", "a", "*"):
        return list(range(1, total + 1))
    idx = set()
    for part in re.split(r"[,\s，]+", raw):
        if not part:
            continue
        m = re.match(r"^(\d+)\s*[-~]\s*(\d+)$", part)
        if m:
            a, b = int(m.group(1)), int(m.group(2))
            idx.update(range(min(a, b), max(a, b) + 1))
        elif part.isdigit():
            idx.add(int(part))
        else:
            return None
    if not idx or any(i < 1 or i > total for i in idx):
        return None
    return sorted(idx)


def select_rows(rows: List[dict]) -> List[dict]:
    """交互选择待补录条目, 返回选中的行(空列表表示取消)。"""
    print_learn_rows(rows, numbered=True)
    print("\n输入要补录的编号: 1,3,5 / 2-4 / all / none(直接回车取消)")
    try:
        raw = input("选择> ")
    except (EOFError, KeyboardInterrupt):
        print()
        return []
    idx = parse_selection(raw, len(rows))
    if idx is None:
        print("输入无法识别或编号超出范围, 已取消")
        return []
    picked = [rows[i - 1] for i in idx]
    highs = [r for r in picked if r["sev"] == "high"]
    if highs:
        print("\n选中的条目里有 %d 条「高危」:" % len(highs))
        for r in highs:
            print("  [高危] %s" % r["cmd"][:90])
        try:
            ans = input("确认均为误报并长期放行? 输入 yes 继续: ").strip().lower()
        except (EOFError, KeyboardInterrupt):
            ans = ""
        if ans != "yes":
            picked = [r for r in picked if r["sev"] != "high"]
            print("已取消高危条目, 继续补录其余 %d 条" % len(picked))
    return picked


def cmd_learn(args, cfg: dict, paths: Paths) -> int:
    paths.ensure()
    hash_cache = load_json(paths.hashcache, {}) or {}
    wl = load_wl(paths.whitelist)
    entries = wl["entries"]
    ignore_sub = cfg.get("ignore_cmd_substr") or []
    rows: List[dict] = []
    select = getattr(args, "select", False)

    if args.from_alerts:
        if not os.path.exists(paths.log):
            print("告警流水不存在: " + paths.log)
            return 0
        rows = learn_rows_from_alerts(entries, paths.log, args.hours)
        if not rows:
            print("告警流水近 %g 小时内没有待补录的进程" % args.hours)
            return 0
        print("近 %g 小时告警中未登记的进程 %d 个:" % (args.hours, len(rows)))
        hint = "procmon.py learn --from-alerts --commit"
    else:
        for p in sorted(collect_procs().values(), key=lambda x: x.pid):
            if not p.argv and p.exe is None:
                continue
            if p.state == "Z":
                continue
            if any(s and s in p.cmdline for s in ignore_sub):
                continue
            key = proc_key(p)
            if lookup_entry(entries, key) is not None:
                continue
            rows.append({"key": key, "cmd": p.cmdline, "user": p.user,
                         "sev": "", "count": 1, "last": p.started, "proc": p})
        if not rows:
            print("没有发现未登记的进程")
            return 0
        print("未登记进程 %d 个:" % len(rows))
        hint = "procmon.py learn --commit"

    allow_high = bool(getattr(args, "allow_high", False))
    if select:
        if not stdin_is_tty():
            print_learn_rows(rows)
            print("\n--select 需要在交互终端里运行; 也可用 --commit 直接补录")
            return 0
        picked = select_rows(rows)
        if not picked:
            print("未选择任何条目, 已取消")
            return 0
    else:
        picked = rows
        if not allow_high:
            # 白名单是"长期放行", 高危项多为真问题, 不随批量补录一起写入
            picked = [r for r in rows if r["sev"] != "high"]
        skipped = [r for r in rows if r["sev"] == "high"] if not allow_high else []
        print_learn_rows(picked)
        if skipped:
            print("\n以下 %d 条为「高危」, 默认不写入白名单:" % len(skipped))
            for row in skipped:
                print("  [高危] %s" % row["cmd"][:100])
                print("      key: " + row["key"])
        if not args.commit:
            sel_hint = hint.replace("--commit", "--select")
            if skipped:
                print("\n仅预览。逐条挑选: " + sel_hint)
                print("  全部补录(不含高危): " + hint)
                print("  连高危一起补录: " + hint + " --allow-high")
            else:
                print("\n仅预览。逐条挑选: " + sel_hint + " ；全部补录: " + hint)
            return 0
        if not picked:
            print("\n没有可补录的条目")
            return 0

    for row in picked:
        p = row.get("proc")
        if p is not None:
            add_entry(wl, p, cfg, hash_cache)
        else:
            # 来自告警流水的条目: 进程已退出, 无法取哈希, 只记录主键
            add_key_entry(wl, row["key"], row["user"], row["cmd"])
    wl["updated"] = now_iso()
    save_json(paths.whitelist, wl)
    save_json(paths.hashcache, hash_cache)
    print("\n已补录 %d 条, 白名单共 %d 条" % (len(picked), len(entries)))
    return 0


def cmd_accounts(args, cfg: dict, paths: Paths) -> int:
    """查看/重建登录账号基线(/etc/passwd、高权限组)。"""
    paths.ensure()
    wl = load_wl(paths.whitelist)
    base = wl.get("accounts") or {}
    accounts = read_accounts()
    groups = read_priv_groups()
    bgroups = wl.get("priv_groups") or {}

    print("账号基线: %d 个, 当前系统账号: %d 个 (基线更新于 %s)"
          % (len(base), len(accounts), wl.get("accounts_updated") or "-"))
    diffs = 0
    for name in sorted(accounts):
        a = accounts[name]
        b = base.get(name)
        if b is None:
            diffs += 1
            print("  [新增] %-18s uid=%-6s shell=%s" % (name, a["uid"], a["shell"]))
        elif int(a["uid"]) != int(b.get("uid") or 0) or a["shell"] != b.get("shell"):
            diffs += 1
            print("  [变更] %-18s uid %s->%s  shell %s->%s"
                  % (name, b.get("uid"), a["uid"], b.get("shell"), a["shell"]))
    for name in sorted(base):
        if name not in accounts:
            diffs += 1
            print("  [删除] " + name)
    for g in sorted(groups):
        added = sorted(set(groups[g]) - set(bgroups.get(g) or []))
        if added:
            diffs += 1
            print("  [提权] 组 %s 新增成员: %s" % (g, ",".join(added)))
    if not diffs:
        print("  账号与高权限组均与基线一致")
    if not args.commit:
        print("\n仅预览。确认以上变动均属正常操作后执行: procmon.py accounts --commit")
        return 0
    wl["accounts"] = {n: {"uid": a["uid"], "shell": a["shell"],
                          "added": (base.get(n) or {}).get("added") or now_iso()}
                      for n, a in accounts.items()}
    wl["priv_groups"] = groups
    wl["accounts_updated"] = now_iso()
    save_json(paths.whitelist, wl)
    print("\n账号基线已更新: %d 个账号" % len(accounts))
    return 0


def cmd_refresh_hash(args, cfg: dict, paths: Paths) -> int:
    hash_cache = load_json(paths.hashcache, {}) or {}
    wl = load_wl(paths.whitelist)
    changed = 0
    for key, e in wl["entries"].items():
        if not key.startswith("exe:"):
            continue
        exe = key[4:].split("|")[0]
        if not os.path.exists(exe):
            continue
        digest = file_sha256(exe, hash_cache)
        if digest and digest != e.get("sha256"):
            e["sha256"] = digest
            changed += 1
    wl["updated"] = now_iso()
    save_json(paths.whitelist, wl)
    save_json(paths.hashcache, hash_cache)
    print("已刷新 %d 条哈希" % changed)
    return 0


def cmd_show(args, cfg: dict, paths: Paths) -> int:
    wl = load_wl(paths.whitelist)
    entries = wl["entries"]
    keys = sorted(entries)
    if args.grep:
        keys = [k for k in keys if args.grep.lower() in k.lower()]
    if not keys:
        print("白名单为空 (先执行 init)")
        return 0
    print("白名单 %d 条:" % len(keys))
    for k in keys:
        print("  " + k)
    return 0


def cmd_status(args, cfg: dict, paths: Paths) -> int:
    wl = load_wl(paths.whitelist)
    state = load_state(paths.state)
    real_host = socket.gethostname()
    hn = host_name(cfg)
    print("主机      : " + hn + ("" if hn == real_host else " (系统名 %s)" % real_host))
    print("白名单    : %d 条 (%s)" % (len(wl["entries"]), paths.whitelist))
    print("上次巡检  : " + str(state.get("last_check") or "-"))
    print("待复查条目: %d 个" % len(state.get("seen") or {}))
    print("巡检间隔  : %ss" % cfg.get("interval"))
    print("端口监控  : %s" % ("开" if cfg.get("monitor_ports") else "关"))
    print("哈希校验  : %s" % ("开" if cfg.get("whitelist_hash") else "关"))
    print("短命进程  : %s" % ("开 (cn_proc exec 事件)"
                              if cfg.get("monitor_short_proc", True) else "关"))
    if cfg.get("monitor_login"):
        log_path = resolve_auth_log(cfg)
        win = window_desc(_num(cfg.get("failed_login_window_hours") or 72, 72.0) * 3600.0)
        print("登录检测  : 开 (日志 %s, 连续失败 %s 次/%s -> 高危)" % (
            log_path or "未找到 auth.log/secure", cfg.get("failed_login_threshold"), win))
        ip_win = window_desc(_num(cfg.get("ip_alert_window_hours") or 72, 72.0) * 3600.0)
        print("来源扫描  : %s (%s 次/%s, >=2 个账号)" % (
            cfg.get("ip_alert_level") or "off", cfg.get("ip_alert_threshold"), ip_win))
        print("账号基线  : %d 个 (更新于 %s)" % (
            len(wl.get("accounts") or {}), wl.get("accounts_updated") or "-"))
        rec = ((state.get("login") or {}).get("files") or {}).get(log_path or "") or {}
        if rec.get("offset"):
            print("日志偏移  : " + str(rec.get("offset")))
    else:
        print("登录检测  : 关")
    api_code = (cfg.get("api_code") or "").strip()
    login_code = (cfg.get("login_api_code") or "").strip()
    if api_code or login_code:
        print("预警接口  : %s (app_id=%s, code=%s)" % (
            cfg.get("api_url") or DEF_API_URL, cfg.get("api_app_id"),
            api_code or "(未配置, 进程告警不推送)"))
        print("登录通道  : " + (("code=%s" % login_code) if login_code
                               else "复用 code=%s" % api_code))
    else:
        print("预警接口  : (未配置 api_code, 不推送)")
    print("webhook   : " + (cfg.get("webhook") or "(未配置)"))
    print("推送日志  : " + (cfg.get("notify_log") or "(未启用)"))
    return 0


def do_check(cfg: dict, paths: Paths, hash_cache: Dict[str, dict],
             prev_snapshot=None, quiet: bool = False, as_json: bool = False,
             extra_alerts: Optional[List[Alert]] = None,
             wl: Optional[dict] = None) -> Tuple[List[Alert], tuple]:
    if wl is None:
        wl = load_wl(paths.whitelist)
    state = load_state(paths.state)
    alerts, snapshot = gather_alerts(cfg, wl, hash_cache, prev_snapshot)
    if extra_alerts:
        alerts.extend(extra_alerts)
    # 账号检测需先于登录检测: 首次运行会静默建立账号基线
    ident_alerts, wl_dirty = gather_identity_alerts(cfg, wl, state)
    alerts.extend(ident_alerts)
    alerts.extend(gather_login_alerts(cfg, state, wl))
    alerts.sort(key=lambda a: (SEV_RANK.get(a.sev, 9), a.pid))
    fresh = dedupe(alerts, state, cfg, time.time())
    # 提示级告警不实时推送: 存入缓存, 到当日汇总时刻合并成一条
    live, deferred = split_info_digest(cfg, fresh)
    if deferred:
        state["pending_info"] = append_pending(state.get("pending_info") or [],
                                               deferred)
    digest = flush_info_digest(cfg, state, time.time())
    state["last_check"] = now_iso()
    save_json(paths.state, state)
    if wl_dirty:
        save_json(paths.whitelist, wl)

    if not quiet:
        if as_json:
            print(json.dumps({"time": now_iso(), "host": host_name(cfg),
                              "alerts": [alert_to_dict(a) for a in fresh]},
                             ensure_ascii=False, indent=2))
        elif fresh:
            color = sys.stdout.isatty()
            print("===== %s  %s  新增异常 %d 个 =====" %
                  (now_iso(), host_name(cfg), len(fresh)))
            for a in fresh:
                print(format_alert(a, color))
                print("-" * 60)
    if fresh:
        write_log(paths.log, fresh)
    if live:
        notify(cfg, live)
    if digest:
        write_log(paths.log, digest)
        notify(cfg, digest)
    if not fresh and not quiet and not as_json:
        print("[%s] 巡检完成, 未发现异常" % now_iso())
    return fresh, snapshot


def cmd_check(args, cfg: dict, paths: Paths) -> int:
    paths.ensure()
    hash_cache = load_json(paths.hashcache, {}) or {}
    quiet = not args.json and not sys.stdout.isatty() and args.cron
    try:
        fresh, _ = do_check(cfg, paths, hash_cache, quiet=quiet, as_json=args.json)
    finally:
        save_json(paths.hashcache, hash_cache)
    bad = [a for a in fresh if a.sev in ("high", "medium")]
    return 1 if bad else 0


def cmd_watch(args, cfg: dict, paths: Paths) -> int:
    paths.ensure()
    if os.geteuid() != 0:
        sys.stderr.write("警告: 非 root 运行, 其他用户的进程可能无法完整读取\n")
    interval = float(args.interval or cfg.get("interval") or 30)
    hash_cache = load_json(paths.hashcache, {}) or {}
    snapshot = None

    short_on = bool(cfg.get("monitor_short_proc", True))
    events: "queue.Queue" = queue.Queue(maxsize=2000)
    stop = threading.Event()
    if short_on:
        threading.Thread(
            target=cn_proc_worker, args=(events, stop),
            kwargs={"warn": lambda m: sys.stderr.write(m + "\n")},
            name="cn-proc", daemon=True).start()
        sys.stderr.write("短命进程监听: 开 (cn_proc exec 事件)\n")
    else:
        sys.stderr.write("短命进程监听: 关\n")
    sys.stderr.write("procmon watch 启动, 间隔 %ss\n" % interval)
    try:
        while True:
            try:
                wl = load_wl(paths.whitelist)
                extra = (collect_short_alerts(cfg, wl, events)
                         if short_on else [])
                _, snapshot = do_check(cfg, paths, hash_cache,
                                       prev_snapshot=snapshot,
                                       extra_alerts=extra, wl=wl)
                save_json(paths.hashcache, hash_cache)
            except KeyboardInterrupt:
                raise
            except Exception as e:  # noqa: BLE001 单轮异常不退出
                sys.stderr.write("巡检异常: %s\n" % e)
            time.sleep(interval)
    except KeyboardInterrupt:
        return 0
    finally:
        stop.set()


# --------------------------------------------------------------------------- #
# main
# --------------------------------------------------------------------------- #
def build_parser() -> argparse.ArgumentParser:
    common = argparse.ArgumentParser(add_help=False)
    common.add_argument("--home", default=os.environ.get("PROCMON_HOME", DEF_HOME),
                        help="数据目录 (默认 %s)" % DEF_HOME)
    common.add_argument("--config", default=os.environ.get("PROCMON_CONFIG", DEF_CONFIG),
                        help="配置文件 (默认 %s)" % DEF_CONFIG)
    common.add_argument("--log", default=os.environ.get("PROCMON_LOG", DEF_LOG),
                        help="告警日志 (默认 %s)" % DEF_LOG)

    ap = argparse.ArgumentParser(
        prog="procmon.py", description="Linux 进程异常监控 + 服务器登录检测(白名单基线预警)")
    sub = ap.add_subparsers(dest="cmd")

    p = sub.add_parser("init", parents=[common], help="初始化配置并采集基线")
    p.add_argument("--force", action="store_true", help="白名单已存在时仍追加")
    p.set_defaults(func=cmd_init)

    p = sub.add_parser("check", parents=[common], help="单次巡检")
    p.add_argument("--json", action="store_true", help="输出 JSON")
    p.add_argument("--cron", action="store_true", help="非终端时静默(仅告警/日志)")
    p.set_defaults(func=cmd_check)

    p = sub.add_parser("watch", parents=[common], help="常驻巡检")
    p.add_argument("--interval", type=float, default=None, help="巡检间隔秒")
    p.set_defaults(func=cmd_watch)

    p = sub.add_parser("learn", parents=[common], help="查看/补录未登记进程")
    p.add_argument("--commit", action="store_true", help="写入白名单")
    p.add_argument("--from-alerts", action="store_true",
                   help="从告警流水(alerts.jsonl)取待补录主键, 用于短命进程")
    p.add_argument("--hours", type=float, default=168.0,
                   help="--from-alerts 的时间窗(小时), 默认 168")
    p.add_argument("--allow-high", action="store_true",
                   help="连高危告警的主键一起补录(默认跳过, 高危多为真问题)")
    p.add_argument("--select", action="store_true",
                   help="交互选择要补录的条目(隐含 --commit, 需终端)")
    p.set_defaults(func=cmd_learn)

    p = sub.add_parser("accounts", parents=[common], help="查看/重建登录账号基线")
    p.add_argument("--commit", action="store_true", help="以当前系统账号重建基线")
    p.set_defaults(func=cmd_accounts)

    p = sub.add_parser("refresh-hash", parents=[common], help="刷新白名单 exe 哈希")
    p.set_defaults(func=cmd_refresh_hash)

    p = sub.add_parser("show", parents=[common], help="查看白名单")
    p.add_argument("--grep", default="", help="按关键字过滤")
    p.set_defaults(func=cmd_show)

    p = sub.add_parser("status", parents=[common], help="查看运行状态")
    p.set_defaults(func=cmd_status)

    return ap


def main(argv=None) -> int:
    # systemd 下 stdout 是管道, 默认块缓冲会导致 journalctl 长时间看不到巡检输出
    try:
        sys.stdout.reconfigure(line_buffering=True)
    except (AttributeError, ValueError):
        pass
    ap = build_parser()
    args = ap.parse_args(argv)
    if not getattr(args, "cmd", None):
        ap.print_help()
        return 2
    paths = Paths(args.home, args.config, args.log)
    cfg = load_config(paths.config)
    return args.func(args, cfg, paths) or 0


if __name__ == "__main__":
    sys.exit(main())
