Query Details

24 A02 Screenconnect To Aws Secrets

Query

// REVIEW ONLY workbook query: 8.2 A02 Preview — AWS cross-cloud - ScreenConnect-launched AWS secret retrieval
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let TargetXdrTenant = tolower(trim(" ", base64_decode_tostring("{XdrTenantId:base64}")));
let SafeCloudTrail = union isfuzzy=true
  (datatable(TimeGenerated:datetime, AwsEventId:string, EventSource:string, EventName:string, AWSRegion:string,
    RecipientAccountId:string, UserIdentityAccountId:string, UserIdentityType:string, UserIdentityArn:string,
    UserIdentityAccessKeyId:string, SourceIpAddress:string, UserAgent:string,
    ErrorCode:string, ErrorMessage:string, RequestParameters:string, ResponseElements:string, Resources:string,
    ManagementEvent:bool, ReadOnly:bool, SessionMfaAuthenticated:string)[]),
  (AWSCloudTrail
   | where TimeGenerated between (Start .. End)
   | project TimeGenerated, AwsEventId, EventSource, EventName, AWSRegion, RecipientAccountId,
       UserIdentityAccountId, UserIdentityType, UserIdentityArn,
       UserIdentityAccessKeyId, SourceIpAddress, UserAgent, ErrorCode, ErrorMessage,
       RequestParameters, ResponseElements, Resources, ManagementEvent, ReadOnly, SessionMfaAuthenticated);
let IdentityMap = union isfuzzy=true
  (datatable(SearchKey:string)[]),
  (_GetWatchlist('AWS_EntraIdentityMap'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| extend VerifiedVal = tolower(tostring(column_ifexists("Verified", ""))),
    MappingEvidenceVal = tostring(column_ifexists("MappingEvidence", ""))
| where VerifiedVal == "true" and isnotempty(MappingEvidenceVal)
| 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", "")),
    AppClientId=tolower(tostring(column_ifexists("AppClientId", ""))),
    ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")),
    ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where MappingRows == 1 and isnotempty(EntraTenantId) and isnotempty(EntraObjectId);
let AssetMap = union isfuzzy=true
  (datatable(SearchKey:string)[]),
  (_GetWatchlist('AWS_AssetIdentityMap'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| extend VerifiedVal = tolower(tostring(column_ifexists("Verified", ""))),
    MappingEvidenceVal = tostring(column_ifexists("MappingEvidence", ""))
| where VerifiedVal == "true" and isnotempty(MappingEvidenceVal)
| 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 isnotempty(DeviceId) and isnotempty(AwsInstanceId) and isnotempty(AwsAccessKeyId);
let UserBridge = IdentityMap
| where IdentityType == "User" and (isempty(TargetXdrTenant) or EntraTenantId == TargetXdrTenant);
let ScreenConnectProc = union isfuzzy=true
  (datatable(ProcTime:datetime, DeviceId:string, DeviceName:string, AccountObjectId:string, InitiatingProcessFileName:string, FileName:string, ProcessCommandLine:string)[]),
  (DeviceProcessEvents
   | where Timestamp between (Start .. End)
   | where InitiatingProcessFileName has_any ("ScreenConnect.ClientService.exe", "ScreenConnect.WindowsClient.exe")
       or InitiatingProcessCommandLine has "ScreenConnect"
   | where FileName in~ ("aws.exe", "powershell.exe", "pwsh.exe", "cmd.exe", "python.exe")
       and ProcessCommandLine has_any ("aws ", "secretsmanager", "sts ", "configure", "Get-SECSecretValue")
   | project ProcTime=Timestamp, DeviceId, DeviceName, AccountObjectId=tolower(AccountObjectId),
       InitiatingProcessFileName, FileName, ProcessCommandLine)
| where isnotempty(AccountObjectId);
SafeCloudTrail
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| project TimeGenerated, AwsEventId, AWSAccountId=UserIdentityAccountId, RecipientAccountId, AWSRegion,
    AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, SourceIpAddress, UserAgent
| join kind=inner UserBridge on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where TimeGenerated between (ValidFrom .. ValidTo)
| join kind=inner ScreenConnectProc on $left.EntraObjectId == $right.AccountObjectId
| where ProcTime between ((TimeGenerated - 1h) .. TimeGenerated)
| project TimeGenerated, AwsEventId, AWSAccountId, RecipientAccountId, AWSRegion, AwsPrincipalArn,
    AwsAccessKeyId, SourceIpAddress, EntraObjectId, DeviceId, DeviceName, ProcTime, InitiatingProcessFileName, FileName
| order by TimeGenerated desc
| take 5000

Explanation

This query is designed to identify and analyze specific AWS secret retrieval events that were initiated through ScreenConnect, a remote support tool. Here's a simplified breakdown of what the query does:

  1. Time Range and Account Filtering: It sets a time range and filters based on AWS account information provided in base64 format. It checks if all accounts should be used or a specific one.

  2. Data Collection:

    • CloudTrail Data: It gathers AWS CloudTrail logs within the specified time range, focusing on events related to AWS Secrets Manager.
    • Identity Mapping: It retrieves and processes identity mappings from a watchlist to link AWS access keys and principal ARNs to Entra (Azure AD) identities.
    • Asset Mapping: It retrieves and processes asset mappings from a watchlist to link AWS accounts and principal ARNs to device IDs and instance IDs.
  3. User and Process Filtering:

    • User Bridge: Filters the identity map to include only users and optionally matches a specific XDR tenant.
    • ScreenConnect Process: Collects process events from devices where ScreenConnect was used to launch certain executables (like aws.exe, powershell.exe, etc.) with commands related to AWS secret retrieval.
  4. Event Matching and Joining:

    • It filters CloudTrail events to those involving the retrieval of secrets (GetSecretValue) without errors.
    • It joins these events with the user identity map to ensure the AWS access key and principal ARN are valid and match an Entra identity.
    • It further joins with the ScreenConnect process data to ensure the secret retrieval event is linked to a process initiated by ScreenConnect within an hour of the event.
  5. Output:

    • The query outputs a list of up to 5000 events, showing details like the time of the event, AWS account information, source IP, and the device and process details related to the ScreenConnect activity.

Overall, this query helps in identifying potential security incidents where AWS secrets are accessed through ScreenConnect, providing insights into the involved accounts, devices, and processes.