AWS cross-cloud - Python startup artifacts and metadata access precede secrets use
A03 Python Startup To Aws Secrets
Query
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 query is designed to detect potentially suspicious activity involving Python scripts and AWS EC2 metadata access, which could indicate a security threat. Here's a simplified breakdown of what the query does:
-
Context: The query is inspired by a report from October 2026 and is meant for review purposes only. It focuses on detecting a sequence of events that could suggest unauthorized access to secrets in AWS.
-
Severity: The query is marked as high severity, indicating that if the conditions are met, it could represent a significant security risk.
-
Data Sources: It uses data from AWS CloudTrail and Microsoft Threat Protection, specifically looking at file events and network events on devices.
-
Detection Logic:
- Asset Mapping: It first checks a watchlist to map AWS account details to device IDs and verifies if these mappings are valid.
- Artifact Detection: It looks for the creation or modification of specific Python files (
cook.pthorcook.py) in thesite-packagesdirectory within the last 4 hours. - Metadata Access: It checks for successful network connections to the EC2 metadata IP (
169.254.169.254) initiated by Python processes within the last 2 hours. - Secret Access: It identifies successful calls to AWS Secrets Manager (
GetSecretValueevent) within the last 2 hours, ensuring no errors occurred.
-
Correlation: The query correlates these events to find instances where:
- A Python script is modified or created.
- The same device accesses EC2 metadata.
- The same device successfully retrieves a secret from AWS Secrets Manager.
- All these events occur within a specific timeframe and are linked to verified AWS credentials.
-
Output: If such a sequence is detected, the query outputs details about the event, including timestamps, AWS account information, device details, and a link to the device's security page in Microsoft Defender.
-
Purpose: The goal is to identify potential unauthorized access patterns that could indicate a security breach, focusing on persistence and credential access tactics.
This query is scheduled to run every 15 minutes and looks back over a 4-hour period to detect these patterns.