CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/auxiliary/scanner/mssql/mssql_login.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'metasploit/framework/credential_collection'
7
require 'metasploit/framework/login_scanner/mssql'
8
require 'rex/proto/mssql/client'
9
require 'rex/post/mssql'
10
11
class MetasploitModule < Msf::Auxiliary
12
include Msf::Exploit::Remote::MSSQL
13
include Msf::Auxiliary::Report
14
include Msf::Auxiliary::AuthBrute
15
include Msf::Auxiliary::CommandShell
16
include Msf::Auxiliary::Scanner
17
include Msf::Sessions::CreateSessionOptions
18
include Msf::Auxiliary::ReportSummary
19
20
def initialize
21
super(
22
'Name' => 'MSSQL Login Utility',
23
'Description' => 'This module simply queries the MSSQL instance for a specific user/pass (default is sa with blank).',
24
'Author' => 'MC',
25
'References' =>
26
[
27
[ 'CVE', '1999-0506'] # Weak password
28
],
29
'License' => MSF_LICENSE,
30
# some overrides from authbrute since there is a default username and a blank password
31
'DefaultOptions' =>
32
{
33
'USERNAME' => 'sa',
34
'BLANK_PASSWORDS' => true,
35
'CreateSession' => false
36
}
37
)
38
register_options([
39
Opt::Proxies,
40
OptBool.new('TDSENCRYPTION', [ true, 'Use TLS/SSL for TDS data "Force Encryption"', false]),
41
OptBool.new('CreateSession', [false, 'Create a new session for every successful login', false])
42
])
43
44
if framework.features.enabled?(Msf::FeatureManager::MSSQL_SESSION_TYPE)
45
add_info('New in Metasploit 6.4 - The %grnCreateSession%clr option within this module can open an interactive session')
46
else
47
options_to_deregister = %w[CreateSession]
48
end
49
deregister_options(*options_to_deregister)
50
end
51
52
def create_session?
53
if framework.features.enabled?(Msf::FeatureManager::MSSQL_SESSION_TYPE)
54
datastore['CreateSession']
55
else
56
false
57
end
58
end
59
60
def run
61
results = super
62
logins = results.flat_map { |_k, v| v[:successful_logins] }
63
sessions = results.flat_map { |_k, v| v[:successful_sessions] }
64
print_status("Bruteforce completed, #{logins.size} #{logins.size == 1 ? 'credential was' : 'credentials were'} successful.")
65
return results unless framework.features.enabled?(Msf::FeatureManager::MSSQL_SESSION_TYPE)
66
67
if create_session?
68
print_status("#{sessions.size} MSSQL #{sessions.size == 1 ? 'session was' : 'sessions were'} opened successfully.")
69
else
70
print_status('You can open an MSSQL session with these credentials and %grnCreateSession%clr set to true')
71
end
72
results
73
end
74
75
def run_host(ip)
76
print_status("#{rhost}:#{rport} - MSSQL - Starting authentication scanner.")
77
78
if datastore['TDSENCRYPTION']
79
if create_session?
80
raise Msf::OptionValidateError.new(
81
{
82
'TDSENCRYPTION' => "Cannot create sessions when encryption is enabled. See https://github.com/rapid7/metasploit-framework/issues/18745 to vote for this feature"
83
}
84
)
85
else
86
print_status("TDS Encryption enabled")
87
end
88
end
89
90
cred_collection = build_credential_collection(
91
realm: datastore['DOMAIN'],
92
username: datastore['USERNAME'],
93
password: datastore['PASSWORD']
94
)
95
96
scanner = Metasploit::Framework::LoginScanner::MSSQL.new(
97
configure_login_scanner(
98
host: ip,
99
port: rport,
100
proxies: datastore['PROXIES'],
101
cred_details: cred_collection,
102
stop_on_success: datastore['STOP_ON_SUCCESS'],
103
bruteforce_speed: datastore['BRUTEFORCE_SPEED'],
104
connection_timeout: 30,
105
max_send_size: datastore['TCP::max_send_size'],
106
send_delay: datastore['TCP::send_delay'],
107
auth: datastore['Mssql::Auth'],
108
domain_controller_rhost: datastore['DomainControllerRhost'],
109
hostname: datastore['Mssql::Rhostname'],
110
windows_authentication: datastore['USE_WINDOWS_AUTHENT'],
111
tdsencryption: datastore['TDSENCRYPTION'],
112
framework: framework,
113
framework_module: self,
114
use_client_as_proof: create_session?,
115
ssl: datastore['SSL'],
116
ssl_version: datastore['SSLVersion'],
117
ssl_verify_mode: datastore['SSLVerifyMode'],
118
ssl_cipher: datastore['SSLCipher'],
119
local_port: datastore['CPORT'],
120
local_host: datastore['CHOST']
121
)
122
)
123
successful_logins = []
124
successful_sessions = []
125
scanner.scan! do |result|
126
credential_data = result.to_h
127
credential_data.merge!(
128
module_fullname: self.fullname,
129
workspace_id: myworkspace_id
130
)
131
if result.success?
132
credential_core = create_credential(credential_data)
133
credential_data[:core] = credential_core
134
create_credential_login(credential_data)
135
print_good "#{ip}:#{rport} - Login Successful: #{result.credential}"
136
successful_logins << result
137
138
if create_session?
139
begin
140
successful_sessions << session_setup(result)
141
rescue ::StandardError => e
142
elog('Failed to setup the session', error: e)
143
print_brute level: :error, ip: ip, msg: "Failed to setup the session - #{e.class} #{e.message}"
144
result.connection.close unless result.connection.nil?
145
end
146
end
147
else
148
invalidate_login(credential_data)
149
vprint_error "#{ip}:#{rport} - LOGIN FAILED: #{result.credential} (#{result.status}: #{result.proof})"
150
end
151
end
152
{ successful_logins: successful_logins, successful_sessions: successful_sessions }
153
end
154
155
# @param [Metasploit::Framework::LoginScanner::Result] result
156
# @return [Msf::Sessions::MSSQL]
157
def session_setup(result)
158
return unless (result.connection && result.proof)
159
160
my_session = Msf::Sessions::MSSQL.new(result.connection, { client: result.proof, **result.proof.detect_platform_and_arch })
161
merge_me = {
162
'USERPASS_FILE' => nil,
163
'USER_FILE' => nil,
164
'PASS_FILE' => nil,
165
'USERNAME' => result.credential.public,
166
'PASSWORD' => result.credential.private
167
}
168
169
start_session(self, nil, merge_me, false, my_session.rstream, my_session)
170
end
171
end
172
173