Skip to content

Instantly share code, notes, and snippets.

@Esonhugh
Last active April 2, 2024 07:23
Show Gist options
  • Save Esonhugh/d8849ad3cbf9cfd1c3a7624cd1642f3b to your computer and use it in GitHub Desktop.
Save Esonhugh/d8849ad3cbf9cfd1c3a7624cd1642f3b to your computer and use it in GitHub Desktop.
AWS cloud trail log analysis
import os
SOURCE_FILE="merged_cloudtrail_logs.json"
'''
Sample Event
{
"eventVersion": "1.08",
"userIdentity": {
"type": "Root",
"principalId": "107513503799",
"arn": "arn:aws:iam::107513503799:root",
"accountId": "107513503799",
"accessKeyId": "ASIARSCCN4A3W2FLR6FE",
"sessionContext": {
"sessionIssuer": {},
"webIdFederationData": {},
"attributes": {
"creationDate": "2023-08-26T18:45:20Z",
"mfaAuthenticated": "false"
}
},
"invokedBy": "health.amazonaws.com"
},
"eventTime": "2023-08-26T20:52:05Z",
"eventSource": "health.amazonaws.com",
"eventName": "DescribeEventAggregates",
"awsRegion": "us-east-1",
"sourceIPAddress": "health.amazonaws.com",
"userAgent": "AWS Internal",
"requestParameters": {
"filter": {
"startTimes": [
{
"from": "Aug 19, 2023, 8:52:05 PM"
}
],
"eventStatusCodes": [
"open",
"upcoming"
]
},
"aggregateField": "eventTypeCategory"
},
"responseElements": null,
"requestID": "4ad97058-6edd-40a6-b5f9-cb7fd0cd44e1",
"eventID": "f6dad1c1-c787-476c-a200-d74fde8a5a8e",
"readOnly": true,
"eventType": "AwsApiCall",
"managementEvent": true,
"recipientAccountId": "107513503799",
"eventCategory": "Management",
"sessionCredentialFromConsole": "true"
},
'''
condition = ".eventName == \"GetCallerIdentity\""
# condition = ".eventName == \"DescribeInstances\" and .sourceIPAddress == \"1.12.13.14\""
# condition = '.sourceIPAddress == "1.12.13.14" and .errorCode == "AccessDenied" or .errorCode == "UnauthorizedOperation" '
output_format = [
".eventTime",
".userIdentity.arn",
".sourceIPAddress",
".eventName",
".requestID",
]
jq_command_template = '''cat {sourcefile} | jq -cr '.[] | select( {condition} ) | [{output_format}] | @csv ' | sort '''
cmd = jq_command_template.format(
sourcefile=SOURCE_FILE,
condition=condition,
output_format=", ".join(output_format),
)
# print("command: ", cmd)
print("output: ")
print(", ".join(output_format))
print(os.popen(cmd).read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment