複雑なカスタム AWS WAF JSON ルールを作成する方法を教えてください。
最終更新日: 2022 年 7 月 21 日
複雑なカスタム AWS WAF JSON ルールを作成する方法を教えてください。
解決方法
AWS WAF のカスタムルールを追加するときに、ルール JSON エディタで複雑なカスタムルールを作成します。ルールは、AWS WAF コンソールのウェブ ACL とルールグループで作成および管理されます。ルールには、定義されているルールグループまたはウェブ ACL の名前でアクセスできます。
ユースケースで AND、OR、または NOT ロジック (ネストされたステートメント) の組み合わせを必要とするカスタムルールが必要な場合は、JSON エディタを使用してルールを作成する必要があります。詳細については、「AWS WAF ルールステートメント」を参照してください。
ヒント: AWS WAF コンソールにあるルールビジュアルエディタを使用して、ユースケースに似たルールステートメントを作成できます。次に、JSON ステートメントを表示するには、[Rule JSON editor (ルール JSON エディタ)] を選択し、JSON エディタで必要な変更を加えます。
次の例は、独自のカスタムルールロジックを作成する際に参考になるものです:
例 1
これは、リクエストが米国 -US から発信され、URI が /wp-admin/ または /wp-login/ である場合にリクエストを許可するカスタムルールステートメントです:
ルールロジック: リクエストが (米国から) で、URIが (/a OR /b)) の場合
{
"Name": "test",
"Priority": 100,
"Statement": {
"AndStatement": {
"Statements": [
{
"GeoMatchStatement": {
"CountryCodes": [
"US"
]
}
},
{
"OrStatement": {
"Statements": [
{
"ByteMatchStatement": {
"SearchString": "/wp-admin/",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "NONE"
}
],
"PositionalConstraint": "CONTAINS"
}
},
{
"ByteMatchStatement": {
"SearchString": "/wp-login/",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "NONE"
}
],
"PositionalConstraint": "CONTAINS"
}
}
]
}
}
]
}
},
"Action": {
"Allow": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "test"
}
}
例 2
これは、本文に XSS 署名がある場合にリクエストをブロックし、URI /test、/login、/admin を検査から除外するカスタムルールステートメントです:
ルールロジック: リクエストに (本文にXSS署名) があり、URIが (/a OR /b、OR /c) ではない場合。
{
"Name": "XSS_Block_Allow_Uploads",
"Priority": 100,
"Statement": {
"AndStatement": {
"Statements": [
{
"XssMatchStatement": {
"FieldToMatch": {
"Body": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "NONE"
}
]
}
},
{
"NotStatement": {
"Statement": {
"OrStatement": {
"Statements": [
{
"ByteMatchStatement": {
"SearchString": "/test",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "NONE"
}
],
"PositionalConstraint": "EXACTLY"
}
},
{
"ByteMatchStatement": {
"SearchString": "/admin",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "NONE"
}
],
"PositionalConstraint": "EXACTLY"
}
},
{
"ByteMatchStatement": {
"SearchString": "/login",
"FieldToMatch": {
"UriPath": {}
},
"TextTransformations": [
{
"Priority": 0,
"Type": "NONE"
}
],
"PositionalConstraint": "EXACTLY"
}
}
]
}
}
}
}
]
}
},
"Action": {
"Block": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "XSS_Block_Allow_Uploads"
}
}
例 3
これは、カスタムラベル internal を含むものの、ホスト、IP、URI が特定の組み合わせでない場合にリクエストをブロックするカスタムルールステートメントです:
ルールロジック: リクエスト (ラベルを含む) で、(URI、IP、ホスト) でない場合、ブロックします。
{
"Name": "Block_internal_unauthorized",
"Priority": 100,
"Statement": {
"AndStatement": {
"Statements": [
{
"LabelMatchStatement": {
"Scope": "LABEL",
"Key": "internal"
}
},
{
"NotStatement": {
"Statement": {
"AndStatement": {
"Statements": [
{
"ByteMatchStatement": {
"FieldToMatch": {
"UriPath": {}
},
"PositionalConstraint": "EXACTLY",
"SearchString": "/test",
"TextTransformations": [{
"Type": "NONE",
"Priority": 0
}]
}
},
{
"IPSetReferenceStatement": {
"ARN": "arn:aws:wafv2:us-east-1:xxxxxxxxxxxx:regional/ipset/internal_IPs/xxx-xx-xxx"
}
},
{
"ByteMatchStatement": {
"FieldToMatch": {
"SingleHeader": {
"Name": "host"
}
},
"PositionalConstraint": "EXACTLY",
"SearchString": "example.com",
"TextTransformations": [{
"Type": "NONE",
"Priority": 0
}]
}
}
]
}
}
}
}
]
}
},
"Action": {
"Block": {}
},
"VisibilityConfig": {
"SampledRequestsEnabled": true,
"CloudWatchMetricsEnabled": true,
"MetricName": "Block_internal_unauthorized"
}
}