T1505.001

SQL Stored Procedures

Adversaries abuse SQL stored procedures to establish persistent access to database servers. In MSSQL, the sp_addstartup or marking a procedure as a startup procedure causes it to execute automatically when SQL Server starts. Enabling xp_cmdshell allows execution of operating system commands. CLR assemblies compiled from .NET code can be registered and linked to stored procedures for arbitrary code execution. Stuxnet used xp_cmdshell for this purpose; Kaspersky documented attackers using startup procedures for persistent backdoor access.

Microsoft Sentinel / Defender
kusto
// T1505.001 — SQL Stored Procedure persistence detection
// Focus on process execution from SQL Server processes
// Part 1: Detect OS commands spawned by SQL Server (xp_cmdshell execution)
let SQLShellExec = DeviceProcessEvents
| where Timestamp > ago(24h)
| where InitiatingProcessFileName in~ ("sqlservr.exe", "sqlagent.exe")
| where FileName in~ ("cmd.exe", "powershell.exe", "wscript.exe", "cscript.exe",
                      "mshta.exe", "certutil.exe", "bitsadmin.exe", "rundll32.exe",
                      "net.exe", "net1.exe", "whoami.exe", "ipconfig.exe")
| extend DetectionType = "SQL_Server_OS_Command"
| project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine,
          InitiatingProcessFileName, InitiatingProcessCommandLine, DetectionType;
// Part 2: Detect SQL Server process writing files to disk (CLR assembly drop or payload)
let SQLFileWrite = DeviceFileEvents
| where Timestamp > ago(24h)
| where InitiatingProcessFileName in~ ("sqlservr.exe", "sqlagent.exe")
| where ActionType == "FileCreated"
| where FolderPath !has "\\Microsoft SQL Server\\"
| where FolderPath !has "\\MSSQL\\"
| extend DetectionType = "SQL_Server_File_Write"
| project Timestamp, DeviceName, AccountName, FileName, FolderPath,
          InitiatingProcessFileName, DetectionType;
// Part 3: Detect registry changes related to SQL Server CLR/startup configuration
let SQLRegistryMod = DeviceRegistryEvents
| where Timestamp > ago(24h)
| where RegistryKey has_any ("SQL Server", "MSSQLSERVER", "SQLServer")
| where RegistryValueName has_any ("xp_cmdshell", "clr enabled", "startup", "CLR")
| extend DetectionType = "SQL_Server_Config_Change"
| project Timestamp, DeviceName, AccountName, RegistryKey, RegistryValueName, RegistryValueData,
          InitiatingProcessFileName, DetectionType;
union SQLShellExec, SQLFileWrite, SQLRegistryMod
| sort by Timestamp desc
high severity high confidence

Data Sources

Process: Process Creation File: File Creation Windows Registry: Registry Value Modification Microsoft Defender for Endpoint

Required Tables

DeviceProcessEvents DeviceFileEvents DeviceRegistryEvents

False Positives

  • Database maintenance scripts using xp_cmdshell for legitimate file system operations (backup to network share, log archival)
  • SQL Server Agent jobs that run OS commands as part of scheduled database maintenance
  • CLR assemblies deployed by legitimate database applications for custom data processing
  • DBA-initiated configuration changes to SQL Server settings during maintenance windows

Unlock Pro Content

Get the full detection package for T1505.001 including response playbook, investigation guide, and atomic red team tests.

Response PlaybookInvestigation GuideHunting QueriesAtomic Red Team TestsTuning Guidance

Related Detections