AWS WAF Classic 使用 的範例 AWS CLI - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS WAF Classic 使用 的範例 AWS CLI

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 來執行動作和實作常見案例 AWS WAF Classic。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 put-logging-configuration

AWS CLI

使用指定的 Kinesis Firehose 串流 ARN 建立 Web ACL ARN 的記錄組態

下列put-logging-configuration範例顯示 WAF 與 CloudFront 的記錄組態。

aws waf put-logging-configuration \ --logging-configuration ResourceArn=arn:aws:waf::123456789012:webacl/3bffd3ed-fa2e-445e-869f-a6a7cf153fd3,LogDestinationConfigs=arn:aws:firehose:us-east-1:123456789012:deliverystream/aws-waf-logs-firehose-stream,RedactedFields=[]

輸出:

{ "LoggingConfiguration": { "ResourceArn": "arn:aws:waf::123456789012:webacl/3bffd3ed-fa2e-445e-869f-a6a7cf153fd3", "LogDestinationConfigs": [ "arn:aws:firehose:us-east-1:123456789012:deliverystream/aws-waf-logs-firehose-stream" ] } }

下列程式碼範例示範如何使用 update-byte-match-set

AWS CLI

更新位元組比對集

下列update-byte-match-set命令會刪除 ByteMatchSet 中的 ByteMatchTuple 物件 (篩選條件): ByteMatchSet

aws waf update-byte-match-set --byte-match-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates Action="DELETE",ByteMatchTuple={FieldToMatch={Type="HEADER",Data="referer"},TargetString="badrefer1",TextTransformation="NONE",PositionalConstraint="CONTAINS"}

如需詳細資訊,請參閱 AWS WAF 開發人員指南中的使用字串比對條件。

下列程式碼範例示範如何使用 update-ip-set

AWS CLI

更新 IP 集

下列update-ip-set命令會使用 IPv4 地址更新 IPSet,並刪除 IPv6 地址:

aws waf update-ip-set --ip-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates Action="INSERT",IPSetDescriptor={Type="IPV4",Value="12.34.56.78/16"},Action="DELETE",IPSetDescriptor={Type="IPV6",Value="1111:0000:0000:0000:0000:0000:0000:0111/128"}

或者,您可以使用 JSON 檔案來指定輸入。例如:

aws waf update-ip-set --ip-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates file://change.json

JSON 檔案的內容為:

[ { "Action": "INSERT", "IPSetDescriptor": { "Type": "IPV4", "Value": "12.34.56.78/16" } }, { "Action": "DELETE", "IPSetDescriptor": { "Type": "IPV6", "Value": "1111:0000:0000:0000:0000:0000:0000:0111/128" } } ]

如需詳細資訊,請參閱 AWS WAF 開發人員指南中的使用 IP 比對條件。

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateIpSet

下列程式碼範例示範如何使用 update-rule

AWS CLI

更新規則

下列update-rule命令會刪除規則中的述詞物件:

aws waf update-rule --rule-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates Action="DELETE",Predicate={Negated=false,Type="ByteMatch",DataId="MyByteMatchSetID"}

如需詳細資訊,請參閱 AWS WAF 開發人員指南中的使用規則。

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateRule

下列程式碼範例示範如何使用 update-size-constraint-set

AWS CLI

更新大小限制集

下列update-size-constraint-set命令會刪除大小限制集中的 SizeConstraint 物件 (篩選條件):

aws waf update-size-constraint-set --size-constraint-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates Action="DELETE",SizeConstraint={FieldToMatch={Type="QUERY_STRING"},TextTransformation="NONE",ComparisonOperator="GT",Size=0}

如需詳細資訊,請參閱 AWS WAF 開發人員指南中的使用大小限制條件。

下列程式碼範例示範如何使用 update-sql-injection-match-set

AWS CLI

更新 SQL Injection Match Set

下列update-sql-injection-match-set命令會刪除 SQL 注入比對集中的 SqlInjectionMatchTuple 物件 (篩選條件):

aws waf update-sql-injection-match-set --sql-injection-match-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates Action="DELETE",SqlInjectionMatchTuple={FieldToMatch={Type="QUERY_STRING"},TextTransformation="URL_DECODE"}

如需詳細資訊,請參閱 AWS WAF 開發人員指南中的使用 SQL Injection Match 條件。

下列程式碼範例示範如何使用 update-web-acl

AWS CLI

更新 Web ACL

下列update-web-acl命令會刪除 WebACL 中的ActivatedRule物件。

aws waf update-web-acl --web-acl-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates Action="DELETE",ActivatedRule='{Priority=1,RuleId="WAFRule-1-Example",Action={Type="ALLOW"},Type="REGULAR"}'

輸出:

{ "ChangeToken": "12cs345-67cd-890b-1cd2-c3a4567d89f1" }

如需詳細資訊,請參閱 AWS WAF、 AWS Firewall Manager 和 AWS Shield 進階開發人員指南中的使用 Web ACLs

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateWebAcl

下列程式碼範例示範如何使用 update-xss-match-set

AWS CLI

更新 XSSMatchSet

下列update-xss-match-set命令會刪除 XssMatchSet 中的 XssMatchTuple 物件 (篩選條件): XssMatchSet

aws waf update-xss-match-set --xss-match-set-id a123fae4-b567-8e90-1234-5ab67ac8ca90 --change-token 12cs345-67cd-890b-1cd2-c3a4567d89f1 --updates Action="DELETE",XssMatchTuple={FieldToMatch={Type="QUERY_STRING"},TextTransformation="URL_DECODE"}

如需詳細資訊,請參閱 AWS WAF 開發人員指南中的使用跨網站指令碼比對條件。

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateXssMatchSet