karna244/Cyberpunk-keylogger-detector
GitHub: karna244/Cyberpunk-keylogger-detector
一个基于Python的跨平台键记录器检测工具,通过进程分析和风险评分监控键盘活动,用于防御恶意软件威胁。
Stars: 0 | Forks: 0
# 键盘记录器检测工具
### 实习安全项目 — v1.0
一款跨平台的 Python 工具,用于检测可能表明键盘记录器或 RAT 活动的可疑进程、键盘钩子以及隐藏进程。
## 项目结构
```
keylogger_detector/
├── main.py ← Entry point (interactive + headless modes)
├── detector.py ← Core detection engine
├── alert_system.py ← Alert logging & report generation
├── dashboard.py ← Terminal UI (colorized)
├── requirements.txt ← Dependencies
├── reports/ ← Saved scan reports (auto-created)
└── alerts.log ← Running alert log (auto-created)
```
## 功能特性
| 特性 | 描述 |
|------|------|
| **进程风险评分** | 每个进程的10因素评分引擎(0-100) |
| **可疑名称检测** | 标记已知键盘记录器/RAT 名称和关键词 |
| **路径启发式** | 检测从 Temp/Roaming 目录运行的进程 |
| **键盘钩子检测** | Linux: 通过 lsof 检查 /dev/input;Windows: 模块检查 |
| **隐藏进程检测** | 交叉比对 psutil ↔ /proc (Linux) 或 tasklist (Windows) |
| **网络分析** | 标记非标准端口上的连接 |
| **权限检查** | 标记未知的 SYSTEM/root 进程 |
| **实时监控** | 后台线程以可配置的间隔重新扫描 |
| **警报系统** | 内存中的环形缓冲区 + 持久日志文件 |
| **报告** | JSON + 纯文本报告导出 |
| **终端仪表盘** | 彩色化、交互式、菜单驱动的 UI |
| **无头模式** | `--scan` 标志用于 CI/cron 集成 |
## 快速开始
### 1. 安装依赖
```
pip install psutil colorama
# 仅限 Windows(用于更深层的钩子检测):
pip install pywin32
```
### 2. 运行交互式仪表盘
```
python main.py
```
### 3. 运行无头扫描(CI / cron)
```
python main.py --scan
# Exit code 0 = 干净,Exit code 1 = 发现 HIGH/CRITICAL 威胁
```
## 风险评分细分
| 评分 | 等级 | 含义 |
|------|------|------|
| 70-100 | 🔴 危险 | 强烈的键盘记录器指标;需立即调查 |
| 45-69 | 🟠 高危 | 多个可疑信号 |
| 20-44 | 🟡 中等 | 存在一些值得审查的异常 |
| 0-19 | 🟢 低危 | 正常行为 |
### 评分因素
| 因素 | 分数 | 触发条件 |
|------|------|----------|
| 可疑名称关键词 | +35 | 名称/命令行匹配已知键盘记录器模式 |
| 可疑可执行文件路径 | +25 | 从 Temp、AppData\Roaming、/tmp 等运行 |
| 无可执行文件路径 | +20 | 未找到 exe(可能为注入) |
| 权限高但未知 | +20 | SYSTEM/root 进程,但非已知系统进程 |
| 异常文件扩展名 | +20 | .vbs、.scr、.pif、.bat |
| 加载了钩子 DLL (Win) | +30 | DLL 名称包含 "hook"、"keybd"、"input" |
| 高 CPU 占用 | +15 | CPU > 40%(键盘记录器轮询输入) |
| 特殊端口连接 | +15 | 非标准端口上有2个以上连接 |
| 高打开文件描述符数 | +10 | 文件描述符 > 200 |
## 架构
```
main.py
├── Dashboard (dashboard.py) ← Interactive TUI
│ └── KeyloggerDetector
│ ├── _score_process() ← Per-process risk scoring
│ ├── detect_hooks() ← /dev/input or Windows API
│ ├── detect_hidden() ← psutil ↔ OS discrepancy
│ └── run_scan() ← Full scan orchestrator
└── AlertSystem (alert_system.py)
├── log_alert() ← Write to alerts.log
├── save_json_report() ← reports/scan_*.json
└── save_text_report() ← reports/scan_*.txt
```
## 平台说明
### Linux
- 钩子检测使用 `lsof` 检查 `/dev/input/event*` 设备
- 隐藏进程检测比较 `/proc` PID 与 psutil 结果
- 内核线程(kworker、rcu_* 等)被加入白名单以防止误报
### Windows
- 钩子检测通过 `memory_maps()` 检查加载的 DLL 名称
- 隐藏进程检测比较 `tasklist` 输出与 psutil 结果
- 要完整枚举 SetWindowsHookEx,需用 `ctypes` + `pywin32` 扩展
- 以管理员身份运行以获得完整的进程访问权限
## 使用 Cron 自动化(Linux)
```
# 每 30 分钟运行一次,记录结果
*/30 * * * * /usr/bin/python3 /path/to/keylogger_detector/main.py --scan >> /var/log/kld.log 2>&1
```
## 任务计划程序(Windows)
```
schtasks /create /tn "KeyloggerScan" /tr "python C:\tools\keylogger_detector\main.py --scan" /sc minute /mo 30
```
## 扩展工具
### 添加新的检测启发式规则
在 `detector.py` 的 `_score_process()` 函数内,添加新的评分代码块:
```
# 示例:标记无父进程的进程(孤儿进程)
try:
if proc.ppid() == 0 and proc.name() not in TRUSTED_SYSTEM_PROCS:
score += 15
reasons.append("Orphaned process (no parent)")
except psutil.AccessDenied:
pass
```
### 添加新的警报输出(例如电子邮件)
在 `alert_system.py` 中,扩展 `log_alert()` 函数:
```
import smtplib
def _send_email_alert(self, alert):
if alert.risk_level == "CRITICAL":
# send email ...
pass
```
## 涵盖的学习概念
- 使用 psutil 的**进程枚举**
- 基于启发式的**恶意软件检测**(无需签名)
- **键盘钩子理论** — Windows SetWindowsHookEx,Linux /dev/input
- 通过操作系统级与 API 级差异进行** Rootkit 检测**
- 实际终端安全工具中使用的**风险评分系统**
- **跨平台 Python** 安全工具开发
- 使用 colorama 的**终端用户界面**
- **日志记录和报告**设计模式
## 免责声明
本工具仅用于**教育和防御性安全目的**。
在您不拥有的系统上运行前,请务必获得适当授权。
该工具本身不安装任何钩子——它只进行读取和分析。
标签:AMSI绕过, WSL, 威胁检测, 子域名枚举, 无头模式, 系统安全, 终端仪表盘, 网络安全, 警报系统, 输入捕获检测, 进程扫描, 进程风险评估, 逆向工具, 键盘活动监控, 键盘记录器, 隐私保护, 风险评分