Skip to content

Instantly share code, notes, and snippets.

@alexmags
Last active February 26, 2022 08:10
Show Gist options
  • Save alexmags/1eb9b1ca220975b87162e1c4fcd32726 to your computer and use it in GitHub Desktop.
Save alexmags/1eb9b1ca220975b87162e1c4fcd32726 to your computer and use it in GitHub Desktop.
Report in AAD sign in logs the last time an Azure AD guest account was used
// https://blog.alexmags.com/tags/kql/
let last_sign_in_by_account =
SigninLogs
| where TimeGenerated > now(-90d)
| where ResultType == 0
// filtering out local accounts to identify guest accounts. I couldn't identify account type in log data. Maybe TimeGenerated.HomeTenantId??
| where UserPrincipalName !endswith "companyname.com" and UserPrincipalName !endswith "AlsoCompanyname.com" and UserPrincipalName !endswith "tenantname.onmicrosoft.com" and UserPrincipalName !endswith "YetAnotherVerifiedDomain.com" and UserPrincipalName !endswith "SeriouslyICouldntIdentifyAccountTypeInLogData.com"
// get last login per account
| summarize argmax(TimeGenerated, *) by UserPrincipalName;
last_sign_in_by_account
| project max_TimeGenerated, domain=split(UserPrincipalName,'@')[1], UserPrincipalName, max_TimeGenerated_AppDisplayName, max_TimeGenerated_AuthenticationRequirement, max_TimeGenerated_ClientAppUsed, max_TimeGenerated_UserDisplayName
| order by max_TimeGenerated desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment