Home
%3CLINGO-SUB%20id%3D%22lingo-sub-712381%22%20slang%3D%22en-US%22%3EUsing%20KQL%20functions%20to%20speed%20up%20analysis%20in%20Azure%20Sentinel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-712381%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22text-align%3A%20left%3B%22%3ESecurity%20operations%20can%20often%20be%20a%20very%20repetitive%20role.%20As%20a%20security%20analyst%20you%20will%20often%20find%20yourself%20conducting%20the%20same%20actions%20and%20tasks%20as%20you%20work%20through%20an%20investigation.%20Whilst%20it%20is%20a%20good%20objective%20to%20try%20and%20automate%20these%20tasks%20as%20much%20as%20possible%20often%20it%20is%20not%20suitable%20or%20practical%20to%20fully%20automate%20them.%20In%20these%20cases%2C%20the%20most%20effective%20thing%20you%20can%20do%20is%20modularize%20those%20actions%20into%20tools%20that%20are%20easy%20to%20use%20when%20required.%20KQL%20functions%20in%20Azure%20Sentinel%20provide%20a%20way%20in%20which%20analysts%20can%20build%20up%20a%20collection%20of%20investigation%20tools%20to%20call%20upon%20quickly%20and%20simply.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20what%20is%20a%20KQL%20function%3F%20It%20is%20simply%20a%20query%20that%20you%20create%2C%20save%2C%20and%20then%20assign%20an%20alias%20to%20call%20it%20by.%20A%20very%20basic%20example%20of%20how%20you%20might%20use%20a%20KQL%20function%20is%20a%20query%20that%20you%20run%20on%20a%20regular%20basis%2C%20such%20as%20summarizing%20certain%20events%20over%20a%20given%20time%20period.%20Rather%20than%20typing%20out%20the%20full%20query%20each%20time%20we%20can%20save%20it%20with%20an%20alias%2C%20allowing%20us%20to%20call%20the%20full%20query%20just%20be%20entering%20its%20name.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20how%20do%20you%20go%20about%20creating%20a%20KQL%20function.%20The%20first%20thing%20to%20do%20is%20write%20a%20query%2C%20for%20this%20initial%20example%20we%20are%20just%20going%20to%20write%20a%20very%20simply%20query%20to%20get%20all%20the%20Security%20Events%20collected%20over%20the%20last%20week%20and%20return%20a%20list%20of%20event%20types%20and%20a%20count%20of%20how%20many%20times%20there%20were%20observed%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESecurityEvent%0A%7C%20%3CFONT%20color%3D%22%233366FF%22%3Ewhere%3C%2FFONT%3E%20TimeGenerated%20%26gt%3B%3D%20ago(%3CFONT%20color%3D%22%23339966%22%3E7%3C%2FFONT%3Ed)%0A%7C%20%3CFONT%20color%3D%22%233366FF%22%3Esummarize%20count%3C%2FFONT%3E()%20%3CFONT%20color%3D%22%233366FF%22%3Eby%3C%2FFONT%3E%20Activity%0A%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20create%20a%20KQL%20function%20for%20this%20query%20we%20simply%20enter%20it%20into%20the%20query%20box%20in%20the%20Logs%20blade%20of%20Azure%20Sentinel%20and%20click%20the%20Save%20button.%20This%20will%20open%20a%20new%20blade%20where%20we%20can%20choose%20to%20save%20this%20query%20as%20a%20function%20and%20assign%20it%20an%20alias%20by%20which%20we%20can%20call%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2F8gportalvhdsf9v440s15hrt.blob.core.windows.net%2Flithium%2FAzureSentinel%2F1.PNG%22%20border%3D%220%22%20alt%3D%22Screenshot%20of%20the%20function%20save%20blade%22%20width%3D%22277%22%20height%3D%22404%22%20align%3D%22center%22%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20saved%20we%20can%20call%20the%20KQL%20function%20by%20simply%20typing%20the%20alias%20name%20into%20the%20query%20box%20and%20selecting%20Run.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2F8gportalvhdsf9v440s15hrt.blob.core.windows.net%2Flithium%2FAzureSentinel%2F2.PNG%22%20border%3D%220%22%20alt%3D%22Screenshot%20of%20the%20output%20of%20calling%20a%20function%22%20width%3D%22596%22%20height%3D%22444%22%20align%3D%22center%22%20%2F%3E%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20left%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20left%3B%22%3EYou%20can%20keep%20track%20of%20the%20KQL%20functions%20you%20have%20saved%20by%20referring%20to%20the%20functions%20tab%20of%20the%20navigation%20pane%20within%20the%20Sentinel%20Logs%20blade%3A%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20left%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2F8gportalvhdsf9v440s15hrt.blob.core.windows.net%2Flithium%2FAzureSentinel%2F3.PNG%22%20border%3D%220%22%20alt%3D%22Screenshot%20of%20the%20functions%20tab%20of%20the%20navigation%20pane%22%20width%3D%22277%22%20height%3D%22417%22%20align%3D%22center%22%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20this%20example%20is%20not%20the%20best%20use%20of%20a%20KQL%20function%2C%20there%20are%20better%20ways%20to%20visualize%20that%20data%20and%20we%20could%20choose%20to%20save%20it%20as%20a%20query%20rather%20than%20turn%20it%20into%20a%20function.%20But%20what%20if%20we%20had%20a%20more%20complex%20query%2C%20one%20that%20we%20didn%E2%80%99t%20want%20to%20have%20to%20memorize%20and%20type%20each%20time%2C%20and%20one%20that%20we%20would%20want%20to%20use%20in%20conjunction%20of%20other%20queries%20as%20part%20of%20our%20investigation%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20good%20example%20of%20this%20would%20be%20a%20parser.%20The%20most%20useful%20log%20sources%20are%20ones%20rich%20in%20data%2C%20however%20these%20can%20be%20the%20most%20difficult%20to%20work%20with%20as%20they%20often%20contain%20complex%20data.%20Office%20365%20Activity%20logs%20are%20a%20good%20example%20of%20this%2C%20they%20contain%20detail%20on%20a%20very%20wide%20range%20of%20different%20activity.%20As%20you%20can%20see%20in%20the%20example%20below%20a%20lot%20of%20this%20data%20appears%20as%20JSON%20data%20under%20a%20single%20indexed%20field.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2F8gportalvhdsf9v440s15hrt.blob.core.windows.net%2Flithium%2FAzureSentinel%2F4.png%22%20border%3D%220%22%20alt%3D%22Screenshot%20of%20JSON%20data%20within%20Office%20365%20logs%22%20width%3D%22844%22%20height%3D%22470%22%20align%3D%22center%22%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20write%20a%20parser%20to%20parse%20these%20nested%20elements%20for%20every%20conceivable%20log%20but%20that%20might%20not%20be%20practical%20or%20worthwhile%20depending%20on%20the%20log%20source.%20However%2C%20an%20analyst%20may%20just%20want%20to%20write%20a%20parser%20for%20a%20subset%20of%20the%20data%20source%20that%20is%20particularly%20interesting%20from%20a%20security%20perspective.%20One%20such%20example%20I%20came%20across%20recently%20for%20Office%20logs%20related%20to%20the%20creation%20of%20mailbox%20forwarding%20rules.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMailbox%20forwarding%20is%20a%20technique%20often%20used%20by%20attackers%20for%20data%20exfiltration.%20Once%20they%20have%20compromised%20an%20Office%20365%20account%20they%20will%20log%20in%20and%20create%20rules%20to%20forward%20users%20mail%20to%20an%20external%20mailbox%20that%20they%20control%2C%20meaning%20that%20even%20if%20the%20loose%20their%20access%20to%20the%20Office%20365%20account%20they%20will%20keep%20access%20to%20the%20victim%E2%80%99s%20email%20for%20as%20long%20as%20that%20rule%20is%20in%20place.%20The%20problem%20for%20security%20analysts%20is%20that%20mailbox%20rules%20are%20widely%20used%20for%20legitimate%20purpose%20so%20finding%20those%20rules%20that%20are%20potentially%20malicious%20can%20be%20a%20time-consuming%20task.%20To%20make%20it%20as%20easy%20as%20possible%20you%20ideally%20want%20the%20data%20in%20an%20easy%20to%20read%20and%20review%20format.%20So%20when%20I%20was%20conducting%20an%20investigation%20recently%20and%20wanted%20to%20look%20at%20mailbox%20forward%20activity%20I%20created%20a%20parser%20to%20take%20that%20nested%20JSON%20data%20in%20the%20Office%20365%20Activity%20log%20and%20parse%20out%20the%20key%20data%20elements%20I%20wanted%20to%20see%20for%20analysis%2C%20namely%20what%20condition%20did%20the%20forwarding%20rule%20have%2C%20and%20where%20was%20it%20forwarding%20data%3A%3C%2FP%3E%0A%3CPRE%3EOfficeActivity%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Ewhere%3C%2FFONT%3E%20TimeGenerated%20%26gt%3B%3D%20ago(%3CFONT%20color%3D%22%23339966%22%3E30%3C%2FFONT%3Ed)%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Ewhere%3C%2FFONT%3E%20Operation%20%3D%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'New-InboxRule'%3C%2FFONT%3E%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Eextend%3C%2FFONT%3E%20details%20%3D%20parse_json(Parameters)%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Ewhere%3C%2FFONT%3E%20details%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo'%3C%2FFONT%3E%20%3CFONT%20color%3D%22%233366FF%22%3Eor%3C%2FFONT%3E%20details%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Eextend%3C%2FFONT%3E%20ForwardTo%20%3D%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E0%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E0%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E1%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E1%3C%2FFONT%3E%5D.Value%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo%3C%2FFONT%3E'%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Value%2C%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Value%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo%3C%2FFONT%3E'%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFONT%20color%3D%22%23FF0000%22%3E'Check%20Parameters'%3C%2FFONT%3E)))))%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Eextend%3C%2FFONT%3E%20RedirectTo%20%3D%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E0%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E0%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E1%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E1%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'Check%20Parameters')))))%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Eextend%3C%2FFONT%3E%20RuleName%20%3D%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'Name'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'Name'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E5%3C%2FFONT%3E%5D.Name%20%3CFONT%20color%3D%22%233366FF%22%3Econtains%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23FF0000%22%3E'Name'%3C%2FFONT%3E%2C%20details%5B%3CFONT%20color%3D%22%23339966%22%3E5%3C%2FFONT%3E%5D.Value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFONT%20color%3D%22%23FF0000%22%3E'Check%20Parameters'%3C%2FFONT%3E)))%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Eextend%3C%2FFONT%3E%20RuleParameters%20%3D%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo'%3C%2FFONT%3E%20%3CFONT%20color%3D%22%233366FF%22%3Eand%20%3C%2FFONT%3E%20details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%2C%20%0A%20%20%20%20%20%20%20%20strcat(tostring(details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Name)%2C%20'-'%2C%20tostring(details%5B%3CFONT%20color%3D%22%23339966%22%3E2%3C%2FFONT%3E%5D.Value))%2C%0A%20%20%20%20%20%20%20%20iif(details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo'%3C%2FFONT%3E%20%3CFONT%20color%3D%22%233366FF%22%3Eand%20%3C%2FFONT%3E%20details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%20%3CFONT%20color%3D%22%233366FF%22%3Eand%3C%2FFONT%3E%20details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'Name'%3C%2FFONT%3E%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20strcat(tostring(details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Name)%2C%20'-'%2C%20tostring(details%5B%3CFONT%20color%3D%22%23339966%22%3E3%3C%2FFONT%3E%5D.Value))%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20iff(details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'ForwardTo'%3C%2FFONT%3E%20%3CFONT%20color%3D%22%233366FF%22%3Eand%3C%2FFONT%3E%20details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'RedirectTo'%3C%2FFONT%3E%20%3CFONT%20color%3D%22%233366FF%22%3Eand%3C%2FFONT%3E%20details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'Name'%3C%2FFONT%3E%20and%20details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name%20!%3D%20%3CFONT%20color%3D%22%23FF0000%22%3E'StopProcessingRules'%3C%2FFONT%3E%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20strcat(tostring(details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Name)%2C%20'-'%2C%20tostring(details%5B%3CFONT%20color%3D%22%23339966%22%3E4%3C%2FFONT%3E%5D.Value))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFONT%20color%3D%22%23FF0000%22%3E'All%20Mail'%3C%2FFONT%3E)))%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Eproject%3C%2FFONT%3E%20TimeGenerated%2C%20Operation%2C%20RuleName%2C%20RuleParameters%2C%20iif(details%20contains%20'ForwardTo'%2C%20ForwardTo%2C%20RedirectTo)%2C%20ClientIP%2C%20UserId%0A%20%20%20%20%7C%20%3CFONT%20color%3D%22%233366FF%22%3Eproject-rename%3C%2FFONT%3E%20Email_Forwarded_To%20%3D%20Column1%2C%20Creating_User%20%3D%20UserId%0A%3C%2FPRE%3E%0A%3CP%3EThis%20parser%20produces%20the%20following%20output%3A%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2F8gportalvhdsf9v440s15hrt.blob.core.windows.net%2Flithium%2FAzureSentinel%2F5.png%22%20border%3D%220%22%20alt%3D%22Screenshot%20of%20the%20output%20of%20the%20parser%20function%20in%20Azure%20Sentinel%22%20width%3D%22911%22%20height%3D%22245%22%20align%3D%22center%22%20%2F%3E%3C%2FP%3E%0A%3CP%3ECreating%20a%20parser%20like%20this%20requires%20that%20you%20spend%20some%20time%20identifying%20where%20in%20the%20JSON%20data%20the%20elements%20you%20want%20to%20see%20could%20be%2C%20and%20as%20such%20I%20didn%E2%80%99t%20want%20to%20have%20to%20memorize%20it%20and%20type%20it%20out%20each%20time%20I%20wanted%20to%20look%20for%20this%20activity%20as%20part%20of%20an%20investigation.%20This%20made%20it%20an%20ideal%20query%20to%20save%20as%20a%20KQL%20function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2F8gportalvhdsf9v440s15hrt.blob.core.windows.net%2Flithium%2FAzureSentinel%2F6.png%22%20border%3D%220%22%20alt%3D%22Screenshot%20of%20the%20function%20save%20blade%22%20width%3D%22277%22%20height%3D%22312%22%20align%3D%22center%22%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20when%20conducting%20an%20investigation%20where%20I%20want%20to%20review%20all%20mailbox%20forwarding%20rules%20I%20can%20simply%20call%20this%20parser%20by%20including%20mail_parse%20in%20my%20query.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20below%20example%20I%20am%20investigating%20a%20host%20that%20I%20suspect%20may%20have%20been%20compromised.%20I%20want%20to%20see%20if%20this%20host%20has%20been%20used%20to%20access%20my%20Office%20365%20tenant%2C%20and%20specifically%20whether%20any%20suspicious%20forwarding%20rules%20have%20been%20created%20during%20connections%20from%20this%20host.%20To%20do%20this%20I%20write%20a%20query%20looking%20for%20all%20Office%20365%20activity%20where%20the%20Client%20IP%20is%20that%20of%20the%20host%20in%20question.%20I%20then%20call%20my%20parser%20by%20joining%20the%20query%20to%20the%20mail_parse%20function%20I%20created.%20This%20then%20selects%20the%20relevant%20data%20and%20parses%20out%20the%20fields%20I%20want%20to%20see%2C%26nbsp%3B%20allowing%20me%20to%20easily%20identify%20potential%20exfiltration%20of%20confidential%20data%20outside%20my%20organization.%20Whilst%20I%20don't%20need%20to%20in%20this%20example%20I%20could%20also%26nbsp%3Bexpand%20my%20investigation%20from%20here%20and%20look%20at%20further%20refining%20this%20search%20to%20look%20at%20rules%20created%20where%20the%20address%20being%20forward%20to%20is%20one%20outside%20of%20my%20control.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2F8gportalvhdsf9v440s15hrt.blob.core.windows.net%2Flithium%2FAzureSentinel%2F7.png%22%20border%3D%220%22%20alt%3D%22Screenshot%20of%20an%20Azure%20Sentinel%20query%20invoking%20the%20parser%20function%22%20width%3D%22932%22%20height%3D%22502%22%20align%3D%22center%22%20%2F%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EKQL%20functions%20are%20a%20quick%20and%20simple%20way%20to%20make%20repetitive%20actions%20simpler%20and%20quicker%2C%20and%20is%20one%20of%20the%20many%20ways%20that%20Azure%20Sentinel%20aims%20to%20make%20the%20job%20of%20a%20security%20analyst%20more%20efficient%20and%20effective.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EMore%20details%20on%20KQL%20functions%20can%20be%20found%20here%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Flog-query%2Ffunctions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-monitor%2Flog-query%2Ffunctions%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Ftree%2Fmaster%2FFunctions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Ftree%2Fmaster%2FFunctions%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EIn%20addition%2C%20there%20are%20useful%20parsers%20on%20our%20GitHub%20site%20that%20you%20may%20want%20to%20consider%20converting%20to%20functions%20as%20well%20as%20some%20other%20prebuilt%20functions%3A%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Ftree%2Fmaster%2FParsers%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Ftree%2Fmaster%2FParsers%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-712381%22%20slang%3D%22en-US%22%3E%3CP%3ESecurity%20Operations%20can%20often%20be%20a%20very%20repetitive%20role.%20As%20a%20security%20analyst%20you%20will%20often%20find%20yourself%20conducting%20the%20same%20actions%20and%20tasks%20as%20you%20work%20through%20an%20investigation.%20KQL%20functions%20in%20Azure%20Sentinel%20provide%20a%20way%20in%20which%20analysts%20can%20build%20up%20a%20collection%20of%20investigation%20tools%20to%20call%20upon%20quickly%20and%20simply.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20657px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120280i6845DEC4AE18CB85%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-712381%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHunting%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EInvestigation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EQuery%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Security operations can often be a very repetitive role. As a security analyst you will often find yourself conducting the same actions and tasks as you work through an investigation. Whilst it is a good objective to try and automate these tasks as much as possible often it is not suitable or practical to fully automate them. In these cases, the most effective thing you can do is modularize those actions into tools that are easy to use when required. KQL functions in Azure Sentinel provide a way in which analysts can build up a collection of investigation tools to call upon quickly and simply.

 

So, what is a KQL function? It is simply a query that you create, save, and then assign an alias to call it by. A very basic example of how you might use a KQL function is a query that you run on a regular basis, such as summarizing certain events over a given time period. Rather than typing out the full query each time we can save it with an alias, allowing us to call the full query just be entering its name.

 

So how do you go about creating a KQL function. The first thing to do is write a query, for this initial example we are just going to write a very simply query to get all the Security Events collected over the last week and return a list of event types and a count of how many times there were observed:

 

SecurityEvent
| where TimeGenerated >= ago(7d)
| summarize count() by Activity

 

To create a KQL function for this query we simply enter it into the query box in the Logs blade of Azure Sentinel and click the Save button. This will open a new blade where we can choose to save this query as a function and assign it an alias by which we can call it.

 

Screenshot of the function save blade

 

Once saved we can call the KQL function by simply typing the alias name into the query box and selecting Run.

 

Screenshot of the output of calling a function

 

You can keep track of the KQL functions you have saved by referring to the functions tab of the navigation pane within the Sentinel Logs blade:

 

Screenshot of the functions tab of the navigation pane

 

Now this example is not the best use of a KQL function, there are better ways to visualize that data and we could choose to save it as a query rather than turn it into a function. But what if we had a more complex query, one that we didn’t want to have to memorize and type each time, and one that we would want to use in conjunction of other queries as part of our investigation?

 

A good example of this would be a parser. The most useful log sources are ones rich in data, however these can be the most difficult to work with as they often contain complex data. Office 365 Activity logs are a good example of this, they contain detail on a very wide range of different activity. As you can see in the example below a lot of this data appears as JSON data under a single indexed field. 

 

Screenshot of JSON data within Office 365 logs

 

You can write a parser to parse these nested elements for every conceivable log but that might not be practical or worthwhile depending on the log source. However, an analyst may just want to write a parser for a subset of the data source that is particularly interesting from a security perspective. One such example I came across recently for Office logs related to the creation of mailbox forwarding rules.

 

Mailbox forwarding is a technique often used by attackers for data exfiltration. Once they have compromised an Office 365 account they will log in and create rules to forward users mail to an external mailbox that they control, meaning that even if the loose their access to the Office 365 account they will keep access to the victim’s email for as long as that rule is in place. The problem for security analysts is that mailbox rules are widely used for legitimate purpose so finding those rules that are potentially malicious can be a time-consuming task. To make it as easy as possible you ideally want the data in an easy to read and review format. So when I was conducting an investigation recently and wanted to look at mailbox forward activity I created a parser to take that nested JSON data in the Office 365 Activity log and parse out the key data elements I wanted to see for analysis, namely what condition did the forwarding rule have, and where was it forwarding data:

OfficeActivity
    | where TimeGenerated >= ago(30d)
    | where Operation == 'New-InboxRule'
    | extend details = parse_json(Parameters)
    | where details contains 'ForwardTo' or details contains 'RedirectTo'
    | extend ForwardTo = iif(details[0].Name contains 'ForwardTo', details[0].Value,
        iif(details[1].Name contains 'ForwardTo', details[1].Value, 
            iif(details[2].Name contains 'ForwardTo', details[2].Value,  
                iif(details[3].Name contains 'ForwardTo', details[3].Value, 
                    iif(details[4].Name contains 'ForwardTo', details[4].Value,
                        'Check Parameters')))))
    | extend RedirectTo = iif(details[0].Name contains 'RedirectTo', details[0].Value,
        iif(details[1].Name contains 'RedirectTo', details[1].Value,
            iif(details[2].Name contains 'RedirectTo', details[2].Value,
                iif(details[3].Name contains 'RedirectTo', details[3].Value,
                    iif(details[4].Name contains 'RedirectTo', details[4].Value,
                        'Check Parameters')))))
    | extend RuleName = iif(details[3].Name contains 'Name', details[3].Value,
         iif(details[4].Name contains 'Name', details[4].Value,
            iif(details[5].Name contains 'Name', details[5].Value,
                'Check Parameters')))
    | extend RuleParameters = iif(details[2].Name != 'ForwardTo' and  details[2].Name != 'RedirectTo', 
        strcat(tostring(details[2].Name), '-', tostring(details[2].Value)),
        iif(details[3].Name != 'ForwardTo' and  details[3].Name != 'RedirectTo' and details[3].Name != 'Name',
            strcat(tostring(details[3].Name), '-', tostring(details[3].Value)), 
                iff(details[4].Name != 'ForwardTo' and details[4].Name != 'RedirectTo' and details[4].Name != 'Name' and details[4].Name != 'StopProcessingRules',
                strcat(tostring(details[4].Name), '-', tostring(details[4].Value)),
                'All Mail')))
    | project TimeGenerated, Operation, RuleName, RuleParameters, iif(details contains 'ForwardTo', ForwardTo, RedirectTo), ClientIP, UserId
    | project-rename Email_Forwarded_To = Column1, Creating_User = UserId

This parser produces the following output:

Screenshot of the output of the parser function in Azure Sentinel

Creating a parser like this requires that you spend some time identifying where in the JSON data the elements you want to see could be, and as such I didn’t want to have to memorize it and type it out each time I wanted to look for this activity as part of an investigation. This made it an ideal query to save as a KQL function.

 

Screenshot of the function save blade

 

Now when conducting an investigation where I want to review all mailbox forwarding rules I can simply call this parser by including mail_parse in my query.

 

In the below example I am investigating a host that I suspect may have been compromised. I want to see if this host has been used to access my Office 365 tenant, and specifically whether any suspicious forwarding rules have been created during connections from this host. To do this I write a query looking for all Office 365 activity where the Client IP is that of the host in question. I then call my parser by joining the query to the mail_parse function I created. This then selects the relevant data and parses out the fields I want to see,  allowing me to easily identify potential exfiltration of confidential data outside my organization. Whilst I don't need to in this example I could also expand my investigation from here and look at further refining this search to look at rules created where the address being forward to is one outside of my control.

 

Screenshot of an Azure Sentinel query invoking the parser function

 

KQL functions are a quick and simple way to make repetitive actions simpler and quicker, and is one of the many ways that Azure Sentinel aims to make the job of a security analyst more efficient and effective.

 

More details on KQL functions can be found here:

https://docs.microsoft.com/en-us/azure/azure-monitor/log-query/functions

https://github.com/Azure/Azure-Sentinel/tree/master/Functions

In addition, there are useful parsers on our GitHub site that you may want to consider converting to functions as well as some other prebuilt functions:

https://github.com/Azure/Azure-Sentinel/tree/master/Parsers