labunix's blog

labunixのラボUnix

CloudTrailのログ検索を効率化するために、debian busterにaws-cli/2.Xを入れてみる。

■CloudTrailのログ検索を効率化するために、debian busterにaws-cli/2.Xを入れてみる。
 終わってから気づいたけど、以下の公式の方法の方が良かったのかも知れない。

 Linux での AWS CLI バージョン 2 のインストール、更新、アンインストール
 https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-cliv2-linux.html

■後述のCloudTrailのログ「userAgent: aws-cli/1.16.113 Python/3.7.3 Linux/4.19.0-13-amd64 botocore/1.12.103」
 から、Python/3.7.3と、botocore/2(?)、aws-cli/2にすればよいことがわかる。
 これも終わってから気づいたけど、やり方は同じだった。

 AWS CLI v2をpipからインストールしてみた
 https://dev.classmethod.jp/articles/install-aws-cli-v2-from-sourcecode/

■既存のpip listにあるのが1.6.113なので、githubからpip installする。

$ lsb_release -d
Description:	Debian GNU/Linux 10 (buster)

$ python3 --version
Python 3.7.3

$ dpkg -l | awk '$1 ~ /ii/ && $2 ~ /aws|botocore/{print $2,$3}'
awscli 1.16.113-1
python3-botocore 1.12.103+repack-1

$ python3 -m pip list | grep "aws\|botocore"
awscli              1.16.113
botocore            1.12.103

$ python3 -m pip install https://github.com/boto/botocore/archive/v2.tar.gz
$ python3 -m pip install https://github.com/aws/aws-cli/archive/v2.tar.gz

$ python3 -m pip list | grep "aws\|botocore"
awscli              2.1.16   
botocore            2.0.0.dev84

$ which aws
/usr/bin/aws

$ file /usr/bin/aws
/usr/bin/aws: Python script, ASCII text executable

$ aws --version
aws-cli/2.1.16 Python/3.7.3 Linux/4.19.0-13-amd64 source/x86_64.debian.10 prompt/off

$ aws iam list-users | jq -r '.Users[].UserName' | wc -l
3

■「cloudtrail lookup-events」の検索ログとして、aws-cli/2.Xのログが残った。

$ date;aws cloudtrail lookup-events \
    --start-time $(TZ=+9 date --date '1 hour ago' '+%Y-%m-%dT%H:%MZ') \
    --end-time   $(TZ=+9 date '+%Y-%m-%dT%H:%MZ') | \
    jq -r '.Events[].CloudTrailEvent' | jq -r . | grep userAgent | sort -V | uniq -c
202118日 金曜日 01:31:14 JST
     22   "userAgent": "aws-cli/2.1.16 Python/3.7.3 Linux/4.19.0-13-amd64 source/x86_64.debian.10 prompt/off command/cloudtrail.lookup-events",

■aws-cli/1では何が良くなかったのか。
 CloudTrailのログからどうやって有益な情報を抽出するか考えていた。
 余計なことしないでS3のバケットに入れていればよかったのかもしれない。
 おそらくマネージメントコンソールから証跡の作成を行えばJSTになるはず。

■昨日のCloudWatchログを取得、検索するスクリプトを作ってみた。
 以下が準備。

$ git clone https://github.com/labunix/lsec
$ git clone https://github.com/labunix/aws

■正規表現で検索出来るので、下記ですべて検索となる。
 「aws_cloudtrail_lookup-events_yesterday.txt」が出来る。

$ ./aws-cloudtrail-logsearch . | wc -l
803

■昨日とは?
 UTC時間なので、+9時間(昨日の18:00〜今日の18:00)

$ cat aws_cloudtrail_lookup-events_yesterday.txt | \
    jq -r '.Events[].CloudTrailEvent' | jq -r . | \
    awk '/eventTime/{print substr($NF,2,13) | "sort | uniq -c"}'
      1 2021-01-07T04
      3 2021-01-07T05
     21 2021-01-07T06

■日本時間に直すと18時〜20時代のこと。

$ cat aws_cloudtrail_lookup-events_yesterday.txt | \
    jq -r '.Events[].CloudTrailEvent' | jq -r . | \
    awk '/eventTime/{print substr($NF,2,11) (substr($NF,13,2)+9)%24 | "sort | uniq -c"}'
      1 2021-01-07T13
      3 2021-01-07T14
     21 2021-01-07T15

■昨日は誰が?

$ ./aws-cloudtrail-logsearch arn:aws:iam:: | \
  awk '/arn.:/&&/:root|:user/ \
    {gsub("[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]","XXXXXXXXXXXX",$0); \
     gsub("^  *|,|\042","",$0); \
     print $0 | "sort -uV"}'
arn: arn:aws:iam::XXXXXXXXXXXX:user/labunix-admin
arn: arn:aws:iam::XXXXXXXXXXXX:user/labunix-cloudshell

■昨日、labunix-cloudshellは「List|Get|Describe」以外で何をした?⇒変更操作はあったか?

$ ./aws-cloudtrail-logsearch user/labunix-cloudshell | awk '/eventName/&& !/List|Get|Describe/' | sort -V | uniq -c


■昨日、labunix-cloudshellは「List|Get|Describe」の何をした?

$ ./aws-cloudtrail-logsearch user/labunix-cloudshell | awk '/eventName/' | sort -V | uniq -c
      2   "eventName": "GetAccountPasswordPolicy",
      2   "eventName": "GetAccountSummary",
      2   "eventName": "ListAccessKeys",
      2   "eventName": "ListAccountAliases",
      3   "eventName": "ListRepositories",

■昨日、labunix-cloudshellが行った「ListRepositories」イベントの詳細は?

$ ./aws-cloudtrail-logsearch user/labunix-cloudshell | ./myscripts/lsec -sep '{' ListRepositories
{
  "eventVersion": "1.08",
  "userIdentity": {
    "type": "IAMUser",
    "principalId": "XXXXXXXXXXXXXXXXXXXXX",
    "arn": "arn:aws:iam::XXXXXXXXXXXX:user/labunix-cloudshell",
    "accountId": "XXXXXXXXXXXX",
    "accessKeyId": "XXXXXXXXXXXXXXXXXXXX",
    "userName": "labunix-cloudshell",
    "sessionContext": {
      "sessionIssuer": {},
      "webIdFederationData": {},
      "attributes": {
        "mfaAuthenticated": "false",
        "creationDate": "2021-01-07T06:26:56Z"
      }
    }
  },
  "eventTime": "2021-01-07T06:30:10Z",
  "eventSource": "codecommit.amazonaws.com",
  "eventName": "ListRepositories",
  "awsRegion": "us-east-2",
  "sourceIPAddress": "XXX.XXX.XXX.XXX",
  "userAgent": "aws-cli/2.0.58 Python/3.7.3 Linux/4.14.209-160.335.amzn2.x86_64 exec-env/CloudShell exe/x86_64.amzn.2 command/codecommit.list-repositories",
  "errorCode": "AccessDenied",
  "errorMessage": "User: arn:aws:iam::XXXXXXXXXXXX:user/labunix-cloudshell is not authorized to perform: codecommit:ListRepositories",
  "requestParameters": null,
  "responseElements": null,
  "requestID": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "eventID":   "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "readOnly": true,
  "eventType": "AwsApiCall",
  "managementEvent": true,
  "eventCategory": "Management",
  "recipientAccountId": "XXXXXXXXXXXX"
}

■昨日、labunix-cloudshellはどこからアクセスした?

$ ./aws-cloudtrail-logsearch user/labunix-cloudshell | awk '/userAgent/{gsub("\042|,","",$0);print $0 | "sort -uV"}'
  userAgent: aws-cli/2.0.58 Python/3.7.3 Linux/4.14.209-160.335.amzn2.x86_64 exec-env/CloudShell exe/x86_64.amzn.2 command/codecommit.list-repositories
  userAgent: console.amazonaws.com

■過去90日間のログから、CloudShellというかaws-cli/2.XではUserAgentに操作内容が残る様子。

$ cat aws_cloudtrail_lookup-events_90days.txt* | \
  jq -r '.Events[].CloudTrailEvent' | jq -r . | \
  awk '/userAgent/&&/aws-cli.2/{gsub("\042|,","",$0);print $2,$3,$5,$NF | "sort -uV"}'
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/codecommit.list-repositories
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.get-credential-report
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.get-policy
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.get-user
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.list-groups
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.list-mfa-devices
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.list-policies
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.list-roles
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.list-signing-certificates
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.list-users
aws-cli/2.0.58 Python/3.7.3 exec-env/CloudShell command/iam.list-virtual-mfa-devices

■aws-cli/1.XではUserAgentに操作内容が残らない。

$ cat aws_cloudtrail_lookup-events_90days.txt* | \
  jq -r '.Events[].CloudTrailEvent' | jq -r . | \
  awk '/userAgent/&&/aws-cli.1/{gsub("\042|,","",$0);print $0 | "sort -uV"}'
  userAgent: aws-cli/1.16.113 Python/3.7.3 Linux/4.19.0-13-amd64 botocore/1.12.103