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", "m365_defender", "sentinel_one_cloud_funnel", "crowdstrike"]
  4maturity = "production"
  5updated_date = "2024/11/02"
  6min_stack_version = "8.14.0"
  7min_stack_comments = "Breaking change at 8.14.0 for the Windows Integration."
  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 = [
 17    "winlogbeat-*",
 18    "logs-endpoint.events.process-*",
 19    "logs-windows.forwarded*",
 20    "logs-windows.sysmon_operational-*",
 21    "endgame-*",
 22    "logs-system.security*",
 23    "logs-m365_defender.event-*",
 24    "logs-sentinel_one_cloud_funnel.*",
 25    "logs-crowdstrike.fdr*",
 26]
 27language = "eql"
 28license = "Elastic License v2"
 29name = "Execution via MSSQL xp_cmdshell Stored Procedure"
 30note = """## Triage and analysis
 31
 32### Investigating Execution via MSSQL xp_cmdshell Stored Procedure
 33
 34Microsoft 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.
 35
 36The 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.
 37
 38#### Possible investigation steps
 39
 40- 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.
 41- Investigate other alerts associated with the user/host during the past 48 hours.
 42- Investigate any abnormal account behavior, such as command executions, file creations or modifications, and network connections.
 43- Investigate any abnormal behavior by the subject process such as network connections, registry or file modifications, and any spawned child processes.
 44- Examine the command line to determine if the command executed is potentially harmful or malicious.
 45- Inspect the host for suspicious or abnormal behavior in the alert timeframe.
 46
 47### False positive analysis
 48
 49- 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.
 50
 51### Response and remediation
 52
 53- Initiate the incident response process based on the outcome of the triage.
 54- Isolate the involved hosts to prevent further post-compromise behavior.
 55- Remove and block malicious artifacts identified during triage.
 56- 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.
 57- 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.
 58- Disable the xp_cmdshell stored procedure.
 59- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
 60- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 61- 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).
 62"""
 63references = ["https://thedfirreport.com/2022/07/11/select-xmrig-from-sqlserver/"]
 64risk_score = 73
 65rule_id = "4ed493fc-d637-4a36-80ff-ac84937e5461"
 66severity = "high"
 67tags = [
 68    "Domain: Endpoint",
 69    "OS: Windows",
 70    "Use Case: Threat Detection",
 71    "Tactic: Persistence",
 72    "Resources: Investigation Guide",
 73    "Data Source: Elastic Endgame",
 74    "Data Source: Elastic Defend",
 75    "Data Source: System",
 76    "Data Source: Microsoft Defender for Endpoint",
 77    "Data Source: Sysmon",
 78    "Data Source: SentinelOne",
 79    "Data Source: Crowdstrike",
 80]
 81timestamp_override = "event.ingested"
 82type = "eql"
 83
 84query = '''
 85process where host.os.type == "windows" and event.type == "start" and process.parent.name : "sqlservr.exe" and 
 86  (
 87   (process.name : "cmd.exe" and 
 88    not process.args : ("\\\\*", "diskfree", "rmdir", "mkdir", "dir", "del", "rename", "bcp", "*XMLNAMESPACES*", 
 89                        "?:\\MSSQL\\Backup\\Jobs\\sql_agent_backup_job.ps1", "K:\\MSSQL\\Backup\\msdb", "K:\\MSSQL\\Backup\\Logins")) or 
 90                        
 91   (process.name : "vpnbridge.exe" or ?process.pe.original_file_name : "vpnbridge.exe") or 
 92
 93   (process.name : "certutil.exe" or ?process.pe.original_file_name == "CertUtil.exe") or 
 94
 95   (process.name : "bitsadmin.exe" or ?process.pe.original_file_name == "bitsadmin.exe")
 96  )
 97'''
 98
 99
100[[rule.threat]]
101framework = "MITRE ATT&CK"
102[[rule.threat.technique]]
103id = "T1505"
104name = "Server Software Component"
105reference = "https://attack.mitre.org/techniques/T1505/"
106[[rule.threat.technique.subtechnique]]
107id = "T1505.001"
108name = "SQL Stored Procedures"
109reference = "https://attack.mitre.org/techniques/T1505/001/"
110
111
112
113[rule.threat.tactic]
114id = "TA0003"
115name = "Persistence"
116reference = "https://attack.mitre.org/tactics/TA0003/"
117[[rule.threat]]
118framework = "MITRE ATT&CK"
119[[rule.threat.technique]]
120id = "T1059"
121name = "Command and Scripting Interpreter"
122reference = "https://attack.mitre.org/techniques/T1059/"
123[[rule.threat.technique.subtechnique]]
124id = "T1059.003"
125name = "Windows Command Shell"
126reference = "https://attack.mitre.org/techniques/T1059/003/"
127
128
129
130[rule.threat.tactic]
131id = "TA0002"
132name = "Execution"
133reference = "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