Execution from Unusual Directory - Command Line

Identifies process execution from suspicious default Windows directories. This may be abused by adversaries to hide malware in trusted paths.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2020/10/30"
  3integration = ["endpoint", "windows", "system"]
  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 process execution from suspicious default Windows directories. This may be abused by adversaries to hide
 37malware in trusted paths.
 38"""
 39from = "now-9m"
 40index = [
 41    "winlogbeat-*",
 42    "logs-endpoint.events.process-*",
 43    "logs-windows.*",
 44    "endgame-*",
 45    "logs-system.security*",
 46]
 47language = "eql"
 48license = "Elastic License v2"
 49name = "Execution from Unusual Directory - Command Line"
 50note = """## Triage and analysis
 51
 52### Investigating Execution from Unusual Directory - Command Line
 53
 54This rule looks for the execution of scripts from unusual directories. Attackers can use system or application paths to hide malware and make the execution less suspicious.
 55
 56> **Note**:
 57> 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.
 58
 59#### Possible investigation steps
 60
 61- Investigate the process 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- Investigate other alerts associated with the user/host during the past 48 hours.
 63- Examine the command line to determine which commands or scripts were executed.
 64- Examine the host for derived artifacts that indicate suspicious activities:
 65  - Analyze the script using a private sandboxed analysis system.
 66  - Observe and collect information about the following activities in both the sandbox and 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  - Retrieve the files' SHA-256 hash values using the PowerShell `Get-FileHash` cmdlet and search for the existence and reputation of the hashes in resources like VirusTotal, Hybrid-Analysis, CISCO Talos, Any.run, etc.
 77- Investigate potentially compromised accounts. Analysts can do this by searching for login events (for example, 4624) to the target host after the registry modification.
 78
 79### False positive analysis
 80
 81- If this activity is expected and noisy in your environment, consider adding exceptions — preferably with a combination of parent process executable and command line conditions.
 82
 83### Related rules
 84
 85- Process Execution from an Unusual Directory - ebfe1448-7fac-4d59-acea-181bd89b1f7f
 86
 87### Response and remediation
 88
 89- Initiate the incident response process based on the outcome of the triage.
 90- Isolate the involved host to prevent further post-compromise behavior.
 91- If the triage identified malware, search the environment for additional compromised hosts.
 92  - Implement temporary network rules, procedures, and segmentation to contain the malware.
 93  - Stop suspicious processes.
 94  - Immediately block the identified indicators of compromise (IoCs).
 95  - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
 96- Remove and block malicious artifacts identified during triage.
 97- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
 98- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 99- 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).
100"""
101risk_score = 47
102rule_id = "cff92c41-2225-4763-b4ce-6f71e5bda5e6"
103setup = """## Setup
104
105If enabling an EQL rule on a non-elastic-agent index (such as beats) for versions <8.2,
106events will not define `event.ingested` and default fallback for EQL rules was not added until version 8.2.
107Hence for this rule to work effectively, users will need to add a custom ingest pipeline to populate
108`event.ingested` to @timestamp.
109For more details on adding a custom ingest pipeline refer - https://www.elastic.co/guide/en/fleet/current/data-streams-pipeline-tutorial.html
110"""
111severity = "medium"
112tags = [
113    "Domain: Endpoint",
114    "OS: Windows",
115    "Use Case: Threat Detection",
116    "Tactic: Execution",
117    "Tactic: Defense Evasion",
118    "Resources: Investigation Guide",
119    "Data Source: Elastic Endgame",
120    "Data Source: Elastic Defend",
121]
122timestamp_override = "event.ingested"
123type = "eql"
124
125query = '''
126process where host.os.type == "windows" and event.type == "start" and
127  process.name : ("wscript.exe",
128                  "cscript.exe",
129                  "rundll32.exe",
130                  "regsvr32.exe",
131                  "cmstp.exe",
132                  "RegAsm.exe",
133                  "installutil.exe",
134                  "mshta.exe",
135                  "RegSvcs.exe",
136                  "powershell.exe",
137                  "pwsh.exe",
138                  "cmd.exe") and
139
140  /* add suspicious execution paths here */
141  process.args : ("C:\\PerfLogs\\*",
142                  "C:\\Users\\Public\\*",
143                  "C:\\Windows\\Tasks\\*",
144                  "C:\\Intel\\*",
145                  "C:\\AMD\\Temp\\*",
146                  "C:\\Windows\\AppReadiness\\*",
147                  "C:\\Windows\\ServiceState\\*",
148                  "C:\\Windows\\security\\*",
149                  "C:\\Windows\\IdentityCRL\\*",
150                  "C:\\Windows\\Branding\\*",
151                  "C:\\Windows\\csc\\*",
152                  "C:\\Windows\\DigitalLocker\\*",
153                  "C:\\Windows\\en-US\\*",
154                  "C:\\Windows\\wlansvc\\*",
155                  "C:\\Windows\\Prefetch\\*",
156                  "C:\\Windows\\Fonts\\*",
157                  "C:\\Windows\\diagnostics\\*",
158                  "C:\\Windows\\TAPI\\*",
159                  "C:\\Windows\\INF\\*",
160                  "C:\\Windows\\System32\\Speech\\*",
161                  "C:\\windows\\tracing\\*",
162                  "c:\\windows\\IME\\*",
163                  "c:\\Windows\\Performance\\*",
164                  "c:\\windows\\intel\\*",
165                  "c:\\windows\\ms\\*",
166                  "C:\\Windows\\dot3svc\\*",
167                  "C:\\Windows\\panther\\*",
168                  "C:\\Windows\\RemotePackages\\*",
169                  "C:\\Windows\\OCR\\*",
170                  "C:\\Windows\\appcompat\\*",
171                  "C:\\Windows\\apppatch\\*",
172                  "C:\\Windows\\addins\\*",
173                  "C:\\Windows\\Setup\\*",
174                  "C:\\Windows\\Help\\*",
175                  "C:\\Windows\\SKB\\*",
176                  "C:\\Windows\\Vss\\*",
177                  "C:\\Windows\\servicing\\*",
178                  "C:\\Windows\\CbsTemp\\*",
179                  "C:\\Windows\\Logs\\*",
180                  "C:\\Windows\\WaaS\\*",
181                  "C:\\Windows\\twain_32\\*",
182                  "C:\\Windows\\ShellExperiences\\*",
183                  "C:\\Windows\\ShellComponents\\*",
184                  "C:\\Windows\\PLA\\*",
185                  "C:\\Windows\\Migration\\*",
186                  "C:\\Windows\\debug\\*",
187                  "C:\\Windows\\Cursors\\*",
188                  "C:\\Windows\\Containers\\*",
189                  "C:\\Windows\\Boot\\*",
190                  "C:\\Windows\\bcastdvr\\*",
191                  "C:\\Windows\\TextInput\\*",
192                  "C:\\Windows\\security\\*",
193                  "C:\\Windows\\schemas\\*",
194                  "C:\\Windows\\SchCache\\*",
195                  "C:\\Windows\\Resources\\*",
196                  "C:\\Windows\\rescache\\*",
197                  "C:\\Windows\\Provisioning\\*",
198                  "C:\\Windows\\PrintDialog\\*",
199                  "C:\\Windows\\PolicyDefinitions\\*",
200                  "C:\\Windows\\media\\*",
201                  "C:\\Windows\\Globalization\\*",
202                  "C:\\Windows\\L2Schemas\\*",
203                  "C:\\Windows\\LiveKernelReports\\*",
204                  "C:\\Windows\\ModemLogs\\*",
205                  "C:\\Windows\\ImmersiveControlPanel\\*",
206                  "C:\\$Recycle.Bin\\*") and
207
208  /* noisy FP patterns */
209
210  not process.parent.executable : ("C:\\WINDOWS\\System32\\DriverStore\\FileRepository\\*\\igfxCUIService*.exe",
211                                   "C:\\Windows\\System32\\spacedeskService.exe",
212                                   "C:\\Program Files\\Dell\\SupportAssistAgent\\SRE\\SRE.exe") and
213  not (process.name : "rundll32.exe" and
214       process.args : ("uxtheme.dll,#64",
215                       "PRINTUI.DLL,PrintUIEntry",
216                       "?:\\Windows\\System32\\FirewallControlPanel.dll,ShowNotificationDialog",
217                       "?:\\WINDOWS\\system32\\Speech\\SpeechUX\\sapi.cpl",
218                       "?:\\Windows\\system32\\shell32.dll,OpenAs_RunDLL")) and
219
220  not (process.name : "cscript.exe" and process.args : "?:\\WINDOWS\\system32\\calluxxprovider.vbs") and
221
222  not (process.name : "cmd.exe" and process.args : "?:\\WINDOWS\\system32\\powercfg.exe" and process.args : "?:\\WINDOWS\\inf\\PowerPlan.log") and
223
224  not (process.name : "regsvr32.exe" and process.args : "?:\\Windows\\Help\\OEM\\scripts\\checkmui.dll") and
225
226  not (process.name : "cmd.exe" and
227       process.parent.executable : ("?:\\Windows\\System32\\oobe\\windeploy.exe",
228                                    "?:\\Program Files (x86)\\ossec-agent\\wazuh-agent.exe",
229                                    "?:\\Windows\\System32\\igfxCUIService.exe",
230                                    "?:\\Windows\\Temp\\IE*.tmp\\IE*-support\\ienrcore.exe"))
231'''
232
233
234[[rule.threat]]
235framework = "MITRE ATT&CK"
236[[rule.threat.technique]]
237id = "T1059"
238name = "Command and Scripting Interpreter"
239reference = "https://attack.mitre.org/techniques/T1059/"
240[[rule.threat.technique.subtechnique]]
241id = "T1059.003"
242name = "Windows Command Shell"
243reference = "https://attack.mitre.org/techniques/T1059/003/"
244
245
246
247[rule.threat.tactic]
248id = "TA0002"
249name = "Execution"
250reference = "https://attack.mitre.org/tactics/TA0002/"
251[[rule.threat]]
252framework = "MITRE ATT&CK"
253[[rule.threat.technique]]
254id = "T1036"
255name = "Masquerading"
256reference = "https://attack.mitre.org/techniques/T1036/"
257[[rule.threat.technique.subtechnique]]
258id = "T1036.005"
259name = "Match Legitimate Name or Location"
260reference = "https://attack.mitre.org/techniques/T1036/005/"
261
262
263
264[rule.threat.tactic]
265id = "TA0005"
266name = "Defense Evasion"
267reference = "https://attack.mitre.org/tactics/TA0005/"

Triage and analysis

Investigating Execution from Unusual Directory - Command Line

This rule looks for the execution of scripts from unusual directories. Attackers can use system or application paths to hide malware and make the execution less suspicious.

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

  • Investigate the process 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.
  • Investigate other alerts associated with the user/host during the past 48 hours.
  • Examine the command line to determine which commands or scripts were executed.
  • Examine the host for derived artifacts that indicate suspicious activities:
    • Analyze the script using a private sandboxed analysis system.
    • Observe and collect information about the following activities in both the sandbox and 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
    • Retrieve the files' SHA-256 hash values using the PowerShell Get-FileHash cmdlet and search for the existence and reputation of the hashes in resources like VirusTotal, Hybrid-Analysis, CISCO Talos, Any.run, etc.
  • Investigate potentially compromised accounts. Analysts can do this by searching for login events (for example, 4624) to the target host after the registry modification.

False positive analysis

  • If this activity is expected and noisy in your environment, consider adding exceptions — preferably with a combination of parent process executable and command line conditions.
  • Process Execution from an Unusual Directory - ebfe1448-7fac-4d59-acea-181bd89b1f7f

Response and remediation

  • Initiate the incident response process based on the outcome of the triage.
  • Isolate the involved host 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.
  • 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