A02 Screenconnect To Aws Secrets
Query
// Standalone KQL for Analytic Rule: AWS cross-cloud - ScreenConnect-launched AWS secret retrieval
// Rule File: analytics/A02-screenconnect-to-aws-secrets.yaml | Rule ID: 54f1f278-4d53-4f20-a2a1-0e357f02da02 | 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 IdentityMap = _GetWatchlist('AWS_EntraIdentityMap')
| where Configured and tolower(tostring(column_ifexists("Verified", ""))) == "true" and isnotempty(tostring(column_ifexists("MappingEvidence", "")))
| project AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")), AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")),
AWSAccountId=tostring(column_ifexists("AWSAccountId", "")), EntraTenantId=tolower(tostring(column_ifexists("EntraTenantId", ""))),
EntraObjectId=tolower(tostring(column_ifexists("EntraObjectId", ""))), IdentityType=tostring(column_ifexists("IdentityType", "")),
ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")), ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where MappingRows == 1 and IdentityType == "User"
and EntraTenantId == tolower(XdrEntraTenantId) and isnotempty(EntraObjectId);
let Processes = DeviceProcessEvents
| where Timestamp > ago(2h)
| where FileName in~ ("aws.exe", "aws")
| where InitiatingProcessFileName startswith "ScreenConnect"
or InitiatingProcessParentFileName startswith "ScreenConnect"
| where ProcessCommandLine matches regex @"(?i)\b(get-secret-value|batch-get-secret-value|get-parameter|get-parameters|get-parameters-by-path)\b"
| project ProcessTime=Timestamp, ProcessIngest=ingestion_time(), DeviceId, DeviceName,
ProcessUniqueId, ProcessReportId=ReportId, EntraObjectId=tolower(AccountObjectId)
| where isnotempty(ProcessUniqueId) and isnotempty(EntraObjectId);
let Networks = DeviceNetworkEvents
| where Timestamp > ago(2h) and ActionType == "ConnectionSuccess"
| where RemoteUrl endswith ".amazonaws.com" or RemoteUrl endswith ".api.aws"
| where RemoteUrl startswith "secretsmanager." or RemoteUrl startswith "ssm."
| project NetworkTime=Timestamp, NetworkIngest=ingestion_time(), DeviceId,
ProcessUniqueId=InitiatingProcessUniqueId, AwsEndpoint=RemoteUrl
| 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")
or (EventSource == "ssm.amazonaws.com" and EventName in ("GetParameter", "GetParameters", "GetParametersByPath")
and tobool(parse_json(RequestParameters).withDecryption) == true)
| project AwsTime=TimeGenerated, AwsIngest=ingestion_time(), AwsEventId, EventName,
AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn,
AWSAccountId=UserIdentityAccountId, RecipientAccountId, SourceIpAddress
| where isnotempty(AwsAccessKeyId) and isnotempty(AwsPrincipalArn) and isnotempty(AWSAccountId)
| join kind=inner IdentityMap on AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where AwsTime between (ValidFrom .. ValidTo)
| join kind=inner Processes on EntraObjectId
| where ProcessTime between (ValidFrom .. ValidTo)
| join kind=inner Networks on DeviceId, ProcessUniqueId
| where NetworkTime between (ProcessTime .. (ProcessTime + 10m))
and AwsTime between ((NetworkTime - 5m) .. (NetworkTime + 5m))
| extend CorrelationIngest=max_of(AwsIngest, ProcessIngest, NetworkIngest)
| 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, EntraTenantId, EntraObjectId,
DeviceId, DeviceName, HostName, DnsDomain, ProcessTime, ProcessUniqueId,
ProcessReportId, NetworkTime, AwsEndpoint, DefenderDeviceUrl,
CorrelationBasis="Verified user/session + ScreenConnect ancestry + same-process AWS connection";Explanation
This query is designed to detect suspicious activity involving the retrieval of AWS secrets that are initiated by processes linked to ScreenConnect. Here's a simplified breakdown of what the query does:
-
Setup and Configuration:
- It starts by setting up a tenant ID for Entra (Azure Active Directory) and ensures that it's configured correctly.
-
Identity Mapping:
- It retrieves a list of verified AWS identities from a watchlist, ensuring that each identity is mapped to a single AWS access key and is associated with a user in the specified Entra tenant.
-
Process Monitoring:
- It looks for processes on devices that have run AWS command-line tools (like
aws.exe) within the last 2 hours. These processes must have been initiated by or have a parent process related to ScreenConnect. - It specifically checks if these processes executed commands related to retrieving secrets or parameters from AWS.
- It looks for processes on devices that have run AWS command-line tools (like
-
Network Activity:
- It examines network events to find successful connections to AWS endpoints related to secrets management (like
secretsmanager.amazonaws.comorssm.amazonaws.com) within the last 2 hours.
- It examines network events to find successful connections to AWS endpoints related to secrets management (like
-
AWS CloudTrail Logs:
- It analyzes AWS CloudTrail logs for events where secrets were accessed without errors, focusing on specific AWS services and actions related to secrets retrieval.
-
Correlation and Joining Data:
- The query correlates the identity, process, and network data with AWS CloudTrail logs to find matches based on time and identity.
- It ensures that the AWS event, process, and network activity are all related and occur within a specific timeframe.
-
Result Compilation:
- It compiles the results, providing details such as the time of the AWS event, the AWS access key used, the device involved, and a URL to the device in Microsoft Defender.
- The results are filtered to show only recent activity (within the last 15 minutes) and are summarized to highlight the most recent correlated events.
Overall, this query is part of an analytic rule to detect potential unauthorized access to AWS secrets initiated through ScreenConnect, a remote support tool, by correlating various data sources to identify suspicious patterns.