H07 Aws Secrets To Endpoint Egress
Query
// REVIEW ONLY. A connection is not an upload. Device/role attribution is not process-level cloud causality.
// IPv4-only candidate egress; approved non-AWS SaaS and proxies need analyst review.
let Start = ago(1d);
let End = now();
let SeedAWSAccountId = "REPLACE_WITH_12_DIGIT_ACCOUNT";
let SeedAccessKeyId = "REPLACE_WITH_ACCESS_KEY_ID";
let XdrEntraTenantId = "REPLACE_WITH_XDR_ENTRA_TENANT_GUID";
let InputsValid = assert(SeedAWSAccountId matches regex @"^[0-9]{12}$"
and isnotempty(SeedAccessKeyId) and SeedAccessKeyId != "REPLACE_WITH_ACCESS_KEY_ID"
and isnotnull(toguid(XdrEntraTenantId)), "Set incident account/key and XDR Entra tenant.");
let AssetMap = _GetWatchlist('AWS_AssetIdentityMap')
| where 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(AwsAccessKeyId);
let Secrets = AWSCloudTrail
| where InputsValid and TimeGenerated between (Start .. End)
| where UserIdentityAccountId == SeedAWSAccountId and UserIdentityAccessKeyId == SeedAccessKeyId
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| where isempty(ErrorCode) and isempty(ErrorMessage) and isnotempty(AwsEventId)
| project SecretTime=TimeGenerated, AwsEventId, AWSAccountId=UserIdentityAccountId,
AwsPrincipalArn=UserIdentityArn, AwsAccessKeyId=UserIdentityAccessKeyId
| join kind=inner AssetMap on AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where SecretTime between (ValidFrom .. ValidTo);
Secrets
| join kind=inner (
DeviceNetworkEvents
| where Timestamp between (Start .. End) and ActionType == "ConnectionSuccess"
| where isnotnull(parse_ipv4(RemoteIP)) and ipv4_is_private(RemoteIP) == false
| where RemoteIP !startswith "127." and RemoteIP !startswith "169.254."
| where RemoteUrl !endswith ".amazonaws.com" and RemoteUrl !endswith ".api.aws"
| where InitiatingProcessFileName in~ ("python", "python3", "python.exe", "node", "node.exe",
"curl", "curl.exe", "wget", "wget.exe", "powershell.exe", "pwsh.exe")
| project NetworkTime=Timestamp, DeviceId, DeviceName, RemoteIP, RemoteUrl, RemotePort,
ProcessUniqueId=InitiatingProcessUniqueId, InitiatingProcessFileName,
InitiatingProcessAccountObjectId, NetworkReportId=ReportId
) on DeviceId
| where NetworkTime between (SecretTime .. (SecretTime + 30m)) and NetworkTime between (ValidFrom .. ValidTo)
| extend DefenderDeviceUrl=strcat("https://security.microsoft.com/machines/", DeviceId),
Confidence="Candidate egress only; no payload, byte-count or successful exfiltration evidence",
NextStep="H03 for device alerts; validate process lineage, approved destinations and proxy logs"
| project SecretTime, AwsEventId, AwsPrincipalArn, AWSAccountId, AwsInstanceId, DeviceId, DeviceName,
NetworkTime, RemoteIP, RemoteUrl, RemotePort, ProcessUniqueId, InitiatingProcessFileName,
InitiatingProcessAccountObjectId, NetworkReportId, Confidence, DefenderDeviceUrl, NextStep
| order by NetworkTime asc
| take 5000Explanation
This query is designed to identify and analyze potential security incidents involving AWS secrets access and network connections from devices. Here's a simplified breakdown of what the query does:
-
Time Frame: It looks at data from the past day up to the current moment.
-
Input Validation: It checks if the provided AWS account ID, access key ID, and XDR Entra tenant ID are valid and properly set.
-
Asset Mapping: It retrieves a list of AWS assets from a watchlist, ensuring they are verified and have mapping evidence. It filters these assets to include only those associated with the specified XDR Entra tenant ID and have a unique mapping.
-
Secrets Access: It searches AWS CloudTrail logs for events where the specified AWS account and access key accessed secrets using the "GetSecretValue" operation. It ensures there are no errors in these events and joins this data with the asset map.
-
Network Events: It looks at device network events where a successful connection was made to a public IP (not AWS-related) using specific processes (like Python, Node, curl, etc.). It filters out connections to AWS domains and certain IP ranges.
-
Correlation: It correlates the secrets access events with network events from the same device within a 30-minute window. It ensures the network events fall within the valid time range of the asset mapping.
-
Output: It generates a list of potential security incidents, including details about the secret access and network connection, along with URLs for further investigation in Microsoft Defender. It provides a confidence level and suggests next steps for analysts.
-
Result Limitation: The query limits the output to the first 5000 results, ordered by the network event time.
Overall, this query helps security analysts identify suspicious activity involving AWS secrets and network connections, providing them with the necessary context and next steps for investigation.