Query Details

AWS cross-cloud - ScreenConnect-launched AWS secret retrieval

A02 Screenconnect 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 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 part of a security monitoring system designed to detect potentially unauthorized access to AWS secrets using ScreenConnect. Here's a simplified breakdown:

  1. Purpose: The query checks for suspicious activity involving AWS secret retrieval commands executed through ScreenConnect, a remote support tool. It aims to identify if an AWS secret retrieval command was executed, and if there was any related network activity within the same process, along with a verified mapping of a user session from Microsoft Entra (formerly Azure AD) to AWS.

  2. Data Sources: It uses data from AWS CloudTrail, Microsoft Threat Protection (specifically device process and network events), and a watchlist that maps Entra users to AWS sessions.

  3. Process:

    • Identity Mapping: It first verifies if there is a valid mapping between Entra users and AWS sessions.
    • Process Events: It looks for AWS CLI commands related to secret retrieval that were initiated by ScreenConnect.
    • Network Events: It checks for successful network connections to AWS endpoints related to secrets management.
    • CloudTrail Events: It searches for AWS CloudTrail logs indicating secret retrieval actions.
  4. Correlation: The query correlates these events to confirm if they occurred within a specific timeframe and context, indicating a potential security incident.

  5. Output: If a correlation is found, it generates an alert with details such as the AWS event ID, process and network times, device information, and a URL to the device in Microsoft Defender.

  6. Severity and Tactics: The severity is marked as high, focusing on tactics like credential access and command and control.

  7. Frequency: The query runs every 15 minutes, looking back over the last 2 hours for relevant events.

Overall, this query helps security teams identify and investigate potential unauthorized access to AWS secrets through a combination of process, network, and cloud activity analysis.