diff --git a/lib/msf/base/sessions/mssql.rb b/lib/msf/base/sessions/mssql.rb index 6bea51cb642aa..68df784899e2f 100644 --- a/lib/msf/base/sessions/mssql.rb +++ b/lib/msf/base/sessions/mssql.rb @@ -4,10 +4,6 @@ class Msf::Sessions::MSSQL < Msf::Sessions::Sql - # @return [String] The address MSSQL is running on - attr_accessor :address - # @return [Integer] The port MSSQL is running on - attr_accessor :port attr_reader :framework def initialize(rstream, opts = {}) @@ -40,18 +36,4 @@ def self.can_cleanup_files def desc 'MSSQL' end - - def address - return @address if @address - - @address, @port = client.peerinfo.split(':') - @address - end - - def port - return @port if @port - - @address, @port = client.peerinfo.split(':') - @port - end end diff --git a/lib/msf/base/sessions/mysql.rb b/lib/msf/base/sessions/mysql.rb index c0c100eef461d..13353c4192f37 100644 --- a/lib/msf/base/sessions/mysql.rb +++ b/lib/msf/base/sessions/mysql.rb @@ -36,20 +36,4 @@ def self.can_cleanup_files def desc 'MySQL' end - - # @return [Object] The peer address - def address - return @address if @address - - @address, @port = @client.peerinfo.split(':') - @address - end - - # @return [Object] The peer host - def port - return @port if @port - - @address, @port = @client.peerinfo.split(':') - @port - end end diff --git a/lib/msf/base/sessions/postgresql.rb b/lib/msf/base/sessions/postgresql.rb index 970f3a940cd04..37c72ed8b1e4d 100644 --- a/lib/msf/base/sessions/postgresql.rb +++ b/lib/msf/base/sessions/postgresql.rb @@ -39,18 +39,4 @@ def self.can_cleanup_files def desc 'PostgreSQL' end - - def address - return @address if @address - - @address, @port = @client.peerinfo.split(':') - @address - end - - def port - return @port if @port - - @address, @port = @client.peerinfo.split(':') - @port - end end diff --git a/lib/msf/base/sessions/sql.rb b/lib/msf/base/sessions/sql.rb index 23e3388031825..6b2834fb6d522 100644 --- a/lib/msf/base/sessions/sql.rb +++ b/lib/msf/base/sessions/sql.rb @@ -73,14 +73,14 @@ def desc raise ::NotImplementedError end - # @return [Object] The peer address + # @return [String] The peer address def address - raise ::NotImplementedError + client.peerhost end - # @return [Object] The peer host + # @return [Integer] The peer port def port - raise ::NotImplementedError + client.peerport end # Initializes the console's I/O handles. diff --git a/modules/auxiliary/scanner/mysql/mysql_login.rb b/modules/auxiliary/scanner/mysql/mysql_login.rb index d304dc786bdcc..49bf6b4f7d0d4 100644 --- a/modules/auxiliary/scanner/mysql/mysql_login.rb +++ b/modules/auxiliary/scanner/mysql/mysql_login.rb @@ -200,7 +200,7 @@ def int_version(str) def session_setup(result, client) return unless (result && client) - my_session = Msf::Sessions::MySQL.new(client.socket, { client: client }) + my_session = Msf::Sessions::MySQL.new(client.io, { client: client }) merging = { 'USERPASS_FILE' => nil, 'USER_FILE' => nil, diff --git a/spec/lib/msf/base/sessions/mssql_spec.rb b/spec/lib/msf/base/sessions/mssql_spec.rb index ec961f30d298a..406ecf3c831dc 100644 --- a/spec/lib/msf/base/sessions/mssql_spec.rb +++ b/spec/lib/msf/base/sessions/mssql_spec.rb @@ -15,7 +15,7 @@ let(:description) { 'MSSQL' } let(:can_cleanup_files) { false } let(:address) { '192.0.2.1' } - let(:port) { '1433' } + let(:port) { 1433 } let(:peer_info) { "#{address}:#{port}" } let(:console) do console = Rex::Post::MSSQL::Ui::Console.new(session) @@ -29,6 +29,8 @@ allow(user_input).to receive(:output=) allow(client).to receive(:initial_info_for_envchange).with({ envchange: 1 }).and_return(envchange_result) allow(client).to receive(:peerinfo).and_return(peer_info) + allow(client).to receive(:peerport).and_return(port) + allow(client).to receive(:peerhost).and_return(address) end it_behaves_like 'client session' diff --git a/spec/lib/msf/base/sessions/mysql_spec.rb b/spec/lib/msf/base/sessions/mysql_spec.rb index 397d81c69c126..48a46ecc02a61 100644 --- a/spec/lib/msf/base/sessions/mysql_spec.rb +++ b/spec/lib/msf/base/sessions/mysql_spec.rb @@ -15,7 +15,7 @@ let(:description) { 'MySQL' } let(:can_cleanup_files) { false } let(:address) { '192.0.2.1' } - let(:port) { '3306' } + let(:port) { 3306 } let(:peerinfo) { "#{address}:#{port}" } let(:current_database) { 'database_name' } @@ -23,6 +23,8 @@ allow(user_input).to receive(:output=) allow(user_input).to receive(:intrinsic_shell?).and_return(true) allow(client).to receive(:peerinfo).and_return(peerinfo) + allow(client).to receive(:peerport).and_return(port) + allow(client).to receive(:peerhost).and_return(address) allow(client).to receive(:current_database).and_return(current_database) allow(::Rex::Proto::MySQL::Client).to receive(:connect).and_return(client) end diff --git a/spec/lib/msf/base/sessions/postgresql_spec.rb b/spec/lib/msf/base/sessions/postgresql_spec.rb index 0d1dc3d88cdd0..e655bc7a4c90e 100644 --- a/spec/lib/msf/base/sessions/postgresql_spec.rb +++ b/spec/lib/msf/base/sessions/postgresql_spec.rb @@ -23,6 +23,8 @@ allow(user_input).to receive(:intrinsic_shell?).and_return(true) allow(user_input).to receive(:output=) allow(client).to receive(:peerinfo).and_return(peer_info) + allow(client).to receive(:peerhost).and_return(address) + allow(client).to receive(:peerport).and_return(port) allow(client).to receive(:params).and_return({ 'database' => current_database }) allow(client).to receive(:current_database).and_return(current_database) end diff --git a/spec/lib/msf/base/sessions/smb_spec.rb b/spec/lib/msf/base/sessions/smb_spec.rb index 9fa46b77e8cd6..e3027340ca20f 100644 --- a/spec/lib/msf/base/sessions/smb_spec.rb +++ b/spec/lib/msf/base/sessions/smb_spec.rb @@ -16,7 +16,7 @@ let(:description) { 'SMB' } let(:can_cleanup_files) { false } let(:address) { '192.0.2.1' } - let(:port) { '1337' } + let(:port) { 1337 } let(:peer_info) { "#{address}:#{port}" } before(:each) do diff --git a/spec/lib/msf/core/option_group_spec.rb b/spec/lib/msf/core/option_group_spec.rb index 152370887e6ed..d6669b9429744 100644 --- a/spec/lib/msf/core/option_group_spec.rb +++ b/spec/lib/msf/core/option_group_spec.rb @@ -66,7 +66,7 @@ end it 'validates the options in the group' do - expect { subject.validate(options, datastore) }.not_to raise_error(Msf::OptionValidateError) + expect { subject.validate(options, datastore) }.not_to raise_error end end @@ -78,7 +78,7 @@ end it 'validates the options in the group' do - expect { subject.validate(options, datastore) }.not_to raise_error(Msf::OptionValidateError) + expect { subject.validate(options, datastore) }.not_to raise_error end end @@ -89,7 +89,7 @@ end it 'does not attempt to validate the options' do - expect { subject.validate(options, datastore) }.not_to raise_error(Msf::OptionValidateError) + expect { subject.validate(options, datastore) }.not_to raise_error end end end @@ -118,7 +118,7 @@ end it 'validates the options in the group' do - expect { subject.validate(options, datastore) }.not_to raise_error(Msf::OptionValidateError) + expect { subject.validate(options, datastore) }.not_to raise_error end end @@ -129,7 +129,7 @@ end it 'does not attempt to validate the options' do - expect { subject.validate(options, datastore) }.not_to raise_error(Msf::OptionValidateError) + expect { subject.validate(options, datastore) }.not_to raise_error end end end diff --git a/spec/lib/rex/proto/postgresql/client_spec.rb b/spec/lib/rex/proto/postgresql/client_spec.rb new file mode 100644 index 0000000000000..90d6847ca1df4 --- /dev/null +++ b/spec/lib/rex/proto/postgresql/client_spec.rb @@ -0,0 +1,23 @@ +# -*- coding: binary -*- + +require 'spec_helper' +require 'postgres/postgres-pr/connection' + +RSpec.describe Msf::Db::PostgresPR::Connection do + let(:host) { '127.0.0.1' } + let(:port) { 1234 } + let(:info) { "#{host}:#{port}" } + let(:db_name) { 'my_db_name' } + let(:socket) { double(Rex::Socket, peerhost: host, peerport: port) } + let(:message) { Msf::Db::PostgresPR::ReadyForQuery.new('') } + + subject do + allow(socket).to receive(:<<) + allow(Msf::Db::PostgresPR::Message).to receive(:read).and_return(message) + allow(Rex::Socket).to receive(:create).and_return(socket) + client = described_class.new(db_name, 'username', 'password', "tcp://#{host}:#{port}") + client + end + + it_behaves_like 'session compatible SQL client' +end