CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/mysql/client.rb
Views: 11704
1
require 'mysql'
2
3
module Rex
4
module Proto
5
module MySQL
6
7
# This is a Rex Proto wrapper around the ::Mysql client which is currently coming from the 'ruby-mysql' gem.
8
# The purpose of this wrapper is to provide 'peerhost' and 'peerport' methods to ensure the client interfaces
9
# are consistent between various SQL implementations/protocols.
10
class Client < ::Mysql
11
# @return [String] The remote IP address that the Mysql server is running on
12
def peerhost
13
io.remote_address.ip_address
14
end
15
16
# @return [Integer] The remote port that the Mysql server is running on
17
def peerport
18
io.remote_address.ip_port
19
end
20
21
# @return [String] The remote peer information containing IP and port
22
def peerinfo
23
"#{peerhost}:#{peerport}"
24
end
25
26
# @return [String] The database this client is currently connected to
27
def current_database
28
# Current database is stored as an array under the type 1 key.
29
session_track.fetch(1, ['']).first
30
end
31
32
# List of supported MySQL platforms & architectures:
33
# https://www.mysql.com/support/supportedplatforms/database.html
34
def map_compile_os_to_platform(compile_os)
35
return '' if compile_os.blank?
36
37
compile_os = compile_os.downcase.encode(::Encoding::BINARY)
38
39
if compile_os.match?('linux')
40
platform = Msf::Platform::Linux.realname
41
elsif compile_os.match?('unix')
42
platform = Msf::Platform::Unix.realname
43
elsif compile_os.match?(/(darwin|mac|osx)/)
44
platform = Msf::Platform::OSX.realname
45
elsif compile_os.match?('win')
46
platform = Msf::Platform::Windows.realname
47
elsif compile_os.match?('solaris')
48
platform = Msf::Platform::Solaris.realname
49
else
50
platform = compile_os
51
end
52
53
platform
54
end
55
56
def map_compile_arch_to_architecture(compile_arch)
57
return '' if compile_arch.blank?
58
59
compile_arch = compile_arch.downcase.encode(::Encoding::BINARY)
60
61
if compile_arch.match?('sparc')
62
if compile_arch.include?('64')
63
arch = ARCH_SPARC64
64
else
65
arch = ARCH_SPARC
66
end
67
elsif compile_arch.match?('arm')
68
if compile_arch.match?('64')
69
arch = ARCH_AARCH64
70
elsif compile_arch.match?('arm')
71
arch = ARCH_ARMLE
72
end
73
elsif compile_arch.match?('64')
74
arch = ARCH_X86_64
75
elsif compile_arch.match?('86') || compile_arch.match?('i686')
76
arch = ARCH_X86
77
else
78
arch = compile_arch
79
end
80
81
arch
82
end
83
84
# @return [Hash] Detect the platform and architecture of the MySQL server:
85
# * :arch [String] The server architecture.
86
# * :platform [String] The server platform.
87
def detect_platform_and_arch
88
result = {}
89
90
server_vars = query("show variables where variable_name in ('version_compile_machine', 'version_compile_os')").entries.to_h
91
result[:arch] = map_compile_arch_to_architecture(server_vars['version_compile_machine'])
92
result[:platform] = map_compile_os_to_platform(server_vars['version_compile_os'])
93
94
result
95
end
96
end
97
end
98
end
99
end
100
101