2つの異なるEC2 Instance Connect EndpointでEC2に接続すると、CloudTrailのイベント履歴はどう変わるのか?

今回は2つの異なるEC2 Instance Connect EndpointでEC2に接続して、ClouTrailに記録されるイベント履歴にどう違いが出るのか比較してみます。
2023.09.25

はじめに

こんにちは、おのやんです。

みなさん、2つの異なるEC2 Instance Connect EndpointでEC2に接続すると、CloudTrailのイベント履歴はどう変わるのか、気になりませんか?私はむっちゃ気になります。

別の記事で、EC2インスタンスに接続した際にCloudTrailに記録されるイベント履歴をローカルのPythonから取得しました。

これらの調査を行う中で、ふと「EC2は同じにして、EC2 Instance Connect Endpointを変えると、イベント履歴にはどういう違いが出るのか?」と思いました。

ということで今回は、2つの異なるEC2 Instance Connect EndpointでEC2に接続して、ClouTrailに記録されるイベント履歴にどう違いが出るのか比較したいと思います。

前提条件

今回使用するEC2インスタンスのスペックは以下の通りです。

  • マシンイメージ: Amazon Linux 2023 AMI (ami-079cd5448deeace01 64 ビット x86)
  • インスタンスタイプ: t2.micro

またイベント履歴の取得には、こちらの記事の記載のあるPythonコードを参考にしました。

get_event_history.py

import boto3

aws_region = "ap-northeast-1"

session = boto3.Session(profile_name="get-event-history", region_name=aws_region)
cloudtrail_client = session.client("cloudtrail")

response = cloudtrail_client.lookup_events(
    LookupAttributes=[
        {"AttributeKey": "ReadOnly", "AttributeValue": "false"},
    ],
    MaxResults=10,
)

events = response.get("Events", [])
for event in events:
    print(event)
    print("\n")

同様のイベント履歴取得をこちらの記事でも紹介しているので、よければ参考にしてもらえれば、と思います。

調査方法

EC2 Instance Connect Endpointは、ひとつのサブネットにひとつしか配置できません。そのため、以下の流れでエンドポイントを変えながら調査していきます。

    1. EC2インスタンスが動いているVPCのサブネットにeic-endpoint-1を作成する
    2. eic-endpoint-1経由でAWS マネジメントコンソールからEC2インスタンスに接続する
    3. Pythonのコードからイベント履歴を取得する
    4. VPCに作成したeic-endpoint-1を削除する
    5. EC2インスタンスが動いているVPCのサブネットに、新たにeic-endpoint-2を作成する
    6. eic-endpoint-2経由でAWSマネジメントコンソールからEC2インスタンスに接続する
    7. Pythonのコードからイベント履歴を取得する

取得されたイベント

eic-endpoint-1eic-endpoint-2でそれぞれ取得したイベント履歴を以下に載せます。

なお、今回はOpenTunnelイベントとSendSSHPublicKeyイベントが取得されたので、各イベントごとに比較していきます。

eic-endpoint-1で取得されたイベント

OpenTunnel at eic-endpoint-1

{
    "EventId": "6a88...",
    "EventName": "OpenTunnel",
    "ReadOnly": "false",
    "AccessKeyId": "ASIA...",
    "EventTime": datetime.datetime(2023,9,21,10,24,44, tzinfo=tzlocal()),
    "EventSource": "ec2-instance-connect.amazonaws.com",
    "Username": "username",
    "Resources": [],
    "CloudTrailEvent": '{
        "eventVersion": "1.08",
        "userIdentity": {
            "type": "AssumedRole",
            "principalId": "AROA...:username",
            "arn": "arn:aws:sts::1234...:assumed-role/username/username",
            "accountId": "1234...",
            "accessKeyId": "ASIA...",
            "userName": ""
        },
        "eventTime": "2023-09-21T01:24:44Z",
        "eventSource": "ec2-instance-connect.amazonaws.com",
        "eventName": "OpenTunnel",
        "awsRegion": "ap-northeast-1",
        "sourceIPAddress": "12.34...",
        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
        "requestParameters": {
            "instanceConnectEndpointId": "eice-0f94...",
            "maxTunnelDuration": "3600",
            "remotePort": "22",
            "privateIpAddress": "12.34..."
        },
        "responseElements": null,
        "requestID": "626d...",
        "eventID": "6a88...",
        "readOnly": false,
        "resources": [
            {
                "accountId": "1234...",
                "type": "AWS::EC2::InstanceConnectEndpoint",
                "ARN": "arn:aws:ec2:ap-northeast-1:1234...:instance-connect-endpoint/eice-0f94..."
            }
        ],
        "eventType": "AwsApiCall",
        "managementEvent": true,
        "recipientAccountId": "1234...",
        "eventCategory": "Management"
    }',
}

SendSSHPublicKey at eic-endpoint-1

{
    "EventId": "b4df...",
    "EventName": "SendSSHPublicKey",
    "ReadOnly": "false",
    "AccessKeyId": "ASIB...",
    "EventTime": datetime.datetime(2023,9,21,10,24,44, tzinfo=tzlocal()),
    "EventSource": "ec2-instance-connect.amazonaws.com",
    "Username": "username",
    "Resources": [
        {
            "ResourceType": "AWS::EC2::Instance",
            "ResourceName": "i-0df5..."
        }
    ],
    "CloudTrailEvent": '{
        "eventVersion": "1.08",
        "userIdentity": {
            "type": "AssumedRole",
            "principalId": "AROA...",
            "arn": "arn:aws:sts::1234...:assumed-role/username/username",
            "accountId": "1234...",
            "accessKeyId": "ASIA...",
            "sessionContext": {
                "sessionIssuer": {
                    "type": "Role",
                    "principalId": "AROA...",
                    "arn": "arn:aws:iam::1234...:role/username",
                    "accountId": "1234...",
                    "userName": "username"
                },
                "webIdFederationData": {},
                "attributes": {
                    "creationDate": "2023-09-21T01:15:18Z",
                    "mfaAuthenticated": "true"
                }
            }
        },
        "eventTime": "2023-09-21T01:24:44Z",
        "eventSource": "ec2-instance-connect.amazonaws.com",
        "eventName": "SendSSHPublicKey",
        "awsRegion": "ap-northeast-1",
        "sourceIPAddress": "12.34...",
        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
        "requestParameters": {
            "instanceId": "i-0df5...",
            "instanceOSUser": "ec2-user",
            "sSHPublicKey": "ssh-ed25519 AAAA..."
        },
        "responseElements": {
            "requestId": "b3e3...",
            "success": true
        },
        "requestID": "b3e3...",
        "eventID": "b4df...",
        "readOnly": false,
        "eventType": "AwsApiCall",
        "managementEvent": true,
        "recipientAccountId": "1234...",
        "eventCategory": "Management",
        "tlsDetails": {
            "tlsVersion": "TLSv1.3",
            "cipherSuite": "TLS_AES_128_GCM_SHA256",
            "clientProvidedHostHeader": "ec2-instance-connect.ap-northeast-1.amazonaws.com"
        },
        "sessionCredentialFromConsole": "true"
    }',
}

eic-endpoint-2で取得されたイベント

OpenTunnel at eic-endpoint-2

{
    "EventId": "85f0...",
    "EventName": "OpenTunnel",
    "ReadOnly": "false",
    "AccessKeyId": "ASIB...",
    "EventTime": datetime.datetime(2023,9,21,15,38,34, tzinfo=tzlocal()),
    "EventSource": "ec2-instance-connect.amazonaws.com",
    "Username": "username",
    "Resources": [],
    "CloudTrailEvent": '{
        "eventVersion": "1.08",
        "userIdentity": {
            "type": "AssumedRole",
            "principalId": "AROA...:username",
            "arn": "arn:aws:sts::1234...:assumed-role/username/username",
            "accountId": "1234...",
            "accessKeyId": "ASIB...",
            "userName": ""
        },
        "eventTime": "2023-09-21T06:38:34Z",
        "eventSource": "ec2-instance-connect.amazonaws.com",
        "eventName": "OpenTunnel",
        "awsRegion": "ap-northeast-1",
        "sourceIPAddress": "12.34...",
        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
        "requestParameters": {
            "instanceConnectEndpointId": "eice-0307",
            "maxTunnelDuration": "3600",
            "remotePort": "22",
            "privateIpAddress": "12.34..."
        },
        "responseElements": null,
        "requestID": "a35d...",
        "eventID": "85f0...",
        "readOnly": false,
        "resources": [
            {
                "accountId": "1234...",
                "type": "AWS::EC2::InstanceConnectEndpoint",
                "ARN": "arn:aws:ec2:ap-northeast-1:1234...:instance-connect-endpoint/eice-0307..."
            }
        ],
        "eventType": "AwsApiCall",
        "managementEvent": true,
        "recipientAccountId": "1234...",
        "eventCategory": "Management"
    }',
}

SendSSHPublicKey at eic-endpoint-2

{
    "EventId": "8a2a",
    "EventName": "SendSSHPublicKey",
    "ReadOnly": "false",
    "AccessKeyId": "ASIB...",
    "EventTime": datetime.datetime(2023,9,21,15,38,33, tzinfo=tzlocal()),
    "EventSource": "ec2-instance-connect.amazonaws.com",
    "Username": "username",
    "Resources": [
        {
            "ResourceType": "AWS::EC2::Instance",
            "ResourceName": "i-0df5..."
        }
    ],
    "CloudTrailEvent": '{
        "eventVersion": "1.08",
        "userIdentity": {
            "type": "AssumedRole",
            "principalId": "AROA...:username",
            "arn": "arn:aws:sts::1234...:assumed-role/username/username",
            "accountId": "1234...",
            "accessKeyId": "ASIB...",
            "sessionContext": {
                "sessionIssuer": {
                    "type": "Role",
                    "principalId": "AROA...",
                    "arn": "arn:aws:iam::1234...:role/username",
                    "accountId": "1234...",
                    "userName": "username"
                },
                "webIdFederationData": {},
                "attributes": {
                    "creationDate": "2023-09-21T06:32:29Z",
                    "mfaAuthenticated": "true"
                }
            }
        },
        "eventTime": "2023-09-21T06:38:33Z",
        "eventSource": "ec2-instance-connect.amazonaws.com",
        "eventName": "SendSSHPublicKey",
        "awsRegion": "ap-northeast-1",
        "sourceIPAddress": "12.34...",
        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
        "requestParameters": {
            "instanceId": "i-0df5...",
            "instanceOSUser": "ec2-user",
            "sSHPublicKey": "ssh-ed25519 AAAA..."
        },
        "responseElements": {
            "requestId": "2b9b...",
            "success": true
        },
        "requestID": "2b9b...",
        "eventID": "8a2a...",
        "readOnly": false,
        "eventType": "AwsApiCall",
        "managementEvent": true,
        "recipientAccountId": "1234...",
        "eventCategory": "Management",
        "tlsDetails": {
            "tlsVersion": "TLSv1.3",
            "cipherSuite": "TLS_AES_128_GCM_SHA256",
            "clientProvidedHostHeader": "ec2-instance-connect.ap-northeast-1.amazonaws.com"
        },
        "sessionCredentialFromConsole": "true"
    }',
}

EC2 Instance Connect Endpointごとの比較

上記のイベント履歴を見てみて、EC2 Instance Connect Endpointごとに異なる部分をまとめてみます。なお、タイムスタンプ情報に差異が生じるのは自明なので除いています。

OpenTunnelイベント履歴の差異

OpenTunnelイベント履歴では、EC2 Instance Connect Endpointを変えると以下の値が変わりました。

  • EventId
  • AccessKeyId
  • CloudTrailEvent.userIdentity.accessKeyId
  • CloudTrailEvent.requestParameters.instanceConnectEndpointId
  • CloudTrailEvent.requestID
  • CloudTrailEvent.eventID
  • CloudTrailEvent.resources[0].ARN

OpenTunnel

{
    "EventId": "85f0...",
    "EventName": "OpenTunnel",
    "ReadOnly": "false",
    "AccessKeyId": "ASIB...",
    "EventTime": datetime.datetime(2023,9,21,15,38,34, tzinfo=tzlocal()),
    "EventSource": "ec2-instance-connect.amazonaws.com",
    "Username": "username",
    "Resources": [],
    "CloudTrailEvent": '{
        "eventVersion": "1.08",
        "userIdentity": {
            "type": "AssumedRole",
            "principalId": "AROA...:username",
            "arn": "arn:aws:sts::1234...:assumed-role/username/username",
            "accountId": "1234...",
            "accessKeyId": "ASIB...",
            "userName": ""
        },
        "eventTime": "2023-09-21T06:38:34Z",
        "eventSource": "ec2-instance-connect.amazonaws.com",
        "eventName": "OpenTunnel",
        "awsRegion": "ap-northeast-1",
        "sourceIPAddress": "12.34...",
        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
        "requestParameters": {
            "instanceConnectEndpointId": "eice-0307",
            "maxTunnelDuration": "3600",
            "remotePort": "22",
            "privateIpAddress": "12.34..."
        },
        "responseElements": null,
        "requestID": "a35d...",
        "eventID": "85f0...",
        "readOnly": false,
        "resources": [
            {
                "accountId": "1234...",
                "type": "AWS::EC2::InstanceConnectEndpoint",
                "ARN": "arn:aws:ec2:ap-northeast-1:1234...:instance-connect-endpoint/eice-0307..."
            }
        ],
        "eventType": "AwsApiCall",
        "managementEvent": true,
        "recipientAccountId": "1234...",
        "eventCategory": "Management"
    }',
}

EventId, CloudTrailEvent.eventID

こちらの値について、Boto3のlookup_eventsのレスポンスの説明には、以下の記述があります。

  • EventId (string) –The CloudTrail ID of the event returned. (返されたイベントの CloudTrail ID - DeepLにて翻訳)

EC2 Instance Connect Endpointに関わらず、これらはイベント自体が別々のものです。そのため、イベントIDに差異が生じています。同様の理由で、CloudTrailEvent.eventIDも異なる値になっていると思われます。 逆にひとつのイベントにおいて、EventIdCloudTrailEvent.eventIDは同じ値になっていることがわかります。

AccessKeyId, CloudTrailEvent.userIdentity.accessKeyId

こちらの値について、Boto3のlookup_eventsのレスポンスの説明には、以下の記述があります。

  • AccessKeyId (string) –The Amazon Web Services access key ID that was used to sign the request. If the request was made with temporary security credentials, this is the access key ID of the temporary credentials.(リクエストの署名に使用された Amazon Web Services のアクセスキー ID。リクエストが一時的なセキュリティ認証情報を使用して行われた場合、これは一時的な認証情報のアクセスキー ID です。 - DeepLにて翻訳)

ここでは、OpenTunnelのリクエストに使われたアクセスキーを表すIDが表示されています。この値に違いがみられることから、各EC2 Instance Connect Endpointごとにアクセスキーを作成・使用しているみたいですね。

CloudTrailEvent.requestParameters.instanceConnectEndpointId

こちらの値は、AWSのCloudTrailレコードの公式ドキュメントに記載があります。

requestParameters

リクエストとともに送信されたパラメータ (ある場合)。これらのパラメータは、該当する AWS のサービスの API リファレンスドキュメントに記載されています。このフィールドの最大サイズは 100 KB です。この制限を超えるコンテンツは切り捨てられます。

今回のケースでは、EC2 Instance ConnectのサービスにてOpenTunnelのリクエストが行われ、その際のパラメータとしてEC2 Instance Connect Endpointが指定された形になります。Endpointを変えてイベント履歴を比較しているため、この項目に差異が見られます。

CloudTrailEvent.requestID

こちらの値も、AWSのCloudTrailレコードの公式ドキュメントに記載があります。

requestID

リクエストを識別する値。呼び出されているサービスがこの値を生成します。このフィールドの最大サイズは 1 KB です。この制限を超えるコンテンツは切り捨てられます。

EC2 Instance Connectのサービスにて別々のEndpointでそれぞれOpenTunnelのリクエストが行われたため、値に差が出ています。

CloudTrailEvent.resources[0].ARN

こちらの値も、AWSのCloudTrailレコードの公式ドキュメントに記載があります。

resources

イベントでアクセスされたリソースのリスト。このフィールドには以下の情報が含まれます。

  • リソース ARN
  • リソース所有者のアカウント ID
  • 以下の形式でのリソースタイプ識別子 : AWS::aws-service-name::>data-type-name

今回アクセスされたリソースはEC2 Instance Connect Endpointです。前述の通り、異なるEndpointにてリクエストを行っていますので、ARNも別の値になっています。

SendSSHPublicKeyイベント履歴の差異

SendSSHPublicKeyイベント履歴では、EC2 Instance Connect Endpointを変えると以下の値が変わりました。

  • EventId
  • AccessKeyId
  • CloudTrailEvent.userIdentity.accessKeyId
  • CloudTrailEvent.sourceIPAddres
  • CloudTrailEvent.requestParameters.sSHPublicKey
  • CloudTrailEvent.responseElements.requestId
  • CloudTrailEvent.requestID
  • CloudTrailEvent.eventID

SendSSHPublicKey

{
    "EventId": "8a2a",
    "EventName": "SendSSHPublicKey",
    "ReadOnly": "false",
    "AccessKeyId": "ASIB...",
    "EventTime": datetime.datetime(2023,9,21,15,38,33, tzinfo=tzlocal()),
    "EventSource": "ec2-instance-connect.amazonaws.com",
    "Username": "username",
    "Resources": [
        {
            "ResourceType": "AWS::EC2::Instance",
            "ResourceName": "i-0df5..."
        }
    ],
    "CloudTrailEvent": '{
        "eventVersion": "1.08",
        "userIdentity": {
            "type": "AssumedRole",
            "principalId": "AROA...:username",
            "arn": "arn:aws:sts::1234...:assumed-role/username/username",
            "accountId": "1234...",
            "accessKeyId": "ASIB...",
            "sessionContext": {
                "sessionIssuer": {
                    "type": "Role",
                    "principalId": "AROA...",
                    "arn": "arn:aws:iam::1234...:role/username",
                    "accountId": "1234...",
                    "userName": "username"
                },
                "webIdFederationData": {},
                "attributes": {
                    "creationDate": "2023-09-21T06:32:29Z",
                    "mfaAuthenticated": "true"
                }
            }
        },
        "eventTime": "2023-09-21T06:38:33Z",
        "eventSource": "ec2-instance-connect.amazonaws.com",
        "eventName": "SendSSHPublicKey",
        "awsRegion": "ap-northeast-1",
        "sourceIPAddress": "12.34...",
        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
        "requestParameters": {
            "instanceId": "i-0df5...",
            "instanceOSUser": "ec2-user",
            "sSHPublicKey": "ssh-ed25519 AAAA..."
        },
        "responseElements": {
            "requestId": "2b9b...",
            "success": true
        },
        "requestID": "2b9b...",
        "eventID": "8a2a...",
        "readOnly": false,
        "eventType": "AwsApiCall",
        "managementEvent": true,
        "recipientAccountId": "1234...",
        "eventCategory": "Management",
        "tlsDetails": {
            "tlsVersion": "TLSv1.3",
            "cipherSuite": "TLS_AES_128_GCM_SHA256",
            "clientProvidedHostHeader": "ec2-instance-connect.ap-northeast-1.amazonaws.com"
        },
        "sessionCredentialFromConsole": "true"
    }',
}

EventId, CloudTrailEvent.eventID

こちらはOpenTunnelのケースと同じく、そもそも記録したイベントが別々のものであったために違いが生じたと考えられます

AccessKeyId, CloudTrailEvent.userIdentity.accessKeyId

こちらもOpenTunnelのケースと同じく、Instance Connectにて一時的な認証情報が使われたため、この値に違いが出たと考えられます。

CloudTrailEvent.sourceIPAddres

こちらについては、CloudTrailレコードのドキュメントに記載があります。

sourceIPAddress リクエストが行われた IP アドレス。サービスコンソールから行われたアクションの場合、報告されるアドレスは、コンソールウェブサーバーではなく、基礎となるカスタマーリソースのものです。AWS のサービスでは、DNS 名のみが表示されます。

こちらは、EC2インスタンスにリクエストを送ったサービスがEC2 Instance Connect Endpointであるためだと思われます。sourceIPAdressは、EC2インスタンスへの接続リクエストがどこからきたのかを示します。今回は異なる2つのEC2 Instance Connect Endpoint経由でEC2インスタンスに接続したため、CloudTrailのイベント履歴にはリクエストを送ったそれぞれのEndpointのアドレスが記録されたと考えられます。

CloudTrailEvent.requestParameters.sSHPublicKey

こちらは、SendSSHPublicKeyのドキュメントに記載があります。

SSHPublicKey

The public key material. To use the public key, you must have the matching private key.

この値が異なることから、Instance ConnectでのEC2接続では各EndpointごとにSSHキーが作成されていると考えられます。

CloudTrailEvent.responseElements.requestId, CloudTrailEvent.requestID

こちらはOpenTunnelのケースのように、EndpointごとにEC2にリクエストを投げているため、値に差が出ています。

さいごに

Endpointに関わるイベント履歴を参照にすることはそこまで多くないと思います。が、こういった情報がいざ必要になった際には本記事が参考になれば幸いです。

では!