Query Details
//Detect when Defender for Endpoint alerts on suspicious PowerShell usage. If command is encoded it will be decoded.
//Data connector required for this query - Security Alert (free table that other Defender products send alert info to)
SecurityAlert
| where AlertName == "Suspicious PowerShell command line"
| mv-expand todynamic(Entities)
| extend CommandLine = tostring(Entities.CommandLine)
//This particular query looks for only encoded Powershell commands, if you want all Powershell commands just remove the lines below
| extend EncodedCommand = extract(@'\s+([A-Za-z0-9+/]{20}\S+$)', 1, CommandLine)
| where EncodedCommand != ""
| extend DecodedCommand = base64_decode_tostring(EncodedCommand)
| where DecodedCommand != ""
//
| project TimeGenerated, CompromisedEntity, AlertName, CommandLine, DecodedCommand
//Advanced Hunting query - depending on the content of the decoded string AH can struggle to render the command occasionally
//Data connector required for this query - Advanced Hunting license
let alertid=
AlertInfo
| where Title == @"Suspicious PowerShell command line"
| distinct AlertId;
AlertEvidence
| where AlertId in (alertid)
| where EntityType == "Process"
| extend EncodedCommand = extract(@'\s+([A-Za-z0-9+/]{20}\S+$)', 1, ProcessCommandLine)
| where EncodedCommand != ""
| extend DecodedCommand = base64_decode_tostring(EncodedCommand)
| where DecodedCommand != ""
| project Timestamp, AlertId, ProcessCommandLine, DecodedCommandThis query is used to detect when Defender for Endpoint alerts on suspicious PowerShell usage. It looks for encoded PowerShell commands and decodes them. The query retrieves information from the Security Alert data connector and the Advanced Hunting data connector. The results include the time generated, compromised entity, alert name, command line, and decoded command. Note that the Advanced Hunting query may struggle to render the command depending on its content.

Matt Zorich
Released: June 17, 2022
Tables
Keywords
Operators