|
| 1 | +name: Windows PowerShell Invoke-Sqlcmd Execution |
| 2 | +id: 5eb76fe2-a869-4865-8c4c-8cff424b18a1 |
| 3 | +version: 1 |
| 4 | +date: '2025-02-03' |
| 5 | +author: Michael Haag, Splunk |
| 6 | +status: production |
| 7 | +type: Anomaly |
| 8 | +description: This detection identifies potentially suspicious usage of Invoke-Sqlcmd PowerShell cmdlet, which can be used for database operations and potential data exfiltration. The detection looks for suspicious parameter combinations and query patterns that may indicate unauthorized database access, data theft, or malicious database operations. Threat actors may prefer using PowerShell Invoke-Sqlcmd over sqlcmd.exe as it provides a more flexible programmatic interface and can better evade detection. |
| 9 | +data_source: |
| 10 | +- Powershell Script Block Logging 4104 |
| 11 | +search: '`powershell` EventCode=4104 ScriptBlockText="*invoke-sqlcmd*" |
| 12 | + | eval script_lower=lower(ScriptBlockText) |
| 13 | + | eval |
| 14 | + has_query=case( |
| 15 | + match(script_lower, "(?i)-query\\s+"), 1, |
| 16 | + match(script_lower, "(?i)-q\\s+"), 1, |
| 17 | + true(), 0 |
| 18 | + ), |
| 19 | + has_input_file=case( |
| 20 | + match(script_lower, "(?i)-inputfile\\s+"), 1, |
| 21 | + match(script_lower, "(?i)-i\\s+"), 1, |
| 22 | + true(), 0 |
| 23 | + ), |
| 24 | + has_url_input=case( |
| 25 | + match(script_lower, "(?i)-inputfile\\s+https?://"), 1, |
| 26 | + match(script_lower, "(?i)-i\\s+https?://"), 1, |
| 27 | + match(script_lower, "(?i)-inputfile\\s+ftp://"), 1, |
| 28 | + match(script_lower, "(?i)-i\\s+ftp://"), 1, |
| 29 | + true(), 0 |
| 30 | + ), |
| 31 | + has_admin_conn=case( |
| 32 | + match(script_lower, "(?i)-dedicatedadministratorconnection"), 1, |
| 33 | + true(), 0 |
| 34 | + ), |
| 35 | + has_suspicious_auth=case( |
| 36 | + match(script_lower, "(?i)-username\\s+sa\\b"), 1, |
| 37 | + match(script_lower, "(?i)-u\\s+sa\\b"), 1, |
| 38 | + match(script_lower, "(?i)-username\\s+admin\\b"), 1, |
| 39 | + match(script_lower, "(?i)-u\\s+admin\\b"), 1, |
| 40 | + true(), 0 |
| 41 | + ), |
| 42 | + has_suspicious_query=case( |
| 43 | + match(script_lower, "(?i)(xp_cmdshell|sp_oacreate|sp_execute_external|openrowset|bulk\\s+insert)"), 1, |
| 44 | + match(script_lower, "(?i)(master\\.\\.\\.sysdatabases|msdb\\.\\.\\.backuphistory|sysadmin|securityadmin)"), 1, |
| 45 | + match(script_lower, "(?i)(select.*from.*sys\\.|select.*password|dump\\s+database)"), 1, |
| 46 | + match(script_lower, "(?i)(sp_addextendedproc|sp_makewebtask|sp_addsrvrolemember)"), 1, |
| 47 | + match(script_lower, "(?i)(sp_configure.*show\\s+advanced|reconfigure|enable_xp_cmdshell)"), 1, |
| 48 | + match(script_lower, "(?i)(exec.*master\\.dbo\\.|exec.*msdb\\.dbo\\.)"), 1, |
| 49 | + match(script_lower, "(?i)(sp_password|sp_control_dbmasterkey_password|sp_dropextendedproc)"), 1, |
| 50 | + match(script_lower, "(?i)(powershell|cmd\\.exe|rundll32|regsvr32|certutil)"), 1, |
| 51 | + true(), 0 |
| 52 | + ), |
| 53 | + has_data_exfil=case( |
| 54 | + match(script_lower, "(?i)-outputas\\s+(dataset|datatables)"), 1, |
| 55 | + match(script_lower, "(?i)-as\\s+(dataset|datatables)"), 1, |
| 56 | + match(script_lower, "(?i)(for\\s+xml|for\\s+json)"), 1, |
| 57 | + match(script_lower, "(?i)(select.*into.*from|select.*into.*outfile)"), 1, |
| 58 | + true(), 0 |
| 59 | + ), |
| 60 | + has_cert_bypass=case( |
| 61 | + match(script_lower, "(?i)-trustservercertificate"), 1, |
| 62 | + true(), 0 |
| 63 | + ) |
| 64 | +
|
| 65 | + | eval risk_score=0 |
| 66 | + | eval risk_score=case( |
| 67 | + has_suspicious_query=1 AND has_data_exfil=1, risk_score + 90, |
| 68 | + has_url_input=1, risk_score + 80, |
| 69 | + has_suspicious_query=1, risk_score + 60, |
| 70 | + has_data_exfil=1, risk_score + 60, |
| 71 | + has_admin_conn=1, risk_score + 50, |
| 72 | + has_suspicious_auth=1, risk_score + 40, |
| 73 | + has_cert_bypass=1, risk_score + 20, |
| 74 | + true(), risk_score |
| 75 | + ) |
| 76 | +
|
| 77 | + | eval command_type=case( |
| 78 | + match(script_lower, "xp_cmdshell"), "xp_cmdshell abuse", |
| 79 | + match(script_lower, "https?://"), "Remote file execution", |
| 80 | + match(script_lower, "sys\\.server_principals"), "System enumeration", |
| 81 | + match(script_lower, "fn_my_permissions"), "Permission enumeration", |
| 82 | + match(script_lower, "username\\s+sa\\b"), "SA account usage", |
| 83 | + match(script_lower, "show\\s+advanced\\s+options"), "Configuration change attempt", |
| 84 | + match(script_lower, "select.*from\\s+customers"), "Large data export", |
| 85 | + match(script_lower, "select.*password"), "Sensitive data query", |
| 86 | + match(script_lower, "sp_configure.*xp_cmdshell"), "Enable xp_cmdshell", |
| 87 | + 1=1, "General database access" |
| 88 | + ) |
| 89 | +
|
| 90 | + | eval risk_factors=mvappend( |
| 91 | + if(has_suspicious_query=1 AND has_data_exfil=1, "High-risk query with data extraction: ".command_type, null()), |
| 92 | + if(has_url_input=1, "Remote file input detected in command", null()), |
| 93 | + if(has_suspicious_query=1, "Suspicious SQL query pattern: ".command_type, null()), |
| 94 | + if(has_data_exfil=1, "Potential data exfiltration using ".command_type, null()), |
| 95 | + if(has_admin_conn=1, "Administrative database connection", null()), |
| 96 | + if(has_suspicious_auth=1, "Suspicious authentication method used", null()), |
| 97 | + if(has_cert_bypass=1, "Certificate validation bypassed", null()) |
| 98 | + ) |
| 99 | + | eval risk_message="PowerShell Invoke-Sqlcmd execution with risk factors: ".mvjoin(risk_factors, ", ") |
| 100 | +
|
| 101 | + | where risk_score >= 30 |
| 102 | + | stats count min(_time) as firstTime max(_time) as lastTime by EventCode ScriptBlockText UserID Computer risk_message risk_score command_type |
| 103 | + | rename Computer as dest, UserID as user |
| 104 | + | `security_content_ctime(firstTime)` |
| 105 | + | `security_content_ctime(lastTime)` |
| 106 | + | `windows_powershell_invoke_sqlcmd_execution_filter`' |
| 107 | +how_to_implement: To successfully implement this detection, you need to be ingesting PowerShell logs with Script Block Logging and Module Logging enabled. The detection looks for Invoke-Sqlcmd usage in PowerShell scripts and evaluates the parameters and queries for suspicious patterns. Configure your PowerShell logging to capture script block execution and ensure the logs are mapped to the PowerShell node of the Endpoint data model. |
| 108 | +known_false_positives: Database administrators and developers frequently use Invoke-Sqlcmd as a legitimate tool for various database management tasks. This includes running automated database maintenance scripts, performing ETL (Extract, Transform, Load) processes, executing data migration jobs, implementing database deployment and configuration scripts, and running monitoring and reporting tasks. To effectively manage false positives in your environment, consider implementing several mitigation strategies. First, establish a whitelist of known administrator and service accounts that regularly perform these operations. Second, create exceptions for approved script paths where legitimate database operations typically occur. Additionally, it's important to baseline your environment's normal PowerShell database interaction patterns and implement monitoring for any deviations from these established patterns. Finally, consider adjusting the risk score thresholds based on your specific environment and security requirements to achieve an optimal balance between security and operational efficiency. |
| 109 | +references: |
| 110 | +- https://learn.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd |
| 111 | +- https://attack.mitre.org/techniques/T1059.001/ |
| 112 | +- https://attack.mitre.org/techniques/T1059.003/ |
| 113 | +drilldown_searches: |
| 114 | +- name: View process details for suspicious Invoke-Sqlcmd execution |
| 115 | + search: '%original_detection_search% | search dest="$dest$" user="$user$"' |
| 116 | + earliest_offset: $info_min_time$ |
| 117 | + latest_offset: $info_max_time$ |
| 118 | +- name: View risk events for the last 7 days for - "$dest$" and "$user$" |
| 119 | + search: '| from datamodel Risk.All_Risk | search normalized_risk_object IN ("$dest$", "$user$") starthoursago=168 | stats count min(_time) as firstTime max(_time) as lastTime values(search_name) as "Search Name" values(risk_message) as "Risk Message" values(analyticstories) as "Analytic Stories" values(annotations._all) as "Annotations" values(annotations.mitre_attack.mitre_tactic) as "ATT&CK Tactics" by normalized_risk_object | `security_content_ctime(firstTime)` | `security_content_ctime(lastTime)`' |
| 120 | + earliest_offset: $info_min_time$ |
| 121 | + latest_offset: $info_max_time$ |
| 122 | +rba: |
| 123 | + message: A PowerShell script contains Invoke-Sqlcmd command with EventCode $EventCode$ on host $dest$ |
| 124 | + risk_objects: |
| 125 | + - field: dest |
| 126 | + type: system |
| 127 | + score: 49 |
| 128 | + - field: user |
| 129 | + type: user |
| 130 | + score: 49 |
| 131 | + threat_objects: [] |
| 132 | +tags: |
| 133 | + analytic_story: |
| 134 | + - SQL Server Abuse |
| 135 | + asset_type: Endpoint |
| 136 | + mitre_attack_id: |
| 137 | + - T1059.001 |
| 138 | + - T1059.003 |
| 139 | + product: |
| 140 | + - Splunk Enterprise |
| 141 | + - Splunk Enterprise Security |
| 142 | + - Splunk Cloud |
| 143 | + security_domain: endpoint |
| 144 | +tests: |
| 145 | +- name: True Positive Test |
| 146 | + attack_data: |
| 147 | + - data: https://media.githubusercontent.com/media/splunk/attack_data/master/datasets/attack_techniques/T1059.003/atomic_red_team/invokesqlcmd_powershell.log |
| 148 | + source: XmlWinEventLog:Microsoft-Windows-PowerShell/Operational |
| 149 | + sourcetype: XmlWinEventLog |
0 commit comments