OjasJogdand/static_malware_analysis
GitHub: OjasJogdand/static_malware_analysis
基于LangChain和OpenAI大模型的Windows可执行文件静态恶意软件分析管道,能自动提取二进制特征并生成专业分析报告。
Stars: 0 | Forks: 0
# 静态恶意软件分析模拟器
## 概述
这是一个使用 Python 3.11+ 和 LangChain 构建的静态恶意软件分析管道。它演示了如何使用静态分析技术和来自 OpenAI LLM 的 AI 洞察来分析 Windows 可执行文件。
## 功能
- **字符串提取** - 使用 Sysinternals `strings.exe` 从二进制文件中提取可读字符串
- **导入分析** - 使用 `dumpbin.exe` 解析导入地址表 (IAT)
- **智能预处理** - 检测可疑 API、URL、文件路径并推断其功能
- **阶段 1 LangChain Pipeline** - 使用 ChatOpenAI 生成结构化的 JSON 分析
- **阶段 2 LangChain Pipeline** - 将 JSON 转换为专业的网络安全报告
- **PDF 报告生成** - 生成具有专业格式的 PDF 报告
- **清晰的架构** - 模块化设计,关注点分离
## 架构
```
Static Malware Analysis Pipeline
│
├─► STAGE 0: Binary Extraction
│ ├─ Extract Strings (strings.exe)
│ └─ Extract IAT (dumpbin /imports)
│
├─► STAGE 0.5: Preprocessing
│ ├─ Detect Suspicious APIs
│ ├─ Extract URLs & Paths
│ ├─ Infer Capabilities
│ └─ Create Structured Payload
│
├─► STAGE 1: LangChain - JSON Analysis
│ ├─ Input: Structured preprocessed data
│ ├─ LLM Role: Senior Malware Analyst
│ └─ Output: analysis.json (strict schema)
│ {
│ "risk_level": "CRITICAL|HIGH|MEDIUM|LOW",
│ "capabilities": [...],
│ "suspicious_apis": [...],
│ "network_indicators": [...],
│ "file_operations": [...],
│ "summary": "...",
│ "confidence": "HIGH|MEDIUM|LOW"
│ }
│
└─► STAGE 2: LangChain - Report Generation
├─ Input: analysis.json
├─ LLM Role: Cybersecurity Analyst
└─ Output:
├─ report.txt (professional text report)
└─ report.pdf (formatted PDF report)
```
## 项目结构
```
safe-static-analysis/
│
├── main.py # Pipeline orchestration
├── requirements.txt # Python dependencies
├── .env # Local configuration (add your API key)
├── .env.example # Configuration template
├── .gitignore # Git ignore patterns
├── README.md # This file
│
├── sample/
│ ├── sample.cpp # Benign Windows program source
│ └── sample.exe # Compiled benign executable (user-created)
│
├── tools/
│ ├── extract_strings.py # Sysinternals strings.exe wrapper
│ ├── extract_iat.py # dumpbin /imports parser
│ ├── preprocess.py # Data normalization & API mapping
│ └── file_writer.py # Output writing utilities
│
├── chains/
│ ├── analysis_chain.py # Stage 1: LangChain JSON analysis
│ └── report_chain.py # Stage 2: LangChain report generation
│
├── prompts/
│ ├── analysis_prompt.txt # System prompt for JSON analysis
│ └── report_prompt.txt # System prompt for report generation
│
└── outputs/
├── analysis.json # Generated JSON analysis
├── report.txt # Generated text report
└── report.pdf # Generated PDF report
```
## 设置
### 前置条件
1. **Python 3.11+** - 从 [python.org](https://www.python.org) 下载
2. **Visual Studio Build Tools** - `dumpbin.exe` 所需
- 从 [visualstudio.com](https://visualstudio.microsoft.com/downloads/) 下载
- 确保已安装 "Desktop development with C++"
3. **Sysinternals strings.exe** - 从 [sysinternals.com](https://live.sysinternals.com/) 下载
4. **OpenAI API Key** - 从 [platform.openai.com](https://platform.openai.com/api-keys) 获取
### 安装说明
1. **克隆或下载此项目**
cd safe-static-analysis
2. **创建并激活 Python 虚拟环境**
# Windows
python -m venv venv
venv\Scripts\activate
# macOS/Linux
python3 -m venv venv
source venv/bin/activate
3. **安装依赖**
pip install -r requirements.txt
4. **配置环境**
# 复制 template 到 .env
copy .env.example .env
# 编辑 .env 并添加你的 OpenAI API key
# 如果 strings.exe 和 dumpbin.exe 不在 PATH 中,也请配置它们的路径
## 编译示例程序
项目在 `sample/sample.cpp` 中包含了一个安全的、良性的 C++ 程序。在运行管道之前,你必须将其编译为 `sample/sample.exe`。
### 选项 1:使用 Visual Studio Developer Command Prompt(推荐)
```
# 打开 Visual Studio Developer Command Prompt for VS [year]
cd sample
cl.exe sample.cpp kernel32.lib user32.lib
# 在同一目录中创建 sample.exe
```
### 选项 2:直接使用 cl.exe
```
# 从已在 PATH 中包含 cl.exe 的 Command Prompt 中
cd sample
cl.exe sample.cpp /link kernel32.lib user32.lib
```
### 选项 3:使用 Visual Studio IDE
1. 打开 Visual Studio
2. 创建一个新的 C++ 控制台应用程序项目
3. 将 `sample/sample.cpp` 的内容复制到主源文件中
4. 构建项目 (Build → Build Solution)
5. 将生成的 `.exe` 复制到 `sample/sample.exe`
## 运行 Pipeline
一旦你编译了 `sample/sample.exe` 并配置了 `.env`:
```
# 运行完整 pipeline
python main.py
# 或者指定自定义路径
python main.py path/to/custom.exe outputs
```
## 输出文件
运行管道后,请检查 `outputs/` 目录:
1. **analysis.json** - 包含以下内容的结构化 JSON:
- 风险等级评估
- 检测到的功能
- 可疑 API
- 网络指标
- 文件操作
- 分析师置信度
2. **report.txt** - 包含以下内容的专业文本报告:
- 执行摘要
- 二进制元数据
- 行为分析
- 可疑指标
- 网络分析
- 文件系统活动
- 最终结论
3. **report.pdf** - 格式专业的 PDF 版本报告
## 示例输出
### analysis.json
```
{
"risk_level": "LOW",
"capabilities": [
"Core Windows APIs",
"GUI interaction",
"File system interaction"
],
"suspicious_apis": [
"CreateFileA",
"MessageBoxA"
],
"network_indicators": [],
"file_operations": [
"C:\\sample_output.txt"
],
"summary": "Benign Windows application with basic file I/O and GUI capabilities. No malicious indicators detected.",
"confidence": "HIGH"
}
```
标签:AI安全, Chat Copilot, DAST, DLL 劫持, DNS 反向解析, dumpbin, IAT解析, IP 地址批量处理, JSON结构化分析, LangChain, OpenAI, PDF报告生成, Petitpotam, Python, Sysinternals, Windows可执行文件, 二进制分析, 云安全监控, 云安全运维, 云资产清单, 内存规避, 大语言模型, 威胁情报, 字符串提取, 导入表分析, 开发者工具, 恶意软件分析, 无后门, 网络信息收集, 网络安全, 网络安全报告, 自动化管道, 轻量级, 逆向工具, 逆向工程, 隐私保护, 静态分析