Skip to content

Instantly share code, notes, and snippets.

@dirkjanm
Last active March 14, 2023 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkjanm/bf9a6475ed673b26597294e6278f27af to your computer and use it in GitHub Desktop.
Save dirkjanm/bf9a6475ed673b26597294e6278f27af to your computer and use it in GitHub Desktop.
KQL query to monitor or hunt for users modifying their own identities
// Users changing their own identities hunting
// Query by @_dirkjan / Outsider Security - released as CC BY (https://creativecommons.org/licenses/by/2.0/)
AuditLogs
| where OperationName =~ "Update user"
| where Result =~ "success"
| mv-expand target = TargetResources
| extend targetUPN = tostring(TargetResources[0].userPrincipalName)
| extend targetId = tostring(TargetResources[0].id)
| extend targetType = tostring(TargetResources[0].type)
| extend modifiedProps = TargetResources[0].modifiedProperties
| extend initiatedUser = tostring(InitiatedBy.user.userPrincipalName)
| where targetUPN == initiatedUser
| mv-expand modifiedProps
| where modifiedProps.displayName =~ "AlternativeSecurityId"
| extend new_value_set = parse_json(tostring(modifiedProps.newValue))
| extend old_value_set = parse_json(tostring(modifiedProps.oldValue))
| where array_length(new_value_set) > 0
| extend identityProvider = new_value_set[0].IdentityProvider
| extend identity = base64_decode_tostring(tostring(new_value_set[0].Key))
| project-away old_value_set, new_value_set, modifiedProps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment