Potential Antimalware Scan Interface Bypass via PowerShell

Identifies the execution of PowerShell script with keywords related to different Antimalware Scan Interface (AMSI) bypasses. An adversary may attempt first to disable AMSI before executing further malicious powershell scripts to evade detection.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2023/01/17"
  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[transform]
 10[[transform.osquery]]
 11label = "Osquery - Retrieve DNS Cache"
 12query = "SELECT * FROM dns_cache"
 13
 14[[transform.osquery]]
 15label = "Osquery - Retrieve All Services"
 16query = "SELECT description, display_name, name, path, pid, service_type, start_type, status, user_account FROM services"
 17
 18[[transform.osquery]]
 19label = "Osquery - Retrieve Services Running on User Accounts"
 20query = """
 21SELECT description, display_name, name, path, pid, service_type, start_type, status, user_account FROM services WHERE
 22NOT (user_account LIKE '%LocalSystem' OR user_account LIKE '%LocalService' OR user_account LIKE '%NetworkService' OR
 23user_account == null)
 24"""
 25
 26[[transform.osquery]]
 27label = "Osquery - Retrieve Service Unsigned Executables with Virustotal Link"
 28query = """
 29SELECT concat('https://www.virustotal.com/gui/file/', sha1) AS VtLink, name, description, start_type, status, pid,
 30services.path FROM services JOIN authenticode ON services.path = authenticode.path OR services.module_path =
 31authenticode.path JOIN hash ON services.path = hash.path WHERE authenticode.result != 'trusted'
 32"""
 33
 34
 35[rule]
 36author = ["Elastic"]
 37description = """
 38Identifies the execution of PowerShell script with keywords related to different Antimalware Scan Interface (AMSI)
 39bypasses. An adversary may attempt first to disable AMSI before executing further malicious powershell scripts to evade
 40detection.
 41"""
 42from = "now-9m"
 43index = ["winlogbeat-*", "logs-windows.powershell*"]
 44language = "kuery"
 45license = "Elastic License v2"
 46name = "Potential Antimalware Scan Interface Bypass via PowerShell"
 47note = """## Triage and analysis
 48
 49### Investigating Potential Antimalware Scan Interface Bypass via PowerShell
 50
 51PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks. This makes it available for use in various environments, and creates an attractive way for attackers to execute code.
 52
 53The Windows Antimalware Scan Interface (AMSI) is a versatile interface standard that allows your applications and services to integrate with any antimalware product on a machine. AMSI integrates with multiple Windows components, ranging from User Account Control (UAC) to VBA macros and PowerShell.
 54
 55This rule identifies scripts that contain methods and classes that can be abused to bypass AMSI.
 56
 57> **Note**:
 58> This investigation guide uses the [Osquery Markdown Plugin](https://www.elastic.co/guide/en/security/master/invest-guide-run-osquery.html) introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide.
 59
 60#### Possible investigation steps
 61
 62- 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.
 63- 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.
 64- Determine whether the script was executed and capture relevant information, such as arguments that reveal intent or are indicators of compromise (IoCs).
 65- Investigate other alerts associated with the user/host during the past 48 hours.
 66- Investigate commands and scripts executed after this activity was observed.
 67- Inspect the host for suspicious or abnormal behavior in the alert timeframe:
 68  - Observe and collect information about the following activities in the alert subject host:
 69    - Attempts to contact external domains and addresses.
 70      - Use the Elastic Defend network events to determine domains and addresses contacted by the subject process by filtering by the process' `process.entity_id`.
 71      - Examine the DNS cache for suspicious or anomalous entries.
 72        - $osquery_0
 73    - Use the Elastic Defend registry events to examine registry keys accessed, modified, or created by the related processes in the process tree.
 74    - Examine the host services for suspicious or anomalous entries.
 75      - $osquery_1
 76      - $osquery_2
 77      - $osquery_3
 78
 79### False positive analysis
 80
 81- This activity is unlikely to happen legitimately. Benign true positives (B-TPs) can be added as exceptions if necessary.
 82
 83### Response and remediation
 84
 85- Initiate the incident response process based on the outcome of the triage.
 86- Isolate the involved hosts to prevent further post-compromise behavior.
 87- If the triage identified malware, search the environment for additional compromised hosts.
 88  - Implement temporary network rules, procedures, and segmentation to contain the malware.
 89  - Stop suspicious processes.
 90  - Immediately block the identified indicators of compromise (IoCs).
 91  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
 92- Remove and block malicious artifacts identified during triage.
 93- Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
 94- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
 95- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 96- 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).
 97"""
 98references = ["https://github.com/S3cur3Th1sSh1t/Amsi-Bypass-Powershell"]
 99risk_score = 73
100rule_id = "1f0a69c0-3392-4adf-b7d5-6012fd292da8"
101severity = "high"
102tags = ["Domain: Endpoint", "OS: Windows", "Use Case: Threat Detection", "Tactic: Defense Evasion", "Data Source: PowerShell Logs", "Resources: Investigation Guide"]
103timestamp_override = "event.ingested"
104type = "query"
105
106query = '''
107event.category:"process" and host.os.type:windows and
108  (
109    powershell.file.script_block_text : (
110      "System.Management.Automation.AmsiUtils" or
111			amsiInitFailed or 
112			"Invoke-AmsiBypass" or 
113			"Bypass.AMSI" or 
114			"amsi.dll" or 
115			AntimalwareProvider  or 
116			amsiSession or 
117			amsiContext or
118			AmsiInitialize or 
119			unloadobfuscated or 
120			unloadsilent or 
121			AmsiX64 or 
122			AmsiX32 or 
123			FindAmsiFun
124    ) or
125    powershell.file.script_block_text:("[System.Runtime.InteropServices.Marshal]::Copy" and "VirtualProtect") or
126    powershell.file.script_block_text:("[Ref].Assembly.GetType(('System.Management.Automation" and ".SetValue(")
127  ) and
128  not powershell.file.script_block_text : (
129    "sentinelbreakpoints" and "Set-PSBreakpoint"
130  )
131'''
132
133
134[[rule.threat]]
135framework = "MITRE ATT&CK"
136[[rule.threat.technique]]
137id = "T1562"
138name = "Impair Defenses"
139reference = "https://attack.mitre.org/techniques/T1562/"
140[[rule.threat.technique.subtechnique]]
141id = "T1562.001"
142name = "Disable or Modify Tools"
143reference = "https://attack.mitre.org/techniques/T1562/001/"
144
145
146
147[rule.threat.tactic]
148id = "TA0005"
149name = "Defense Evasion"
150reference = "https://attack.mitre.org/tactics/TA0005/"
151[[rule.threat]]
152framework = "MITRE ATT&CK"
153[[rule.threat.technique]]
154id = "T1059"
155name = "Command and Scripting Interpreter"
156reference = "https://attack.mitre.org/techniques/T1059/"
157[[rule.threat.technique.subtechnique]]
158id = "T1059.001"
159name = "PowerShell"
160reference = "https://attack.mitre.org/techniques/T1059/001/"
161
162
163
164[rule.threat.tactic]
165id = "TA0002"
166name = "Execution"
167reference = "https://attack.mitre.org/tactics/TA0002/"

Triage and analysis

Investigating Potential Antimalware Scan Interface Bypass via PowerShell

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

The Windows Antimalware Scan Interface (AMSI) is a versatile interface standard that allows your applications and services to integrate with any antimalware product on a machine. AMSI integrates with multiple Windows components, ranging from User Account Control (UAC) to VBA macros and PowerShell.

This rule identifies scripts that contain methods and classes that can be abused to bypass AMSI.

Note: This investigation guide uses the Osquery Markdown Plugin introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide.

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.
  • Determine whether the script was executed and capture relevant information, such as arguments that reveal intent or are indicators of compromise (IoCs).
  • Investigate other alerts associated with the user/host during the past 48 hours.
  • Investigate commands and scripts executed after this activity was observed.
  • Inspect the host for suspicious or abnormal behavior in the alert timeframe:
    • Observe and collect information about the following activities in the alert subject host:
      • Attempts to contact external domains and addresses.
        • Use the Elastic Defend network events to determine domains and addresses contacted by the subject process by filtering by the process' process.entity_id.
        • Examine the DNS cache for suspicious or anomalous entries.
          • $osquery_0
      • Use the Elastic Defend registry events to examine registry keys accessed, modified, or created by the related processes in the process tree.
      • Examine the host services for suspicious or anomalous entries.
        • $osquery_1
        • $osquery_2
        • $osquery_3

False positive analysis

  • This activity is unlikely to happen legitimately. Benign true positives (B-TPs) can be added as exceptions if necessary.

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.
  • If the triage identified malware, search the environment for additional compromised hosts.
    • Implement temporary network rules, procedures, and segmentation to contain the malware.
    • Stop suspicious processes.
    • Immediately block the identified indicators of compromise (IoCs).
    • Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
  • Remove and block malicious artifacts identified during triage.
  • Restrict PowerShell usage outside of IT and engineering business units using GPOs, AppLocker, Intune, or similar software.
  • 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).

References

Related rules

to-top