PowerShell Obfuscation via Negative Index String Reversal

Identifies PowerShell scripts that use negative index ranges to reverse the contents of a string or array at runtime as a form of obfuscation. This technique avoids direct use of reversal functions by iterating through array elements in reverse order. These methods are designed to evade static analysis and bypass security protections such as the Antimalware Scan Interface (AMSI).

Elastic rule (View on GitHub)

 1[metadata]
 2creation_date = "2025/04/14"
 3integration = ["windows"]
 4maturity = "production"
 5updated_date = "2025/07/16"
 6
 7[rule]
 8author = ["Elastic"]
 9description = """
10Identifies PowerShell scripts that use negative index ranges to reverse the contents of a string or array at runtime as
11a form of obfuscation. This technique avoids direct use of reversal functions by iterating through array elements in
12reverse order. These methods are designed to evade static analysis and bypass security protections such as the
13Antimalware Scan Interface (AMSI).
14"""
15from = "now-9m"
16language = "esql"
17license = "Elastic License v2"
18name = "PowerShell Obfuscation via Negative Index String Reversal"
19note = """ ## Triage and analysis
20
21> **Disclaimer**:
22> This investigation guide was created using generative AI technology and has been reviewed to improve its accuracy and relevance. While every effort has been made to ensure its quality, we recommend validating the content and adapting it to suit your specific environment and operational needs.
23
24### Investigating PowerShell Obfuscation via Negative Index String Reversal
25
26PowerShell, a powerful scripting language, can be exploited by adversaries using obfuscation techniques like negative index string reversal to evade detection. This method manipulates strings or arrays by iterating in reverse, bypassing static analysis tools. The detection rule identifies scripts with obfuscation patterns by analyzing script length and specific indexing patterns, flagging potential threats for further investigation.
27
28### Possible investigation steps
29
30- Review the `powershell.file.script_block_text` to understand the script's intent and identify any suspicious or malicious behavior.
31- Check the `host.name` and `user.id` fields to determine the affected system and user, assessing if they are high-value targets or have a history of similar alerts.
32- Analyze the `file.path` to identify the location of the script and assess if it is in a common or suspicious directory.
33- Investigate the `powershell.file.script_block_id` and `powershell.sequence` to trace the execution flow and determine if this script is part of a larger, potentially malicious sequence.
34- Correlate the `agent.id` with other logs to see if there are additional related activities or alerts from the same endpoint.
35- Examine the `count` of detected patterns to assess the level of obfuscation and potential threat severity.
36
37### False positive analysis
38
39- Scripts containing the keyword "GENESIS-5654" are known false positives and are automatically excluded from triggering alerts. Ensure that any legitimate scripts using this keyword are documented to prevent unnecessary investigations.
40- Legitimate administrative scripts that use negative indexing for valid purposes may trigger false positives. Review these scripts and consider adding them to an exception list if they are frequently flagged but verified as non-malicious.
41- Automated scripts generated by trusted software that use similar obfuscation patterns for performance or compatibility reasons can be excluded by identifying unique identifiers or patterns within these scripts and updating the exclusion criteria accordingly.
42- Regularly update the exclusion list to include new patterns or identifiers from trusted sources as they are identified, ensuring that legitimate activities are not hindered by the detection rule.
43- Collaborate with IT and security teams to maintain a list of known safe scripts and their characteristics, which can be referenced when analyzing potential false positives.
44
45### Response and remediation
46
47- Isolate the affected host immediately to prevent further spread of potentially malicious scripts or unauthorized access.
48- Terminate any suspicious PowerShell processes identified by the alert to halt ongoing obfuscation activities.
49- Conduct a thorough review of the PowerShell script block text and related logs to identify any malicious payloads or commands executed.
50- Remove any identified malicious scripts or files from the affected system to prevent re-execution.
51- Reset credentials for any user accounts involved in the alert to mitigate potential unauthorized access.
52- Escalate the incident to the security operations team for further analysis and to determine if additional systems are compromised.
53- Update endpoint protection and monitoring tools to enhance detection capabilities for similar obfuscation techniques in the future.
54"""
55risk_score = 21
56rule_id = "9edd1804-83c7-4e48-b97d-c776b4c97564"
57setup = """## Setup
58
59The 'PowerShell Script Block Logging' logging policy must be enabled.
60Steps to implement the logging policy with Advanced Audit Configuration:

Computer Configuration > Administrative Templates > Windows PowerShell > Turn on PowerShell Script Block Logging (Enable)

1
2Steps to implement the logging policy via registry:

reg add "hklm\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" /v EnableScriptBlockLogging /t REG_DWORD /d 1

 1"""
 2severity = "low"
 3tags = [
 4    "Domain: Endpoint",
 5    "OS: Windows",
 6    "Use Case: Threat Detection",
 7    "Tactic: Defense Evasion",
 8    "Data Source: PowerShell Logs",
 9    "Resources: Investigation Guide",
10]
11timestamp_override = "event.ingested"
12type = "esql"
13
14query = '''
15from logs-windows.powershell_operational* metadata _id, _version, _index
16| where event.code == "4104"
17
18// Filter out smaller scripts that are unlikely to implement obfuscation using the patterns we are looking for
19| eval Esql.script_block_length = length(powershell.file.script_block_text)
20| where Esql.script_block_length > 500
21
22// replace the patterns we are looking for with the 🔥 emoji to enable counting them
23// The emoji is used because it's unlikely to appear in scripts and has a consistent character length of 1
24| eval Esql.script_block_tmp = replace(
25    powershell.file.script_block_text,
26    """\$\w+\[\-\s?1\.\.""",
27    "🔥"
28)
29
30// count how many patterns were detected by calculating the number of 🔥 characters inserted
31| eval Esql.script_block_pattern_count = length(Esql.script_block_tmp) - length(replace(Esql.script_block_tmp, "🔥", ""))
32
33// keep the fields relevant to the query, although this is not needed as the alert is populated using _id
34| keep
35    Esql.script_block_pattern_count,
36    Esql.script_block_length,
37    Esql.script_block_tmp,
38    powershell.file.script_block_text,
39    powershell.file.script_block_id,
40    file.path,
41    powershell.sequence,
42    powershell.total,
43    _id,
44    _index,
45    host.name,
46    agent.id,
47    user.id
48
49// Filter for scripts that match the pattern at least once
50| where Esql.script_block_pattern_count >= 1
51
52// FP Patterns
53| where not powershell.file.script_block_text like "*GENESIS-5654*"
54'''
55
56
57[[rule.threat]]
58framework = "MITRE ATT&CK"
59[[rule.threat.technique]]
60id = "T1027"
61name = "Obfuscated Files or Information"
62reference = "https://attack.mitre.org/techniques/T1027/"
63
64[[rule.threat.technique]]
65id = "T1140"
66name = "Deobfuscate/Decode Files or Information"
67reference = "https://attack.mitre.org/techniques/T1140/"
68
69
70[rule.threat.tactic]
71id = "TA0005"
72name = "Defense Evasion"
73reference = "https://attack.mitre.org/tactics/TA0005/"
74[[rule.threat]]
75framework = "MITRE ATT&CK"
76[[rule.threat.technique]]
77id = "T1059"
78name = "Command and Scripting Interpreter"
79reference = "https://attack.mitre.org/techniques/T1059/"
80[[rule.threat.technique.subtechnique]]
81id = "T1059.001"
82name = "PowerShell"
83reference = "https://attack.mitre.org/techniques/T1059/001/"
84
85
86
87[rule.threat.tactic]
88id = "TA0002"
89name = "Execution"
90reference = "https://attack.mitre.org/tactics/TA0002/"

Triage and analysis

Disclaimer: This investigation guide was created using generative AI technology and has been reviewed to improve its accuracy and relevance. While every effort has been made to ensure its quality, we recommend validating the content and adapting it to suit your specific environment and operational needs.

Investigating PowerShell Obfuscation via Negative Index String Reversal

PowerShell, a powerful scripting language, can be exploited by adversaries using obfuscation techniques like negative index string reversal to evade detection. This method manipulates strings or arrays by iterating in reverse, bypassing static analysis tools. The detection rule identifies scripts with obfuscation patterns by analyzing script length and specific indexing patterns, flagging potential threats for further investigation.

Possible investigation steps

  • Review the powershell.file.script_block_text to understand the script's intent and identify any suspicious or malicious behavior.
  • Check the host.name and user.id fields to determine the affected system and user, assessing if they are high-value targets or have a history of similar alerts.
  • Analyze the file.path to identify the location of the script and assess if it is in a common or suspicious directory.
  • Investigate the powershell.file.script_block_id and powershell.sequence to trace the execution flow and determine if this script is part of a larger, potentially malicious sequence.
  • Correlate the agent.id with other logs to see if there are additional related activities or alerts from the same endpoint.
  • Examine the count of detected patterns to assess the level of obfuscation and potential threat severity.

False positive analysis

  • Scripts containing the keyword "GENESIS-5654" are known false positives and are automatically excluded from triggering alerts. Ensure that any legitimate scripts using this keyword are documented to prevent unnecessary investigations.
  • Legitimate administrative scripts that use negative indexing for valid purposes may trigger false positives. Review these scripts and consider adding them to an exception list if they are frequently flagged but verified as non-malicious.
  • Automated scripts generated by trusted software that use similar obfuscation patterns for performance or compatibility reasons can be excluded by identifying unique identifiers or patterns within these scripts and updating the exclusion criteria accordingly.
  • Regularly update the exclusion list to include new patterns or identifiers from trusted sources as they are identified, ensuring that legitimate activities are not hindered by the detection rule.
  • Collaborate with IT and security teams to maintain a list of known safe scripts and their characteristics, which can be referenced when analyzing potential false positives.

Response and remediation

  • Isolate the affected host immediately to prevent further spread of potentially malicious scripts or unauthorized access.
  • Terminate any suspicious PowerShell processes identified by the alert to halt ongoing obfuscation activities.
  • Conduct a thorough review of the PowerShell script block text and related logs to identify any malicious payloads or commands executed.
  • Remove any identified malicious scripts or files from the affected system to prevent re-execution.
  • Reset credentials for any user accounts involved in the alert to mitigate potential unauthorized access.
  • Escalate the incident to the security operations team for further analysis and to determine if additional systems are compromised.
  • Update endpoint protection and monitoring tools to enhance detection capabilities for similar obfuscation techniques in the future.

Related rules

to-top