AWS Builder Center: Learn, Build and Connect with builders in the AWS community
AWS Builder Center is the official home for builders on AWS. Share and read what others are working on, follow people who inspire you, explore training and workshops, and find tools to support what you're building.
如何同時設定 Network Firewall 標準規則群組和網域清單規則群組規則?
我想要設定 AWS Network Firewall 標準規則群組規則和網域清單規則群組規則,讓兩者搭配運作,以便順利控制流量。
簡短說明
設定標準規則群組規則,以捨棄已建立的 TCP 流量。接著設定網域清單規則群組規則,以允許 TCP (TLS) 流程從網域清單規則群組傳送到允許的網域。您可以設定含關鍵字 flow 的網域清單規則群組和標準規則群組規則,來達成此目的。
本文使用其中一種分散式部署模型。此模型使用用戶端 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體和網際網路閘道,保護工作負載公有子網路之間的流量。防火牆政策規則順序設定為預設處理行動順序。
注意: Amazon Virtual Private Cloud (Amazon VPC) 主控台只會顯示先前設定的規則選項,您無法新增規則選項。如需更多資訊,請參閱 Standard stateful rule groups in AWS Network Firewall (AWS Network Firewall 的標準具狀態規則群組)。
使用 AWS CloudFormation 或 API 即可為標準規則群組規則指定規則選項。本文中的範例使用 AWS Command Line Interface (AWS CLI)。
解決方法
注意: 如果您在執行 AWS CLI 命令時收到錯誤訊息,請參閱 Troubleshooting errors for the AWS CLI (對 AWS CLI 錯誤進行疑難排解)。此外,請確認您使用的是最新版 AWS CLI。
先決條件
確認已允許 Amazon EC2 執行個體在 VPC 安全群組和網路 ACL 中的 TCP 連接埠 443 上傳送流量。
注意: 在本文的程式碼範例中,使用了刪節號 (...) 省略輸出結果。
設定不含關鍵字 flow 的網域清單規則群組和標準規則群組
您在設定網域清單規則群組和標準規則群組時,組態可能與範例類似。如果建立網域清單規則群組和標準規則群組,您必須使用關鍵字 flow。如果不使用關鍵字 flow,則您可能會遇到範例中的問題。
在下列範例中,Amazon VPC 主控台建立了網域清單規則群組。規則允許 HTTPS 流量傳輸至 example.com:
Domain name source: example.com Source IPs type: Default Protocol: HTTPs Action: Allow
注意: 動作設為 Allow 的網域清單規則群組會產生另一個規則。此規則會拒絕與網域規格不符的指定通訊協定類型流量。如需更多資訊,請參閱 Domain filtering (網域篩選)。
Amazon VPC 主控台會建立具有下列組態的標準規則群組規則:通訊協定 TCP、來源 Any、目的地 Any、來源連接埠 Any、目的地連接埠 Any、方向 Forward,以及動作 Drop。
當您傳送要求至允許的網域以測試規則組態時,流量會遭到封鎖,而您會收到 "connection timed out" 錯誤訊息:
$ curl -kv -so /dev/null https://example.com * Trying [IP_ADDRESS]... * connect to [IP_ADDRESS] port 443 failed: Connection timed out * Failed to connect to example.com port 443 after 129180 ms: Connection timed out * Closing connection 0
此組態會導致所有 TCP 流量遭捨棄且連線逾時。這包括封鎖 TCP 型流量傳輸至允許的網域 example.com。
允許透過 HTTPS 進行** example.com **的網域清單規則群組規則失敗,因為 TCP 是第一個出現在初始流程中的通訊協定。流程從較低層 TCP 交握開始,並評估拒絕規則。此時沒有可比對的 TLS 通訊協定,因此符合捨棄規則。這會導致 example.com 的所有流量都遭捨棄。
注意: 您可為防火牆的具狀態引擎設定日誌層級,以存取有關篩選流量的詳細資訊。如需更多資訊,請參閱 Logging network traffic from AWS Network Firewall (記錄來自 AWS Network Firewall 的網路流量)。
設定含關鍵字 flow 的網域清單規則群組和標準規則群組規則
使用 Network Firewall 的 describe-rule-group 與 update-rule-group 命令,更新您的標準規則群組規則以包含額外的關鍵字 flow。
-
若要擷取狀態規則群組的 UpdateToken 值,請執行下列 describe-rule-group AWS CLI 命令:
$ aws network-firewall describe-rule-group --rule-group-arn "arn:aws:network-firewall:us-east-1:XXXXXXXX0575:stateful-rulegroup/stateful-rg-5-tuple" --output json注意: 請將 XXXXXXXX0575 替換為您的 AWS 帳戶 ID,並將 stateful-rg-5-tuple 替換為您的規則群組名稱。此命令輸出的部分內容後續會作為其他調整的 JSON 範本。
輸出內容類似以下範例:
{ "UpdateToken": "40b87af5-a20c-4f8c-8afd-6777c81add3c", (...) "RulesSource": { "StatefulRules": [{ "Action": "DROP", "Header": { "Protocol": "TCP", "Source": "Any", "SourcePort": "Any", "Direction": "FORWARD", "Destination": "Any", "DestinationPort": "Any" }, "RuleOptions": [{ "Keyword": "sid", "Settings": [ "5" ] }] }] } (...) } -
若要驗證 JSON 規則檔案內容,請建立含修改後規則組態的 JSON 規則檔案,並執行下列命令:
$ cat tcp-drop-rule-updated.json { "RulesSource": { "StatefulRules": [ { "Action": "DROP", "Header": { "Direction": "FORWARD", "Protocol": "TCP", "Destination": "Any", "Source": "Any", "DestinationPort": "Any", "SourcePort": "Any" }, "RuleOptions": [ { "Keyword": "sid", "Settings": [ "5" ] }, { "Keyword": "flow", "Settings": [ "established, to_server" ] } ] } ] } }在此範例中,當您傳送要求至 example.com 時,關鍵字 flow 允許在評估 TCP 捨棄規則前完成 TCP 交握。在此之後,規則預設動作順序優先運作。網域清單允許** example.com** 相符項目的 HTTPS 規則,進而允許其餘流量通過該流程。傳輸至不允許網域的任何流量都會遭封鎖,以及任何已建立的 TCP 流量。
-
若要更新標準規則群組,請使用 UpdateToken 值與 JSON 規則檔案執行下列 update-rule-group AWS CLI 命令:
$ aws network-firewall update-rule-group --rule-group-arn "arn:aws:network-firewall:us-east-1:XXXXXXXX0575:stateful-rulegroup/stateful-rg-5-tuple" --update-token 40b87af5-a20c-4f8c-8afd-6777c81add3c --rule-group file://tcp-drop-rule-updated.json --output json注意: 請將 XXXXXXXX0575 替換為您的 AWS 帳戶 ID、將 stateful-rg-5-tuple 替換為您的規則群組名稱,並將 40b87af5-a20c-4f8c-8afd-6777c81add3c 替換為步驟 1 的 UpdateToken 值。
輸出結果會類似下方內容:
{ "UpdateToken": "bf8fe6d4-f13e-406c-90c1-9e3bad2118a7", "RuleGroupResponse": {...}, "LastModifiedTime": "2023-02-07T14:12:14.993000+11:00" } } -
若要驗證狀態規則群組的變更,請執行下列 describe-rule-group AWS CLI 命令:
$ aws network-firewall describe-rule-group --rule-group-arn "arn:aws:network-firewall:us-east-1:XXXXXXXX0575:stateful-rulegroup/stateful-rg-5-tuple" --output json注意: 請將 XXXXXXXX0575 替換為您的 AWS 帳戶 ID,並將 stateful-rg-5-tuple 替換為您的規則群組名稱。
輸出內容類似以下範例:
{(...) "RulesSource": { "StatefulRules": [ { "Action": "DROP", "Header": { "Protocol": "TCP", "Source": "Any", (...) }, "RuleOptions": [ { "Keyword": "sid", "Settings": [ "5" ] }, { "Keyword": "flow", "Settings": [ "established, to_server" ] (...) } }, "RuleGroupResponse": {...}, "LastModifiedTime": "2023-02-07T14:12:14.993000+11:00" } }注意: 在前述輸出中,established, to_server 反映了 update-rule-group 命令的變更。
-
若要確認網域清單規則群組和標準規則群組皆能正確篩選流量,請執行下列命令:
$ curl -kv -so /dev/null https://example.com * Trying [IP_ADDRESS]... * Connected to example.com ([IP_ADDRESS]) port 443 (#0) (...) > GET / HTTP/1.1 > Host: example.com (...) < HTTP/1.1 200 OK (...)輸出顯示,傳輸至允許網域 example.com 的 HTTPS 流量成功,符合設定。
在下個範例中,傳輸至不允許網域的 HTTPS 流量如預期遭封鎖:
$ curl -m 5 -kv -so /dev/null https://www.amazon.com * Trying [IP_ADDRESS]... * Connected to www.amazon.com ([IP_ADDRESS]) port 443 (#0) (...) * TLSv1.2 (OUT), TLS handshake, Client hello (1): (...) * Operation timed out after 5000 milliseconds with 0 out of 0 bytes received * Closing connection 0
(選擇性) 若要確認其他已建立的 TCP 流量也遭封鎖,請執行下列 s3 ls AWS CLI 命令:
aws s3 ls --cli-read-timeout 30 --debug
相關資訊
Hands-on walkthrough of the AWS Network Firewall flexible rules engine – Part 1 (AWS Network Firewall 彈性規則引擎實作逐步解說 – 第 1 部分)
- 語言
- 中文 (繁體)

This article was reviewed and updated on 2026-02-22.
相關內容
已提問 2 年前
AWS 官方已更新 6 個月前