PowerShell Script with Encryption/Decryption Capabilities

Identifies the use of Cmdlets and methods related to encryption/decryption of files in PowerShell scripts, which malware and offensive security tools can abuse to encrypt data or decrypt payloads to bypass security solutions.

Elastic rule (View on GitHub)

 1[metadata]
 2creation_date = "2023/01/23"
 3integration = ["windows"]
 4maturity = "production"
 5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
 6min_stack_version = "8.3.0"
 7updated_date = "2024/03/08"
 8
 9[rule]
10author = ["Elastic"]
11description = """
12Identifies the use of Cmdlets and methods related to encryption/decryption of files in PowerShell scripts, which malware
13and offensive security tools can abuse to encrypt data or decrypt payloads to bypass security solutions.
14"""
15false_positives = ["Legitimate PowerShell Scripts which makes use of encryption."]
16from = "now-9m"
17index = ["winlogbeat-*", "logs-windows.powershell*"]
18language = "kuery"
19license = "Elastic License v2"
20name = "PowerShell Script with Encryption/Decryption Capabilities"
21note = """## Triage and analysis
22
23### Investigating PowerShell Script with Encryption/Decryption Capabilities
24
25PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks, making it available for use in various environments, creating an attractive way for attackers to execute code.
26
27PowerShell offers encryption and decryption functionalities that attackers can abuse for various purposes, such as concealing payloads, C2 communications, and encrypting data as part of ransomware operations.
28
29#### Possible investigation steps
30
31- Examine the script content that triggered the detection; look for suspicious DLL imports, collection or exfiltration capabilities, suspicious functions, encoded or compressed data, and other potentially malicious characteristics.
32- Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
33- Examine file or network events from the involved PowerShell process for suspicious behavior.
34- Investigate other alerts associated with the user/host during the past 48 hours.
35- Evaluate whether the user needs to use PowerShell to complete tasks.
36
37### False positive analysis
38
39- This is a dual-use mechanism, meaning its usage is not inherently malicious. Analysts can dismiss the alert if the script doesn't contain malicious functions or potential for abuse, no other suspicious activity was identified, and there are justifications for the execution.
40
41### Response and remediation
42
43- Initiate the incident response process based on the outcome of the triage.
44- Isolate the involved hosts to prevent further post-compromise behavior.
45- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
46- Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
47- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
48- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
49- Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
50"""
51risk_score = 47
52rule_id = "1d9aeb0b-9549-46f6-a32d-05e2a001b7fd"
53severity = "medium"
54tags = ["Domain: Endpoint", "OS: Windows", "Use Case: Threat Detection", "Tactic: Defense Evasion", "Data Source: PowerShell Logs", "Resources: Investigation Guide"]
55timestamp_override = "event.ingested"
56type = "query"
57
58query = '''
59event.category:process and host.os.type:windows and
60  powershell.file.script_block_text : (
61    (
62      "Cryptography.AESManaged" or
63      "Cryptography.RijndaelManaged" or
64      "Cryptography.SHA1Managed" or
65      "Cryptography.SHA256Managed" or
66      "Cryptography.SHA384Managed" or
67      "Cryptography.SHA512Managed" or
68      "Cryptography.SymmetricAlgorithm" or
69      "PasswordDeriveBytes" or
70      "Rfc2898DeriveBytes"
71    ) and
72    (
73      CipherMode and PaddingMode
74    ) and
75    (
76      ".CreateEncryptor" or
77      ".CreateDecryptor"
78    )
79  ) and not user.id : "S-1-5-18"
80'''
81
82
83[[rule.threat]]
84framework = "MITRE ATT&CK"
85[[rule.threat.technique]]
86id = "T1027"
87name = "Obfuscated Files or Information"
88reference = "https://attack.mitre.org/techniques/T1027/"
89
90[[rule.threat.technique]]
91id = "T1140"
92name = "Deobfuscate/Decode Files or Information"
93reference = "https://attack.mitre.org/techniques/T1140/"
94
95
96[rule.threat.tactic]
97id = "TA0005"
98name = "Defense Evasion"
99reference = "https://attack.mitre.org/tactics/TA0005/"

Triage and analysis

Investigating PowerShell Script with Encryption/Decryption Capabilities

PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks, making it available for use in various environments, creating an attractive way for attackers to execute code.

PowerShell offers encryption and decryption functionalities that attackers can abuse for various purposes, such as concealing payloads, C2 communications, and encrypting data as part of ransomware operations.

Possible investigation steps

  • Examine the script content that triggered the detection; look for suspicious DLL imports, collection or exfiltration capabilities, suspicious functions, encoded or compressed data, and other potentially malicious characteristics.
  • Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
  • Examine file or network events from the involved PowerShell process for suspicious behavior.
  • Investigate other alerts associated with the user/host during the past 48 hours.
  • Evaluate whether the user needs to use PowerShell to complete tasks.

False positive analysis

  • This is a dual-use mechanism, meaning its usage is not inherently malicious. Analysts can dismiss the alert if the script doesn't contain malicious functions or potential for abuse, no other suspicious activity was identified, and there are justifications for the execution.

Response and remediation

  • Initiate the incident response process based on the outcome of the triage.
  • Isolate the involved hosts to prevent further post-compromise behavior.
  • Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
  • Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
  • Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
  • Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
  • Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).

Related rules

to-top