雲端環境(AWS、GCP、Azure)提供靈活的資源管理,但**配置錯誤(Misconfiguration)**已成為現代雲端資安事件的主要來源之一。
常見問題包括:
AdministratorAccess
)0.0.0.0/0
)這些問題通常不是技術漏洞(CVE),而是人為配置錯誤。
因此,本篇將示範如何利用AI Agent 自動分析雲端設定檔或 API 響應,幫助快速識別潛在安全風險。
收集階段 (Collector Agent)
透過 AWS CLI / GCP CLI / Azure CLI 匯出設定:
aws s3api list-buckets > aws_s3.json
gcloud compute firewall-rules list --format=json > gcp_firewall.json
az storage account list --output json > azure_storage.json
分析階段 (Analyzer Agent)
報告階段 (Reporter Agent)
這裡使用 Express
建立一個簡單的 HTTP 伺服器,讓前端或 AI Agent 可以透過 /api/cloud/config
取得模擬的雲端設定資料。
import express from "express";
const app = express();
這個 API 模擬 AWS 的部分設定資料:
public-assets
bucket 設為公開且未加密(潛在風險)AdminRole
權限過大,屬於高風險設定AI Agent 稍後會分析這些資料。
app.get("/api/cloud/config", (req, res) => {
res.json({
aws: {
s3Buckets: [
{ name: "public-assets", publicAccess: true, encryption: false },
{ name: "internal-logs", publicAccess: false, encryption: true },
],
iamRoles: [
{ name: "AdminRole", policy: "AdministratorAccess" },
{ name: "ReadOnlyRole", policy: "ReadOnlyAccess" },
],
},
});
});
app.listen(3000, () => console.log("伺服器啟動:http://localhost:3000"));
node-fetch
:用來向伺服器取得設定資料。dotenv
:讀取 .env
裡的 GEMINI_API_KEY
。GoogleGenerativeAI
:初始化 Gemini 模型。
import fetch from "node-fetch";
import dotenv from "dotenv";
import { GoogleGenerativeAI } from "@google/generative-ai";
dotenv.config();
const API_KEY = process.env.GEMINI_API_KEY;
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
server.js
取得模擬的雲端設定資料。async function detectCloudMisconfig() {
const res = await fetch("http://localhost:3000/api/cloud/config");
const config = await res.json();
const prompt = `
你是一位雲端安全顧問,請檢查以下 AWS 設定是否存在安全風險。
若發現問題,請依以下格式回覆:
- 資源名稱
- 問題說明
- 風險等級(高 / 中 / 低)
- 修正建議
設定資料:
${JSON.stringify(config, null, 2)}
`;
const result = await model.generateContent(prompt);
console.log("AI 分析報告:\n");
console.log(result.response.text());
}
detectCloudMisconfig();