cedricbonhomme/pyHIDS

GitHub: cedricbonhomme/pyHIDS

一个用 Python 编写的轻量级主机入侵检测系统,通过文件哈希校验和外部威胁情报集成来监控系统完整性并发出告警。

Stars: 60 | Forks: 15

## pyHIDS ### 简介 [pyHIDS](https://github.com/cedricbonhomme/pyHIDS) 是一个 [HIDS](https://en.wikipedia.org/wiki/Host-based_intrusion_detection_system) (基于主机的入侵检测系统),用于验证系统的完整性。 建议使用 Python >= 3.11。 ### 功能特性 * 使用规则列表检查系统文件的完整性; * 检查命令的输出(*iptables* 等); * 可以使用 RSA 签名来检查其数据库的完整性; * 警报会写入系统日志; * 警报可以通过电子邮件发送给用户列表; * 警报可以通过 [irker](https://gitlab.com/esr/irker) IRC 客户端发送到 IRC 频道(该客户端应作为 守护进程运行); * 使用 [Hashlookup](https://github.com/hashlookup)、 [Pandora](https://github.com/pandora-analysis)、 [MISP](https://github.com/MISP) 和 [YARA](https://github.com/virustotal/yara) 验证文件; * 可以将数据库导出为 Bloom 过滤器或 Cuckoo 过滤器。 您可以定义规则来指定要检查的文件。 ### 安装 您可以使用 [pipx](https://pypa.github.io/pipx)。 ``` $ pipx install pyHIDS $ export PYHIDS_CONFIG=~/.pyHIDS/conf.cfg ``` 这里提供了一个配置文件的[示例](./conf.cfg-sample)。 通过此文件您可以配置: - 与 Hashlookup、Pandora、MISP 和 YARA 的集成; - 用于通知的 IRC 连接; - 用于电子邮件通知的 SMTP 连接; - 要扫描的文件列表; - 用于指定文件夹中要扫描的文件的正则表达式; - 要检查的命令输出。 ### 用法 ``` $ pyhids gen-keys --size 2048 Generating 2048 bits RSA keys… Dumping Keys Done. $ pyhids gen-base --sign Generating database… 2427 files in the database. $ pyhids run --check-signature Verifying the integrity of the base of hashes… Database integrity verified. Verifying the integrity of the files… [12/10/23 21:35:26] Error(s) : 0 [12/10/23 21:35:26] Warning(s) : 0 [12/10/23 21:35:26] HIDS finished. ``` 如果您不想使用 pyHIDS 提供的解决方案(RSA)对数据库进行签名,或者您根本不想对数据库进行签名,则可以跳过第一步(生成密钥)。 修改一个被监控的文件并重新运行程序: ``` $ pyhids run Verifying the integrity of the files… [12/10/23 14:41:51] [warning] /bin/cifsdd changed. ``` 程序会警告文件已更改。当这种情况发生时,系统日志中会生成一条警告,并向管理员发送一封电子邮件。如果未检测到更改,则仅更新日志文件。 生成的日志文件: ``` $ tail var/log [09/10/23 14:41:51] [notice] /bin/cifscreds ok [09/10/23 14:41:51] [notice] /bin/mbim-network ok [09/10/23 14:41:51] [notice] /bin/xclip ok [09/10/23 14:41:51] [notice] /bin/preparetips5 ok [09/10/23 14:41:51] [notice] /bin/pamperspective ok [12/10/23 14:41:51] [warning] /bin/cifsdd changed. [09/10/23 14:41:51] [notice] /bin/pod2usage ok [09/10/23 14:41:51] [notice] /bin/mkzftree ok [09/10/23 14:41:51] Error(s) : 0 [09/10/23 14:41:51] Warning(s) : 1 [09/10/23 14:41:51] HIDS finished. ``` 如果您想在 ``syslog`` 中查看日志,根据您的系统不同,有不同的选项: ``` $ journalctl --follow Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /etc/resolv.conf changed. Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/mdsearch changed. Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/smbcacls changed. Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/smbspool changed. Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/smbclient changed. Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/smbcquotas changed. Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/smbget changed. Oct 12 22:58:47 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/nmblookup changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:47] [warning] /bin/rpcclient changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:48] [warning] /bin/smbpasswd changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:48] [warning] /bin/dbwrap_tool changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:48] [warning] /bin/cifsdd changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:48] [warning] /bin/net changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:48] [warning] /bin/samba-regedit changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:48] [warning] /bin/testparm changed. Oct 12 22:58:48 debian pyhids[98135]: pyHIDS - [12/10/23 22:58:48] [warning] /bin/smbtree changed. ``` ``` $ journalctl --since="1 minute ago" ``` ``` $ tail -f /var/log/syslog ``` ### 其他功能 #### 使用外部工具进行检查 使用 Hashlookup、Pandora、MISP 或 YARA 检查已知的恶意文件。 ``` $ pyhids hashlookup $ pyhids pandora $ pyhids misp $ pyhids yara ``` #### 导出功能 ``` $ pyhids export --bloom-filter Bloom filter generated and stored: var/bloom/bloomfilter.bf ``` ``` $ pyhids export --cuckoo-filter Cuckoo filter generated and stored: var/cuckoo/cuckoofilter.cf ``` ### 自动执行 使用基于时间的作业调度器 Cron 来安排系统扫描。 在您的 shell 中输入以下命令: ``` $ crontab -e ``` 添加以下行以每五十分钟检查一次系统的完整性: ``` */50 * * * * pyhids run ``` 每次系统检查后,pyHIDS 会向管理员发送一份报告。 以防攻击者删除了 cron 行等情况。 ### 许可证 [pyHIDS](https://github.com/cedricbonhomme/pyHIDS) 采用 [GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt) 许可证。 版权所有 (C) 2010-2025 [Cédric Bonhomme](https://www.cedricbonhomme.org)
标签:AMSI绕过, DevSecOps, EDR, HIDS, Python安全工具, RSA签名, YARA, 上游代理, 主机入侵检测, 云资产可视化, 入侵检测系统, 告警系统, 威胁检测, 安全数据湖, 文件哈希, 系统加固, 网络安全, 脆弱性评估, 逆向工具, 隐私保护