AWS WAF 概述
AWS WAF(Web Application Firewall)是一個雲端網頁應用程式防火牆,可協助保護您的 Web 應用程式免受常見的網路攻擊。WAF 可讓您建立安全規則,控制機器人流量,並封鎖常見的攻擊模式,例如 SQL 注入或跨站腳本(XSS)攻擊。
WAF 主要功能
- 可自訂規則:根據 IP 位址、HTTP 標頭、HTTP 主體、URI 字串等條件建立規則
- 受管規則群組:使用 AWS 和 AWS Marketplace 賣家提供的預先設定規則
- 即時可見性:透過 CloudWatch 指標監控流量
- 與 AWS 服務整合:可與 ALB、CloudFront、API Gateway 等服務整合
WAF 與 ALB 整合
AWS WAF 可以直接與 Application Load Balancer(ALB)整合,在請求到達您的應用程式之前進行過濾。以下是整合的架構流程:
1
| 使用者請求 → AWS WAF → ALB → 目標群組 → EC2/ECS/Lambda
|
建立 ALB
首先,確保您有一個 ALB。以下是使用 AWS CLI 建立 ALB 的範例:
1
2
3
4
5
6
7
8
| # 建立 Application Load Balancer
aws elbv2 create-load-balancer \
--name my-web-alb \
--subnets subnet-12345678 subnet-87654321 \
--security-groups sg-12345678 \
--scheme internet-facing \
--type application \
--ip-address-type ipv4
|
Web ACL 建立
Web ACL(Access Control List)是 WAF 的核心元件,用於定義允許或封鎖請求的規則集合。
使用 AWS CLI 建立 Web ACL
1
2
3
4
5
6
7
| # 建立 Web ACL
aws wafv2 create-web-acl \
--name my-web-acl \
--scope REGIONAL \
--default-action Allow={} \
--visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=myWebACLMetric \
--region ap-northeast-1
|
將 Web ACL 關聯到 ALB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 取得 ALB ARN
ALB_ARN=$(aws elbv2 describe-load-balancers \
--names my-web-alb \
--query 'LoadBalancers[0].LoadBalancerArn' \
--output text)
# 取得 Web ACL ARN
WEB_ACL_ARN=$(aws wafv2 list-web-acls \
--scope REGIONAL \
--query "WebACLs[?Name=='my-web-acl'].ARN" \
--output text)
# 關聯 Web ACL 到 ALB
aws wafv2 associate-web-acl \
--web-acl-arn $WEB_ACL_ARN \
--resource-arn $ALB_ARN
|
受管規則群組
AWS 提供多種受管規則群組,可快速部署常見的安全防護。
常用的 AWS 受管規則
| 規則群組名稱 | 說明 |
|---|
| AWSManagedRulesCommonRuleSet | 防護常見威脅,如 OWASP Top 10 |
| AWSManagedRulesSQLiRuleSet | 防護 SQL 注入攻擊 |
| AWSManagedRulesKnownBadInputsRuleSet | 封鎖已知的惡意輸入 |
| AWSManagedRulesAmazonIpReputationList | 封鎖已知惡意 IP |
新增受管規則到 Web ACL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| # 更新 Web ACL 加入 AWS 受管規則
aws wafv2 update-web-acl \
--name my-web-acl \
--scope REGIONAL \
--id <web-acl-id> \
--default-action Allow={} \
--lock-token <lock-token> \
--visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=myWebACLMetric \
--rules '[
{
"Name": "AWS-AWSManagedRulesCommonRuleSet",
"Priority": 1,
"Statement": {
"ManagedRuleGroupStatement": {
"VendorName": "AWS",
"Name": "AWSManagedRulesCommonRuleSet"
}
},
"OverrideAction": {"None": {}},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "AWSCommonRules"
}
}
]'
|
自訂規則設定
除了受管規則,您也可以建立自訂規則來滿足特定需求。
封鎖特定 User-Agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| # 建立封鎖特定 User-Agent 的規則
aws wafv2 update-web-acl \
--name my-web-acl \
--scope REGIONAL \
--id <web-acl-id> \
--lock-token <lock-token> \
--default-action Allow={} \
--visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=myWebACLMetric \
--rules '[
{
"Name": "BlockBadUserAgent",
"Priority": 2,
"Statement": {
"ByteMatchStatement": {
"SearchString": "BadBot",
"FieldToMatch": {"SingleHeader": {"Name": "user-agent"}},
"TextTransformations": [{"Priority": 0, "Type": "LOWERCASE"}],
"PositionalConstraint": "CONTAINS"
}
},
"Action": {"Block": {}},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "BlockBadUserAgent"
}
}
]'
|
速率限制規則
速率限制規則可防止 DDoS 攻擊和暴力破解攻擊。
建立速率限制規則
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| # 建立每 5 分鐘限制 2000 次請求的規則
aws wafv2 update-web-acl \
--name my-web-acl \
--scope REGIONAL \
--id <web-acl-id> \
--lock-token <lock-token> \
--default-action Allow={} \
--visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=myWebACLMetric \
--rules '[
{
"Name": "RateLimitRule",
"Priority": 3,
"Statement": {
"RateBasedStatement": {
"Limit": 2000,
"AggregateKeyType": "IP"
}
},
"Action": {"Block": {}},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "RateLimitRule"
}
}
]'
|
IP 封鎖清單
建立 IP 集合來封鎖已知的惡意 IP 位址。
建立 IP 集合
1
2
3
4
5
6
| # 建立 IP 集合
aws wafv2 create-ip-set \
--name blocked-ips \
--scope REGIONAL \
--ip-address-version IPV4 \
--addresses "192.0.2.0/24" "198.51.100.0/24"
|
使用 IP 集合建立封鎖規則
1
2
3
4
5
6
7
8
| # 取得 IP 集合 ARN
IP_SET_ARN=$(aws wafv2 list-ip-sets \
--scope REGIONAL \
--query "IPSets[?Name=='blocked-ips'].ARN" \
--output text)
# 在 Web ACL 中使用 IP 集合
# 規則需要加入到 Web ACL 的規則陣列中
|
日誌與監控
啟用 WAF 日誌
1
2
3
4
5
6
7
8
9
| # 建立 Kinesis Data Firehose 傳遞串流(名稱必須以 aws-waf-logs- 開頭)
# 然後啟用日誌
aws wafv2 put-logging-configuration \
--logging-configuration '{
"ResourceArn": "<web-acl-arn>",
"LogDestinationConfigs": [
"arn:aws:firehose:ap-northeast-1:123456789012:deliverystream/aws-waf-logs-my-stream"
]
}'
|
CloudWatch 監控
WAF 會自動將指標傳送到 CloudWatch,您可以監控以下指標:
- AllowedRequests:允許的請求數量
- BlockedRequests:封鎖的請求數量
- CountedRequests:計數的請求數量
- PassedRequests:通過的請求數量
1
2
3
4
5
6
7
8
9
| # 查看 WAF 指標
aws cloudwatch get-metric-statistics \
--namespace AWS/WAFV2 \
--metric-name BlockedRequests \
--dimensions Name=WebACL,Value=my-web-acl Name=Region,Value=ap-northeast-1 Name=Rule,Value=ALL \
--start-time 2024-07-06T00:00:00Z \
--end-time 2024-07-07T00:00:00Z \
--period 3600 \
--statistics Sum
|
成本考量
AWS WAF 的計費方式如下:
| 項目 | 費用(美元) |
|---|
| Web ACL | $5.00 / 月 |
| 規則 | $1.00 / 月 / 規則 |
| 請求 | $0.60 / 百萬個請求 |
| Bot Control | $10.00 / 月 + $1.00 / 百萬個請求 |
成本優化建議
- 合併規則:盡可能將多個條件合併到單一規則中
- 使用受管規則:受管規則群組計為單一規則
- 監控使用量:定期檢視 CloudWatch 指標以優化規則
- 設定預算警示:使用 AWS Budgets 監控 WAF 支出
最佳實踐
- 從 Count 模式開始:新規則先使用 Count 動作,確認不會誤封正常流量後再改為 Block
- 定期更新規則:隨時關注新的威脅並更新規則
- 啟用日誌:保留日誌以便事後分析和稽核
- 測試規則:在生產環境部署前,先在測試環境驗證規則行為
- 監控誤報:定期檢視被封鎖的請求,確認沒有誤封正常使用者
參考資料