Execution via MSSQL xp_cmdshell Stored Procedure

Identifies execution via MSSQL xp_cmdshell stored procedure. Malicious users may attempt to elevate their privileges by using xp_cmdshell, which is disabled by default, thus, it's important to review the context of it's use.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2020/08/14"
  3integration = ["endpoint", "windows", "system"]
  4maturity = "production"
  5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
  6min_stack_version = "8.3.0"
  7updated_date = "2024/03/28"
  8
  9[rule]
 10author = ["Elastic"]
 11description = """
 12Identifies execution via MSSQL xp_cmdshell stored procedure. Malicious users may attempt to elevate their privileges by
 13using xp_cmdshell, which is disabled by default, thus, it's important to review the context of it's use.
 14"""
 15from = "now-9m"
 16index = ["winlogbeat-*", "logs-endpoint.events.process-*", "logs-windows.*", "endgame-*", "logs-system.security*"]
 17language = "eql"
 18license = "Elastic License v2"
 19name = "Execution via MSSQL xp_cmdshell Stored Procedure"
 20note = """## Triage and analysis
 21
 22### Investigating Execution via MSSQL xp_cmdshell Stored Procedure
 23
 24Microsoft SQL Server (MSSQL) has procedures meant to extend its functionality, the Extended Stored Procedures. These procedures are external functions written in C/C++; some provide interfaces for external programs. This is the case for xp_cmdshell, which spawns a Windows command shell and passes in a string for execution. Attackers can use this to execute commands on the system running the SQL server, commonly to escalate their privileges and establish persistence.
 25
 26The xp_cmdshell procedure is disabled by default, but when used, it has the same security context as the MSSQL Server service account, which is often privileged.
 27
 28#### Possible investigation steps
 29
 30- 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.
 31- Investigate other alerts associated with the user/host during the past 48 hours.
 32- Investigate any abnormal account behavior, such as command executions, file creations or modifications, and network connections.
 33- Investigate any abnormal behavior by the subject process such as network connections, registry or file modifications, and any spawned child processes.
 34- Examine the command line to determine if the command executed is potentially harmful or malicious.
 35- Inspect the host for suspicious or abnormal behavior in the alert timeframe.
 36
 37### False positive analysis
 38
 39- This mechanism can be used legitimately, but it brings inherent risk. The security team must monitor any activity of it. If recurrent tasks are being executed using this mechanism, consider adding exceptions — preferably with a full command line.
 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- Remove and block malicious artifacts identified during triage.
 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- Ensure that SQL servers are not directly exposed to the internet. If there is a business justification for such, use an allowlist to allow only connections from known legitimate sources.
 48- Disable the xp_cmdshell stored procedure.
 49- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
 50- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 51- 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).
 52"""
 53references = ["https://thedfirreport.com/2022/07/11/select-xmrig-from-sqlserver/"]
 54risk_score = 73
 55rule_id = "4ed493fc-d637-4a36-80ff-ac84937e5461"
 56setup = """## Setup
 57
 58If enabling an EQL rule on a non-elastic-agent index (such as beats) for versions <8.2,
 59events will not define `event.ingested` and default fallback for EQL rules was not added until version 8.2.
 60Hence for this rule to work effectively, users will need to add a custom ingest pipeline to populate
 61`event.ingested` to @timestamp.
 62For more details on adding a custom ingest pipeline refer - https://www.elastic.co/guide/en/fleet/current/data-streams-pipeline-tutorial.html
 63"""
 64severity = "high"
 65tags = ["Domain: Endpoint", "OS: Windows", "Use Case: Threat Detection", "Tactic: Persistence", "Resources: Investigation Guide", "Data Source: Elastic Endgame", "Data Source: Elastic Defend"]
 66timestamp_override = "event.ingested"
 67type = "eql"
 68
 69query = '''
 70process where host.os.type == "windows" and event.type == "start" and process.parent.name : "sqlservr.exe" and 
 71  (
 72   (process.name : "cmd.exe" and 
 73    not process.args : ("\\\\*", "diskfree", "rmdir", "mkdir", "dir", "del", "rename", "bcp", "*XMLNAMESPACES*", 
 74                        "?:\\MSSQL\\Backup\\Jobs\\sql_agent_backup_job.ps1", "K:\\MSSQL\\Backup\\msdb", "K:\\MSSQL\\Backup\\Logins")) or 
 75                        
 76   (process.name : "vpnbridge.exe" or ?process.pe.original_file_name : "vpnbridge.exe") or 
 77
 78   (process.name : "certutil.exe" or ?process.pe.original_file_name == "CertUtil.exe") or 
 79
 80   (process.name : "bitsadmin.exe" or ?process.pe.original_file_name == "bitsadmin.exe")
 81  )
 82'''
 83
 84[[rule.threat]]
 85framework = "MITRE ATT&CK"
 86
 87[[rule.threat.technique]]
 88id = "T1505"
 89name = "Server Software Component"
 90reference = "https://attack.mitre.org/techniques/T1505/"
 91
 92[[rule.threat.technique.subtechnique]]
 93id = "T1505.001"
 94name = "SQL Stored Procedures"
 95reference = "https://attack.mitre.org/techniques/T1505/001/"
 96
 97[rule.threat.tactic]
 98id = "TA0003"
 99name = "Persistence"
100reference = "https://attack.mitre.org/tactics/TA0003/"
101
102[[rule.threat]]
103framework = "MITRE ATT&CK"
104
105[[rule.threat.technique]]
106id = "T1059"
107name = "Command and Scripting Interpreter"
108reference = "https://attack.mitre.org/techniques/T1059/"
109
110[[rule.threat.technique.subtechnique]]
111id = "T1059.003"
112name = "Windows Command Shell"
113reference = "https://attack.mitre.org/techniques/T1059/003/"
114
115
116[rule.threat.tactic]
117id = "TA0002"
118name = "Execution"
119reference = "https://attack.mitre.org/tactics/TA0002/"

Triage and analysis

Investigating Execution via MSSQL xp_cmdshell Stored Procedure

Microsoft SQL Server (MSSQL) has procedures meant to extend its functionality, the Extended Stored Procedures. These procedures are external functions written in C/C++; some provide interfaces for external programs. This is the case for xp_cmdshell, which spawns a Windows command shell and passes in a string for execution. Attackers can use this to execute commands on the system running the SQL server, commonly to escalate their privileges and establish persistence.

The xp_cmdshell procedure is disabled by default, but when used, it has the same security context as the MSSQL Server service account, which is often privileged.

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.
  • Investigate any abnormal account behavior, such as command executions, file creations or modifications, and network connections.
  • Investigate any abnormal behavior by the subject process such as network connections, registry or file modifications, and any spawned child processes.
  • Examine the command line to determine if the command executed is potentially harmful or malicious.
  • Inspect the host for suspicious or abnormal behavior in the alert timeframe.

False positive analysis

  • This mechanism can be used legitimately, but it brings inherent risk. The security team must monitor any activity of it. If recurrent tasks are being executed using this mechanism, consider adding exceptions — preferably with a full command line.

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.
  • Remove and block malicious artifacts identified during triage.
  • 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.
  • Ensure that SQL servers are not directly exposed to the internet. If there is a business justification for such, use an allowlist to allow only connections from known legitimate sources.
  • Disable the xp_cmdshell stored procedure.
  • 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