Home
%3CLINGO-SUB%20id%3D%22lingo-sub-228256%22%20slang%3D%22en-US%22%3ERe%3A%20Hunting%20tip%20of%20the%20month%3A%20PowerShell%20commands%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-228256%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20awesome.%20Thanks%20!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-210898%22%20slang%3D%22en-US%22%3EHunting%20tip%20of%20the%20month%3A%20PowerShell%20commands%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-210898%22%20slang%3D%22en-US%22%3E%3CP%3EPowerShell%20scripts%20have%20clearly%20become%20one%20of%20the%20weapons%20of%20choice%20for%20attackers%20who%20want%20to%20stay%20extremely%20stealthy.%20Like%20other%20scripts%2C%20they%20are%20easily%20obfuscated%2C%20downloaded%2C%20tucked%20away%20in%20the%20registry%20and%20among%20other%20benign-looking%20content%2C%20and%20launched%20using%20a%20legitimate%20process%E2%80%94the%20scripting%20engine.%20As%20the%20de%20facto%20scripting%20standard%20for%20administrative%20tasks%20on%20Windows%2C%20PowerShell%20trumps%20other%20scripting%20languages%20because%20it%20can%20easily%20invoke%20system%20APIs%20and%20access%20a%20variety%20of%20.Net%20classes%20and%20objects.%20PowerShell%20is%20loved%20by%20system%20administrators%20and%20defenders%2C%20but%20unfortunately%20it%20is%20just%20as%20appealing%20to%20attackers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20post%2C%20I%E2%80%99d%20like%20to%20share%20a%20few%20queries%20that%20can%20make%20it%20much%20easier%20for%20you%20to%20find%20suspicious%20PowerShell%20activity%20in%20your%20network%E2%80%A6%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EExplaining%20PowerShellCommand%20events%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThis%20event%20type%20reports%20the%20names%20of%20commands%20executed%20by%20the%20PowerShell%20engine%2C%20including%20PowerShell%20cmdlets%20and%20scripts%2C%20as%20well%20as%20other%20executables.%3C%2FP%3E%0A%3CP%3EIt%20does%20not%20matter%20if%20that%20command%20is%20specified%20in%20the%20commandline%20or%20not%20%E2%80%93%20the%20command%20is%20reported%20nonetheless.%3C%2FP%3E%0A%3CP%3ESimilarly%2C%20it%E2%80%99s%20reported%20whether%20executed%20by%20powershell.exe%2C%20by%20wsmprovhost.exe%20(used%20for%20remote%20PowerShell)%20or%20by%20some%20other%20process%20that%20loads%20the%20PowerShell%20engine.%3C%2FP%3E%0A%3CP%3EThis%20event%20is%20part%20of%20the%20MiscEvents%20table%2C%20and%20its%20ActionType%20is%20%22PowerShellCommand%22.%3C%2FP%3E%0A%3CP%3EThe%20columns%20starting%20with%20InitiatingProcess%20contain%20the%20details%20of%20the%20processes%20that%20has%20executed%20the%20command.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20AdditionalFields%20column%20contains%20the%20Command%20name%20in%20a%20JSON%20structure%2C%20in%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7B%22Command%22%3A%22Create-WmiClass%22%7D%3C%2FP%3E%0A%3CP%3EThis%20column%20is%20where%20we%20keep%20the%20long%20tail%20of%20fields%20%E2%80%93%20the%20uncommon%20ones%20that%20are%20unique%20to%20just%20a%20few%20event%20types.%3C%2FP%3E%0A%3CP%3EYou%20can%20parse%20it%20by%20calling%20%3CA%20href%3D%22https%3A%2F%2Fdocs.loganalytics.io%2Fdocs%2FLanguage-Reference%2FScalar-functions%2Fparse_json()%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eparse_json%3C%2FA%3E%20or%20%3CA%20href%3D%22https%3A%2F%2Fdocs.loganalytics.io%2Fdocs%2FLanguage-Reference%2FScalar-functions%2Fextractjson()%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eextractjson%3C%2FA%3E%2C%20or%20you%20could%20simply%20run%20a%20%E2%80%9Ccontains%E2%80%9D%20where%20filter%20on%20it.%3C%2FP%3E%0A%3CP%3EIf%20your%20organization%20is%20a%20large%20one%2C%20we%20suggest%20you%20speed%20up%20your%20queries%20by%20applying%20more%20filters%20before%20you%20parse%20the%20JSON%20column.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EQuery%20%231%3A%20Find%20all%20executions%20of%20a%20specific%20command%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EHave%20you%20found%20a%20suspicious%20or%20malicious%20command%20in%20your%20network%2C%20and%20want%20to%20find%20other%20instances%20of%20it%3F%20Or%20perhaps%20you%20have%20read%20%3CA%20href%3D%22https%3A%2F%2Fcloudblogs.microsoft.com%2Fmicrosoftsecure%2F2017%2F12%2F04%2Fwindows-defender-atp-machine-learning-and-amsi-unearthing-script-based-attacks-that-live-off-the-land%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ea%20new%20blog%20post%3C%2FA%3E%20about%20some%20attacker%20or%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FPowerShellMafia%2FPowerSploit%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eattack%20framework%3C%2FA%3E%20using%20some%20PowerShell%20cmdlet%3F%3C%2FP%3E%0A%3CP%3EYou%20can%20now%20easily%20find%20out%20if%20it%20was%20executed%20in%20your%20organization%20and%20on%20which%20machines.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3E%2F%2F%20Taken%20from%20%3C%2FFONT%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2FWindowsDefenderATP-Hunting-Queries%2Fblob%2Fmaster%2FExecution%2FPowershellCommand%2520footprint.txt%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FMicrosoft%2FWindowsDefenderATP-Hunting-Queries%2Fblob%2Fmaster%2FExecution%2FPowershellCommand%2520footprint.txt%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23003366%22%3EpowershellCommandName%3C%2FFONT%3E%20%3D%20%3CFONT%20color%3D%22%23993300%22%3E%22Invoke-RickAscii%22%3C%2FFONT%3E%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23800080%22%3EMiscEvents%20%3C%2FFONT%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EActionType%3C%2FFONT%3E%20%3D%3D%20%3CFONT%20color%3D%22%23993300%22%3E%22PowerShellCommand%22%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000FF%22%3Eand%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EAdditionalFields%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000FF%22%3Econtains%20%3C%2FFONT%3EpowershellCommandName%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Eextend%3C%2FFONT%3E%20PowerShellCommand%3D%3CFONT%20color%3D%22%23993300%22%3Eextractjson%3C%2FFONT%3E(%3CFONT%20color%3D%22%23993300%22%3E%22%24.Command%22%3C%2FFONT%3E%2C%20AdditionalFields%2C%20%3CFONT%20color%3D%22%23993300%22%3Etypeof%3C%2FFONT%3E(%3CFONT%20color%3D%22%230000FF%22%3Estring%3C%2FFONT%3E))%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20PowerShellCommand%20%3D~%20powershellCommandName%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Eproject%3C%2FFONT%3E%20EventTime%2C%20ComputerName%2C%20InitiatingProcessCommandLine%2C%20InitiatingProcessParentFileName%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3ENote%3A%20Not%20familiar%20with%20Invoke-RickAscii%3F%20Well%2C%20it%E2%80%99s%20not%20something%20you%20should%20hunt%20for%2C%20but%20we%20still%20recommend%20you%20%3CA%20href%3D%22https%3A%2F%2Fwww.nextofwindows.com%2Fpowershell-fun-watch-rick-astley-sing-and-dance-never-let-you-down%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eread%20all%20about%20it%3C%2FA%3E%20as%20it%E2%80%99s%20the%20coolest%20PowerShell%20cmdlet%20ever.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EQuery%20%232%3A%20Find%20all%20commands%20run%20on%20a%20machine%20in%20some%20time-range%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ENoticed%20some%20interesting%20activities%20on%20a%20machine%3F%20Try%20this%20query%20to%20quickly%20check%20for%20PowerShell%20activity%20around%20the%20same%20time.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23800080%22%3EMiscEvents%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20MachineId%20%3D%3D%20%3CFONT%20color%3D%22%23993300%22%3E%228707b4da675a244f5be391a74b9141896b83bc77%22%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000FF%22%3Eand%3C%2FFONT%3E%20EventTime%20%3CFONT%20color%3D%22%230000FF%22%3Ebetween%3C%2FFONT%3E%20(%3CFONT%20color%3D%22%23993300%22%3Edatetime%3C%2FFONT%3E(2018-06-26T23%3A48%3A51.5401469Z)%20..%201h)%20%3CFONT%20color%3D%22%230000FF%22%3Eand%3C%2FFONT%3E%20ActionType%20%3D%3D%20%3CFONT%20color%3D%22%23993300%22%3E%22PowerShellCommand%22%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3E%2F%2F%20Parse%20Command%20field%20from%20AdditionalFields%20column%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Eextend%3C%2FFONT%3E%20PowerShellCommand%3D%3CFONT%20color%3D%22%23993300%22%3Eextractjson%3C%2FFONT%3E(%3CFONT%20color%3D%22%23993300%22%3E%22%24.Command%22%3C%2FFONT%3E%2C%20AdditionalFields%2C%20%3CFONT%20color%3D%22%23993300%22%3Etypeof%3C%2FFONT%3E(%3CFONT%20color%3D%22%230000FF%22%3Estring%3C%2FFONT%3E))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EQuery%203%3A%20Same%20query%2C%20but%20without%20common%20powershell%20commands%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EGot%20too%20much%20noise%3F%20Let%E2%80%99s%20filter%20out%20the%20commands%20that%20are%20common%20in%20your%20organization.%3C%2FP%3E%0A%3CP%3EIn%20this%20query%20we%20use%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.loganalytics.io%2Fdocs%2FLanguage-Reference%2FQuery-statements%2FLet-statement%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elet%20statement%3C%2FA%3E%2C%20which%20enables%20you%20to%20define%20tabular%20data%2C%20scalar%20data%20or%20user-defined%20functions%20that%20will%20later%20be%20used%20(and%20reused)%20in%20the%20query.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3E%2F%2F%20This%20query%20was%20taken%20from%20%3C%2FFONT%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2FWindowsDefenderATP-Hunting-Queries%2Fblob%2Fmaster%2FExecution%2FPowershellCommand%2520-%2520uncommon%2520commands%2520on%2520machine.txt%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FMicrosoft%2FWindowsDefenderATP-Hunting-Queries%2Fblob%2Fmaster%2FExecution%2FPowershellCommand%2520-%2520uncommon%2520commands%2520on%2520machine.txt%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23003366%22%3EpowershellCommands%3C%2FFONT%3E%20%3D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CFONT%20color%3D%22%23800080%22%3EMiscEvents%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20ActionType%20%3D%3D%20%3CFONT%20color%3D%22%23993300%22%3E%22PowerShellCommand%22%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Eextend%3C%2FFONT%3E%20PowerShellCommand%3D%3CFONT%20color%3D%22%23993300%22%3Eextractjson%3C%2FFONT%3E(%3CFONT%20color%3D%22%23993300%22%3E%22%24.Command%22%3C%2FFONT%3E%2C%20AdditionalFields%2C%20%3CFONT%20color%3D%22%23993300%22%3Etypeof%3C%2FFONT%3E(string))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20PowerShellCommand%20!%3CFONT%20color%3D%22%230000FF%22%3Eendswith%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23993300%22%3E%22.ps1%22%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000FF%22%3Eand%3C%2FFONT%3E%20PowerShellCommand%20!%3CFONT%20color%3D%22%230000FF%22%3Eendswith%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23993300%22%3E%22.exe%22%3C%2FFONT%3E%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%3CSPAN%3E%26nbsp%3B%3CFONT%20color%3D%22%23003366%22%3EcommonCommands%3C%2FFONT%3E%3C%2FSPAN%3E%3CFONT%20color%3D%22%23003366%22%3E%26nbsp%3B%3C%2FFONT%3E%3D%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3EpowershellCommands%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Esummarize%20%3C%2FFONT%3E%3CFONT%20color%3D%22%23800080%22%3EMachineCount%3C%2FFONT%3E%3D%3CFONT%20color%3D%22%23993300%22%3Edcount%3C%2FFONT%3E(MachineId)%20%3CFONT%20color%3D%22%230000FF%22%3Eby%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EPowerShellCommand%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EMachineCount%3C%2FFONT%3E%20%26gt%3B%2020%3B%3C%2FP%3E%0A%3CP%3EpowershellCommands%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20MachineId%20%3D%3D%20%3CFONT%20color%3D%22%23993300%22%3E%228707b4da675a244f5be391a74b9141896b83bc77%22%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000FF%22%3Eand%3C%2FFONT%3E%20EventTime%20%3CFONT%20color%3D%22%230000FF%22%3Ebetween%3C%2FFONT%3E%20(%3CFONT%20color%3D%22%23993300%22%3Edatetime%3C%2FFONT%3E(2018-06-26T23%3A48%3A51.5401469Z)%20..%201h)%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ejoin%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000FF%22%3Ekind%3C%2FFONT%3E%3D%3CFONT%20color%3D%22%230000FF%22%3Eleftanti%3C%2FFONT%3E%20(%3CSPAN%3EcommonCommands%3C%2FSPAN%3E)%20%3CFONT%20color%3D%22%230000FF%22%3Eon%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EPowerShellCommand%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EQuery%204%3A%20pivot%20around%20an%20alert%20%26amp%3B%20parameterize%20your%20query%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ETyping%20a%20full%20datetime%20timestamp%20for%20every%20lookup%20might%20be%20time-consuming.%3C%2FP%3E%0A%3CP%3ETo%20speed%20up%20things%2C%20in%20the%20example%20below%20we%20query%20for%20the%20details%20of%20an%20alert%2C%20and%20then%20pivot%20around%20it%20%E2%80%93%20but%20you%20could%20do%20that%20for%20any%20other%20event%20just%20as%20well.%3C%2FP%3E%0A%3CP%3EWhen%20saving%20a%20shared%20query%20for%20future%20use%20by%20your%20team%2C%20it%E2%80%99s%20best%20to%20define%20constants%20at%20the%20top%20of%20the%20query%20using%20let%20statements.%20This%20makes%20it%20much%20easier%20to%20identify%20configurable%20parameters.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3E%2F%2F%20Query%20for%20alert%20details%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23003366%22%3EalertIdParameter%3C%2FFONT%3E%20%3D%20%3CFONT%20color%3D%22%23993300%22%3E%22636641078490537577_-1905871543%22%3C%2FFONT%3E%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23003366%22%3Ealert%3C%2FFONT%3E%20%3D%20%3CFONT%20color%3D%22%23800080%22%3EAlertEvents%20%3C%2FFONT%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20AlertId%20%3D%3D%20alertIdParameter%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Esummarize%3C%2FFONT%3E%20AlertFirstEventTime%3D%3CFONT%20color%3D%22%23993300%22%3Emin%3C%2FFONT%3E(%3CFONT%20color%3D%22%23800080%22%3EEventTime%3C%2FFONT%3E)%20by%20%3CFONT%20color%3D%22%23800080%22%3EMachineId%3C%2FFONT%3E%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23003366%22%3EmachineId%3C%2FFONT%3E%20%3D%20%3CFONT%20color%3D%22%23993300%22%3Etoscalar%3C%2FFONT%3E(alert%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Eproject%3C%2FFONT%3E%20MachineId)%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23003366%22%3Etimestamp%3C%2FFONT%3E%20%3D%20%3CFONT%20color%3D%22%23993300%22%3Etoscalar%3C%2FFONT%3E(alert%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Eproject%3C%2FFONT%3E%20AlertFirstEventTime)%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23008000%22%3E%2F%2F%20Same%20query%20as%20above%20-%20query%20for%20cmdlets%20executed%20by%20the%20PowerShell%20engine%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Elet%3C%2FFONT%3E%20powershellCommands%20%3D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CFONT%20color%3D%22%23800080%22%3EMiscEvents%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20ActionType%20%3D%3D%20%3CFONT%20color%3D%22%23993300%22%3E%22PowerShellCommand%22%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Eextend%3C%2FFONT%3E%20PowerShellCommand%3D%3CFONT%20color%3D%22%23993300%22%3Eextractjson%3C%2FFONT%3E(%3CFONT%20color%3D%22%23993300%22%3E%22%24.Command%22%3C%2FFONT%3E%2C%20AdditionalFields%2C%20%3CFONT%20color%3D%22%23993300%22%3Etypeof%3C%2FFONT%3E(%3CFONT%20color%3D%22%230000FF%22%3Estring%3C%2FFONT%3E))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20PowerShellCommand%20!%3CFONT%20color%3D%22%230000FF%22%3Eendswith%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23993300%22%3E%22.ps1%22%3C%2FFONT%3E%20and%20PowerShellCommand%20!%3CFONT%20color%3D%22%230000FF%22%3Eendswith%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23993300%22%3E%22.exe%22%3C%2FFONT%3E%3B%3C%2FP%3E%0A%3CP%3EpowershellCommands%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20MachineId%20%3D%3D%20machineId%20%3CFONT%20color%3D%22%230000FF%22%3Eand%3C%2FFONT%3E%20EventTime%20%3CFONT%20color%3D%22%230000FF%22%3Ebetween%3C%2FFONT%3E%20((timestamp-5min)%20..%2010min)%3C%2FP%3E%0A%3CP%3E%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ejoin%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000FF%22%3Ekind%3C%2FFONT%3E%3D%3CFONT%20color%3D%22%230000FF%22%3Eleftanti%3C%2FFONT%3E%20(powershellCommands%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Esummarize%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EMachineCount%3C%2FFONT%3E%3D%3CFONT%20color%3D%22%23993300%22%3Edcount%3C%2FFONT%3E(MachineId)%20%3CFONT%20color%3D%22%230000FF%22%3Eby%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EPowerShellCommand%3C%2FFONT%3E%20%7C%20%3CFONT%20color%3D%22%23FF6600%22%3Ewhere%3C%2FFONT%3E%20MachineCount%20%26gt%3B%2020)%20%3CFONT%20color%3D%22%230000FF%22%3Eon%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23800080%22%3EPowerShellCommand%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EOne%20last%20tip%20%E2%80%93%20machine%20ID%20and%20alert%20ID%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIn%20case%20you%20are%20not%20familiar%20with%20MachineId%20or%20AlertId%2C%20so%20those%20are%20WDATP%20unique%20identifiers%20for%20a%20machine%20or%20an%20alert.%3C%2FP%3E%0A%3CP%3EYou%20can%20copy-paste%20them%20from%20the%20URL%20of%20the%20machine%20page%20or%20the%20alert%20page%2C%20or%20can%20get%20them%20from%20the%20Advanced%20hunting%20tables%2C%20from%20our%20APIs%20or%20from%20PowerBI%20reports.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsecuritycenter.windows.com%2Fmachine%2Fc42046e157d641a6ac9444a002cb2b4ee56a1ddf%2F2018-06-18T02%3A15%3A23.2085379Z%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsecuritycenter.windows.com%2Fmachine%2F%3CSTRONG%3Ec42046e157d641a6ac9444a002cb2b4ee56a1ddf%3C%2FSTRONG%3E%2F2018-06-18T02%3A15%3A23.2085379Z%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fsecuritycenter.windows.com%2Falert%2F636656435044189870_28344485%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsecuritycenter.windows.com%2Falert%2F%3CSTRONG%3E636656435044189870_28344485%3C%2FSTRONG%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EWrap%20up%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ESo%2C%20next%20time%20you%20investigate%20an%20alert%20and%20see%20PowerShell%20running%2C%20you%20could%20easily%20check%20which%20uncommon%20PowerShell%20commands%20ran%20on%20that%20machine%20%E2%80%93%20and%20if%20malicious%2C%20check%20on%20which%20other%20machines%20these%20commands%20were%20executed.%3C%2FP%3E%0A%3CP%3EDon%E2%80%99t%20forget%20to%20bookmark%20our%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2FWindowsDefenderATP-Hunting-Queries%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGitHub%20repository%20of%20Advanced%20hunting%20queries%3C%2FA%3E.%20It%E2%80%99s%20a%20great%20place%20to%20get%20guidance%20on%20how%20to%20hunt%20for%20specific%20threats%20as%20well%20as%20explore%20beautifully%20crafted%20queries%20that%20return%20insight%20into%20possible%20breach%20activities%20in%20your%20network.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20for%20reading%2C%20and%20awaiting%20your%20feedback%20and%20suggestions%20for%20the%20next%20posts%3C%2FP%3E%0A%3CP%3EWindows%20Defender%20ATP%20team%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-210898%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdvanced%20hunting%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

PowerShell scripts have clearly become one of the weapons of choice for attackers who want to stay extremely stealthy. Like other scripts, they are easily obfuscated, downloaded, tucked away in the registry and among other benign-looking content, and launched using a legitimate process—the scripting engine. As the de facto scripting standard for administrative tasks on Windows, PowerShell trumps other scripting languages because it can easily invoke system APIs and access a variety of .Net classes and objects. PowerShell is loved by system administrators and defenders, but unfortunately it is just as appealing to attackers.

 

In this post, I’d like to share a few queries that can make it much easier for you to find suspicious PowerShell activity in your network…

 

Explaining PowerShellCommand events

This event type reports the names of commands executed by the PowerShell engine, including PowerShell cmdlets and scripts, as well as other executables.

It does not matter if that command is specified in the commandline or not – the command is reported nonetheless.

Similarly, it’s reported whether executed by powershell.exe, by wsmprovhost.exe (used for remote PowerShell) or by some other process that loads the PowerShell engine.

This event is part of the MiscEvents table, and its ActionType is "PowerShellCommand".

The columns starting with InitiatingProcess contain the details of the processes that has executed the command.

 

The AdditionalFields column contains the Command name in a JSON structure, in example:

     {"Command":"Create-WmiClass"}

This column is where we keep the long tail of fields – the uncommon ones that are unique to just a few event types.

You can parse it by calling parse_json or extractjson, or you could simply run a “contains” where filter on it.

If your organization is a large one, we suggest you speed up your queries by applying more filters before you parse the JSON column.

 

Query #1: Find all executions of a specific command

Have you found a suspicious or malicious command in your network, and want to find other instances of it? Or perhaps you have read a new blog post about some attacker or attack framework using some PowerShell cmdlet?

You can now easily find out if it was executed in your organization and on which machines.

 

// Taken from https://github.com/Microsoft/WindowsDefenderATP-Hunting-Queries/blob/master/Execution/PowershellComm...

let powershellCommandName = "Invoke-RickAscii";

MiscEvents | where ActionType == "PowerShellCommand" and AdditionalFields contains powershellCommandName

| extend PowerShellCommand=extractjson("$.Command", AdditionalFields, typeof(string))

| where PowerShellCommand =~ powershellCommandName

| project EventTime, ComputerName, InitiatingProcessCommandLine, InitiatingProcessParentFileName

 

Note: Not familiar with Invoke-RickAscii? Well, it’s not something you should hunt for, but we still recommend you read all about it as it’s the coolest PowerShell cmdlet ever.

 

Query #2: Find all commands run on a machine in some time-range

Noticed some interesting activities on a machine? Try this query to quickly check for PowerShell activity around the same time.

 

MiscEvents

| where MachineId == "8707b4da675a244f5be391a74b9141896b83bc77" and EventTime between (datetime(2018-06-26T23:48:51.5401469Z) .. 1h) and ActionType == "PowerShellCommand"

// Parse Command field from AdditionalFields column

| extend PowerShellCommand=extractjson("$.Command", AdditionalFields, typeof(string))

 

Query 3: Same query, but without common powershell commands

Got too much noise? Let’s filter out the commands that are common in your organization.

In this query we use a let statement, which enables you to define tabular data, scalar data or user-defined functions that will later be used (and reused) in the query.

 

// This query was taken from https://github.com/Microsoft/WindowsDefenderATP-Hunting-Queries/blob/master/Execution/PowershellComm...

let powershellCommands =

    MiscEvents

    | where ActionType == "PowerShellCommand"

    | extend PowerShellCommand=extractjson("$.Command", AdditionalFields, typeof(string))

    | where PowerShellCommand !endswith ".ps1" and PowerShellCommand !endswith ".exe";

let commonCommands =

    powershellCommands

    | summarize MachineCount=dcount(MachineId) by PowerShellCommand

    | where MachineCount > 20;

powershellCommands

| where MachineId == "8707b4da675a244f5be391a74b9141896b83bc77" and EventTime between (datetime(2018-06-26T23:48:51.5401469Z) .. 1h)

| join kind=leftanti (commonCommands) on PowerShellCommand

 

Query 4: pivot around an alert & parameterize your query

Typing a full datetime timestamp for every lookup might be time-consuming.

To speed up things, in the example below we query for the details of an alert, and then pivot around it – but you could do that for any other event just as well.

When saving a shared query for future use by your team, it’s best to define constants at the top of the query using let statements. This makes it much easier to identify configurable parameters.

 

// Query for alert details

let alertIdParameter = "636641078490537577_-1905871543";

let alert = AlertEvents | where AlertId == alertIdParameter | summarize AlertFirstEventTime=min(EventTime) by MachineId;

let machineId = toscalar(alert | project MachineId);

let timestamp = toscalar(alert | project AlertFirstEventTime);

// Same query as above - query for cmdlets executed by the PowerShell engine

let powershellCommands =

    MiscEvents

    | where ActionType == "PowerShellCommand"

    | extend PowerShellCommand=extractjson("$.Command", AdditionalFields, typeof(string))

    | where PowerShellCommand !endswith ".ps1" and PowerShellCommand !endswith ".exe";

powershellCommands | where MachineId == machineId and EventTime between ((timestamp-5min) .. 10min)

| join kind=leftanti (powershellCommands | summarize MachineCount=dcount(MachineId) by PowerShellCommand | where MachineCount > 20) on PowerShellCommand

 

One last tip – machine ID and alert ID

In case you are not familiar with MachineId or AlertId, so those are WDATP unique identifiers for a machine or an alert.

You can copy-paste them from the URL of the machine page or the alert page, or can get them from the Advanced hunting tables, from our APIs or from PowerBI reports.

https://securitycenter.windows.com/machine/c42046e157d641a6ac9444a002cb2b4ee56a1ddf/2018-06-18T02:15...

https://securitycenter.windows.com/alert/636656435044189870_28344485

 

Wrap up

So, next time you investigate an alert and see PowerShell running, you could easily check which uncommon PowerShell commands ran on that machine – and if malicious, check on which other machines these commands were executed.

Don’t forget to bookmark our GitHub repository of Advanced hunting queries. It’s a great place to get guidance on how to hunt for specific threats as well as explore beautifully crafted queries that return insight into possible breach activities in your network.

 

Thanks for reading, and awaiting your feedback and suggestions for the next posts

Windows Defender ATP team

1 Comment
Microsoft

This is awesome. Thanks !