Skip to content

Commit 8361cfd

Browse files
authored
[New Rule] Potential PowerShell Obfuscation via String Reordering (#4595)
* [New Rule] Potential PowerShell Obfuscation via String Reordering * Update defense_evasion_posh_obfuscation_string_format.toml * Update rules/windows/defense_evasion_posh_obfuscation_string_format.toml * Update defense_evasion_posh_obfuscation_string_format.toml * Update rules/windows/defense_evasion_posh_obfuscation_string_format.toml * Update rules/windows/defense_evasion_posh_obfuscation_string_format.toml
1 parent 364d9dd commit 8361cfd

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
[metadata]
2+
creation_date = "2025/04/03"
3+
integration = ["windows"]
4+
maturity = "production"
5+
updated_date = "2025/04/03"
6+
7+
[rule]
8+
author = ["Elastic"]
9+
description = """
10+
Identifies PowerShell scripts that use string reordering and runtime reconstruction techniques as a form of obfuscation.
11+
These methods are designed to evade static analysis and bypass security protections such as the Antimalware Scan
12+
Interface (AMSI).
13+
"""
14+
from = "now-9m"
15+
language = "esql"
16+
license = "Elastic License v2"
17+
name = "Potential PowerShell Obfuscation via String Reordering"
18+
risk_score = 21
19+
rule_id = "e903ce9a-5ce6-4246-bb14-75ed3ec2edf5"
20+
setup = """## Setup
21+
22+
The 'PowerShell Script Block Logging' logging policy must be enabled.
23+
Steps to implement the logging policy with Advanced Audit Configuration:
24+
25+
```
26+
Computer Configuration >
27+
Administrative Templates >
28+
Windows PowerShell >
29+
Turn on PowerShell Script Block Logging (Enable)
30+
```
31+
32+
Steps to implement the logging policy via registry:
33+
34+
```
35+
reg add "hklm\\SOFTWARE\\Policies\\Microsoft\\Windows\\PowerShell\\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1
36+
```
37+
"""
38+
severity = "low"
39+
tags = [
40+
"Domain: Endpoint",
41+
"OS: Windows",
42+
"Use Case: Threat Detection",
43+
"Tactic: Defense Evasion",
44+
"Data Source: PowerShell Logs",
45+
]
46+
timestamp_override = "event.ingested"
47+
type = "esql"
48+
49+
query = '''
50+
FROM logs-windows.powershell_operational* metadata _id, _version, _index
51+
| WHERE event.code == "4104"
52+
53+
// Look for scripts with more than 500 chars that contain a related keyword
54+
| EVAL script_len = LENGTH(powershell.file.script_block_text)
55+
| WHERE script_len > 500
56+
| WHERE powershell.file.script_block_text LIKE "*{0}*"
57+
58+
// Replace string format expressions with 🔥 to enable counting the occurrence of the patterns we are looking for
59+
// The emoji is used because it's unlikely to appear in scripts and has a consistent character length of 1
60+
| EVAL replaced_with_fire = REPLACE(powershell.file.script_block_text, """((\{\d+\}){2,}["']\s?-f|::Format[^\{]+(\{\d+\}){2,})""", "🔥")
61+
62+
// Count how many patterns were detected by calculating the number of 🔥 characters inserted
63+
| EVAL count = LENGTH(replaced_with_fire) - LENGTH(REPLACE(replaced_with_fire, "🔥", ""))
64+
65+
// Keep the fields relevant to the query, although this is not needed as the alert is populated using _id
66+
| KEEP count, replaced_with_fire, powershell.file.script_block_text, powershell.file.script_block_id, file.path, powershell.sequence, powershell.total, _id, _index, host.name, agent.id, user.id
67+
| WHERE count > 3
68+
'''
69+
70+
71+
[[rule.threat]]
72+
framework = "MITRE ATT&CK"
73+
[[rule.threat.technique]]
74+
id = "T1027"
75+
name = "Obfuscated Files or Information"
76+
reference = "https://attack.mitre.org/techniques/T1027/"
77+
78+
[[rule.threat.technique]]
79+
id = "T1140"
80+
name = "Deobfuscate/Decode Files or Information"
81+
reference = "https://attack.mitre.org/techniques/T1140/"
82+
83+
84+
[rule.threat.tactic]
85+
id = "TA0005"
86+
name = "Defense Evasion"
87+
reference = "https://attack.mitre.org/tactics/TA0005/"
88+
[[rule.threat]]
89+
framework = "MITRE ATT&CK"
90+
[[rule.threat.technique]]
91+
id = "T1059"
92+
name = "Command and Scripting Interpreter"
93+
reference = "https://attack.mitre.org/techniques/T1059/"
94+
[[rule.threat.technique.subtechnique]]
95+
id = "T1059.001"
96+
name = "PowerShell"
97+
reference = "https://attack.mitre.org/techniques/T1059/001/"
98+
99+
100+
101+
[rule.threat.tactic]
102+
id = "TA0002"
103+
name = "Execution"
104+
reference = "https://attack.mitre.org/tactics/TA0002/"
105+

0 commit comments

Comments
 (0)