Playbook: Suspicious encoded PowerShell
- ATT&CK
- T1059.001 Command and Scripting Interpreter: PowerShell; related T1027.010 Obfuscated Files or Information: Command Obfuscation (ATT&CK v20.1)
- Tactic
- Execution (T1059.001); Stealth (T1027.010)
- Level
- Beginner
- Best time window
- Last 7 to 30 days, then compare against your baseline
Hypothesis
If someone is running scripts on a Windows host without leaving files on disk, some of them will show up as powershell.exe or pwsh.exe started with an encoded command, from a parent process that does not normally launch PowerShell.
PowerShell accepts a base64 string through -EncodedCommand, and also its shortened forms such as -e, -ec and -enc. The string is the script encoded as UTF-16LE. Admin tools use this too, which is why this is a hunt and not a blocking rule.
Data sources
| Source | Event | Why it helps |
|---|---|---|
| Windows Security log | 4688 process creation | Needs "Include command line in process creation events" turned on |
| Sysmon | Event ID 1 | Command line, parent process, hashes, user |
| EDR process telemetry | Process-start events | Same fields, often searchable across all hosts at once |
| PowerShell Operational log | 4104 script block logging | Records the decoded script, so you can read what actually ran |
| PowerShell Operational log | 4103 module logging | Optional: pipeline and parameter detail |
| Defender XDR or Sentinel | DeviceProcessEvents table | Process, parent and command line from the endpoint sensor. The Sentinel copy has TimeGenerated, not Timestamp |
Sample Sigma rules
title: PowerShell Encoded Command With Long Base64 Argument (Hunt)
id: 6f1c0f9e-411a-4d3b-9a52-0b7e5c1d2a41
status: experimental
description: >-
Windows PowerShell or PowerShell 7 started with -EncodedCommand, or one of its
abbreviations (-e, -ec, -en, -enc ...), followed by a long base64 string.
Broad hunting rule: admin and deployment tooling does this too, so tune the filter.
references:
- https://attack.mitre.org/techniques/T1059/001/
- https://attack.mitre.org/techniques/T1027/010/
- https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe
author: 411 Threat Hunting
date: 2026-12-01
tags:
- attack.execution
- attack.stealth
- attack.t1059.001
- attack.t1027.010
logsource:
category: process_creation
product: windows
detection:
selection_img:
- Image|endswith:
- '\powershell.exe'
- '\pwsh.exe'
- OriginalFileName:
- 'PowerShell.EXE'
- 'pwsh.dll'
selection_enc:
# -e, -ec, -en, -enc, -enco ... -encodedcommand (hyphen, slash or Unicode dash),
# then 40+ base64 characters, optionally quoted. Does not match -EncodedArguments.
CommandLine|re: '(?i)\s[-/–—―](e|ec|en|enc|enco|encod|encode|encoded|encodedc|encodedco|encodedcom|encodedcomm|encodedcomma|encodedcomman|encodedcommand)\s+[\x22\x27]?[A-Za-z0-9+/]{40,}'
filter_known_benign:
# Example only: replace with parent + account (+ command hash) pairs you have verified.
ParentImage|endswith: '\CcmExec.exe'
condition: selection_img and selection_enc and not filter_known_benign
falsepositives:
- Software deployment and monitoring agents
- Admin scripts that pass complex arguments as encoded commands
level: lowtitle: PowerShell Encoded Command From Suspicious Parent Or With Evasion Flags
id: 8d2b7c64-411b-4e0a-8f3d-5a9c1e6b7f12
status: experimental
description: >-
Narrower companion to the hunt rule. PowerShell started with an encoded command and
either a parent process that does not normally launch PowerShell (Office, script
hosts, browsers, web server workers) or the hidden-window plus no-profile flag pair.
references:
- https://attack.mitre.org/techniques/T1059/001/
- https://attack.mitre.org/techniques/T1027/010/
author: 411 Threat Hunting
date: 2026-12-01
tags:
- attack.execution
- attack.t1059.001
- attack.t1027.010
logsource:
category: process_creation
product: windows
detection:
selection_img:
- Image|endswith:
- '\powershell.exe'
- '\pwsh.exe'
- OriginalFileName:
- 'PowerShell.EXE'
- 'pwsh.dll'
selection_enc:
CommandLine|re: '(?i)\s[-/–—―](e|ec|en|enc|enco|encod|encode|encoded|encodedc|encodedco|encodedcom|encodedcomm|encodedcomma|encodedcomman|encodedcommand)\s+[\x22\x27]?[A-Za-z0-9+/]{40,}'
selection_parent:
ParentImage|endswith:
- '\winword.exe'
- '\excel.exe'
- '\powerpnt.exe'
- '\outlook.exe'
- '\mshta.exe'
- '\wscript.exe'
- '\cscript.exe'
- '\w3wp.exe'
- '\chrome.exe'
- '\msedge.exe'
- '\firefox.exe'
selection_hidden:
CommandLine|re: '(?i)\s[-/–—―]w[a-z]*\s+(h[a-z]*|1)\b'
selection_noprofile:
CommandLine|re: '(?i)\s[-/–—―]nop[a-z]*\b'
filter_known_benign:
ParentImage|endswith: '\CcmExec.exe'
condition: selection_img and selection_enc and (selection_parent or (selection_hidden and selection_noprofile)) and not filter_known_benign
falsepositives:
- Web-based admin consoles or installers that launch PowerShell from a browser or web server worker
- Legitimate macros or add-ins that shell out to PowerShell
level: mediumTwo original starter rules written for this site, untested in production. The first is a broad hunt rule (level: low) that matches the encoded-command flag and every prefix of -EncodedCommand with a hyphen, slash or Unicode dash, followed by 40 or more base64 characters. The companion (level: medium) keeps only hits with a parent process that rarely starts PowerShell, or the hidden-window plus no-profile pair. Replace filter_known_benign with parent, account and script pairs you have verified.
Sample KQL (Defender XDR and Sentinel)
// UNTESTED IN PRODUCTION. Syntax-checked only; tune before you rely on it.
// Microsoft Defender XDR Advanced Hunting, table DeviceProcessEvents.
// Sentinel: the table has TimeGenerated, not Timestamp. Swap the column name.
let lookback = 7d;
let SuspiciousParents = dynamic(["winword.exe", "excel.exe", "powerpnt.exe", "outlook.exe",
"mshta.exe", "wscript.exe", "cscript.exe", "w3wp.exe", "chrome.exe", "msedge.exe", "firefox.exe"]);
// Example only. Prefer verified parent + account pairs over a parent name alone.
let KnownBenignParents = dynamic(["ccmexec.exe"]);
DeviceProcessEvents
| where Timestamp > ago(lookback)
| where FileName in~ ("powershell.exe", "pwsh.exe")
or ProcessVersionInfoOriginalFileName in~ ("PowerShell.EXE", "pwsh.dll")
// Flag token after a hyphen, slash or Unicode dash, then 40+ base64 characters
| extend EncFlag = tolower(extract(@"(?i)\s[-/\x{2013}\x{2014}\x{2015}](e|ec|en[a-z]*)\s+[""']?[A-Za-z0-9+/]{40,}", 1, ProcessCommandLine))
// Keep -e, -ec, and any prefix of -encodedcommand (this drops -EncodedArguments)
| where isnotempty(EncFlag) and (EncFlag == "ec" or "encodedcommand" startswith EncFlag)
| where InitiatingProcessFileName !in~ (KnownBenignParents)
| extend B64 = extract(@"(?i)\s[-/\x{2013}\x{2014}\x{2015}](?:e|ec|en[a-z]*)\s+[""']?([A-Za-z0-9+/]{40,}={0,2})", 1, ProcessCommandLine)
// PowerShell encodes as UTF-16LE: dropping the NUL bytes makes ASCII scripts readable
| extend Decoded = replace_regex(base64_decode_tostring(B64), @"[^\x20-\x7E]", "")
| extend SuspiciousParent = InitiatingProcessFileName in~ (SuspiciousParents)
| extend HiddenWindow = ProcessCommandLine matches regex @"(?i)\s[-/]w[a-z]*\s+(h[a-z]*|1)\b"
| extend NoProfile = ProcessCommandLine matches regex @"(?i)\s[-/]nop[a-z]*\b"
| extend Score = iff(SuspiciousParent, 2, 0) + iff(HiddenWindow, 1, 0) + iff(NoProfile, 1, 0)
+ iff(Decoded matches regex @"(?i)(iex|invoke-expression|downloadstring|frombase64string|net\.webclient|-nop|bypass)", 2, 0)
| project Timestamp, DeviceName, AccountName, FileName, Parent = InitiatingProcessFileName,
GrandParent = InitiatingProcessParentFileName, ParentCmd = InitiatingProcessCommandLine,
Score, SuspiciousParent, HiddenWindow, NoProfile, Decoded, ProcessCommandLine
| order by Score desc, Timestamp desc// UNTESTED IN PRODUCTION. Rarity view: run after the query above, or paste the same filters first.
// Rare parent, account and script combinations first.
let lookback = 30d;
DeviceProcessEvents
| where Timestamp > ago(lookback)
| where FileName in~ ("powershell.exe", "pwsh.exe")
| where ProcessCommandLine matches regex @"(?i)\s[-/\x{2013}\x{2014}\x{2015}](e|ec|en[a-z]*)\s+[""']?[A-Za-z0-9+/]{40,}"
| extend B64 = extract(@"(?i)\s[-/\x{2013}\x{2014}\x{2015}](?:e|ec|en[a-z]*)\s+[""']?([A-Za-z0-9+/]{40,}={0,2})", 1, ProcessCommandLine)
| summarize Hits = count(), Devices = dcount(DeviceName), FirstSeen = min(Timestamp), LastSeen = max(Timestamp),
SampleCmd = take_any(ProcessCommandLine)
by Parent = InitiatingProcessFileName, AccountName, ScriptHash = hash_sha256(B64)
| order by Devices asc, Hits ascUntested in production. The first query lists each hit with parent, grandparent, decoded preview and a simple score, highest first. The second summarises 30 days by parent, account and script hash so rare combinations surface first. The decode step is a quick preview for ASCII scripts; for anything else, decode the string in a safe analysis setting or read the matching 4104 event.
Sample pseudo-SPL
Pseudo-SPL, untested in Splunk. Adjust index, sourcetype and field names. It needs Sysmon event 1 (or equivalent) with CommandLine and ParentImage.
index=windows source="XmlWinEventLog:Microsoft-Windows-Sysmon/Operational" EventCode=1
(Image="*\\powershell.exe" OR Image="*\\pwsh.exe" OR OriginalFileName="PowerShell.EXE" OR OriginalFileName="pwsh.dll")
| rex field=CommandLine "(?i)\s[-/–—―](?<enc_flag>e|ec|en[a-z]*)\s+[\"']?(?<b64>[A-Za-z0-9+/]{40,})"
| eval enc_flag=lower(enc_flag)
| where isnotnull(enc_flag) AND (enc_flag="ec" OR match("encodedcommand", "^".enc_flag))
| search NOT ParentImage="*\\CcmExec.exe"
| eval susp_parent=if(match(lower(ParentImage), "(winword|excel|powerpnt|outlook|mshta|wscript|cscript|w3wp|chrome|msedge|firefox)\.exe$"), 1, 0)
| eval hidden=if(match(CommandLine, "(?i)\s[-/]w[a-z]*\s+(h[a-z]*|1)\b"), 1, 0)
| eval noprofile=if(match(CommandLine, "(?i)\s[-/]nop[a-z]*\b"), 1, 0)
| eval score=susp_parent*2 + hidden + noprofile
| stats count AS hits dc(host) AS hosts min(_time) AS first_seen max(_time) AS last_seen max(score) AS score first(CommandLine) AS sample_cmd BY ParentImage, User
| convert ctime(first_seen) ctime(last_seen)
| sort 0 -score +hosts +hitsSplunk cannot decode base64 on its own, so decode the b64 value off-box with the steps below.
Decoding a string safely
# Analyst workstation or isolated lab only. These lines only DECODE text; they never run it.
# PowerShell (Windows PowerShell 5.1 or PowerShell 7):
$b64 = '<paste the base64 string from the command line>'
[Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($b64))
# Linux or macOS shell:
echo '<paste the base64 string>' | base64 -d | iconv -f UTF-16LE -t UTF-8
# Browser-free alternative: CyberChef recipe "From Base64" then "Decode text" (UTF-16LE, 1200)This converts base64 into readable text and prints it. It does not execute the content. Do it on an analyst machine or lab, never on the affected host. The Linux line was tested; the PowerShell line was not run.
Good versus suspicious
| Signal | Usually expected | Worth a closer look |
|---|---|---|
| Parent process | Software-deployment or monitoring agent, scheduled task from known software | Office apps, browsers, mshta.exe, wscript.exe, a web server worker, or an unexpected service |
| Other flags | Few or none | Combined -NoProfile, -WindowStyle Hidden, -NonInteractive or -ExecutionPolicy Bypass |
| Decoded content | Short, readable, matches a documented admin task | Download-and-run patterns, Invoke-Expression, FromBase64String, in-memory assembly loading |
| Spread | Same command on many hosts on a schedule | A single host, first time seen, or after-hours activity |
| Account | Service or admin account that normally does this | Ordinary user account, or an account that never ran PowerShell before |
| What follows | Nothing unusual | Rare outbound connection, new scheduled task, new service, or file written to a user-writable path |
Triage steps
- Decode the string and read it. If a 4104 event exists on that host, compare it with your decode.
- Check the parent chain: what started PowerShell, and what started that.
- Check the account and host: is this normal for them? Compare against 30 days of history.
- Look at what happened in the next few minutes: network connections, files written, tasks or services created.
- Search for the same command pattern, hash or destination on other hosts.
- Decide: benign and documented (add it to the baseline or tune the filter), unclear (watch and collect more), or suspicious (escalate to your incident process).
- Record the finding. If the hunt exposed a gap, turn it into a detection or a logging fix.
Limits and tuning
- Attackers can avoid the flag by passing a script file or using other launch methods; this hunt covers one common pattern.
- Without command-line logging, this hunt will find nothing. Check logging first.
- Expect noise from management tools on the first run. Tuning is part of the hunt.
- Exclude known-good by a combination (parent image, account and script hash), never by parent name alone: an attacker can start PowerShell from the same parent.
- The 40-character floor on the base64 string (about 15 script characters) removes tiny admin one-liners. Lower it to catch short commands, at the cost of more noise.
-EncodedArgumentsis a different parameter and is excluded on purpose. Check that your pipeline does not truncate long command lines, because a cut-off string will not decode.- Pair this with script block logging (4104). The rules cannot see a command line that is rebuilt at run time or a script passed by other means.
What was and was not tested
- Sigma: both rules passed sigma-cli 4.1.0 (pySigma 2.2.0) check with no errors or issues, converted to Splunk, and converted to KQL with the Kusto backend under pySigma 1.5.1. A test run of 12 sample events through the SQLite backend matched all expected results.
- KQL: both queries parse with no errors in Microsoft's Kusto.Language parser, checked against a minimal DeviceProcessEvents schema written for the test. The regex logic was also tested on sample command lines in Python.
- Not tested: no query was run in Defender, Sentinel or Splunk, or against real telemetry. The PowerShell decode line was not run, and RE2 and PCRE regex behaviour was not checked in the products themselves.
References
- MITRE ATT&CK T1059.001: PowerShellattack.mitre.org
- MITRE ATT&CK T1027.010: Command Obfuscationattack.mitre.org
- about_PowerShell.exe (command-line parameters)learn.microsoft.com
- about_Logging_Windows (script block and module logging)learn.microsoft.com
- about_Pwsh (PowerShell 7 command-line parameters)learn.microsoft.com
- Sigma rule specification and repositorygithub.com/SigmaHQ
- DeviceProcessEvents table reference (Sentinel and Azure Monitor)learn.microsoft.com
- Advanced hunting overview (Defender XDR)learn.microsoft.com