

# カウントリファラー、IP アドレス、一致ルール
<a name="query-examples-waf-logs-count"></a>

このセクションの例では、対象となるログ項目のカウントをクエリします。
+ [Count the number of referrers that contain a specified term](#waf-example-count-referrers-with-specified-term)
+ [Count all matched IP addresses in the last 10 days that have matched excluded rules](#waf-example-count-matched-ip-addresses)
+ [Group all counted managed rules by the number of times matched](#waf-example-group-managed-rules-by-times-matched)
+ [Group all counted custom rules by number of times matched](#waf-example-group-custom-rules-by-times-matched)

**Example – 指定された用語を含むリファラーの数を計上する**  
次のクエリは、指定された日付範囲内での「amazon」という用語を含むリファラーの数を計上します。  

```
WITH test_dataset AS 
  (SELECT header FROM waf_logs
    CROSS JOIN UNNEST(httprequest.headers) AS t(header) WHERE "date" >= '2021/03/01'
    AND "date" < '2021/03/31')
SELECT COUNT(*) referer_count 
FROM test_dataset 
WHERE LOWER(header.name)='referer' AND header.value LIKE '%amazon%'
```

**Example – 一致する除外ルールがある、過去 10 日間のすべての一致した IP アドレスを計上する**  
次のクエリは、IP アドレスがルールグループ内の除外ルールに一致した過去 10 日間の回数を計上します。  

```
WITH test_dataset AS 
  (SELECT * FROM waf_logs 
    CROSS JOIN UNNEST(rulegrouplist) AS t(allrulegroups))
SELECT 
  COUNT(*) AS count, 
  "httprequest"."clientip", 
  "allrulegroups"."excludedrules",
  "allrulegroups"."ruleGroupId"
FROM test_dataset 
WHERE allrulegroups.excludedrules IS NOT NULL AND from_unixtime(timestamp/1000) > now() - interval '10' day
GROUP BY "httprequest"."clientip", "allrulegroups"."ruleGroupId", "allrulegroups"."excludedrules"
ORDER BY count DESC
```

**Example – カウントされたすべてのマネージドルールをマッチした回数でグループ化する**  
2022 年 10 月 27 日より前にウェブ ACL 設定でルールグループのルールアクションをカウントに設定した場合、AWS WAF はウェブ ACL JSON 内のオーバーライドを `excludedRules` として保存しました。これで、ルールをカウントにオーバーライドする JSON 設定が `ruleActionOverrides` 設定に追加されました。詳しくは、「*AWS WAF デベロッパーガイド*」の「[ルールグループのアクションオーバーライド](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-rule-group-override-options.html)」をご覧ください。新しいログ構造からカウントモードのマネージドルールを抽出するには、次の例のように、`excludedRules` フィールドの代わりに `ruleGroupList` セクションの `nonTerminatingMatchingRules` をクエリします。  

```
SELECT
 count(*) AS count,
 httpsourceid,
 httprequest.clientip,
 t.rulegroupid, 
 t.nonTerminatingMatchingRules
FROM "waf_logs" 
CROSS JOIN UNNEST(rulegrouplist) AS t(t) 
WHERE action <> 'BLOCK' AND cardinality(t.nonTerminatingMatchingRules) > 0 
GROUP BY t.nonTerminatingMatchingRules, action, httpsourceid, httprequest.clientip, t.rulegroupid 
ORDER BY "count" DESC 
Limit 50
```

**Example – カウントされたすべてのカスタムルールをマッチした回数でグループ化する**  
次のクエリは、カウントされたすべてのカスタムルールをマッチした回数でグループ化します。  

```
SELECT
  count(*) AS count,
         httpsourceid,
         httprequest.clientip,
         t.ruleid,
         t.action
FROM "waf_logs" 
CROSS JOIN UNNEST(nonterminatingmatchingrules) AS t(t) 
WHERE action <> 'BLOCK' AND cardinality(nonTerminatingMatchingRules) > 0 
GROUP BY t.ruleid, t.action, httpsourceid, httprequest.clientip 
ORDER BY "count" DESC
Limit 50
```

カスタムルールとマネージドルールグループのログの場所については、「*AWS WAF デベロッパーガイド*」の「[モニタリングとチューニング](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-testing-activities.html)」を参照してください。