26 A04 Webworker Metadata To Aws Secrets
Query
// REVIEW ONLY workbook query: 8.4 A04 Preview — AWS cross-cloud - PHP descendant contacts metadata before EC2 role secrets use
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 ScopedAssets = AssetMap | where isempty(TargetXdrTenant) or XdrTenantId == TargetXdrTenant;
let WebImds = union isfuzzy=true
(datatable(ImdsTime:datetime, DeviceId:string, DeviceName:string, InitiatingProcessFileName:string, RemotePort:int)[]),
(DeviceNetworkEvents
| where Timestamp between (Start .. End)
| where RemoteIP == "169.254.169.254"
| where InitiatingProcessFileName in~ ("php-fpm", "php-cgi", "php", "php.exe", "w3wp.exe", "httpd", "apache2", "nginx", "node", "java")
or InitiatingProcessParentFileName in~ ("php-fpm", "httpd", "apache2", "nginx", "w3wp.exe")
| project ImdsTime=Timestamp, DeviceId, DeviceName, InitiatingProcessFileName, RemotePort);
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
| join kind=inner ScopedAssets on AWSAccountId, AwsPrincipalArn, AwsAccessKeyId
| where TimeGenerated between (ValidFrom .. ValidTo)
| join kind=inner WebImds on DeviceId
| where ImdsTime between ((TimeGenerated - 30m) .. TimeGenerated)
| summarize FirstImdsTime=min(ImdsTime), WorkerProcesses=make_set(InitiatingProcessFileName, 10), take_any(DeviceName, AwsInstanceId)
by TimeGenerated, AwsEventId, AWSAccountId, RecipientAccountId, AWSRegion, AwsPrincipalArn, AwsAccessKeyId, SourceIpAddress, DeviceId
| order by TimeGenerated desc
| take 5000Explanation
This query is designed to analyze AWS CloudTrail logs and network events to identify potential security incidents involving the use of AWS Secrets Manager. Here's a simplified breakdown of what the query does:
-
Time Range and Account Setup:
- It sets up a time range (
StartandEnd) and decodes account information from base64 to filter the data for specific AWS accounts or all accounts if specified.
- It sets up a time range (
-
Data Collection:
- CloudTrail Logs: It gathers AWS CloudTrail logs related to the
GetSecretValueevent from AWS Secrets Manager, filtering out any logs with errors. - Identity Mapping: It retrieves identity mappings from a watchlist to associate AWS access keys and principal ARNs with Entra (Azure AD) identities.
- Asset Mapping: It retrieves asset mappings to associate AWS accounts and instances with specific devices and tenants.
- CloudTrail Logs: It gathers AWS CloudTrail logs related to the
-
Network Events:
- It collects network events where the remote IP is
169.254.169.254, which is typically used for accessing instance metadata. It focuses on processes related to PHP and web servers (e.g., Apache, Nginx).
- It collects network events where the remote IP is
-
Data Joining and Filtering:
- It joins the CloudTrail logs with the asset mappings to ensure that the events are related to known assets.
- It further joins with network events to find instances where metadata was accessed shortly before or after the
GetSecretValueevent, indicating potential misuse of secrets.
-
Result Compilation:
- The query summarizes the findings by listing the first time metadata was accessed, the processes involved, and other relevant details like device names and AWS instance IDs.
- It orders the results by the time of the event and limits the output to 5000 entries.
Overall, this query is used to detect and investigate suspicious activities involving AWS Secrets Manager and PHP-based applications, particularly focusing on cross-cloud interactions and potential unauthorized access to sensitive data.