使用 AWS CLI 的 AWS WAF Classic 範例 - AWS Command Line Interface

使用 AWS CLI 的 AWS WAF Classic 範例

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

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

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

主題

動作

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

AWS CLI

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

下列 put-logging-configuration 範例會顯示具有 CloudFront 之 WAF 的記錄組態。

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 物件 (篩選條件):

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 開發人員指南》中的「使用字串比對條件」。

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

以下程式碼範例顯示如何使用 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 命令會刪除規則中的 Predicate 物件:

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 隱碼攻擊比對集

下列 update-sql-injection-match-set 命令會刪除 SQL Injection 隱碼攻擊比對集中的 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 隱碼攻擊比對條件」。

以下程式碼範例顯示如何使用 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 Advanced 開發人員指南》中的使用 Web ACL

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

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

AWS CLI

更新 XSSMatchSet

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

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