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