Query Details

Security Alert Encoded Powershell

Query

//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, DecodedCommand

Explanation

This 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.

Details

Matt Zorich profile picture

Matt Zorich

Released: June 17, 2022

Tables

SecurityAlertAlertInfoAlertEvidence

Keywords

SecurityAlert,AlertName,Entities,CommandLine,EncodedCommand,DecodedCommand,TimeGenerated,CompromisedEntity,CompromisedEntity,AlertName,CommandLine,DecodedCommand,AlertInfo,Title,AlertId,AlertEvidence,EntityType,Process,ProcessCommandLine,Timestamp

Operators

| wheremv-expandextendtostringextractbase64_decode_tostringprojectletdistinct

Actions