r0ms3c/aisecpulse
GitHub: r0ms3c/aisecpulse
AiSecPulse 是一个基于 Python 的 AI 安全检测平台,通过规则引擎与异常检测的分层架构识别聊天和 Agent 系统中的提示注入、数据泄露及越权操作等威胁。
Stars: 0 | Forks: 1
# AiSecPulse
[](https://python.org)
[](LICENSE)
[]()
[]()
[](https://owasp.org/www-project-top-10-for-large-language-model-applications/)
[](https://atlas.mitre.org/)
[](https://r0ms3c.github.io/aisecpulse/)
## 演示
- ✅ **[查看实时检测报告](https://r0ms3c.github.io/aisecpulse/demo/report.html)**
演示展示了 pipeline 处理完所有 96 个事件后生成的实际 HTML 报告——包括 pipeline 摘要、按级别划分的严重程度分布、按方法(基于规则、异常或两者兼有)划分的检测分布,以及包含每个警报分数、检测类型和触发规则的完整警报表。
## 概述
现代企业正在迅速采用 AI 系统——聊天机器人、copilot 和自主 agent。这些系统引入了新的攻击面,而传统安全工具并非专为监控这些攻击面而构建。
**AiSecPulse** 是一个受生产环境启发的检测平台,充当监控 AI 交互的集中式引擎。它处理来自聊天和 agentic AI 系统的事件,提取风险特征,应用分层检测逻辑,并生成带有严重性评分的结构化警报——模拟真实的 SOC 团队如何进行 AI 安全监控。
这个项目不仅仅是代码。每一条检测规则、每一个特征、以及每一个架构决策,都可以映射到下方框架中记录的某个特定攻击向量。
## 研究基础
本项目建立在三个权威的 AI 安全框架之上:
| 框架 | 范围 | 与本项目的关联性 |
|---|---|---|
| [OWASP LLM Top 10](https://owasp.org/www-project-top-10-for-large-language-model-applications/) | LLM 应用的 Top 10 风险 | 定义了要检测的攻击向量:LLM01 Prompt Injection、LLM02 Sensitive Information Disclosure、LLM06 Excessive Agency、LLM07 System Prompt Leakage |
| [OWASP Agentic skills Top 10](https://owasp.org/www-project-agentic-skills-top-10/) | 针对 agentic AI 系统的特定威胁 | 为 agent 检测层提供支持——动作验证、爆炸半径、提权模式 |
| [MITRE ATLAS](https://atlas.mitre.org/) | AI 系统的对抗性威胁全景 | 为特征工程和混淆检测提供支持——ATLAS 知识库中记录的规避技术 |
### 覆盖的 OWASP LLM 风险
| # | 风险 | 覆盖范围 |
|---|---|---|
| LLM01 | Prompt Injection | 完全覆盖——关键词检测、指令密度、混淆 |
| LLM02 | Sensitive Information Disclosure | 部分覆盖——agent 动作中的数据泄露模式 |
| LLM06 | Excessive Agency | 完全覆盖——危险动作检测、提权 |
| LLM07 | System Prompt Leakage | 部分覆盖——针对系统 prompt 提取的关键词模式 |
| LLM03-LLM05, LLM08-LLM10 | 其他风险 | 不在 v1 范围内——列为 v2 的增强功能 |
## 检测目标
| 威胁 | 事件类型 | OWASP / MITRE 参考 |
|---|---|---|
| Prompt Injection | Chat + Agent | LLM01 |
| Jailbreak 和角色覆盖 | Chat | LLM01 |
| System Prompt 提取 | Chat | LLM07 |
| 社会工程学 | Chat | LLM01, ATLAS AML.T0054 |
| 混淆(base64, 零宽字符) | Chat | ATLAS AML.T0054.000 |
| 数据泄露 | Agent | LLM02, LLM06 |
| 提权 | Agent | LLM06 |
| 破坏性动作 | Agent | LLM06 |
## 架构
```
data/sample_events.json
|
v
+------------------------+
| ETL Pipeline | etl/ingest.py, etl/normalize.py
+------------------------+
|
v
+------------------------+
| Feature Extraction | features/extractor.py
+------------------------+
|
v
+------------------------------------------------+
| Detection Engine |
| detectors/rules.py (rule-based) |
| detectors/anomaly.py (isolation forest) |
| detectors/scorer.py (weighted combiner) |
+------------------------------------------------+
|
v
+------------------------+
| Alerts + Report | alerts/alerting.py, reports/generator.py
+------------------------+
```
## 检测引擎
每个事件都会经过三层检测,并最终组合成一个单一的综合风险评分。
### 第 1 层 —— 基于规则 (detectors/rules.py)
使用关键词模式、动作黑名单和结构性启发式算法进行快速、确定性的检测。针对已知的攻击特征触发。高精度,零延迟。
### 第 2 层 —— 异常检测 (detectors/anomaly.py)
在完整特征矩阵上训练的无监督 Isolation Forest。学习正常流量的特征,然后标记统计偏差。能够捕获任何规则尚未覆盖的新型攻击。
### 第 3 层 —— 评分器 (detectors/scorer.py)
将两种信号加权组合为一个最终得分:
```
final_score = (rule_score x 0.65) + (anomaly_score x 0.35)
```
严重性分类:
```
0.00 - 0.39 -> LOW logged silently
0.40 - 0.69 -> MEDIUM logged with warning
0.70 - 0.89 -> HIGH alert raised
0.90 - 1.00 -> CRITICAL alert raised and flagged
```
## 项目结构
```
aisecpulse/
|-- README.md
|-- requirements.txt
|-- main.py # Entry point -- runs the full pipeline
|-- config.yaml # All thresholds, weights, keywords, paths
|-- data/
| +-- sample_events.json # 96 labelled events -- included in repo
|-- etl/
| |-- ingest.py # Load and parse raw events
| +-- normalize.py # Validate schema, clean and type events
|-- features/
| +-- extractor.py # Feature engineering -- 7 features per event
|-- detectors/
| |-- rules.py # Rule-based detection layer
| |-- anomaly.py # Isolation Forest anomaly detection
| +-- scorer.py # Weighted score combiner + severity classifier
|-- alerts/
| +-- alerting.py # Alert generation and enrichment
|-- logs/
| +-- detections.log # Runtime log (generated, not committed)
+-- reports/
|-- generator.py # HTML report builder
+-- report.html # Generated report (run pipeline to produce)
```
## 数据集
本仓库已包含 `data/sample_events.json`——无需从外部下载。
它包含 **96 个完全标记的合成事件**,没有真实用户或敏感信息。克隆后即可立即运行。
| 划分 | 数量 | 描述 |
|---|---|---|
| Normal | 55 | 合法的聊天查询和常规的 agent 动作 |
| Injection | 41 | Prompt injection、jailbreak、数据泄露、破坏性动作 |
| Chat 事件 | 58 | 人与 AI 的交互 |
| Agent 事件 | 38 | AI 到 API / 动作的执行 |
**数据集中的攻击类别:**
- 经典注入(忽略所有之前的指令、DAN、角色覆盖)
- 社会工程学(奶奶骗局、虚构情景设定、角色扮演绕过)
- 混淆(base64 编码的 payload、零宽 unicode 字符分割)
- Agentic 攻击(文件泄露、破坏性 SQL、提权、远程代码执行)
要扩展数据集,请遵循 `etl/normalize.py` 中的统一事件 schema,并将事件添加到 `data/sample_events.json`。
## 快速开始
```
# 1. Clone the repository
git clone https://github.com/r0ms3c/aisecpulse.git
cd aisecpulse
# 2. Create a virtual environment
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# 3. Install dependencies
pip install -r requirements.txt
# 4. Run the detection pipeline
python3 main.py
```
pipeline 将处理 `data/sample_events.json`,运行所有检测层,在终端打印警报,将日志写入 `logs/detections.log`,并生成 `reports/report.html`。
## 环境要求
```
Python 3.10+
scikit-learn
pandas
pyyaml
loguru
```
## 示例输出
运行 `python3 main.py` 会产生如下输出:
```
2025-05-01 10:00:00 | INFO | AiSecPulse -- AI Security Detection Pipeline
2025-05-01 10:00:00 | INFO | Phase 1 complete (96 events loaded)
2025-05-01 10:00:00 | INFO | Phase 2 complete (96 feature vectors extracted)
2025-05-01 10:00:00 | INFO | Rules -- 40/96 flagged
2025-05-01 10:00:00 | INFO | Anomaly -- 17/96 flagged
2025-05-01 10:00:00 | INFO | Scoring -- LOW=56 | MEDIUM=1 | HIGH=37 | CRITICAL=2
2025-05-01 10:00:00 | WARNING | [CRITICAL] agent | agent_305 | score=1.0
rules=keyword_hit + dangerous_action + instruction_density
2025-05-01 10:00:00 | WARNING | [CRITICAL] chat | user_114 | score=0.983
type=rule + anomaly
2025-05-01 10:00:00 | INFO | Pipeline complete -- 39 alerts raised
2025-05-01 10:00:00 | INFO | Report -> reports/report.html
```
查看完整的交互式报告:**[实时演示](https://r0ms3c.github.io/aisecpulse/)**
## 事件 Schema
平台处理的每个事件都遵循以下统一 schema:
```
{
"timestamp" : "2025-05-01T10:00:00Z",
"source" : "sample",
"type" : "chat | agent",
"user_id" : "user_001",
"prompt" : "...",
"response" : "...",
"action" : null,
"label" : "normal | injection"
}
```
`action` 仅在 agent 事件中填充。Chat 事件的该字段始终为 `null`。
## 设计决策
**为什么选择规则 + 异常检测,而不是分类器?**
有监督的分类器需要标注过的训练数据、训练/测试集划分,并且随着攻击手法的演变需要持续重新训练。规则 + Isolation Forest 无需训练数据即可实现强大的检测能力——规则以确定性的方式处理已知模式,异常检测则以统计的方式处理未知模式。分类器已被列为 v2 的增强功能。
**为什么采用配置驱动?**
每个阈值、关键词和权重都保存在 `config.yaml` 中。没有任何内容是硬编码的。您可以调整整个检测行为——添加关键词、调整严重性阈值、重新加权评分器——而无需修改任何一行 Python 代码。
**为什么要分离 ETL、特征和检测器?**
每一层都有单一的职责。ETL 产出清洁的事件。特征产出数值向量。检测器消费向量。这使得每个组件都可以独立测试和替换——无需触动 ETL 或告警模块,即可将 Isolation Forest 替换为 transformer 模型。
## 计划的增强功能 (v2)
- [ ] 有监督的 ML 分类器(fine-tuned 的 sentence transformer)
- [ ] 通过 RAG/检索上下文进行的间接 prompt injection
- [ ] LLM05 —— 不当输出处理检测(响应分析)
- [ ] LLM10 —— 无限制消耗检测(token 耗尽模式)
- [ ] 实时流处理模式
- [ ] SIEM 集成(Splunk / Elastic 导出格式)
- [ ] 用于 pipeline 集成的 REST API 包装器
## 参考
- [OWASP Top 10 for LLM Applications](https://owasp.org/www-project-top-10-for-large-language-model-applications/)
- [OWASP Agentic AI Threats](https://owasp.org/www-project-agentic-skills-top-10/)
- [MITRE ATLAS -- Adversarial Threat Landscape for AI Systems](https://atlas.mitre.org/)
## 作者
**r0ms3c** -- 安全工程师
[GitHub](https://github.com/r0ms3c)
## 许可证
MIT 许可证——详情请参阅 [LICENSE](LICENSE)。
标签:AI安全, AMSI绕过, Chat Copilot, Python, SOC监控, 威胁检测, 恶意代码分类, 提示词注入检测, 无后门, 逆向工具, 零日漏洞检测