CloudTrail and You

 

Introduction

Traditional cyber or IT security folks have difficulty with the Cloud.

The best way to remove the fear of monitoring the Cloud is to use method that we’re used to, which is to think of it an application (please be kind in the comments), such as Apache, SQL, or IIS. For years we have aggregating application logs and ingesting them into logging tools.

With regards of Amazon Web Services AWS, one event sources we want to monitor is called CloudTrail. CloudTrail is:

“AWS CloudTrail is a service that enables governance, compliance, operational auditing, and risk auditing of your AWS account. With CloudTrail, you can log, continuously monitor, and retain account activity related to actions across your AWS infrastructure. CloudTrail provides event history of your AWS account activity, including actions taken through the AWS Management Console, AWS SDKs, command line tools, and other AWS services. This event history simplifies security analysis, resource change tracking, and troubleshooting. In addition, you can use CloudTrail to detect unusual activity in your AWS accounts. These capabilities help simplify operational analysis and troubleshooting.”

- 2021, aws.amazon.com, https://aws.amazon.com/cloudtrail/

The following post will be a basic high level way to retrieve CloudTrail events so that your logging tools can ingest them.

API Keys

Going with the idea that AWS is an application.

Q: How do we interact with applications?

A: Application Programming Interface API.

Q: How do interact with an API?

A: API keys.

Let create an account, policy, group, and keys.

Login AWS Console, and search for AWS Identity and Access Management (IAM)

On the Left-hand side select “Users”, and then click “Add User”

     Provide a “User name”, select access type of “Programmatic access”, and click “Next”



       Click “Create group”. A new browser tab or window will open.




      Select “Choose a service”

  Search and select “CloudTrail”

Under “Access level” -> Under “Read” -> select “LookupEvents”


          Click “Next”

      Name “Policy”, and click “Create Policy”.

      Navigate to the previous tab or window. Click “Refresh”, search the newly create policy, select the policy, name the group, and click “create group”.


API Script

Now that we have API keys with the minimum permission necessary to retrieve CloudTrail events. We now need to interact with the AWS’s API. For that we turn Python, and the module Boto3.

-2021, boto3.amazonaws.com,  https://boto3.amazonaws.com/v1/documentation/api/latest/index.html

The link in the reference above will provided instructions on installing this module, and all the wonderful things that can be done with it. We will focus on the API class CloudTrail.Client.

-2021, boto3.amazonaws.com, https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudtrail.html

Within the API class CloudTrail.Client a method called lookup_event.

-2021, boto3.amazonaws.com, https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudtrail.html#CloudTrail.Client.lookup_events

Since we’re not making the API call within the AWS account that our CloudTrail events are located, we need 4 things to make a successful the API request. Those things are the class “service_name”, and the variables of, “region_name”, “aws_access_key_id”, and “aws_secrect_access_key”. The “service_name” is “cloudtrail”. The “region_name” is the AWS region of which your CloudTrail events are. The “aws_access_key_id”, and “aws_secrect_access_key” we created in the previous section of this post. When piece all parts together our super basic Python script looks like this.


 So, let take at the JSON/dictionary keys.


Our 3 keys are “Events”, “NextToken”, “ResponseMetadata”. The key we care about is “Events”. Let’s take a look at a event. 

 

In the output of the “Events” is the key “EventTime” with a value of datetime. We’ll need to convert this by using the two python modules “datetime”, and “json”.

Within the “json” module there is the method “dumps” that has a parameter called “default” that allows for us to send the json value to a function that if the value is a datetime covert it to a string.


Looking at our new output with see that the key “EventTime” successfully converted, however we now have backslashes within our output.


However, we now have backslashes in our output. The backslashes occur because running the json from our API call through the method “json.dumps”  resulted in double json encoding. To fix this we can use the method “json.JSONDecoder().decode”. This will decode our json to a list. Then we write the list to file remembering to put a line return in between each list value.

Next post we’ll build on this script to continuously pull events. For the file script see the following link.

https://github.com/wubbaroo/cloudtrail_events/blob/main/aws-cloudtrail.py

 



Comments

Popular posts from this blog

Mapping CIS Critical Controls to Gartner Markets

Malware Can Hide But It Must Run

PCAP Doesn't Lie Part 2