SOLVED

Azure Sentinel rule to identify if user has not produced any events in 60 days

%3CLINGO-SUB%20id%3D%22lingo-sub-2556811%22%20slang%3D%22en-US%22%3EAzure%20Sentinel%20rule%20to%20identify%20if%20user%20has%20not%20produced%20any%20events%20in%2060%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2556811%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20working%20on%20a%20rule%20that%20uses%20a%20watchlist%20of%20elevated%20accounts.%20What%20I%20am%20trying%20to%20create%20is%20a%20rule%20that%20will%20tell%20me%20if%20one%20of%20these%20elevated%20accounts%20has%20not%20been%20used%20in%20over%2060%20days%20so%20we%20can%20mark%20it%20for%20removal%20from%20the%20list.%26nbsp%3B%20I%20am%20looking%20at%20the%20SecurityEvent%20table%20for%20any%20events%20these%20users%20generate.%20I%20can%20not%20seem%20to%20find%20a%20way%20to%20a%20%22last%20event%22%20type%20comparison.%20I%20can%20get%20a%20list%20of%20things%20they%20have%20done%20with%20starttime%20and%20endtime%2C%20but%20nothing%20I%20do%20seems%20to%20show%20a%20user%20that%20has%20not%20done%20any%20activity%20in%2060%20days%20or%20more.%20If%20anyone%20has%20done%20this%2C%20could%20you%20please%20share%20your%20rule%2Fwisdom%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2557405%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Sentinel%20rule%20to%20identify%20if%20user%20has%20not%20produced%20any%20events%20in%2060%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2557405%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1105511%22%20target%3D%22_blank%22%3E%40Marc_Jacquard%3C%2FA%3E%26nbsp%3BI%20would%20use%20the%20SignInLogs%20to%20determine%20if%20the%20user%20has%20signed%20in%20during%20your%20time%20frame%20in%20question.%26nbsp%3B%20You%20could%20use%20the%20UserPrincipalName%20or%20UserDisplayName%20fields%20to%20compare%20against%20your%20Watchlist.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2557439%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Sentinel%20rule%20to%20identify%20if%20user%20has%20not%20produced%20any%20events%20in%2060%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2557439%22%20slang%3D%22en-US%22%3EGary%2C%20I%20tried%20that%20initially%2C%20but%20I%20wanted%20to%20see%20any%20activity.%20Below%20is%20my%20current%20logic.%20I%20just%20can%20not%20seem%20to%20get%20the%20time%20comparison%20correct.%20No%20matter%20which%20log%20source%20I%20use%2C%20I%20always%20have%20people%20who%20who%20have%20been%20active%20in%20the%20last%207%20days%20even%20though%20I%20know%20some%20have%20not%20been.%20I%20switch%20between%20in~%20and%20!in~%20for%20testing.%20It%20appears%20I%20just%20do%20not%20have%20the%20time%20comparison%20logic%20correct.%3CBR%20%2F%3Elet%20adminlist%20%3D%20(_GetWatchlist(%22Elevated_accounts%22)%7Cproject%20UserName)%3B%3CBR%20%2F%3Elet%20starttime%20%3D%2031d%3B%3CBR%20%2F%3E%2F%2Flet%20midtime%20%3D%2030d%3B%3CBR%20%2F%3E%2F%2Flet%20endtime%20%3D10m%3B%3CBR%20%2F%3ESecurityEvent%3CBR%20%2F%3E%7C%20where%20TimeGenerated%20%26gt%3B%20ago(32d)%3CBR%20%2F%3E%7Cextend%20Account%20%3D%20trim_start(%40%22%5E.*%5C%5C%22%2C%20Account)%3CBR%20%2F%3E%7C%20where%20Account%20in~%20(adminlist)%3CBR%20%2F%3E%2F%2F%7Cwhere%20EventID%20%3D%3D%20%224624%22%3CBR%20%2F%3E%2F%2F%7Cwhere%20Account%20!endswith%20%22%24%22%3CBR%20%2F%3E%7C%20summarize%20StartTimeUtc%20%3D%20min(TimeGenerated)%2C%20EndTimeUtc%20%3D%20max(TimeGenerated)%20by%20Account%2C%20EventID%3CBR%20%2F%3E%2F%2F%7Cwhere%20EndTimeUtc%20-%20StartTimeUtc%20%26gt%3B%20starttime%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2557929%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Sentinel%20rule%20to%20identify%20if%20user%20has%20not%20produced%20any%20events%20in%2060%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2557929%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1105511%22%20target%3D%22_blank%22%3E%40Marc_Jacquard%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESomething%20like%20this%20should%20work%20for%20you.%20My%20data%20isn't%20an%20exact%20match%20for%20your%20formatting%20but%20you%20should%20be%20able%20to%20make%20it%20work%20in%20your%20environment.%20I%20created%20a%20test%20WatchList%20with%20a%20single%20column%20of%20Account%2C%20then%20a%20few%20rows%20of%20DOMAIN%5Cusername%20fields%20that%20had%20been%20active%2C%20and%20then%20some%20fake%20accounts%20that%20hadn't%20(fake%20inactive%20admins)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Elet%20adminlist%20%3D%20(_GetWatchlist(%22TestWatchList%22)%7Cproject%20Account)%3B%3CBR%20%2F%3ESecurityEvent%3CBR%20%2F%3E%7C%20where%20TimeGenerated%20%26gt%3B%20ago(10d)%3CBR%20%2F%3E%7C%20where%20Account%20in%20(adminlist)%3CBR%20%2F%3E%7C%20distinct%20Account%3CBR%20%2F%3E%7C%20join%20kind%3Drightanti%20adminlist%20on%20Account%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJoin%20operator%20-%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Fquery%2Fjoinoperator%3Fpivots%3Dazuredataexplorer%23right-anti-join-flavor%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdata-explorer%2Fkusto%2Fquery%2Fjoinoperator%3Fpivots%3Dazuredataexplorer%23right-anti-join-flavor%3C%2FA%3E%20rightanti%20should%20return%20results%20from%20the%20right%20(your%20watchlist)%20that%20aren't%20in%20the%20left%20(your%20query%20for%20active%20users)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2558939%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Sentinel%20rule%20to%20identify%20if%20user%20has%20not%20produced%20any%20events%20in%2060%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2558939%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1105511%22%20target%3D%22_blank%22%3E%40Marc_Jacquard%3C%2FA%3E%26nbsp%3BI%20would%20recommend%20at%20least%20combining%20the%20two%20tables%20if%20you%20want%20to%20get%20the%20best%20picture%20of%20user%20activity.%26nbsp%3B%20%26nbsp%3BMy%20admin%20account%20has%20logged%20into%20my%20Azure%20Sentinel%20instance%20quite%20a%20bit%20in%20the%20last%20week%20but%20has%20not%20performed%20any%20activity%20that%20would%20show%20up%20in%20SecurityEvent%20(which%20shows%20information%20from%20the%20Windows%20machines%20so%20I%20am%20not%20sure%20that%20is%20the%20correct%20table.%26nbsp%3B%20AzureActivity%20may%20be%20better)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2562148%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Sentinel%20rule%20to%20identify%20if%20user%20has%20not%20produced%20any%20events%20in%2060%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2562148%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1085960%22%20target%3D%22_blank%22%3E%40m_zorich%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20use%20SignIn%20logs%20it%20seems%20to%20work%20fine.%20Then%20I%20look%20up%20the%20user%20the%20query%20said%20has%20been%20inactive%20in%20SecurityEvent%20table%20and%20it%20shows%20activity%20within%20the%20last%207%20days.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2563862%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Sentinel%20rule%20to%20identify%20if%20user%20has%20not%20produced%20any%20events%20in%2060%20days%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2563862%22%20slang%3D%22en-US%22%3ECan%20you%20share%20your%20query%3F%20If%20you%20query%20your%20SecurityEvent%20table%20without%20the%20watchlist%20lookup%20are%20you%20getting%20activity%20from%20the%20users%2C%20i.e%20are%20they%20definitely%20not%20triggering%20any%20security%20events%3F%3C%2FLINGO-BODY%3E
Occasional Contributor

I am working on a rule that uses a watchlist of elevated accounts. What I am trying to create is a rule that will tell me if one of these elevated accounts has not been used in over 60 days so we can mark it for removal from the list.  I am looking at the SecurityEvent table for any events these users generate. I can not seem to find a way to a "last event" type comparison. I can get a list of things they have done with starttime and endtime, but nothing I do seems to show a user that has not done any activity in 60 days or more. If anyone has done this, could you please share your rule/wisdom?

11 Replies

@Marc_Jacquard I would use the SignInLogs to determine if the user has signed in during your time frame in question.  You could use the UserPrincipalName or UserDisplayName fields to compare against your Watchlist.

Gary, I tried that initially, but I wanted to see any activity. Below is my current logic. I just can not seem to get the time comparison correct. No matter which log source I use, I always have people who who have been active in the last 7 days even though I know some have not been. I switch between in~ and !in~ for testing. It appears I just do not have the time comparison logic correct.
let adminlist = (_GetWatchlist("Elevated_accounts")|project UserName);
let starttime = 31d;
//let midtime = 30d;
//let endtime =10m;
SecurityEvent
| where TimeGenerated > ago(32d)
|extend Account = trim_start(@"^.*\\", Account)
| where Account in~ (adminlist)
//|where EventID == "4624"
//|where Account !endswith "$"
| summarize StartTimeUtc = min(TimeGenerated), EndTimeUtc = max(TimeGenerated) by Account, EventID
//|where EndTimeUtc - StartTimeUtc > starttime

@Marc_Jacquard 

 

Something like this should work for you. My data isn't an exact match for your formatting but you should be able to make it work in your environment. I created a test WatchList with a single column of Account, then a few rows of DOMAIN\username fields that had been active, and then some fake accounts that hadn't (fake inactive admins)

 

let adminlist = (_GetWatchlist("TestWatchList")|project Account);
SecurityEvent
| where TimeGenerated > ago(10d)
| where Account in (adminlist)
| distinct Account
| join kind=rightanti adminlist on Account

 

Join operator - https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/joinoperator?pivots=azuredataexplor... rightanti should return results from the right (your watchlist) that aren't in the left (your query for active users)

@Marc_Jacquard I would recommend at least combining the two tables if you want to get the best picture of user activity.   My admin account has logged into my Azure Sentinel instance quite a bit in the last week but has not performed any activity that would show up in SecurityEvent (which shows information from the Windows machines so I am not sure that is the correct table.  AzureActivity may be better)

 

@m_zorich 

 

Thank you. I will give this a try and see how it goes.

@m_zorich 

If I use SignIn logs it seems to work fine. Then I look up the user the query said has been inactive in SecurityEvent table and it shows activity within the last 7 days.

Can you share your query? If you query your SecurityEvent table without the watchlist lookup are you getting activity from the users, i.e are they definitely not triggering any security events?
This is what I have been playing with. I tried to do some union stuff, but that just got ugly fast. I did one query for each table: SignIn logs and SecurityEvent logs. I think the SignIn logs one is working properly, but I definitely know the SecurityEvent one is not. There are certain accounts I know for a fact are used daily. They come up in the not active query, but then I just do a serach as flows and they show up active today.
24 hour timeframe
SecurityEvent
|serach "Accountname"
SignIn logs Query
let adminlist = (_GetWatchlist("Elevated_accounts")|project UserName);
let starttime = 91d;
//let midtime = 30d;
let endtime = 1d;
SigninLogs
// historical successful sign-in
|where TimeGenerated > ago(61d)
|extend UserPrincipalName = trim_end(@"@(.*)", UserPrincipalName)
| where UserPrincipalName in~ (adminlist)
|distinct UserPrincipalName
//| where TimeGenerated between (ago(starttime) .. ago(endtime) )
//| summarize by UserPrincipalName, Identity, TimeGenerated
|join kind = rightanti (
SigninLogs
|extend UserPrincipalName = trim_end(@"@(.*)", UserPrincipalName)
| where UserPrincipalName in~ (adminlist)
) on UserPrincipalName


SecurityEvents Query
let adminlist = (_GetWatchlist("Elevated_accounts")|project UserName);
//let starttime = 91d;
//let midtime = 30d;
let endtime = 1d;
SecurityEvent
// historical successful sign-in
|where TimeGenerated > ago(61d)
|extend Account = trim_end(@"@(.*)", UserPrincipalName)
| where Account in~ (adminlist)
|distinct Account
//|summarize by Account, TimeGenerated
|join kind = rightanti (
SecurityEvent
// historical successful sign-in
//|where TimeGenerated > ago(31d)
|extend Account = trim_start(@"^.*\\", Account)
| where Account in~ (adminlist)
//| summarize by Account, TimeGenerated
) on Account

Sure no worries, and what format are your accounts, so UserPrincipalName = bobsmith@yourdomain.com and then account is just bobsmith? And is your watchlist just a list of userprincipalnames?

best response confirmed by Marc_Jacquard (Occasional Contributor)
Solution
Assuming you have just listed your userprincipalnames in your watchlist, and your on premise account is just part before the @ then these two should work. When joining you need to have a column that matches on both sides (your query and your watchlist). For signinlogs userprincipalname is fine because that's what Azure AD uses to identify people. We will just rename userprincipalname to username to match your watchlist. I added ResultType = 0 to only get successful signins, but you can remove if you want

let adminlist = (_GetWatchlist("Elevated_accounts")|project UserName);
SigninLogs
| where TimeGenerated > ago (30d)
| extend UserName = UserPrincipalName
| where UserName in (adminlist)
| where ResultType == 0
| distinct UserName
| join kind=rightanti adminlist on UserName

For SecurityEvent we want to use TargetUserName, so we will rename it when we set our variable and trim the @yourdomain.com part out

let adminlist = _GetWatchlist("Elevated_accounts")|extend TargetUserName = trim_end(@"@(.*)", UserName)|project TargetUserName;
SecurityEvent
| where TimeGenerated > ago(30d)
| where TargetUserName in (adminlist)
| distinct TargetUserName
| join kind = rightanti adminlist on TargetUserName

Try those and let me know
I will give it a try and see what happens