Query Details

A03 Python Startup To Aws Secrets

Query

// Standalone KQL for Analytic Rule: AWS cross-cloud - Python startup artifacts and metadata access precede secrets use
// Rule File: analytics/A03-python-startup-to-aws-secrets.yaml | Rule ID: 54f1f278-4d53-4f20-a2a1-0e357f02da03 | Severity: High
// Schedule: 15m / Lookback: 4h | REVIEW ONLY - Do not deploy/schedule without approval.
let XdrEntraTenantId = "REPLACE_WITH_XDR_ENTRA_TENANT_GUID";
let Configured = assert(isnotnull(toguid(XdrEntraTenantId)),
    "Configure the Entra tenant supplying the XDR tables before use.");
let AssetMap = _GetWatchlist('AWS_AssetIdentityMap')
| where Configured and tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("MappingEvidence", "")))
| project AWSAccountId=tostring(column_ifexists("AWSAccountId", "")), AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")), AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")),
    DeviceId=tostring(column_ifexists("DeviceId", "")), XdrTenantId=tolower(tostring(column_ifexists("XdrTenantId", ""))),
    AwsInstanceId=tostring(column_ifexists("AwsInstanceId", "")), ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where MappingRows == 1 and XdrTenantId == tolower(XdrEntraTenantId)
    and isnotempty(DeviceId) and isnotempty(AwsInstanceId) and isnotempty(AwsAccessKeyId);
let Artifacts = DeviceFileEvents
| where Timestamp > ago(4h)
| where ActionType in ("FileCreated", "FileModified")
| where FileName in~ ("cook.pth", "cook.py") and FolderPath contains "site-packages"
| project ArtifactTime=Timestamp, ArtifactIngest=ingestion_time(), DeviceId,
    ArtifactName=FileName, ArtifactPath=FolderPath;
let Metadata = DeviceNetworkEvents
| where Timestamp > ago(2h) and ActionType == "ConnectionSuccess"
| where RemoteIP == "169.254.169.254" and InitiatingProcessFileName startswith "python"
| project MetadataTime=Timestamp, MetadataIngest=ingestion_time(), DeviceId, DeviceName,
    ProcessUniqueId=InitiatingProcessUniqueId, MetadataReportId=ReportId;
AWSCloudTrail
| where TimeGenerated > ago(2h)
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| project AwsTime=TimeGenerated, AwsIngest=ingestion_time(), AwsEventId, EventName,
    AWSAccountId=UserIdentityAccountId, AwsPrincipalArn=UserIdentityArn,
    AwsAccessKeyId=UserIdentityAccessKeyId, RecipientAccountId, SourceIpAddress
| where isnotempty(AWSAccountId) and isnotempty(AwsPrincipalArn)
| join kind=inner AssetMap on AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where AwsTime between (ValidFrom .. ValidTo)
| join kind=inner Metadata on DeviceId
| where MetadataTime between (ValidFrom .. ValidTo)
    and AwsTime between (MetadataTime .. (MetadataTime + 30m))
| join kind=inner Artifacts on DeviceId
| where ArtifactTime between (ValidFrom .. ValidTo)
    and MetadataTime between (ArtifactTime .. (ArtifactTime + 2h))
| extend CorrelationIngest=max_of(AwsIngest, MetadataIngest, ArtifactIngest)
| where CorrelationIngest > ago(15m)
| summarize arg_max(CorrelationIngest, *) by AwsEventId, DeviceId
| extend HostName=tostring(split(DeviceName, ".")[0]), DnsDomain=extract(@"^[^.]+\.(.+)$", 1, DeviceName),
    DefenderDeviceUrl=strcat("https://security.microsoft.com/machines/", DeviceId)
| project TimeGenerated=AwsTime, AwsEventId, EventName, AwsAccessKeyId, AwsPrincipalArn,
    AWSAccountId, RecipientAccountId, SourceIpAddress, DeviceId, DeviceName, HostName, DnsDomain,
    AwsInstanceId, XdrTenantId, ArtifactTime, ArtifactName, ArtifactPath, MetadataTime,
    ProcessUniqueId, MetadataReportId, DefenderDeviceUrl,
    CorrelationBasis="Verified issued-key/EC2-origin binding + startup artifact + metadata contact; process causality unproven";

Explanation

This KQL query is designed to detect suspicious activity related to AWS Secrets Manager access, specifically when Python startup artifacts and metadata access occur before the use of AWS secrets. Here's a simplified breakdown of what the query does:

  1. Setup and Configuration:

    • It starts by setting up a tenant ID for XDR (Extended Detection and Response) and ensures that this ID is configured correctly.
  2. Asset Mapping:

    • It retrieves a list of AWS assets from a watchlist, filtering for verified entries and ensuring necessary fields like AWS Account ID, Principal ARN, and Access Key ID are present.
  3. Artifact Detection:

    • It looks for recent file events (within the last 4 hours) on devices where Python-related files (cook.pth, cook.py) are created or modified in the site-packages directory.
  4. Metadata Access:

    • It checks for network events in the last 2 hours where a Python process successfully connects to the IP 169.254.169.254, which is typically used for accessing instance metadata in AWS.
  5. AWS Secrets Access:

    • It examines AWS CloudTrail logs from the last 2 hours for successful GetSecretValue events from the AWS Secrets Manager, ensuring no errors occurred and that the event is associated with a valid AWS account and principal.
  6. Correlation:

    • The query correlates the AWS secrets access events with the asset map, metadata access, and artifact creation/modification events. It ensures that these events are linked by the same device and occur within specific time windows.
  7. Result Compilation:

    • It compiles the results, including details like event times, AWS account and principal information, device details, and URLs for further investigation in Microsoft Defender.
  8. Output:

    • The final output includes a summary of the correlated events, highlighting potential security incidents where Python startup artifacts and metadata access precede the use of AWS secrets.

This query is part of an analytic rule with a high severity level, intended for review and not for immediate deployment without approval.