安全:WAF / Shield / GuardDuty 邊界防護與威脅偵測

IAM 設好只是門鎖裝好了。但有人在撬門(DDoS)、有人在找窗戶(SQL Injection)、有人已經進來了(帳號被盜)——你怎麼知道?

先講結論

AWS 安全分三層:邊界防護(WAF 擋 OWASP Top 10、Shield 擋 DDoS)、威脅偵測(GuardDuty 抓異常行為)、合規掃描(Security Hub 統一看板、Inspector 掃描漏洞)。最小配置:開 GuardDuty + WAF 基本規則 + CloudTrail 全開。這三個搞定,你的安全水位就超過 80% 的 AWS 用戶了。


WAF:Web Application Firewall

WAF 掛在 ALB / CloudFront / API Gateway 前面,檢查每個 HTTP request,擋掉惡意流量。

AWS Managed Rules

AWS 提供了一組 Managed Rules,覆蓋最常見的攻擊模式。Common Rule Set 等基本規則免費,但 Bot Control(AWSManagedRulesBotControlRuleSet)有額外的每請求費用:

Rule Group防什麼
AWSManagedRulesCommonRuleSetOWASP Top 10(SQL Injection、XSS 等)
AWSManagedRulesKnownBadInputsRuleSet已知惡意 payload(Log4j、SSRF)
AWSManagedRulesSQLiRuleSetSQL Injection
AWSManagedRulesAmazonIpReputationList已知惡意 IP
AWSManagedRulesBotControlRuleSetBot 流量(爬蟲、scraper)
# 建立 WAF Web ACL
aws wafv2 create-web-acl \
  --name prod-waf \
  --scope REGIONAL \
  --default-action Allow={} \
  --rules '[
    {
      "Name": "AWS-AWSManagedRulesCommonRuleSet",
      "Priority": 1,
      "Statement": {
        "ManagedRuleGroupStatement": {
          "VendorName": "AWS",
          "Name": "AWSManagedRulesCommonRuleSet"
        }
      },
      "OverrideAction": {"None": {}},
      "VisibilityConfig": {
        "SampledRequestsEnabled": true,
        "CloudWatchMetricsEnabled": true,
        "MetricName": "CommonRuleSet"
      }
    },
    {
      "Name": "AWS-AWSManagedRulesKnownBadInputsRuleSet",
      "Priority": 2,
      "Statement": {
        "ManagedRuleGroupStatement": {
          "VendorName": "AWS",
          "Name": "AWSManagedRulesKnownBadInputsRuleSet"
        }
      },
      "OverrideAction": {"None": {}},
      "VisibilityConfig": {
        "SampledRequestsEnabled": true,
        "CloudWatchMetricsEnabled": true,
        "MetricName": "KnownBadInputs"
      }
    }
  ]' \
  --visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=prod-waf
 
# 掛到 ALB
aws wafv2 associate-web-acl \
  --web-acl-arn arn:aws:wafv2:ap-northeast-1:123456789012:regional/webacl/prod-waf/xxx \
  --resource-arn arn:aws:elasticloadbalancing:ap-northeast-1:123456789012:loadbalancer/app/prod-alb/xxx

自定義 Rate Limiting

# 擋掉 5 分鐘內超過 2000 request 的 IP
{
  "Name": "RateLimit",
  "Priority": 0,
  "Statement": {
    "RateBasedStatement": {
      "Limit": 2000,
      "AggregateKeyType": "IP"
    }
  },
  "Action": {"Block": {}},
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "RateLimit"
  }
}

IP 白名單 / 黑名單

# 建立 IP Set
aws wafv2 create-ip-set --name office-ips \
  --scope REGIONAL \
  --ip-address-version IPV4 \
  --addresses "203.0.113.0/24" "198.51.100.0/24"
 
# 在 WAF Rule 裡使用:只允許 office IP 存取 /admin
{
  "Name": "AdminWhitelist",
  "Priority": 3,
  "Statement": {
    "AndStatement": {
      "Statements": [
        {
          "ByteMatchStatement": {
            "SearchString": "/admin",
            "FieldToMatch": {"UriPath": {}},
            "TextTransformations": [{"Priority": 0, "Type": "LOWERCASE"}],
            "PositionalConstraint": "STARTS_WITH"
          }
        },
        {
          "NotStatement": {
            "Statement": {
              "IPSetReferenceStatement": {
                "ARN": "arn:aws:wafv2:...:ipset/office-ips/xxx"
              }
            }
          }
        }
      ]
    }
  },
  "Action": {"Block": {}}
}

Shield:DDoS 防護

Shield Standard(免費)

所有 AWS 帳號自動啟用,防護 L3/L4 的 DDoS 攻擊(SYN flood、UDP reflection)。

Shield Advanced($3,000/月)

  • L7 DDoS 防護
  • 24/7 AWS DDoS 專家支援
  • 費用保護(DDoS 造成的額外 scaling 費用 AWS 吸收)
  • 適合:金融、電商、遊戲等高風險業務

大多數公司不需要 Shield Advanced。Shield Standard + WAF Rate Limiting 就能擋住 99% 的 DDoS。


GuardDuty:智慧威脅偵測

GuardDuty 分析 CloudTrail、VPC Flow Logs、DNS Logs,用 ML 偵測異常行為。

啟用很簡單

# 一鍵啟用
aws guardduty create-detector --enable
 
# 查看發現的威脅
aws guardduty list-findings --detector-id xxx \
  --finding-criteria '{
    "Criterion": {
      "severity": {"Gte": 7}
    }
  }'

GuardDuty 能抓到什麼

威脅類型範例
帳號被盜從異常地點登入、異常時間 API call
EC2 被入侵EC2 連到已知 C2 server、挖礦行為
S3 異常公開了不該公開的 bucket、異常下載量
IAM 異常從 Tor 出口節點呼叫 API
K8s 異常Pod 執行可疑命令、特權容器

自動回應

# GuardDuty → EventBridge → Lambda 自動回應
aws events put-rule --name guardduty-high-severity \
  --event-pattern '{
    "source": ["aws.guardduty"],
    "detail-type": ["GuardDuty Finding"],
    "detail": {"severity": [{"numeric": [">=", 7]}]}
  }'
 
aws events put-targets --rule guardduty-high-severity \
  --targets '[{
    "Id": "auto-response",
    "Arn": "arn:aws:lambda:ap-northeast-1:123456789012:function:security-auto-response"
  }]'

自動回應 Lambda 可以做:

  • 隔離被入侵的 EC2(改 Security Group 阻斷所有流量)
  • 停用可疑的 IAM Access Key
  • 發 Slack/Discord 通知

Security Hub:統一安全看板

Security Hub 整合 GuardDuty、Inspector、WAF、Config 的發現,提供統一的安全評分。

# 啟用 Security Hub
aws securityhub enable-security-hub \
  --enable-default-standards
 
# 查看安全分數
aws securityhub get-findings --filters '{
  "ComplianceStatus": [{"Value": "FAILED", "Comparison": "EQUALS"}],
  "SeverityLabel": [{"Value": "CRITICAL", "Comparison": "EQUALS"}]
}'

AWS Security Hub 支援的合規標準

  • AWS Foundational Security Best Practices — AWS 自己的最佳實踐
  • CIS AWS Foundations Benchmark — CIS 的 AWS 安全基準
  • PCI DSS — 信用卡資料處理標準
  • NIST 800-53 — 美國政府安全標準

Inspector:漏洞掃描

# 啟用 Inspector(自動掃描 EC2、ECR image、Lambda)
aws inspector2 enable --resource-types EC2 ECR LAMBDA
 
# 查看漏洞
aws inspector2 list-findings --filter-criteria '{
  "severity": [{"comparison": "EQUALS", "value": "CRITICAL"}]
}'

Inspector 會自動掃描你的 ECR image 和 EC2 上的套件,找出已知 CVE 漏洞。


安全最小配置 Checklist

  • CloudTrail 全 Region 啟用(記錄所有 API call)
  • GuardDuty 啟用
  • WAF + Managed Rules 掛在 ALB 上
  • S3 Public Access Block 開啟
  • RDS / ElastiCache 在 Private Subnet
  • Security Group 只開必要 port
  • IAM 密碼政策設好(MFA 必須)
  • Budget Alert 設好(異常費用 = 可能被入侵)
  • ECR image scan 啟用

自架 vs AWS

面向自架AWS
WAFModSecurity + NginxAWS WAF
DDoSCloudflare / 自己擋Shield Standard (免費)
威脅偵測OSSEC / Wazuh / FalcoGuardDuty
漏洞掃描Trivy / GrypeInspector
合規掃描OpenSCAPSecurity Hub
審計日誌auditd + ELKCloudTrail
費用人力成本高服務費但省人力

如果你讀過 Application SecuritySecurity Scanning,AWS 的安全服務就是那些概念的託管版——OWASP Top 10 防護、漏洞掃描、入侵偵測,概念完全相同。


K8s 映射

AWS SecurityK8s 對應
WAFNginx Ingress + ModSecurity / Coraza
GuardDuty (K8s)Falco
InspectorTrivy Operator
Security HubKubescape / Polaris
CloudTrailK8s Audit Log
Shield靠 Cloud Provider 或 Cloudflare
Network FirewallNetworkPolicy + Calico

系列導覽


安全不是一次性的設定,是持續的習慣。每週看一次 GuardDuty、每月跑一次 Security Hub——比出事後花三天處理划算得多。