☁️
CTHFM: AWS
  • Welcome
  • Getting Started
    • Account Setup
  • AWS CLI
    • AWS CLI Overview
    • Installation
  • AWS Fundamentals
    • AWS Documentation
    • AWS Shared Responsibility Model
    • Organizational Hierarchy
    • AWS Principals
    • IAM Fundamentals
      • IAM Policy Components
      • IAM Documentation References
    • AWS Security Services Overview
    • AWS Core Services
    • AWS Frameworks
    • Regions and Availability Zones
  • SQL
    • SQL Refresher for Threat Hunting
  • Logging Reference
    • Cloudtrail
      • What is Cloudtrail?
      • Setting Up Cloudtrail
      • Cloudtrail Events Structure
      • Filtering and Searching CloudTrail Logs
      • IAM ID Prefixes
      • Additional Resources
      • API References
    • VPCFlow Logs
    • GuardDuty
      • Multi-Account Setup
      • GuardDuty Concepts
      • GuardDuty Finding References
      • S3 Protection
      • Malware Protection
        • EC2 Malware Protection
          • EC2 Protection Resources
          • Monitoring Scans
          • EC2 Malware Protection Events: CloudWatch
        • S3 Malware Protection
          • Enabling S3 Malware Protection
          • After Enabling S3 Malware Protection
          • S3 Malware Resource Plan Status
          • S3 Malware Protection Quotas
      • RDS Protection Enablement
      • Lambda Protection Enablement
      • Trusted IP Lists and Threat Lists in Amazon GuardDuty
      • Remediation Recommendations
      • GuardDuty API Reference
      • GuardDuty Quotas
    • Access Analyzer
      • Setup
      • External Access and Unused Access Analyzer Findings
      • Review Findings
      • Access Analyzer Resources
      • Access Analyzer API Reference
    • AWS Network Firewall
      • Permissions
      • Firewall Log Contents
      • Logging Destinations
      • CloudWatch Firewall Metrics
    • AWS Config
      • Resource Management in AWS Config
      • AWS Config Integrations
      • AWS Config Resources
      • Configuration Item
      • Config Rules
        • Evaluation Modes
  • CloudWatch
    • Amazon CloudWatch
      • CloudWatch Concepts
      • CloudWatch Metrics
        • Filter Pattern Syntax
      • CloudWatch Alarms
        • Alarm Recommendations
      • Subscriptions
      • CloudWatch Agent
      • CloudWatch Insights
        • Supported Logs and Discovered Fields
        • CloudWatch Insights Query Syntax
      • Anomaly Detection
        • Create Anomaly Detector
        • Alarms for Anomaly Detections
      • CloudWatch Filter Syntax
      • CloudWatch Service Quota
  • Athena For Threat Hunting
    • Introduction to Athena
    • Setting Up Athena
    • SQL For Threat Hunters
    • Automated Response
    • Query Best Practices
  • AWS Security Research and Resources
    • AWS Security Blog
    • AWS Goat
    • Cloud Goat
    • Pacu
    • Prowler
    • Scout Suite
  • Threat Hunting in AWS
    • Threat Hunting in AWS
    • Threat Hunting Introduction
    • Threat Hunting Process
      • Hypothesis Generation
      • Investigation
      • Identification
      • Resolution & Follow Up
    • Pyramid of Pain
    • MITRE Att&ck
      • MITRE Att&ck Concepts
      • MITRE Att&CK Data Sources
      • MITRE Att&CK Mitigations
    • MITRE Att&ck: AWS
      • MITRE Att&CK Matrix
      • Amazon Web Services Security Control Mappings
    • AWS Threat Hunting Ideas
      • AWS Threat Hunting Ideas: EC2
      • AWS Threat Hunting Ideas: Lambda
      • AWS Threat Hunting Ideas: SQS
      • AWS Threat Hunting Ideas: SNS
      • AWS Threat Hunting Ideas: RDS
Powered by GitBook
On this page
  • Introduction
  • 1. Searching CloudTrail Logs Using the AWS Management Console
  • 2. Searching CloudTrail Logs Using the AWS CLI
  • 3. Searching CloudTrail Logs Using AWS SDKs
  • 4. Searching CloudTrail Logs Using Amazon Athena
  • 5. Best Practices for Filtering and Searching CloudTrail Logs
  1. Logging Reference
  2. Cloudtrail

Filtering and Searching CloudTrail Logs

Introduction

AWS CloudTrail generates a significant volume of logs, especially in environments with extensive activity across multiple regions and services. Effectively filtering and searching these logs is essential for identifying relevant events, investigating incidents, and maintaining a secure AWS environment. In this lesson, we will explore the various tools and methods available for filtering and searching CloudTrail logs, including the AWS Management Console, AWS CLI, SDKs, and Amazon Athena.

1. Searching CloudTrail Logs Using the AWS Management Console

The AWS Management Console provides a user-friendly interface for searching and filtering CloudTrail logs. This is an ideal starting point for quick queries and basic log analysis.

  1. Accessing CloudTrail Event History:

    • Navigate to the AWS Management Console.

    • Go to the CloudTrail service and select “Event history” from the left-hand menu.

    • The Event history page provides a searchable and filterable list of recent events.

  2. Filtering by Time Range:

    • Use the “Time range” filter to narrow down the events to a specific time period. You can select predefined ranges (e.g., last 24 hours, last 7 days) or define a custom time range.

  3. Filtering by Event Attributes:

    • Event name: Filter events by the specific API call (e.g., RunInstances, PutObject). This is useful when you know the action you’re looking for.

    • Event source: Filter by the AWS service that generated the event (e.g., ec2.amazonaws.com, s3.amazonaws.com).

    • User name: Filter by the IAM user or role that initiated the event. This helps in tracking activities of specific users or roles.

    • Resource name: Filter by specific AWS resources involved in the events. This is particularly useful when monitoring changes to critical resources like S3 buckets or EC2 instances.

  4. Viewing and Exporting Events:

    • Click on any event in the filtered list to view detailed information, including the JSON representation of the event.

    • You can export the filtered events to a CSV file for further analysis or reporting.

2. Searching CloudTrail Logs Using the AWS CLI

For more advanced filtering and automation, the AWS Command Line Interface (CLI) is a powerful tool. It allows you to query CloudTrail logs programmatically and integrate with other command-line tools.

  1. Listing Recent Events:

    • Use the aws cloudtrail lookup-events command to search for recent events in your AWS account. This command allows you to specify filters such as event name, user name, or resource name.

    aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=RunInstances
    • This command will return a list of events where the API call was RunInstances.

  2. Filtering by Multiple Attributes:

    • You can combine multiple filters to narrow down your search results.

    aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=RunInstances AttributeKey=Username,AttributeValue=johndoe
    • This command filters events where the RunInstances API call was made by the user johndoe.

  3. Specifying a Time Range:

    • Use the --start-time and --end-time options to filter events within a specific time range.

    aws cloudtrail lookup-events --start-time "2023-08-31T00:00:00Z" --end-time "2023-09-01T00:00:00Z"
    • This command retrieves events that occurred between August 31, 2023, and September 1, 2023.

  4. Output Formats:

    • The AWS CLI allows you to output the results in different formats, such as JSON or text, making it easier to integrate with other tools or scripts.

    aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=RunInstances --output json

3. Searching CloudTrail Logs Using AWS SDKs

For developers who prefer to work with code, AWS SDKs provide a flexible way to query and filter CloudTrail logs programmatically. SDKs are available for various programming languages, including Python (Boto3), Java, and Node.js.

  1. Example Using Boto3 (Python):

    import boto3
    
    client = boto3.client('cloudtrail')
    
    response = client.lookup_events(
        LookupAttributes=[
            {
                'AttributeKey': 'EventName',
                'AttributeValue': 'RunInstances'
            },
            {
                'AttributeKey': 'Username',
                'AttributeValue': 'johndoe'
            }
        ],
        StartTime='2023-08-31T00:00:00Z',
        EndTime='2023-09-01T00:00:00Z'
    )
    
    for event in response['Events']:
        print(event)
    • This script uses Boto3 to search for RunInstances events initiated by the user johndoe within a specified time range.

  2. Integrating with Other Systems:

    • The SDKs allow you to integrate CloudTrail log searches into larger applications or workflows, such as automated incident response or auditing systems.

4. Searching CloudTrail Logs Using Amazon Athena

For complex queries across large datasets, Amazon Athena provides a powerful SQL-like query interface to search CloudTrail logs stored in S3.

  1. Setting Up Athena for CloudTrail:

    • Ensure that your CloudTrail logs are being delivered to an S3 bucket.

    • Create a database and table in Athena that points to your CloudTrail logs. AWS provides a CloudFormation template that can automate this setup.

    CREATE EXTERNAL TABLE cloudtrail_logs (
        eventVersion STRING,
        userIdentity STRUCT<type:STRING,principalId:STRING,arn:STRING,accountId:STRING,accessKeyId:STRING>,
        eventTime STRING,
        eventSource STRING,
        eventName STRING,
        awsRegion STRING,
        sourceIPAddress STRING,
        userAgent STRING,
        errorCode STRING,
        errorMessage STRING,
        requestParameters STRING,
        responseElements STRING,
        additionalEventData STRING,
        requestID STRING,
        eventID STRING,
        eventType STRING,
        recipientAccountId STRING
    )
    ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
    LOCATION 's3://your-bucket-name/AWSLogs/your-account-id/CloudTrail/'
    TBLPROPERTIES ('classification'='json');
  2. Running Queries in Athena:

    • Once the table is set up, you can use SQL queries to search and filter your CloudTrail logs.

    SELECT eventTime, eventName, userIdentity.arn, sourceIPAddress
    FROM cloudtrail_logs
    WHERE eventName = 'RunInstances'
    AND userIdentity.arn LIKE '%johndoe%'
    AND eventTime BETWEEN '2023-08-31T00:00:00Z' AND '2023-09-01T00:00:00Z';
    • This query retrieves details about RunInstances events performed by johndoe within a specific time range.

  3. Benefits of Using Athena:

    • Athena is particularly useful for performing complex queries, aggregating data, and generating reports across large volumes of CloudTrail logs.

    • Since Athena is a serverless service, you can run queries on-demand without needing to manage any infrastructure.

5. Best Practices for Filtering and Searching CloudTrail Logs

  1. Use Specific Filters:

    • Always apply specific filters (such as event name, username, or time range) to narrow down the search results and reduce noise.

  2. Combine Multiple Tools:

    • Use the AWS Management Console for quick searches, the AWS CLI for automation, SDKs for integration, and Athena for deep, complex analysis.

  3. Automate Common Queries:

    • Automate frequent log searches using scripts or Lambda functions to save time and ensure consistent monitoring.

  4. Monitor for Anomalies:

    • Set up alerts in CloudWatch for specific patterns or anomalies in your CloudTrail logs, such as unauthorized API calls or unusual IP addresses.

PreviousCloudtrail Events StructureNextIAM ID Prefixes

Last updated 8 months ago