Skip to content

Commit

Permalink
Add Rex::Proto::Mysql client to align peerhost and peerport
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Feb 23, 2024
1 parent faa22a0 commit bb8fc3f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/metasploit/framework/login_scanner/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'mysql'
require 'metasploit/framework/login_scanner/base'
require 'metasploit/framework/login_scanner/rex_socket'
require 'rex/proto/mysql/client'

module Metasploit
module Framework
Expand Down Expand Up @@ -39,7 +40,7 @@ def attempt_login(credential)
disconnect if self.sock
self.sock = connect

mysql_conn = ::Mysql.connect(host, credential.public, credential.private, '', port, io: self.sock)
mysql_conn = ::Rex::Proto::Mysql.connect(host, credential.public, credential.private, '', port, io: self.sock)

rescue ::SystemCallError, Rex::ConnectionError => e
result_options.merge!({
Expand Down
7 changes: 4 additions & 3 deletions lib/msf/core/exploit/remote/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
###


require 'mysql'
require 'rex/proto/mysql/client'

module Msf
module Exploit::Remote::MYSQL
Expand All @@ -38,10 +38,11 @@ def initialize(info = {})

def mysql_login(user='root', pass='', db=nil)
disconnect if sock
connect
self.sock = connect

begin
self.mysql_conn = ::Mysql.connect(rhost, user, pass, db, rport, io: sock)

self.mysql_conn = ::Rex::Proto::Mysql.connect(rhost, user, pass, db, rport, io: self.sock)
# Deprecating this in favor off `mysql_conn`
@mysql_handle = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :mysql_conn, :@mysql_handle, ActiveSupport::Deprecation.new)

Expand Down
16 changes: 0 additions & 16 deletions lib/rex/post/mysql/ui/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ module Ui
# This class provides a shell driven interface to the MySQL client API.
class Console
include Rex::Post::Sql::Ui::Console

# Used to extend/monkey-patch the MySQL Client from the MySQL gem to provide a consistent peer connection API.
module PeerApi

# @return [String] The remote host's IP address.
def peerhost
host
end

# @return [Integer] The remote host's port number.
def peerport
port
end
end

include Rex::Ui::Text::DispatcherShell

# Dispatchers
Expand All @@ -42,7 +27,6 @@ def initialize(session)
self.session = session
self.client = session.client
self.client.socket ||= self.client.io
self.client.extend(PeerApi) unless self.client.is_a?(PeerApi)
prompt = "%undMySQL @ #{client.socket.peerinfo} (#{database_name})%clr"
history_manager = Msf::Config.mysql_session_history
super(prompt, '>', history_manager, nil, :mysql)
Expand Down
22 changes: 22 additions & 0 deletions lib/rex/proto/mysql/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'mysql'

module Rex
module Proto

# This is a Rex Proto wrapper around the ::Mysql client which is currently coming from the 'ruby-mysql' gem.
# The purpose of this wrapper is to provide 'peerhost' and 'peerport' methods to ensure the client interfaces
# are consistent between various SQL implementations/protocols.
class Mysql < ::Mysql
# @return [String] The remote IP address that the Mysql server is running on
def peerhost
io.remote_address.ip_address
end

# @return [Integer] The remote port that the Mysql server is running on
def peerport
io.remote_address.ip_port
end
end
end
end

6 changes: 4 additions & 2 deletions modules/auxiliary/scanner/mysql/mysql_authbypass_hashdump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Current source: https://github.com/rapid7/metasploit-framework
##

require 'rex/proto/mysql/client'

class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::MYSQL
include Msf::Auxiliary::Report
Expand Down Expand Up @@ -62,7 +64,7 @@ def run_host(ip)
begin
socket = connect(false)
close_required = true
mysql_client = ::Mysql.connect(rhost, username, password, nil, rport, io: socket)
mysql_client = ::Rex::Proto::Mysql.connect(rhost, username, password, nil, rport, io: socket)
results << mysql_client
close_required = false

Expand Down Expand Up @@ -118,7 +120,7 @@ def run_host(ip)
# Create our socket and make the connection
close_required = true
s = connect(false)
mysql_client = ::Mysql.connect(rhost, username, password, nil, rport, io: s)
mysql_client = ::Rex::Proto::Mysql.connect(rhost, username, password, nil, rport, io: s)

print_good "#{rhost}:#{rport} Successfully bypassed authentication after #{count} attempts. URI: mysql://#{username}:#{password}@#{rhost}:#{rport}"
results << mysql_client
Expand Down

0 comments on commit bb8fc3f

Please sign in to comment.