iT邦幫忙

2025 iThome 鐵人賽

DAY 24
0
Modern Web

AI 驅動的 Web 資安新時代系列 第 24

Day24 - 雲端資安:AI Agent 偵測雲端配置錯誤(AWS、GCP、Azure)

  • 分享至 

  • xImage
  •  

雲端環境(AWS、GCP、Azure)提供靈活的資源管理,但**配置錯誤(Misconfiguration)**已成為現代雲端資安事件的主要來源之一。

常見問題包括:

  • S3 Bucket 設為公開存取
  • IAM 權限過度開放(AdministratorAccess
  • 未啟用加密功能或多因素認證(MFA)
  • GCP Firewall 開放所有 IP 存取(0.0.0.0/0
  • Azure Storage 未設定適當的 Access Key 限制

這些問題通常不是技術漏洞(CVE),而是人為配置錯誤

因此,本篇將示範如何利用AI Agent 自動分析雲端設定檔或 API 響應,幫助快速識別潛在安全風險。


工作流程 (AI Cloud Misconfig Detector)

  1. 收集階段 (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
      
  2. 分析階段 (Analyzer Agent)

    • AI 解析 JSON 結構,找出常見的配置錯誤。
  3. 報告階段 (Reporter Agent)

    • 以自然語言生成:
      • 問題摘要
      • 風險等級
      • 修正建議

Server - 模擬 API 回傳雲端設定資料

1、建立伺服器環境

這裡使用 Express 建立一個簡單的 HTTP 伺服器,讓前端或 AI Agent 可以透過 /api/cloud/config 取得模擬的雲端設定資料。

import express from "express";
const app = express();

2、建立雲端設定 API

這個 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" },
      ],
    },
  });
});

3、伺服器啟動


app.listen(3000, () => console.log("伺服器啟動:http://localhost:3000"));

Agent - AI 檢測與報告生成

1、載入套件與初始化模型

  • node-fetch:用來向伺服器取得設定資料。
  • dotenv:讀取 .env 裡的 GEMINI_API_KEY
  • GoogleGenerativeAI:初始化 Gemini 模型。
    • 這裡使用 gemini-2.5-flash(速度快、成本低)。
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" });

2、呼叫 API 並生成分析報告

  1. server.js 取得模擬的雲端設定資料。
  2. 將設定資料嵌入 AI Prompt 中。
  3. 呼叫 Gemini 模型進行安全分析。
  4. 將分析結果(報告文字)輸出至終端機。
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();

上一篇
Day23 - DevSecOps 新時代:AI Agent 自動化融入開發流程
下一篇
Day25 - AI Agent 在資安合規(OWASP、ISO、GDPR)中的角色
系列文
AI 驅動的 Web 資安新時代29
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言