iT邦幫忙

2025 iThome 鐵人賽

DAY 23
0
Modern Web

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

Day23 - DevSecOps 新時代:AI Agent 自動化融入開發流程

  • 分享至 

  • xImage
  •  

為什麼要讓 AI 進入 DevSecOps?

在傳統開發流程中,安全檢測(Security Scanning)常常是最後一步

但這樣的模式存在兩個重大問題:

  1. 漏洞發現太晚:程式上線後才發現弱點,修復成本高。
  2. 人工流程太慢:安全團隊必須人工檢查報告與比對風險。

而在 AI Agent 時代,我們能讓安全融入開發全流程,形成真正的「AI 驅動 DevSecOps」。


AI 驅動 DevSecOps 的工作流程

  1. 開發階段 (Dev)
    • 開發者將程式碼提交到 GitHub
    • 自動觸發 Webhook 或 CI Pipeline
  2. 安全檢測 (Sec)
    • Scanner Agent 調用 SAST/Dependency 工具(如 Trivy、Bandit、npm audit)
    • 蒐集弱點清單(CVE、CVSS、套件版本)
  3. AI 分析 (AI Agent)
    • Analyzer Agent 解析掃描結果
    • 運用 LLM(如 Gemini、GPT、Ollama)
    • 自動生成報告、風險排序與修補建議
  4. 自動回饋 (Ops)
    • 將報告回傳至 GitHub PR 或傳送至 Slack/Teams
    • 若發現高風險問題,AI 可自動提出修補 PR 建議

Server - 模擬掃描 API

1、初始化伺服器

建立一個 Express 應用程式,用來提供簡單的 API 服務。

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

2、定義掃描結果路由

建立一個 /api/scan/result API,回傳模擬的掃描結果清單,包含漏洞名稱、CVE 編號、風險分數與修補建議。

app.get("/api/scan/result", (req, res) => {
   res.json([
      {
         file: "src/server.js",
         vuln: "SQL Injection",
         cve: "CVE-2024-56789",
         cvss: 9.1,
         desc: "User input not sanitized before SQL query execution.",
         fix: "Use parameterized queries or ORM."
      },
      {
         file: "package.json",
         vuln: "Outdated library",
         cve: "CVE-2025-12345",
         cvss: 7.3,
         desc: "axios <1.6.0 has prototype pollution vulnerability.",
         fix: "Upgrade to axios@1.6.0 or above."
      }
   ]);
});

3、啟動伺服器

讓伺服器監聽在 3000 端口,啟動後即可在瀏覽器打開 API。

app.listen(3000, () => console.log("Scan API ready: http://localhost:3000"));

Agent - AI 分析與報告生成

1、初始化 AI 模型

載入 .env 的 API 金鑰,建立 Gemini 模型(gemini-2.5-flash),之後會用它來生成 AI 報告。

import fetch from "node-fetch";
import dotenv from "dotenv";
import { GoogleGenerativeAI } from "@google/generative-ai";

dotenv.config();

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });

2、讀取掃描結果並分析

  1. /api/scan/result 抓取掃描資料
  2. 把結果傳給 AI 模型
  3. 請 AI 生成兩份報告(技術報告與管理摘要)
  4. 在終端機印出結果
async function analyzeSecurityReport() {
   const res = await fetch("http://localhost:3000/api/scan/result");
   const data = await res.json();

   const prompt = `
   以下為安全掃描結果,請幫我輸出兩種報告:
   1. 技術報告(含 CVE、描述、修補建議)
   2. 管理摘要(列出高風險項目、風險程度與修補優先順序)
   資料如下:
   ${JSON.stringify(data, null, 2)}
   `;

   const aiRes = await model.generateContent(prompt);
   console.log("=== AI 安全報告 ===\n");
   console.log(aiRes.response.text());
}

analyzeSecurityReport();

上一篇
Day22 - AI 日誌事件分析(Log Analyzer Agent)
系列文
AI 驅動的 Web 資安新時代23
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言