Query Details

A04 Webworker Metadata To Aws Secrets

Query

// Standalone KQL for Analytic Rule: AWS cross-cloud - PHP descendant contacts metadata before EC2 role secrets use
// Rule File: analytics/A04-webworker-metadata-to-aws-secrets.yaml | Rule ID: 54f1f278-4d53-4f20-a2a1-0e357f02da04 | Severity: High
// Schedule: 15m / Lookback: 2h | 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 Children = DeviceProcessEvents
| where Timestamp > ago(2h)
| where InitiatingProcessFileName startswith "php" or InitiatingProcessParentFileName startswith "php"
| where FileName in~ ("curl", "curl.exe", "wget", "wget.exe", "python", "python3", "python.exe", "aws", "aws.exe", "sh", "bash")
| project ProcessTime=Timestamp, ProcessIngest=ingestion_time(), DeviceId, DeviceName,
    ProcessUniqueId, ChildFileName=FileName, ParentFileName=InitiatingProcessFileName
| where isnotempty(ProcessUniqueId);
let Metadata = DeviceNetworkEvents
| where Timestamp > ago(2h) and ActionType == "ConnectionSuccess" and RemoteIP == "169.254.169.254"
| project MetadataTime=Timestamp, MetadataIngest=ingestion_time(), DeviceId,
    ProcessUniqueId=InitiatingProcessUniqueId
| where isnotempty(ProcessUniqueId);
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 Children on DeviceId
| where ProcessTime between (ValidFrom .. ValidTo)
| join kind=inner Metadata on DeviceId, ProcessUniqueId
| where MetadataTime between (ProcessTime .. (ProcessTime + 10m))
    and AwsTime between (MetadataTime .. (MetadataTime + 30m))
| extend CorrelationIngest=max_of(AwsIngest, ProcessIngest, MetadataIngest)
| where CorrelationIngest > ago(15m)
| summarize arg_max(CorrelationIngest, *) by AwsEventId, DeviceId, ProcessUniqueId
| 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, AwsInstanceId, XdrTenantId,
    DeviceId, DeviceName, HostName, DnsDomain, ProcessUniqueId, ProcessTime, ChildFileName,
    ParentFileName, MetadataTime, DefenderDeviceUrl,
    CorrelationBasis="Same PHP-descendant metadata process + verified issued-key/EC2-origin binding; no CVE attribution";

Explanation

This KQL query is designed to detect suspicious activity involving AWS secrets access from PHP processes. Here's a simplified breakdown of what the query does:

  1. Configuration Check: It starts by ensuring that a specific Entra tenant ID is configured correctly. This is necessary for the query to function properly.

  2. Asset Mapping: It retrieves a list of AWS assets from a watchlist, filtering for verified mappings with specific attributes like AWS account ID, principal ARN, and access key ID. It ensures that these mappings are unique and valid for the specified tenant.

  3. Process Monitoring: It looks for processes initiated by PHP scripts that subsequently launch certain tools (like curl, wget, python, aws CLI, etc.) within the last 2 hours. These processes are tracked by their unique IDs.

  4. Metadata Access Detection: The query identifies network events where a device successfully connects to the metadata service IP (169.254.169.254), which is commonly used in cloud environments to retrieve instance metadata.

  5. AWS Secrets Access: It checks AWS CloudTrail logs for successful calls to the GetSecretValue API from the AWS Secrets Manager service within the last 2 hours. It filters out any events with errors and ensures that the event details match the asset mappings.

  6. Correlation: The query correlates the PHP-initiated processes, metadata access, and AWS secrets access events. It ensures that these events occur in a specific sequence and timeframe:

    • Metadata access happens within 10 minutes of the PHP-initiated process.
    • AWS secrets access happens within 30 minutes of the metadata access.
  7. Result Compilation: It compiles the results, including details like event IDs, timestamps, AWS account and principal information, device details, and URLs for further investigation in Microsoft Defender.

  8. Output: The final output includes a summary of correlated events, highlighting potential security incidents where a PHP process might have accessed AWS secrets using verified credentials and metadata information.

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