Skip to content

Commit

Permalink
Add MySQL Arch & Platform detection by query
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Apr 8, 2024
1 parent 9982a46 commit d428737
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/msf/base/sessions/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Msf::Sessions::MySQL < Msf::Sessions::Sql
# @param [Hash] opts
def initialize(rstream, opts = {})
@client = opts.fetch(:client)
self.platform = opts.fetch(:platform)
self.arch = opts.fetch(:arch)
self.console = ::Rex::Post::MySQL::Ui::Console.new(self)
super(rstream, opts)
end
Expand Down
33 changes: 33 additions & 0 deletions lib/rex/proto/mysql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ def current_database
# Current database is stored as an array under the type 1 key.
session_track.fetch(1, ['']).first
end

def map_compile_os_to_platform(compile_os)
return Msf::Platform::Unknown.realname if compile_os.blank?

compile_os = compile_os.downcase.encode(::Encoding::BINARY)

if compile_os.match?('linux')
platform = Msf::Platform::Linux
elsif compile_os.match?('unix')
platform = Msf::Platform::Unix
elsif compile_os.match?(/(darwin|mac|osx)/)
platform = Msf::Platform::OSX
elsif compile_os.match?('win')
platform = Msf::Platform::Windows
else
platform = Msf::Platform::Unknown
end

platform.realname
end

# @return [Hash] Detect the platform and architecture of the MySQL server:
# * :arch [String] The server architecture.
# * :platform [String] The server platform.
def detect_platform_and_arch
result = {}

server_vars = query("show variables where variable_name in ('version_compile_machine', 'version_compile_os')").entries.to_h
result[:arch] = server_vars['version_compile_machine']
result[:platform] = map_compile_os_to_platform(server_vars['version_compile_os'])

result
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion modules/auxiliary/scanner/mysql/mysql_login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def int_version(str)
def session_setup(result)
return unless (result.connection && result.proof)

my_session = Msf::Sessions::MySQL.new(result.connection, { client: result.proof })
my_session = Msf::Sessions::MySQL.new(result.connection, { client: result.proof, **result.proof.detect_platform_and_arch })
merge_me = {
'USERPASS_FILE' => nil,
'USER_FILE' => nil,
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/msf/base/sessions/mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

RSpec.describe Msf::Sessions::MySQL do
let(:client) { instance_double(::Rex::Proto::MySQL::Client) }
let(:opts) { { client: client } }
let(:opts) { { client: client, platform: 'Linux', arch: 'x86_64' } }
let(:console_class) { Rex::Post::MySQL::Ui::Console }
let(:user_input) { instance_double(Rex::Ui::Text::Input::Readline) }
let(:user_output) { instance_double(Rex::Ui::Text::Output::Stdio) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:address) { '192.0.2.1' }
let(:port) { '3306' }
let(:peerinfo) { "#{address}:#{port}" }
let(:session) { Msf::Sessions::MySQL.new(nil, { client: client }) }
let(:session) { Msf::Sessions::MySQL.new(nil, { client: client, platform: 'Linux', arch: 'x86_64' }) }
let(:console) do
console = Rex::Post::MySQL::Ui::Console.new(session)
console.disable_output = true
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/rex/proto/mysql/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,25 @@
end
end
end

describe '#map_compile_os_to_platform' do
[
{ info: 'linux', expected: 'Linux' },
{ info: 'linux2.6', expected: 'Linux' },
{ info: 'debian-linux-gnu', expected: 'Linux' },
{ info: 'win', expected: 'Windows' },
{ info: 'windows', expected: 'Windows' },
{ info: 'darwin', expected: 'OSX' },
{ info: 'osx', expected: 'OSX' },
{ info: 'macos', expected: 'OSX' },
{ info: 'unix', expected: 'Unix' },
{ info: '', expected: 'Unknown' },
{ info: 'blank', expected: 'Unknown' },
{ info: nil, expected: 'Unknown' },
].each do |test|
it "correctly identifies '#{test[:info]}' as '#{test[:expected]}'" do
expect(subject.map_compile_os_to_platform(test[:info])).to eq(test[:expected])
end
end
end
end

0 comments on commit d428737

Please sign in to comment.