概要
やりたいこと
GuardDuty ログをセキュリティ分析したい。そのため、ログがどのような形式でどんな内容なのかを確認する。
GuardDuty ログのフォーマットは S3 にエクスポートしたものを想定。
GuardDuty ログ
実際にどんなアラートが出力されるかは アクティブな結果タイプ を参照する。
ここではログとしてどのなような形式で出力されるかを確認する。
項目
クエリを打つ前に、 GuardDuty のログをテーブル作成時に定義した項目ごとに、ざっくり理解する。
※ GuardDuty ではアラート結果を Findings と呼ぶ
GuardDuty 結果の検索と分析 を参照。
schemaversion
: スキーマバージョンaccountid
: 対象のAWSアカウントのIDregion
: Findings が作成された AWS リージョンpartition
: aws一択id
: Findings の IDarn
: AWS Outpost の Amazon リソースネーム (ARN)?type
: セキュリティ問題について簡潔な形式で記述したもの
参照 : GuardDuty 結果形式resource
: Findings の生成を GuardDuty に求めるアクティビティを実行した AWS リソース、後述service
: Findings の詳細が json 形式で出力されている、後述severity
: GuardDuty が独自に判断した結果の重要度
参照 : GuardDuty 結果の重要度createdate
: 作成日(空だけど自分だけ?)updatedate
: 更新日(空だけど自分だけ?)title
: Findings の詳細なタイトルdescription
: このログ(アラート)の説明
service カラムと resource カラム
GuardDuty の項目のうち service と resource カラムには json がまるごと入っている。
この json は結果タイプによって異なる出力形式となっている。
service
{ "resourcerole": "TARGET", "archived": false, "eventfirstseen": "2020-02-28T09:47:05Z", "evidence": "null", "detectorid": "34b84599b4a9cac0112b7c4c42f8e994", "count": "1590", "action": { "actiontype": "AWS_API_CALL", "awsapicallaction": { "servicename": "athena.amazonaws.com", "remoteipdetails": { "country": { "countryname": "Japan" }, "ipaddressv4": "18.182.25.202", "city": { "cityname": "Tokyo" }, "organization": { "org": "Amazon.com", "asnorg": "AMAZON-02", "isp": "Amazon.com", "asn": "16509" }, "geolocation": { "lon": "139.7532", "lat": "35.6882" } }, "api": "GetQueryExecution", "affectedresources": {}, "callertype": "Remote IP" } }, "servicename": "guardduty", "eventlastseen": "2020-04-17T00:18:51Z", "additionalinfo": {} } |
resource
{ "resourcetype": "AccessKey", "accesskeydetails": { "usertype": "IAMUser", "principalid": "AIDAZSLIDRSO72H43WWCO", "accesskeyid": "ASIAZSLIDRSOQZVVK3E4", "username": "runble1" } } |
これらの json からデータを取り出すために Athena では json_extract_scalar
関数を利用する。
json_extract_scalar 関数
公式のクエリ例を参考にする。json_extract_scalar(service, '$.count') AS Count
で service カラム内の key が count の値を取得している。json_extract_scalar(resource, '$.accesskeydetails.username') AS IAMPrincipal
では resouce カラム内の key が accesskeydetails で username の値を取得している。
SELECT title, severity, type, id, accountid, region, createdate, updatedate, json_extract_scalar(service, '$.count') AS Count, json_extract_scalar(resource, '$.accesskeydetails.username') AS IAMPrincipal, json_extract_scalar(service,'$.action.awsapicallaction.api') AS APIActionCalled FROM gd_logs WHERE type LIKE '%UnauthorizedAccess:IAMUser%' ORDER BY severity desc; |
詳しく公式の JSON からのデータの抽出 を参照。
コメント