AWS cross-cloud - PHP descendant contacts metadata before EC2 role secrets use
A04 Webworker Metadata 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 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 query is designed to detect potential security incidents involving PHP processes on AWS EC2 instances. Here's a simplified breakdown of what it does:
-
Purpose: The query aims to identify scenarios where a PHP process (or its child processes) on an AWS EC2 instance accesses the instance metadata service and subsequently retrieves secrets using AWS Secrets Manager. This is considered a high-severity event because it could indicate unauthorized access to sensitive information.
-
Data Sources: It uses data from AWS CloudTrail and Microsoft Threat Protection, specifically looking at device process events and network events.
-
Process:
- Asset Mapping: It first checks a watchlist to map AWS account details with device IDs and verifies the mapping.
- Process Monitoring: It looks for PHP processes that initiate other processes like
curl,wget,python, etc., which are commonly used for network operations. - Metadata Access: It checks for successful connections to the EC2 metadata IP (169.254.169.254), which is a critical step in accessing instance metadata.
- Secrets Retrieval: It monitors AWS CloudTrail logs for successful secret retrieval events from AWS Secrets Manager.
-
Correlation: The query correlates these events by matching device IDs and process unique IDs, ensuring that the metadata access and secret retrieval are related and occur within a specific time window.
-
Output: If such a sequence of events is detected, it generates an alert with details like the AWS event ID, the involved AWS account and principal, the device ID, and links to further investigate the device in Microsoft Defender.
-
Alerting: The query runs every 15 minutes and looks back over the past 2 hours to identify any such incidents, triggering an alert if any are found.
Overall, this query is a proactive measure to detect and alert on potential unauthorized access to AWS secrets via PHP processes on EC2 instances, helping security teams respond quickly to potential threats.