/
mysql_version.rb
94 lines (83 loc) · 2.37 KB
/
mysql_version.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::Tcp
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::Report
include Msf::OptionalSession::MySQL
def initialize
super(
'Name' => 'MySQL Server Version Enumeration',
'Description' => %q{
Enumerates the version of MySQL servers.
},
'Author' => 'kris katterjohn',
'License' => MSF_LICENSE
)
register_options([
Opt::RPORT(3306)
])
end
# Based on my mysql-info NSE script
def run_host(ip)
begin
if session
sql_conn = session.client
version = sql_conn.server_info
print_good("#{sql_conn.peerhost}:#{sql_conn.peerport} is running MySQL #{version}")
report_service(
:host => sql_conn.peerhost,
:port => sql_conn.peerport,
:name => "mysql",
:info => version
)
return
else
socket = connect(false)
data = socket.get_once(-1, 10)
disconnect(socket)
end
if data.nil?
print_error "The connection to #{rhost}:#{rport} timed out"
return
end
rescue ::Rex::ConnectionError, ::EOFError
vprint_error("#{rhost}:#{rport} - Connection failed")
return
rescue ::Exception
print_error("Error: #{$!}")
return
end
offset = 0
l0, l1, l2 = data[offset, 3].unpack('CCC')
length = l0 | (l1 << 8) | (l2 << 16)
# Read a bad amount of data
return if length != (data.length - 4)
offset += 4
proto = data[offset, 1].unpack('C')[0]
# Application-level error condition
if proto == 255
offset += 2
err_msg = Rex::Text.to_hex_ascii(data[offset..-1].to_s)
print_status("#{rhost}:#{rport} is running MySQL, but responds with an error: #{err_msg}")
report_service(
:host => rhost,
:port => rport,
:name => "mysql",
:info => "Error: #{err_msg}"
)
else
offset += 1
version = data[offset..-1].unpack('Z*')[0]
print_good("#{rhost}:#{rport} is running MySQL #{version} (protocol #{proto})")
report_service(
:host => rhost,
:port => rport,
:name => "mysql",
:info => version
)
end
end
end