Operator On The Wire
Join
← Back to Knowledge Base
BLUE TEAM / THREAT HUNT / ACTIVE DIRECTORY / MSSQL

XP_CMDSHELL

This note documents detection patterns related to MSSQL xp_cmdshell Abuse within Active Directory environments.


Direct Indicators

LogEvent IDMeaningForensic ValueNotes
Security4624Successful logonMediumLogon associated with SQL service account or attacker account.
Security4688Process createdCriticalCommand execution spawned from sqlservr.exe process.
Sysmon1Process creationCriticalChild process of sqlservr.exe such as cmd.exe or powershell.exe.
Sysmon3Network connectionHighOutbound connections from sqlservr.exe child processes.
SQL Server LogsN/Axp_cmdshell executionCriticalExecution of xp_cmdshell command recorded in SQL logs.
EDR TelemetryN/ACommand execution from SQL serviceCriticalDetection of shell commands launched via SQL service.

Indirect Indicators

IndicatorWhat To Look ForForensic ValueNotes
sqlservr.exe spawning cmd.exeSQL Server process launching shell commandsCriticalStrong indicator of xp_cmdshell abuse.
PowerShell executed by SQL servicesqlservr.exe → powershell.exe chainCriticalCommon exploitation technique.
Network connections from SQL child processesReverse shell or lateral movement attemptsHighOften used for pivoting.
Unusual SQL queriesEXEC xp_cmdshell commandsCriticalDirect evidence of command execution.
Service account performing administrative actionsSQL service account executing system commandsHighOften abused for privilege escalation.

Common Tools

ToolUsage
sqlcmdExecutes SQL queries including xp_cmdshell commands.
Impacket mssqlclientAllows remote SQL command execution.
PowerUpSQLEnumerates SQL servers and abuses xp_cmdshell.
Metasploit MSSQL modulesAutomates xp_cmdshell exploitation.
Custom SQL scriptsUsed to execute OS commands via SQL server.

Relevant Artifacts

  • Windows Security logs (4624, 4688)
  • SQL Server error logs and query logs
  • Sysmon logs (1 process creation, 3 network connections)
  • EDR telemetry detecting command execution
  • Prefetch artifacts for cmd.exe or powershell.exe
  • Network logs for lateral movement attempts
  • File system artifacts created by executed commands

MITRE ATT&CK References

  • T1059 Command and Scripting Interpreter
  • T1505 Server Software Component
  • T1047 Windows Management Instrumentation

Decision Tree

  1. Is the suspicious event present?
    • Identify processes spawned by sqlservr.exe.
  2. What host generated the event?
    • Determine which SQL server executed the command.
  3. Is the account expected to perform this action?
    • Investigate whether administrative SQL activity is legitimate.
  4. Pivot:
    • Source host → inspect SQL queries executed.
    • Child processes → analyze command line arguments.
    • Network → inspect outbound connections from SQL child processes.
  5. Confirm exploitation
    • Determine whether attacker executed system commands through xp_cmdshell.

Example Detection Templates

KQL

DeviceProcessEvents
| where InitiatingProcessFileName == "sqlservr.exe"
| where FileName in ("cmd.exe","powershell.exe")
SecurityEvent
| where EventID == 4688
| where ParentProcessName has "sqlservr"

EQL

process where parent.name == "sqlservr.exe"

Sigma

title: MSSQL xp_cmdshell Abuse
id: mssql-xpcmdshell-detection
status: experimental
description: Detects command execution spawned from SQL Server process
logsource:
  product: windows
  category: process_creation
detection:
  selection:
    ParentImage|endswith: sqlservr.exe
    Image|endswith:
      - cmd.exe
      - powershell.exe
  condition: selection
fields:
  - Image
  - ParentImage
  - CommandLine
falsepositives:
  - Legitimate SQL administrative automation
level: high
tags:
  - attack.execution

Mitigation & Hardening

Control AreaMitigationEffectivenessNotes
SQL configurationDisable xp_cmdshell if not requiredCriticalRemoves attack capability.
Access controlRestrict SQL administrative privilegesHighPrevents unauthorized execution.
MonitoringAlert on sqlservr.exe spawning shellsHighStrong detection signal.
Least privilegeRun SQL service with minimal privilegesMediumReduces impact of exploitation.
Threat huntingReview SQL queries invoking xp_cmdshellMediumDetects suspicious activity.