Nov 16 2020 10:45 AM
All,
I put together a query to look for base64-encoded strings on Command Lines where powershell has been executed. So I whipped up the following query:
SecurityEvent
| where TimeGenerated between (ago(1d) .. now())
| where EventID in ("4688","4104")
| where CommandLine contains "powershell" or CommandLine contains "pwsh"
| extend args = split(CommandLine, " ")
| mv-apply l=args to typeof(string) on ( where args matches regex @'^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$' )
It executes, so syntactically it works, but it is extremely inefficient.
I'm looking for EventIDs 4688 and 4104 in the Security Log, where we have powreshell or pwsh executed. I then use the split() function to parse up the CommandLine column into an array with each distinct element. We then check those elements (exhaustively) using an mv-apply against some regex for finding Base64 strings.
Is there a better way to do this? Although this executes, Will my logic even work?
Thanks!
Nov 16 2020 01:01 PM
@JKatzmandu Have you looked at the "Process executed from binary hidden in Base64 encoded file" rule template to see how that does it?
Nov 16 2020 01:14 PM
Nov 30 2020 02:46 PM
Trying to create a rule based detection of Powershell attacks is between hard and impossible. On the one hand based64 is used in valid code and on the other hand evasion is pretty easy. Why should the code be on the command line int the first place. Want the really dire picture? Daniel Bohannon presenatation on PowerShell obfuscation (video, slides) is impresive.
I think that Defender for End Points (a.k.a. Defender ATP), is the right choice for detecting and protecting from such threats.
Dec 01 2020 02:24 AM
@Ofer_Shezaf We came up with a "hedge" which is pretty good for finding base64 on the command line. It's not exhaustive, but essentially looks for longer command lines with a decent length of a base64 string, and it also has to be a valid string (one that can be decoded by the base64_decode() function.)
In practice this worked and did find some "shady" items. We actually found items that were double-encoded.
SecurityEvent
| where TimeGenerated between (ago(7d) .. now())
| where EventID in ("4688")
| where ParentProcessName contains @'\cmd.exe' or NewProcessName contains "powershell" or NewProcessName contains "pwsh"
| where string_size(CommandLine) >= 24 // get rid of CommandLine that is too short.
| extend Evil_Base64 = extract(@'\s+([A-Za-z0-9+/]{20}\S+$)',1,CommandLine )
| where Evil_Base64 != ""
| extend decoded_command = base64_decode_tostring(Evil_Base64)
| where decoded_command != ""
| project TimeGenerated, SubjectAccount, Account, Computer, CommandLine, ParentProcessName, NewProcessName, Evil_Base64, decoded_command