mailtocsprasad/ai-kd

GitHub: mailtocsprasad/ai-kd

基于 Claude 的 WinDbg 扩展,通过 AI 自主驱动调试命令实现 Windows 崩溃转储的自动化根因分析。

Stars: 0 | Forks: 0

# AI-KD — AI 增强内核调试器 ![tests](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/d1ffc51927055528.svg) ## 模式 ### 模式 1 — CLI(离线,无 WinDbg GUI) 将 AI-KD 指向一个 `.DMP` 文件。它会启动一个单独的 `cdb.exe` 会话 — 符号 仅加载一次,随后 Claude 以全速根据需要运行任意数量的命令: ``` python -m ai_kd_extension analyze samples/MEMORY.DMP ``` ``` [*] Dump : samples/MEMORY.DMP [*] cdb.exe: C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\cdb.exe [*] Model : claude-sonnet-4-6 [*] Max iterations: 25 [*] Opening cdb.exe session (symbols load once here)... [AI-KD] Running: !analyze -v [AI-KD] Reason: Initial triage — get bugcheck code and faulting IP [AI-KD] Running: k 30 [AI-KD] Reason: Walk the call stack to identify the faulting frame [AI-KD] Running: u myfault+0x12f0 L20 [AI-KD] Reason: Disassemble faulting offset to identify the paged access [AI-KD] Root-cause analysis: ============================================================ Root cause: myfault.sys allocates from PagedPool then raises IRQL to DISPATCH_LEVEL before dereferencing the pointer in an unbounded loop. Any non-resident page causes the 0xD1 bugcheck. Remediation: use NonPagedPoolNx, or complete all paged-memory operations before raising IRQL. Wrap IRQL elevation in an RAII scope guard. ============================================================ ``` ### 模式 2 — WinDbg 扩展(实时会话,使用 pykd) ``` .load pykd .scriptload C:\path\to\ai_kd_extension\main.py ``` Claude 运行相同的代理循环,通过 pykd 对当前加载的崩溃转储执行命令 — 无需 CLI。 ## 架构 ``` ┌─────────────────────────────────────────────────────────────────┐ │ Two Executor Paths │ │ │ │ CLI mode WinDbg extension mode │ │ ───────────────────── ──────────────────────── │ │ __main__.py (argparse) main.py (WinDbg entry point) │ │ │ │ │ │ ▼ ▼ │ │ analyze_dump.py start_assistant() │ │ │ │ │ │ ▼ ▼ │ │ dump_executor.py probe.py │ │ CdbSession (persistent pykd.dbgCommand() │ │ cdb.exe — symbols load once) │ │ │ │ │ │ └──────────────┬───────────────┘ │ │ ▼ │ │ agent.py ←──── sanitiser.py │ │ (Claude API + tool-calling loop) │ │ │ │ │ ▼ │ │ AnalysisResult { final_text, tool_call_log } │ └─────────────────────────────────────────────────────────────────┘ │ HTTPS/TLS ▼ ┌─────────────────┐ │ Anthropic API │ │ claude-sonnet │ └─────────────────┘ ``` **核心组件:** | 文件 | 角色 | |---|---| | `agent.py` | Claude API 客户端 + 可编程工具调用循环 | | `sanitiser.py` | 在将输出发送给 Claude 之前剥离内核地址 | | `probe.py` | WinDbg 执行器 — 封装 `pykd.dbgCommand()` | | `dump_executor.py` | 离线执行器 — `CdbSession`(持久) + `make_dump_executor`(单次) | | `main.py` | WinDbg 扩展入口点 | | `analyze_dump.py` | CLI 编排器 — 为整个分析过程开启一个 `CdbSession` | | `__main__.py` | CLI 路由器 (`python -m ai_kd_extension analyze ...`) | 不会预加载任何崩溃转储内容。Claude 通过工具调用驱动所有信息收集。 ## 需求 ### CLI 模式(离线) - Windows 10/11 x64 - Python 3.9+ - **Windows Debugger Tools** (`cdb.exe`) — 随 Windows SDK 或 WinDbg Preview 安装 - 自动从 `C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\` 检测 - 或将 `AIKD_CDB_PATH` 设置为确切路径 - **Anthropic API 密钥** — 设置 `ANTHROPIC_API_KEY` 或在项目根目录创建 `Claude-Key.txt` ### WinDbg 扩展模式 除上述内容外,还需: - 带有 pykd 扩展的 WinDbg(`pip install pykd`) ## 安装 ``` # 完整安装 — WinDbg (通过 winget) + Python deps .\install.ps1 # 仅限开发/测试 — 无需 WinDbg,所有测试均使用 mocks 运行 .\install.ps1 -DevOnly ``` ## 用法 ### CLI — 离线转储分析 ``` # 基础 python -m ai_kd_extension analyze samples/MEMORY.DMP # 针对复杂 dump 增加迭代次数 python -m ai_kd_extension analyze C:\dumps\crash.dmp --max-iterations 30 # 运行时打印每条命令 + 输出大小 python -m ai_kd_extension analyze samples/MEMORY.DMP --verbose # 覆盖 cdb.exe 路径 python -m ai_kd_extension analyze samples/MEMORY.DMP --cdb "C:\DbgTools\cdb.exe" ``` ``` python -m ai_kd_extension analyze --help positional arguments: DUMP Path to the .DMP file options: --max-iterations N Maximum WinDbg tool calls Claude may make (default: 25) --cdb PATH Path to cdb.exe (auto-detected if omitted) --timeout SECS Per-command cdb.exe timeout in seconds (default: 180) --verbose, -v Print each command and output size as it runs ``` ### WinDbg 扩展 ``` .load pykd .scriptload C:\path\to\ai_kd_extension\main.py ``` ## 测试 单元/集成测试不需要 WinDbg — pykd 已完全模拟。 ``` # 单元 + 集成测试 (无 API key,无 WinDbg) pytest -v # Live API 测试 — 真实 Anthropic 调用,executor 为 mock pytest tests/test_agent_live.py -m live -v # E2E dump 测试 — 真实 cdb.exe + 真实 Claude API + 真实 .DMP 文件 pytest tests/test_e2e_dump.py -m e2e_windbg -v -s # 跳过所有 live/E2E 测试 pytest -m "not live and not e2e_windbg" ``` ## 状态 - [x] sanitiser — 已测试,100% 无 pykd 依赖 - [x] 代理循环 (agent loop) — 使用模拟 Claude API 测试;已验证并行工具调用处理 - [x] 集成试运行 (integration dry-run) — 带有模拟崩溃转储的完整流水线 - [x] 实时 API 测试 — 真实 Anthropic 调用,执行器已模拟;15/15 通过 - [x] 探测层 (probe layer) — 接口忠实 (interface-faithful) 的 `fake_pykd` 桩 (stub);25 项测试,已验证 pykd 契约 - [x] **离线 CLI 模式** — 通过 `cdb.exe` 执行 `python -m ai_kd_extension analyze ` - [x] **持久 `CdbSession`** — 符号在启动时加载一次;此后所有命令执行快速 - [x] **E2E 转储测试** — 真实 `cdb.exe` + 真实 Claude + `samples/MEMORY.DMP`;9/9 通过 - [x] 示例转储 — `samples/` 包含真实的 NotMyfault 小内存转储,可立即试用 - [ ] WinDbg 扩展实时验证 — 针对真实转储的完整 pykd 会话(受限于 WinDbg 环境) ## 许可证 MIT
标签:BSOD, CDB, Claude, CVE检测, DLL 劫持, pykd, Python, WinDbg, Windows, Windows调试, 云安全监控, 人工智能, 内核调试, 大语言模型, 崩溃转储分析, 故障排查, 无后门, 根因分析, 用户模式Hook绕过, 网络安全监控, 自动化分类, 蓝屏分析, 运维自动化, 逆向工具, 静态分析, 驱动开发