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/mysql/mysql_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/mysql'
8
9
class MetasploitModule < Msf::Auxiliary
10
include Msf::Exploit::Remote::MYSQL
11
include Msf::Auxiliary::Report
12
include Msf::Auxiliary::AuthBrute
13
include Msf::Auxiliary::Scanner
14
include Msf::Sessions::CreateSessionOptions
15
include Msf::Auxiliary::CommandShell
16
include Msf::Auxiliary::ReportSummary
17
18
def initialize(info = {})
19
super(update_info(info,
20
'Name' => 'MySQL Login Utility',
21
'Description' => 'This module simply queries the MySQL instance for a specific user/pass (default is root with blank).',
22
'Author' => [ 'Bernardo Damele A. G. <bernardo.damele[at]gmail.com>' ],
23
'License' => MSF_LICENSE,
24
'References' =>
25
[
26
[ 'CVE', '1999-0502'] # Weak password
27
],
28
# some overrides from authbrute since there is a default username and a blank password
29
'DefaultOptions' =>
30
{
31
'USERNAME' => 'root',
32
'BLANK_PASSWORDS' => true,
33
'CreateSession' => false
34
}
35
))
36
37
register_options(
38
[
39
Opt::Proxies,
40
OptBool.new('CreateSession', [false, 'Create a new session for every successful login', false])
41
])
42
43
if framework.features.enabled?(Msf::FeatureManager::MYSQL_SESSION_TYPE)
44
add_info('New in Metasploit 6.4 - The %grnCreateSession%clr option within this module can open an interactive session')
45
else
46
options_to_deregister = %w[CreateSession]
47
end
48
deregister_options(*options_to_deregister)
49
end
50
51
# @return [FalseClass]
52
def create_session?
53
if framework.features.enabled?(Msf::FeatureManager::MYSQL_SESSION_TYPE)
54
datastore['CreateSession']
55
else
56
false
57
end
58
end
59
60
def target
61
[rhost,rport].join(":")
62
end
63
64
def run
65
results = super
66
logins = results.flat_map { |_k, v| v[:successful_logins] }
67
sessions = results.flat_map { |_k, v| v[:successful_sessions] }
68
print_status("Bruteforce completed, #{logins.size} #{logins.size == 1 ? 'credential was' : 'credentials were'} successful.")
69
return results unless framework.features.enabled?(Msf::FeatureManager::MYSQL_SESSION_TYPE)
70
71
if create_session?
72
print_status("#{sessions.size} MySQL #{sessions.size == 1 ? 'session was' : 'sessions were'} opened successfully.")
73
else
74
print_status('You can open an MySQL session with these credentials and %grnCreateSession%clr set to true')
75
end
76
results
77
end
78
79
def run_host(ip)
80
begin
81
if mysql_version_check("4.1.1") # Pushing down to 4.1.1.
82
cred_collection = build_credential_collection(
83
username: datastore['USERNAME'],
84
password: datastore['PASSWORD']
85
)
86
87
scanner = Metasploit::Framework::LoginScanner::MySQL.new(
88
configure_login_scanner(
89
cred_details: cred_collection,
90
stop_on_success: datastore['STOP_ON_SUCCESS'],
91
bruteforce_speed: datastore['BRUTEFORCE_SPEED'],
92
connection_timeout: 30,
93
max_send_size: datastore['TCP::max_send_size'],
94
send_delay: datastore['TCP::send_delay'],
95
framework: framework,
96
framework_module: self,
97
use_client_as_proof: create_session?,
98
ssl: datastore['SSL'],
99
ssl_version: datastore['SSLVersion'],
100
ssl_verify_mode: datastore['SSLVerifyMode'],
101
ssl_cipher: datastore['SSLCipher'],
102
local_port: datastore['CPORT'],
103
local_host: datastore['CHOST']
104
)
105
)
106
107
successful_logins = []
108
successful_sessions = []
109
scanner.scan! do |result|
110
credential_data = result.to_h
111
credential_data.merge!(
112
module_fullname: self.fullname,
113
workspace_id: myworkspace_id
114
)
115
if result.success?
116
credential_core = create_credential(credential_data)
117
credential_data[:core] = credential_core
118
create_credential_login(credential_data)
119
120
print_brute :level => :good, :ip => ip, :msg => "Success: '#{result.credential}'"
121
successful_logins << result
122
123
if create_session?
124
begin
125
successful_sessions << session_setup(result)
126
rescue ::StandardError => e
127
elog('Failed to setup the session', error: e)
128
print_brute level: :error, ip: ip, msg: "Failed to setup the session - #{e.class} #{e.message}"
129
result.connection.close unless result.connection.nil?
130
end
131
end
132
else
133
invalidate_login(credential_data)
134
vprint_error "#{ip}:#{rport} - LOGIN FAILED: #{result.credential} (#{result.status}: #{result.proof})"
135
end
136
end
137
138
else
139
vprint_error "#{target} - Unsupported target version of MySQL detected. Skipping."
140
end
141
rescue ::Rex::ConnectionError, ::EOFError => e
142
vprint_error "#{target} - Unable to connect: #{e.to_s}"
143
end
144
{ successful_logins: successful_logins, successful_sessions: successful_sessions }
145
end
146
147
# Tmtm's rbmysql is only good for recent versions of mysql, according
148
# to http://www.tmtm.org/en/mysql/ruby/. We'll need to write our own
149
# auth checker for earlier versions. Shouldn't be too hard.
150
# This code is essentially the same as the mysql_version module, just less
151
# whitespace and returns false on errors.
152
def mysql_version_check(target="5.0.67") # Oldest the library claims.
153
begin
154
s = connect(false)
155
data = s.get
156
disconnect(s)
157
rescue ::Rex::ConnectionError, ::EOFError => e
158
raise e
159
rescue ::Exception => e
160
vprint_error("#{rhost}:#{rport} error checking version #{e.class} #{e}")
161
return false
162
end
163
offset = 0
164
l0, l1, l2 = data[offset, 3].unpack('CCC')
165
return false if data.length < 3
166
length = l0 | (l1 << 8) | (l2 << 16)
167
# Read a bad amount of data
168
return if length != (data.length - 4)
169
offset += 4
170
proto = data[offset, 1].unpack('C')[0]
171
# Error condition
172
return if proto == 255
173
offset += 1
174
version = data[offset..-1].unpack('Z*')[0]
175
report_service(:host => rhost, :port => rport, :name => "mysql", :info => version)
176
short_version = version.split('-')[0]
177
vprint_good "#{rhost}:#{rport} - Found remote MySQL version #{short_version}"
178
int_version(short_version) >= int_version(target)
179
end
180
181
# Takes a x.y.z version number and turns it into an integer for
182
# easier comparison. Useful for other things probably so should
183
# get moved up to Rex. Allows for version increments up to 0xff.
184
def int_version(str)
185
int = 0
186
begin # Okay, if you're not exactly what I expect, just return 0
187
return 0 unless str =~ /^[0-9]+\x2e[0-9]+/
188
digits = str.split(".")[0,3].map {|x| x.to_i}
189
digits[2] ||= 0 # Nil protection
190
int = (digits[0] << 16)
191
int += (digits[1] << 8)
192
int += digits[2]
193
rescue
194
return int
195
end
196
end
197
198
# @param [Metasploit::Framework::LoginScanner::Result] result
199
# @return [Msf::Sessions::MySQL]
200
def session_setup(result)
201
return unless (result.connection && result.proof)
202
203
my_session = Msf::Sessions::MySQL.new(result.connection, { client: result.proof, **result.proof.detect_platform_and_arch })
204
merge_me = {
205
'USERPASS_FILE' => nil,
206
'USER_FILE' => nil,
207
'PASS_FILE' => nil,
208
'USERNAME' => result.credential.public,
209
'PASSWORD' => result.credential.private
210
}
211
212
start_session(self, nil, merge_me, false, my_session.rstream, my_session)
213
end
214
end
215
216