Skip to content

Commit 55a8d67

Browse files
committed
Add Rex Proto MySQL Client
1 parent 26214cb commit 55a8d67

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

lib/metasploit/framework/login_scanner/mysql.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'mysql'
33
require 'metasploit/framework/login_scanner/base'
44
require 'metasploit/framework/login_scanner/rex_socket'
5+
require 'rex/proto/mysql/client'
56

67
module Metasploit
78
module Framework
@@ -39,7 +40,7 @@ def attempt_login(credential)
3940
disconnect if self.sock
4041
self.sock = connect
4142

42-
mysql_conn = ::Mysql.connect(host, credential.public, credential.private, '', port, io: self.sock)
43+
mysql_conn = ::Rex::Proto::MySQL::Client.connect(host, credential.public, credential.private, '', port, io: self.sock)
4344

4445
rescue ::SystemCallError, Rex::ConnectionError => e
4546
result_options.merge!({

lib/msf/core/exploit/remote/mysql.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
###
1313

1414

15-
require 'mysql'
15+
require 'rex/proto/mysql/client'
1616

1717
module Msf
1818
module Exploit::Remote::MYSQL
@@ -38,10 +38,10 @@ def initialize(info = {})
3838

3939
def mysql_login(user='root', pass='', db=nil)
4040
disconnect if sock
41-
connect
41+
self.sock = connect
4242

4343
begin
44-
self.mysql_conn = ::Mysql.connect(rhost, user, pass, db, rport, io: sock)
44+
self.mysql_conn = ::Rex::Proto::MySQL::Client.connect(rhost, user, pass, db, rport, io: self.sock)
4545
# Deprecating this in favor off `mysql_conn`
4646
@mysql_handle = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :mysql_conn, :@mysql_handle, ActiveSupport::Deprecation.new)
4747

lib/rex/proto/mysql/client.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 database this client is currently connected to
22+
def current_database
23+
# Current database is stored as an array under the type 1 key.
24+
session_track.fetch(1, ['']).first
25+
end
26+
end
27+
end
28+
end
29+
end

modules/auxiliary/scanner/mysql/mysql_authbypass_hashdump.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Current source: https://github.com/rapid7/metasploit-framework
44
##
55

6+
require 'rex/proto/mysql/client'
7+
68
class MetasploitModule < Msf::Auxiliary
79
include Msf::Exploit::Remote::MYSQL
810
include Msf::Auxiliary::Report
@@ -62,7 +64,7 @@ def run_host(ip)
6264
begin
6365
socket = connect(false)
6466
close_required = true
65-
mysql_client = ::Mysql.connect(rhost, username, password, nil, rport, io: socket)
67+
mysql_client = ::Rex::Proto::MySQL::Client.connect(rhost, username, password, nil, rport, io: socket)
6668
results << mysql_client
6769
close_required = false
6870

@@ -118,7 +120,7 @@ def run_host(ip)
118120
# Create our socket and make the connection
119121
close_required = true
120122
s = connect(false)
121-
mysql_client = ::Mysql.connect(rhost, username, password, nil, rport, io: s)
123+
mysql_client = ::Rex::Proto::MySQL::Client.connect(rhost, username, password, nil, rport, io: s)
122124

123125
print_good "#{rhost}:#{rport} Successfully bypassed authentication after #{count} attempts. URI: mysql://#{username}:#{password}@#{rhost}:#{rport}"
124126
results << mysql_client
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: binary -*-
2+
3+
require 'spec_helper'
4+
require 'rex/proto/mysql/client'
5+
6+
RSpec.describe Rex::Proto::MySQL::Client do
7+
it { is_expected.to be_a ::Mysql }
8+
9+
[
10+
{ method: :peerhost, return_type: String },
11+
{ method: :peerport, return_type: Integer },
12+
{ method: :database_name, return_type: String }
13+
].each do |method_hash|
14+
it { is_expected.to respond_to method_hash[:method] }
15+
end
16+
end

0 commit comments

Comments
 (0)