Query Details

19 H07 Egress Candidates

Query

// REVIEW ONLY workbook query: 6.2 H07 — AWS Secrets Manager Read Followed by Endpoint Public Egress (≤30m)
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let SeedKey = trim(" ", base64_decode_tostring("{AccessKeyId:base64}"));
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 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 ScopedAssets = AssetMap | where isempty(TargetXdrTenant) or XdrTenantId == TargetXdrTenant;
let SecretReads = SafeCloudTrail
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue"
| where isempty(ErrorCode) and isempty(ErrorMessage)
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| where isempty(SeedKey) or UserIdentityAccessKeyId == SeedKey
| project SecretTime=TimeGenerated, AwsEventId, AWSAccountId=UserIdentityAccountId, RecipientAccountId,
    AWSRegion, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn, SourceIpAddress
| join kind=inner ScopedAssets on AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where SecretTime between (ValidFrom .. ValidTo);
let Egress = union isfuzzy=true
  (datatable(NetTime:datetime, DeviceId:string, DeviceName:string, RemoteIP:string, RemotePort:int, RemoteUrl:string, InitiatingProcessFileName:string, InitiatingProcessCommandLine:string)[]),
  (DeviceNetworkEvents
   | where Timestamp between (Start .. End)
   | where ActionType == "ConnectionSuccess" and isnotempty(RemoteIP)
       and not(ipv4_is_private(RemoteIP)) and RemoteIP != "169.254.169.254"
   | where not(RemoteUrl has_any (".amazonaws.com", ".windows.net", ".microsoft.com", ".live.com"))
   | project NetTime=Timestamp, DeviceId, DeviceName, RemoteIP, RemotePort, RemoteUrl,
       InitiatingProcessFileName, InitiatingProcessCommandLine);
SecretReads
| join kind=inner Egress on DeviceId
| where NetTime between (SecretTime .. (SecretTime + 30m))
| project NetTime, SecretTime, MinutesAfterSecret=round(datetime_diff('second', NetTime, SecretTime)/60.0, 1),
    AWSAccountId, RecipientAccountId, AwsPrincipalArn, AwsAccessKeyId, AwsInstanceId,
    DeviceId, DeviceName, InitiatingProcessFileName, RemoteIP, RemotePort, RemoteUrl, AwsEventId
| order by NetTime desc
| take 5000

Explanation

This query is designed to identify potential security incidents involving AWS Secrets Manager and network activity. Here's a simplified breakdown:

  1. Time Range and Filters: The query starts by setting a time range and filters for AWS account and access key ID.

  2. CloudTrail Data: It retrieves AWS CloudTrail logs within the specified time range, focusing on events related to AWS Secrets Manager.

  3. Asset Mapping: It uses a watchlist to map AWS account details to specific devices and verifies these mappings.

  4. Secret Reads: The query identifies instances where the "GetSecretValue" operation was successfully executed in AWS Secrets Manager, filtering by account and access key if specified.

  5. Network Egress: It collects network events where a device made a successful connection to a public IP address, excluding known AWS and Microsoft domains.

  6. Correlation: The query correlates the secret read events with network egress events from the same device within 30 minutes after the secret was accessed.

  7. Output: It outputs details of these correlated events, including timestamps, AWS account information, device details, and network connection specifics, sorted by the network event time.

The goal is to detect scenarios where a secret is accessed and then potentially used for unauthorized external communication shortly after.