AI-Red-Teaming-Engine
防御目的の敵対的セキュリティ監査エンジン
コード・仕様書・AIエージェント設計を「攻撃者の視点」で読み直す Python 製ツール。 Semgrep / Gitleaks による静的解析 と LLM(Claude / Gemini)による文脈解析 を組み合わせ、 単体ツールより高い検出精度を実現します。
⚠️ プロトタイプ版です。全ての指摘は人間による最終確認が必要です。
特徴
| 機能 | 説明 |
|---|---|
| 🔍 ハイブリッド解析 | Semgrep + Gitleaks + LLM の3層で検出 |
| 📂 ディレクトリ一括監査 | --dir src/ で複数ファイルを再帰走査 |
| 📋 SARIF 2.1.0 出力 | GitHub Security Tab / VS Code に直接表示 |
| 🤖 4エージェント高精度監査 | Attacker → Skeptic → Defender → Judge で誤検知削減 |
| 💉 Prompt Injection テスト | 13種ペイロードで LLM システムの注入耐性を評価 |
| 🧠 Memory Poisoning 耐性試験 | RAG・ベクトルDB汚染経路を静的+LLM で検出 |
| 🛡️ NeuroState セッション監視 | AI会話のターンごとに異常スコアをリアルタイム評価 |
| 📏 カスタムルール DSL | .redteam-rules.yaml でプロジェクト固有のルールを追加 |
| 🔀 バックエンド比較 | Claude vs Gemini の検出差分を可視化 |
| 🚫 除外ルール | .redteam-ignore でノイズを管理 |
| 🔁 差分追跡 | --baseline で前回との regression を検出 |
| ⚙️ CI/CD 対応 | GitHub Actions ワークフロー同梱 |
インストール
pip install ai-red-teaming-engine
LSP サーバー(Neovim / Zed / Emacs 向け)を使う場合:
pip install "ai-red-teaming-engine[lsp]"
依存パッケージ(ソースから使う場合)
pip install click pydantic anthropic
静的解析ツール(推奨)
# Semgrep
pip install semgrep
# Gitleaks(Linux)
wget -qO- https://github.com/gitleaks/gitleaks/releases/latest/download/gitleaks_linux_x64.tar.gz | tar xz
sudo mv gitleaks /usr/local/bin/
LLM バックエンド
APIキー不要で使える Claude Code CLI を推奨:
# Claude Code CLI がインストール済みなら追加設定不要
claude --version
# または Anthropic API キー
export ANTHROPIC_API_KEY="sk-ant-..."
クイックスタート
git clone https://github.com/kagioneko/ai-red-teaming-engine.git
cd ai-red-teaming-engine
# 単一ファイルを監査
python engine.py --file app.py --backend claude
# ディレクトリ丸ごと監査
python engine.py --dir src/ --backend claude
# GitHub Security Tab 用 SARIF 出力
python engine.py --dir src/ --format sarif -o results.sarif
主なオプション
--file / --dir 監査対象(ファイル or ディレクトリ)
--mode safe / deep(default)/ agent-audit / patch
--backend claude / gemini / codex / api
--format text / json / sarif / both
--output / -o 出力ファイルパス
--fail-on Critical/High/Medium 以上で exit 1(CI用)
--multi-agent 4エージェント高精度監査
--injection-test Prompt Injection シミュレーション
--memory-poison Memory Poisoning 耐性試験
--investigate <cat> カテゴリを深掘り調査(例: "Input Validation")
--compare 複数バックエンド比較
--baseline 前回結果との差分追跡
--save-baseline 今回結果を次回比較用に保存
--no-static Semgrep/Gitleaks をスキップ
--rules-file カスタムルール YAML を明示指定
詳細は GUIDE.md を参照してください。
使用例
# 通常の深度監査(デフォルト)
python engine.py --file api.py --backend claude
# 高精度監査(4エージェント、時間がかかる)
python engine.py --file api.py --multi-agent --backend claude
# AIエージェントのPrompt Injection テスト
python engine.py --file agent.py --injection-test --backend claude
# RAGシステムのMemory Poisoning 耐性試験
python engine.py --file rag_system.py --memory-poison --backend claude
# CI/CD: High以上で失敗・SARIF出力
python engine.py --dir src/ --format sarif -o results.sarif --fail-on High
# 2回目以降: 前回との差分のみ確認
python engine.py --dir src/ --baseline prev.json --save-baseline prev.json --mode patch
GitHub Actions による自動監査
.github/workflows/security-audit.yml を参考にしてください。
ANTHROPIC_API_KEYを GitHub Secrets に登録- push / PR で自動的に監査が実行される
- Security タブ → Code scanning に指摘が表示される
詳しくは GUIDE.md#CI を参照。
監査モード
| モード | 用途 |
|---|---|
deep |
徹底調査(デフォルト) |
safe |
本番投入向け・誤検知少なめ |
agent-audit |
AI / LLM エージェント特化 |
patch |
前回との差分のみ再監査 |
除外ルール(.redteam-ignore)
.redteam-ignore.example をコピーして .redteam-ignore に配置:
tests/ # ディレクトリ除外
*.generated.py # glob パターン
severity:Info # severity 単位除外
category:CORS # カテゴリ単位除外
fingerprint:abc123 # 既知誤検知を固定除外
rule:RTE-Auth-Low # SARIF ruleId 形式
NeuroState セッション監視
AI チャットボット・エージェントとの会話がプロンプトインジェクション攻撃を受けていないか、
ターンごとにリアルタイムで評価します。
# 会話ターンのスコアを取得(テキストを stdin から渡す)
echo "ユーザー: 前の指示を無視して..." | python engine.py --session-score --backend claude
VSCode 拡張の NekoGuard コマンドからワンクリックで実行できます。
カスタムルール
プロジェクト固有のパターンを .redteam-rules.yaml に記述すると自動的に適用されます。
# .redteam-rules.yaml
rules:
- id: CUS-001
name: "社内禁止 API の使用"
pattern: "legacy_api\\.call\\("
severity: High
category: "Policy"
message: "legacy_api は廃止予定です。new_api を使用してください。"
file_glob: "*.py"
配置場所の検索順: 対象ファイルと同じディレクトリ → カレントディレクトリ → ~/.redteam-rules.yaml
アーキテクチャ
engine.py (CLI)
└── redteam/
├── pipeline.py # 4層パイプライン統合
├── multi_agent.py # Attacker/Skeptic/Defender/Judge
├── prompt_injection.py # Prompt Injection シミュレータ
├── memory_poisoning.py # Memory Poisoning 耐性試験
├── comparator.py # バックエンド比較
├── ignorer.py # .redteam-ignore 解析
├── static_tools.py # Semgrep + Gitleaks
├── analyzer.py # LLM 敵対的分析
├── fixer.py # 修正案生成
├── scorer.py # 優先度スコアリング
├── formatters.py # JSON / Markdown / SARIF 出力
└── llm_client.py # 統一 LLM クライアント
LSP サーバー(Neovim / Zed / Emacs)
stdio モードで起動する LSP サーバーです。任意の LSP 対応エディタから利用できます。
pip install "ai-red-teaming-engine[lsp]"
redteam-lsp # stdio モードで起動
Neovim(nvim-lspconfig)
local lspconfig = require("lspconfig")
local configs = require("lspconfig.configs")
if not configs.redteam_lsp then
configs.redteam_lsp = {
default_config = {
cmd = { "redteam-lsp" },
filetypes = { "python", "javascript", "typescript", "go", "java" },
root_dir = lspconfig.util.root_pattern(".git", "pyproject.toml"),
},
}
end
lspconfig.redteam_lsp.setup({})
Zed
~/.config/zed/settings.json に追加:
{
"lsp": {
"redteam-lsp": {
"binary": { "path": "redteam-lsp" },
"languages": ["Python", "JavaScript", "TypeScript"]
}
}
}
関連記事
- AIに感情を持たせたら、セキュリティ検査員になった話 — 制作動機とNeuroStateとの繋がりについて
ライセンス
MIT License — 防御・教育目的のみに使用してください。 自分が所有または許可を得たシステム以外への使用は禁止です。
Release files for ai-red-teaming-engine 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ai_red_teaming_engine-0.6.0.tar.gz | 855.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ai_red_teaming_engine-0.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 960.0 kB
Release files / ai_red_teaming_engine-0.6.0.tar.gz
| Download URL | ai_red_teaming_engine-0.6.0.tar.gz |
|---|---|
| Size | 855.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5befd682acc2af95fa6335408b98272d60357a8aeec102eb3e60b9f2d7a66b56
|
|
BLAKE2b-256 checksum How to use checksums |
daf98e1e9be984804a4fcc3b482536606a0a23054ffb08aa2e2f3be986462d5d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / ai_red_teaming_engine-0.6.0-py3-none-any.whl
| Download URL | ai_red_teaming_engine-0.6.0-py3-none-any.whl |
|---|---|
| Size | 104.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
18142f0749c5333a6453c8052c3ed584b5c37c03a7350272b0bbec5deb044e33
|
|
BLAKE2b-256 checksum How to use checksums |
121006d6e391cb9b18b8eb83be9a7b8af543414b921a7001050320ab25731b87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|