Auditlogs Apporserviceprincipalcredential
KQL detection rule by ep3p · MIT
No description provided
let _ExpectedApps = toscalar(
_GetWatchlist("Activity-ExpectedSignificantActivity")
| where Activity == "AppServiceCredentialModification" and Notes has "[App]"
| summarize make_list(ActorId)
);
AuditLogs
| where OperationName has_any ("Add application", "Add service principal", "Certificates and secrets management") // Capture "Add application", "Add service principal", "Add service principal credentials", and "Update application - Certificates and secrets management" events
// | where Result == "success"
| extend
Initiator = iif(isnotempty(InitiatedBy["app"]), tostring(InitiatedBy["app"]["displayName"]), tostring(InitiatedBy["user"]["userPrincipalName"])),
InitiatorId = iif(isnotempty(InitiatedBy["app"]), tostring(InitiatedBy["app"]["servicePrincipalId"]), tostring(InitiatedBy["user"]["id"])),
IPAddress = tostring(InitiatedBy[tostring(bag_keys(InitiatedBy)[0])]["ipAddress"]),
// Information about the target object
TargetType = tostring(TargetResources[0].type),
TargetDisplayName = tostring(TargetResources[0]["displayName"]),
TargetId = tostring(TargetResources[0]["id"])
| where not(InitiatorId in (_ExpectedApps))
| mv-apply ModifiedProperty = TargetResources[0]["modifiedProperties"] on (
summarize ModifiedProperties = make_bag(bag_pack(tostring(ModifiedProperty["displayName"]), pack("oldValue", trim(@'[\"\s]+', tostring(ModifiedProperty["oldValue"])), "newValue", trim(@'[\"\s]+', tostring(ModifiedProperty["newValue"])))))
)
| where isnotempty(ModifiedProperties["KeyDescription"])
| extend
AddedCredentials = set_difference(todynamic(tostring(ModifiedProperties["KeyDescription"]["newValue"])), todynamic(tostring(ModifiedProperties["KeyDescription"]["oldValue"]))),
RemovedCredentials = set_difference(todynamic(tostring(ModifiedProperties["KeyDescription"]["oldValue"])), todynamic(tostring(ModifiedProperties["KeyDescription"]["newValue"])))
// | mv-expand AddedCredentials = iff(array_length(AddedCredentials) != 0, AddedCredentials, dynamic([""])) to typeof(string)
// | parse AddedCredentials with "[KeyIdentifier=" KeyIdentifier: string ",KeyType=" KeyType: string ",KeyUsage=" KeyUsage: string ",DisplayName=" KeyDisplayName: string "]"
| project
TimeGenerated,
LoggedByService,
Category,
AADOperationType,
Initiator,
IPAddress,
OperationName,
Result,
ResultDescription,
TargetType,
TargetDisplayName,
AddedCredentials,
RemovedCredentials,
TargetId,
InitiatorId,
AdditionalDetails,
InitiatedBy,
TargetResources,
CorrelationId