18 H03 Alert Evidence
Query
// REVIEW ONLY workbook query: 6.1 H03 — AWS Sessions Correlated with Microsoft Defender XDR Alert Evidence
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 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 AwsSessions = SafeCloudTrail
| where UseAllAccounts or UserIdentityAccountId == AccountFilter or RecipientAccountId == AccountFilter
| where isempty(SeedKey) or UserIdentityAccessKeyId == SeedKey
| where isnotempty(UserIdentityAccessKeyId) and isnotempty(UserIdentityArn)
| summarize FirstAws=min(TimeGenerated), LastAws=max(TimeGenerated), AwsEvents=count(), AwsActions=make_set(EventName, 15)
by AWSAccountId=UserIdentityAccountId, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn;
let MappedUsers = AwsSessions
| join kind=inner (IdentityMap | where IdentityType == "User" and (isempty(TargetXdrTenant) or EntraTenantId == TargetXdrTenant))
on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn
| where FirstAws <= ValidTo and LastAws >= ValidFrom
| project MatchKey=EntraObjectId, MatchKind="EntraUserObjectId", AWSAccountId, AwsPrincipalArn, AwsAccessKeyId, AwsEvents, AwsActions;
let MappedDevices = AwsSessions
| join kind=inner (AssetMap | where isempty(TargetXdrTenant) or XdrTenantId == TargetXdrTenant)
on AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where FirstAws <= ValidTo and LastAws >= ValidFrom
| project MatchKey=tolower(DeviceId), MatchKind="MdeDeviceId", AWSAccountId, AwsPrincipalArn, AwsAccessKeyId, AwsEvents, AwsActions;
let SeedTargets = union MappedUsers, MappedDevices;
let Evidence = union isfuzzy=true
(datatable(EvTime:datetime, AlertId:string, EntityType:string, EvidenceRole:string, AccountObjectId:string, DeviceId:string, DeviceName:string, RemoteIP:string)[]),
(AlertEvidence
| where Timestamp between (Start .. End)
| project EvTime=Timestamp, AlertId, EntityType, EvidenceRole,
AccountObjectId=tolower(AccountObjectId), DeviceId=tolower(DeviceId), DeviceName, RemoteIP)
| extend MatchKey = iff(isnotempty(AccountObjectId), AccountObjectId, tolower(DeviceId))
| where isnotempty(MatchKey);
let Alerts = union isfuzzy=true
(datatable(AlertTime:datetime, AlertId:string, Title:string, Severity:string, Category:string, ServiceSource:string, AttackTechniques:string)[]),
(AlertInfo
| where Timestamp between (Start .. End)
| summarize arg_max(Timestamp, *) by AlertId
| project AlertTime=Timestamp, AlertId, Title, Severity, Category, ServiceSource, AttackTechniques);
SeedTargets
| join kind=inner Evidence on MatchKey
| join kind=leftouter Alerts on AlertId
| extend DefenderAlertUrl=strcat("https://security.microsoft.com/alerts/", AlertId),
DefenderDeviceUrl=iff(isnotempty(DeviceId), strcat("https://security.microsoft.com/machines/", DeviceId), "")
| project AlertTime=coalesce(AlertTime, EvTime), AlertId, Severity, Title, Category, ServiceSource,
MatchKind, MatchKey, EvidenceRole, EntityType, DeviceName, AWSAccountId, AwsPrincipalArn, AwsAccessKeyId, AwsEvents, AwsActions, DefenderAlertUrl, DefenderDeviceUrl
| order by AlertTime desc
| take 5000Explanation
This query is designed to correlate AWS session data with Microsoft Defender XDR alert evidence. Here's a simplified breakdown of what it does:
-
Time Range and Filters: It sets a time range and filters for AWS account and access key ID, as well as a target XDR tenant ID.
-
Data Collection:
- AWS CloudTrail Data: It collects AWS CloudTrail logs within the specified time range, focusing on events related to user identity and access.
- Identity and Asset Mapping: It retrieves mappings of AWS identities and assets from watchlists, ensuring they are verified and have valid mapping evidence.
-
AWS Sessions: It filters AWS session data based on the specified account and access key filters, summarizing the sessions by account ID, access key ID, and principal ARN.
-
User and Device Mapping:
- Mapped Users: It joins AWS session data with identity mappings to find sessions related to specific users.
- Mapped Devices: It joins AWS session data with asset mappings to find sessions related to specific devices.
-
Evidence and Alerts:
- Evidence Collection: It gathers alert evidence within the specified time range, focusing on account and device identifiers.
- Alert Information: It collects alert details, such as time, severity, and category.
-
Correlation and Output:
- It correlates the mapped user and device sessions with alert evidence.
- It joins this data with alert information to provide a comprehensive view of AWS sessions linked to security alerts.
- It constructs URLs for detailed views of alerts and devices in Microsoft Defender.
- Finally, it orders the results by alert time and limits the output to 5000 records.
In essence, this query helps security analysts identify AWS sessions that are potentially related to security alerts in Microsoft Defender, providing a detailed view of the associated user or device activity.