el-bakkali/azure-bruteforce-defense

GitHub: el-bakkali/azure-bruteforce-defense

基于Azure OpenAI的SSH暴力破解防护系统,用自然语言对话替代KQL实现威胁狩猎和日志分析。

Stars: 0 | Forks: 0

Azure Brute-Force Defense GPT-4o-mini NullClaw Sentinel

🛡️ Azure 暴力破解 SSH 防御

AI 驱动的威胁狩猎与日志分析 — 无需 KQL。
用通俗的语言与您的安全日志对话。Azure OpenAI 负责其余的工作。

快速部署架构功能演示结构许可证

## 💡 这是什么? 这是一个**概念验证**项目,展示了 Azure AI 如何变革安全运营。您无需编写复杂的 KQL 查询在日志中搜寻威胁,只需与 AI 助手**聊天**,它即可: - 自动**查询您的 Azure Log Analytics** 工作区 - 使用 GPT-4o-mini **分析 SSH 暴力破解攻击** - 在 Syslog 和 auditd 数据中**搜寻 NullClaw**(一款基于 Zig 构建的攻击工具) - **提供可操作的建议** — 严重性评级、修复步骤、安全态势评估 ## 🏗 架构 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ Azure Resource Group │ │ │ │ ┌──────────────┐ SSH Brute-Force ┌──────────────────────┐ │ │ │ Defender VM │◄──────────────────────│ Function App (B1) │ │ │ │ Ubuntu 22.04 │ SimulateAttack() │ │ │ │ │ ── Fail2ban │ │ ├─ SimulateAttack │ │ │ │ ── AMA │ │ ├─ AnalyzeLogs │ │ │ │ ── auditd │ │ └─ ChatProxy ──────►├──┤ │ └──────┬───────┘ └──────────┬───────────┘ │ │ │ Syslog │ │ │ ▼ │ │ │ ┌──────────────┐ KQL Queries ┌─────────┴────────┐ │ │ │ Log Analytics │◄──────────────────────│ Azure OpenAI │ │ │ │ Workspace │ │ GPT-4o-mini │ │ │ │ ── Sentinel │ │ (Managed ID) │ │ │ └──────────────┘ └──────────────────┘ │ │ │ │ ┌──────────────────────────────────────────────────────────────┐ │ │ │ Frontend — Static Website (Azure Blob Storage) │ │ │ │ Chat UI → AnalyzeLogs → ChatProxy → GPT-4o-mini response │ │ │ └──────────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────────┘ ``` | 组件 | 用途 | |---|---| | **Defender VM** | Ubuntu 22.04 B2s,配备 Fail2ban、Azure Monitor Agent、auditd | | **Function App** | 3 个函数 — 攻击模拟、日志分析、AI 聊天代理 | | **Log Analytics** | 集中式日志存储,通过 DCR 采集 Syslog | | **Microsoft Sentinel** | 用于安全分析和威胁狩猎的 SIEM 层 | | **Azure OpenAI** | 用于自然语言日志分析的 GPT-4o-mini | | **Static Website** | 用于对话式威胁狩猎的聊天型前端 | ## ✨ 功能 ### 🗣️ 对话式威胁狩猎 用通俗的语言提问 — AI 会查询您的日志并返回结构化的分析结果: ``` You: "Are there any brute-force attacks in the last hour?" AI: "I detected 47 failed SSH login attempts from 3 unique IPs. Top attacker: 185.220.101.x with 32 attempts targeting 'root'. Severity: MEDIUM. Fail2ban has banned 2 of 3 IPs..." ``` ### 🔍 NullClaw 威胁狩猎 自动检测 [NullClaw](https://github.com/nullclaw) — 一款 Zig 编译的攻击工具 — 覆盖 4 个向量: - **安装** — git clone / 下载尝试 - **编译** — Syslog 中的 Zig 构建命令 - **执行** — auditd 中 Zig 编译的二进制签名 - **行为** — 亚秒级 SSH 暴力破解爆发(Zig 速度指纹) ### 🤖 Azure OpenAI 集成 - 使用 **managed identity**(代码中无 API 密钥) - `DefaultAzureCredential` 实现零密钥认证 - ChatProxy 暴露兼容 OpenAI 的 `/v1/chat/completions` 端点 ### 🛡️ 自动化防御 - **Fail2ban** 在 3 次 SSH 登录失败后自动封禁 IP - **NSG rules** 用于网络层面的封锁 - **严重性评估** — NONE / LOW / MEDIUM / HIGH / CRITICAL - 每次分析生成**可操作的建议** ## 🚀 快速部署 ### 前置条件 - 具有 **Contributor** 权限的 Azure 订阅 - 已安装 Azure CLI(`az login`) - PowerShell 7+ - Python 3.11+ ### 选项 1:Bicep 模板(推荐) 使用一条命令部署整个基础设施: ``` # Clone the repo git clone https://github.com/el-bakkali/azure-bruteforce-defense.git cd azure-bruteforce-defense # Create resource group az group create --name rg-bruteforce-defense --location uksouth # Deploy infrastructure az deployment group create ` --resource-group rg-bruteforce-defense ` --template-file infra/main.bicep ` --parameters adminPassword='YourSecurePassword123!' # Deploy Function App code .\post-deploy-config.ps1 # Enable static website & deploy frontend $storageAccount = (az deployment group show -g rg-bruteforce-defense -n main --query properties.outputs.storageAccountName.value -o tsv) az storage blob service-properties update ` --account-name $storageAccount ` --static-website --index-document index.html --auth-mode login az storage blob upload ` --account-name $storageAccount ` --container-name '$web' ` --file frontend/index.html ` --name index.html ` --content-type "text/html" --auth-mode login --overwrite ``` ### 选项 2:PowerShell 脚本(分步执行) ``` # 1. Deploy infrastructure (VM, Function App, LAW, Sentinel, NSG, DCRs) .\deploy-infrastructure.ps1 # 2. Deploy Function App code .\post-deploy-config.ps1 # 3. (可选) 部署 Sentinel 分析规则 .\deploy-sentinel-rules.ps1 ``` ### 部署后:Azure OpenAI 设置(如果使用选项 2) 1. 在 Azure 门户中**创建 Azure OpenAI 资源** 2. **部署 gpt-4o-mini** 模型(GlobalStandard SKU) 3. 为 Function App managed identity **分配角色**: az role assignment create ` --assignee (az functionapp identity show -g rg-bruteforce-defense -n --query principalId -o tsv) ` --role "Cognitive Services OpenAI User" ` --scope (az cognitiveservices account show -g rg-bruteforce-defense -n --query id -o tsv) 4. **设置应用配置**: az functionapp config appsettings set ` -g rg-bruteforce-defense -n ` --settings ` AZURE_OPENAI_ENDPOINT="https://.openai.azure.com/" ` AZURE_OPENAI_DEPLOYMENT="gpt-4o-mini" ## 🎮 演示流程 ### 1. 打开 SSH 以进行攻击模拟 ``` Azure Portal → NSG → Inbound rules → AllowSSH → Source: Any → Save ``` ### 2. 模拟暴力破解攻击 ``` POST https://.azurewebsites.net/api/SimulateAttack?code= Content-Type: application/json { "target": "", "username": "root", "attempts": 20 } ``` ### 3. 使用 AI 分析 打开前端页面 `https://.z33.web.core.windows.net/` - 输入您的 Function App URL 和 Function Key - 点击 **"Attack Summary (1h)"** → AI 分析暴力破解数据 - 点击 **"NullClaw Hunt"** → 扫描高级威胁指标 - 或直接问:*"What should I do to improve security?"* ### 4. 在 Sentinel 中探索 ``` Azure Portal → Sentinel → Hunting → Run hunting queries Log Analytics → Syslog | where SyslogMessage has "Failed password" ``` ### 5. 锁定环境 ``` Azure Portal → NSG → AllowSSH rule → Delete (or restrict source IP) ``` ## 📁 项目结构 ``` azure-bruteforce-defense/ │ ├── infra/ │ ├── main.bicep # Bicep IaC — full stack deployment │ └── main.parameters.json # Parameter file (customize per env) │ ├── function-app/ │ ├── host.json # Azure Functions host config │ ├── requirements.txt # Python: azure-functions, azure-identity, │ │ # azure-monitor-query, paramiko │ ├── AnalyzeLogs/ │ │ └── __init__.py # Log analysis + NullClaw threat hunting │ ├── ChatProxy/ │ │ └── __init__.py # Azure OpenAI proxy (managed identity) │ └── SimulateAttack/ │ └── __init__.py # SSH brute-force simulation (paramiko) │ ├── frontend/ │ └── index.html # Chat UI — conversational threat hunting │ ├── nullclaw-func/ # NullClaw custom handler (experimental) │ ├── host.json │ ├── syslog-hunter-prompt.md │ └── ... │ ├── deploy-infrastructure.ps1 # Full infrastructure deployment ├── post-deploy-config.ps1 # Function App code deployment ├── setup-fail2ban.sh # Fail2ban + auditd on Defender VM ├── deploy-sentinel-rules.ps1 # Sentinel analytics rules ├── alerts-guide.md # Alert configuration guide └── README.md ``` ## 🔧 API 参考 ### `POST /api/AnalyzeLogs` 查询 Log Analytics 获取 SSH 攻击数据或运行 NullClaw 威胁搜寻。 | 参数 | 类型 | 默认值 | 描述 | |---|---|---|---| | `hours` | int | `1` | 回溯窗口(1–168 小时) | | `hunt` | string | — | 设为 `"nullclaw"` 进行威胁搜寻 | | `query` | string | — | 自定义 KQL 查询(高级用户) | **响应** — 包含严重性、攻击者 IP、Fail2ban 操作、建议的结构化报告。 ### `POST /api/v1/chat/completions` 兼容 OpenAI 的聊天补全(通过 managed identity 代理到 Azure OpenAI)。 | 参数 | 类型 | 描述 | |---|---|---| | `messages` | array | 聊天消息 `[{role, content}]` | | `max_tokens` | int | 最大响应 token 数 | | `temperature` | float | 创造性(0.0–2.0) | ### `POST /api/SimulateAttack` 针对 Defender VM 模拟 SSH 暴力破解。 | 参数 | 类型 | 默认值 | 描述 | |---|---|---|---| | `target` | string | env var | VM IP 地址 | | `username` | string | `root` | SSH 用户名 | | `attempts` | int | `15` | 尝试次数(最大:50) | ## 🔐 安全设计 | 方面 | 实现 | |---|---| | **无硬编码密钥** | 所有认证通过 managed identity + `DefaultAzureCredential` | | **前端无密钥** | Function Key 在运行时输入,仅存储在浏览器 localStorage 中 | | **Azure AD 认证** | Function App → Azure OpenAI 使用 AAD 令牌,绝不使用 API 密钥 | | **网络隔离** | NSG 控制 SSH 访问;演示结束后锁定 | | **RBAC 最小权限** | Functions 仅获得所需角色(Log Analytics Reader, OpenAI User) | | **强制 HTTPS** | 所有端点均要求 TLS | ## 💰 成本估算 | 资源 | SKU | ~月度成本 | |---|---|---| | Defender VM | Standard_B2s | ~$30 | | Function App | B1 (Linux) | ~$13 | | Log Analytics | Pay-per-GB | ~$2–5 | | Azure OpenAI | GPT-4o-mini (10K TPM) | ~$1–3 | | Storage | Standard LRS | < $1 | | **总计** | | **~$47–52/月** | ## 🧹 清理 ``` az group delete --name rg-bruteforce-defense --yes --no-wait ``` ## 📝 许可证 本项目是开源的,基于 [MIT License](LICENSE) 发布。

Mehdi El Bakkali 构建 — Azure Security & AI

标签:AI, AI合规, auditd, Azure, GPT-4o, Libemu, LLM, Log Analytics, NullClaw, PE 加载器, Python, Sentinel, SSH, SSH安全, Syslog, Unmanaged PE, 安全运营, 扫描框架, 无KQL查询, 无后门, 暴力破解防御, 自动化代码审查, 逆向工具