let ScoreIndicators = (cmd:string) {
0
// =========================
// Encoding / Obfuscation
// =========================
+ iif(cmd has_any ("-en", "-enc", "-enco", "-encod", "-encode", "-encoded", "-encodedc", "-encodedco", "-encodedcom", "-encodedcomm", "-encodedcomma", "-encodedcomman", "-encodedcommand"), 3, 0)
+ iif(cmd matches regex @"[A-Za-z0-9+/=]{100,}", 2, 0)
+ iif(cmd has "0x", 1, 0)
+ iif(cmd has_any ("[char]", "chr("), 2, 0)
+ iif(cmd matches regex @"('.*'\s*\+\s*){4,}", 2, 0)
// =========================
// Evasion / Execution Tricks
// =========================
+ iif(cmd has_any ("-nop", "-noni", "-w h", "-noprofile", "-windowstyle hidden"), 1, 0)
+ iif(cmd has_any ("bypass", "ExecutionPolicy", "Unrestricted"), 2, 0)
+ iif(cmd has_any ("amsiutils", "amsiInitFailed", "amsi"), 2, 0)
+ iif(cmd has "& {", 1, 0)
// =========================
// Variables / Obfuscation Patterns
// =========================
+ iif(cmd contains "&$" or cmd matches regex @"\$[a-zA-Z0-9]+\s*=", 2, 0)
+ iif(cmd matches regex @"\$[a-zA-Z]{2}", 1, 0)
+ iif(cmd matches regex @"(\$[a-zA-Z]{2}\s*=){3,}", 2, 0)
+ iif(cmd matches regex @"(\$[a-zA-Z]{2}\s*=){4,}", 2, 0)
+ iif(cmd matches regex @"\$[a-zA-Z]{2}\s*=\s*'i'\s*\+\s*'ex'", 2, 0)
// =========================
// String manipulation / staging
// =========================
+ iif(cmd has_any ("split", "join", "replace"), 1, 0)
+ iif(cmd matches regex @"Split\(',.*'\)", 2, 0)
+ iif(cmd matches regex @"\.\(['""][a-zA-Z]+\s*\+\s*[a-zA-Z]+['""]\)", 2, 0)
// =========================
// Download / Web activity
// =========================
+ iif(cmd has_any ("DownloadString", "DownloadFile", "WebClient", "Invoke-WebRequest", "Invoke-RestMethod"), 3, 0)
+ iif(cmd has_all ("New-Object", "Net.WebClient"), 2, 0)
+ iif(cmd contains "http" or cmd contains "https", 2, 0)
+ iif(cmd matches regex @"(Invoke-(WebRequest|RestMethod).*){2,}", 2, 0)
+ iif(cmd has_any ("Invoke-WebRequest", "DownloadFile", "Start-BitsTransfer") and cmd has_any (".bat", ".exe", ".vbs"), 2, 0)
// =========================
// Execution primitives
// =========================
+ iif(cmd has_any ("iex", "Invoke-Expression", "Invoke-Command", "Add-Type"), 2, 0)
+ iif(cmd matches regex @"(i|I)nvo\w+\+\w+", 2, 0)
// =========================
// Reflection / .NET abuse
// =========================
+ iif(cmd has_any ("GetType", "InvokeMember", "GetMethod", "BindingFlags", "Reflection"), 2, 0)
// =========================
// Compression / Encoding APIs
// =========================
+ iif(cmd has_any ("GzipStream", "DeflateStream", "FromBase64String", "MemoryStream", "IO.Compression"), 2, 0)
// =========================
// Control flow / structure
// =========================
+ iif(cmd has_any ("try{", "catch{"), 1, 0)
// =========================
// Suspicious file paths / staging
// =========================
+ iif(cmd matches regex @"C:\\Users\\[^\\]+\\(Desktop|Documents|Downloads|OneDrive)\\", 2, 0)
+ iif(cmd has_any (@"\AppData\Local\Temp\", @"\ProgramData\"), 1, 0)
+ iif(cmd matches regex @"\\Temp\\[a-z0-9]{6,}\.(bat|vbs)", 3, 0)
+ iif(cmd matches regex @"\\[a-z0-9]{8,16}\.(bat|vbs|exe)", 2, 0)
+ iif(cmd matches regex @"\\[0-9]{3,6}\.exe", 2, 0)
// =========================
// Archive / payload handling
// =========================
+ iif(cmd has_any (".zip", ".rar", "expand", "extract"), 1, 0)
+ iif(cmd has_any ("WinRAR", "7z", "tar"), 2, 0)
// =========================
// LOLBins / Script engines
// =========================
+ iif(cmd has_any ("msbuild.exe", "wscript.exe", "cscript.exe", "rundll32.exe", "regsvr32.exe"), 2, 0)
+ iif(cmd has "msbuild.exe", 3, 0)
+ iif(cmd has_any ("wscript.exe", "cscript.exe") and cmd has ".vbs", 2, 0)
+ iif(cmd has "msbuild" and cmd has_any (".bat", ".cmd"), 3, 0)
// =========================
// Command chaining / execution flow
// =========================
+ iif(cmd has_all ("cmd.exe", "/c") and cmd has ".bat", 2, 0)
+ iif(cmd has_any (".vbs", ".bat") and cmd has_any ("cmd.exe", "wscript", "msbuild"), 2, 0)
// =========================
// Persistence
// =========================
+ iif(cmd has "schtasks" and cmd has_any ("/create", "/sc", "/mo"), 2, 0)
+ iif(cmd has "New-Item" and cmd has "Directory", 1, 0)
// =========================
// Defender / security tampering
// =========================
+ iif(cmd has_any ("Set-ExecutionPolicy", "Add-MpPreference", "Set-MpPreference"), 3, 0)
+ iif(cmd has_any ("Get-MpComputerStatus", "Get-MpPreference", "MpCmdRun.exe"), 3, 0)
+ iif(cmd has_any ("Get-NetFirewallRule", "netsh advfirewall", "netsh firewall"), 3, 0)
// =========================
// Discovery (kept from original)
// =========================
+ iif(cmd has "whoami", 2, 0)
+ iif(cmd has_any ("hostname", "systeminfo", "query user", "quser", "net user", "net localgroup", "net group"), 2, 0)
+ iif(cmd has_any ("tasklist", "taskkill", "sc query", "sc qc", "services.msc", "Get-Service"), 2, 0)
+ iif(cmd has_any ("ipconfig", "Get-NetIPConfiguration", "Get-NetAdapter", "route print"), 2, 0)
+ iif(cmd has_any ("netstat", "Get-NetTCPConnection", "Get-NetUDPEndpoint"), 2, 0)
+ iif(cmd has_any ("arp -a", "nbtstat", "net view", "net use", "net session"), 2, 0)
+ iif(cmd has_any ("Get-ADUser", "Get-ADComputer", "Get-ADGroupMember", "dsquery", "dsget"), 3, 0)
+ iif(cmd has_any ("Get-Process", "Get-WmiObject", "gwmi", "wmic process", "wmic service", "wmic useraccount"), 2, 0)
+ iif(cmd has_any ("Get-ItemProperty", "reg query", "reg export", "HKLM", "HKCU"), 2, 0)
// =========================
// Credential / browser targeting
// =========================
+ iif(cmd has_any ("Login Data", "Web Data", "Cookies") and cmd has_any ("Chrome", "Edge"), 3, 0)
// =========================
// Process Injection (HIGH SIGNAL)
// =========================
+ iif(cmd has_any ("WriteProcessMemory", "VirtualAllocEx", "CreateRemoteThread", "QueueUserAPC"), 4, 0)
// =========================
// Misc
// =========================
+ iif(strlen(cmd) > 300, 2, 0)
+ iif(cmd matches regex @"\\[\\\w]{30,}", 1, 0)
+ iif(cmd matches regex @"-OutFile\s+[^ ]+\s+-OutFile", 2, 0)
+ iif(cmd matches regex @"\$[a-zA-Z]+\s*=\s*""[A-Za-z0-9+/=]{50,}""", 2, 0)
};
DeviceProcessEvents
| where TimeGenerated > ago(1h)
| where FileName in~ ("powershell.exe","cmd.exe","explorer.exe","userinit.exe","mshta.exe","wscript.exe","svchost","wsmprovhost.exe")
| where InitiatingProcessFileName in~ ("powershell.exe","cmd.exe","explorer.exe","userinit.exe","mshta.exe","wscript.exe","svchost","wsmprovhost.exe")
| where InitiatingProcessParentFileName in~ ("powershell.exe","cmd.exe","explorer.exe","userinit.exe","mshta.exe","wscript.exe","svchost","wsmprovhost.exe")
| where AccountDomain !~ "nt authority"
| where AccountDomain <> "autorite nt"
| extend RawCommand = tostring(ProcessCommandLine)
| extend EncodedString = extract(@"(?i)-enc(?:oded(?:command)?)?\s+([A-Za-z0-9+/=]+)", 1, RawCommand)
| extend DecodedCommand = tostring(base64_decode_tostring(EncodedString))
| extend DecodedCleanCommand = replace(@'\x00', '', DecodedCommand)
| extend ScoreRaw = ScoreIndicators(RawCommand)
| extend ScoreDecoded = ScoreIndicators(DecodedCleanCommand)
| extend Case = "Suspicious command executed"
| where ScoreRaw >= 10 or ScoreDecoded >= 10
| project-reorder TimeGenerated, Case,ScoreRaw,ScoreDecoded, RawCommand, DecodedCleanCommand, FileName, InitiatingProcessFileName