Skip to content

Commit 4fc9e26

Browse files
Land rapid7#18862, Align SQL sessions peerhost and peerport
2 parents e912509 + 3c8f43e commit 4fc9e26

34 files changed

+262
-259
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ def mysql_login(user='root', pass='', db=nil)
6262
return false
6363
end
6464

65-
vprint_good "#{mysql_conn.host}:#{mysql_conn.port} MySQL - Logged in to '#{db}' with '#{user}':'#{pass}'"
65+
vprint_good "#{mysql_conn.peerhost}:#{mysql_conn.peerport} MySQL - Logged in to '#{db}' with '#{user}':'#{pass}'"
6666

6767
return true
6868
end
6969

7070
def mysql_logoff
71+
temp_rhost = mysql_conn.peerhost if mysql_conn
72+
temp_rport = mysql_conn.peerport if mysql_conn
7173
mysql_conn.close if mysql_conn
7274
disconnect if sock
73-
vprint_status "#{rhost}:#{rport} MySQL - Disconnected"
75+
vprint_status "#{temp_rhost || rhost}:#{temp_rport || rport} MySQL - Disconnected"
7476
end
7577

7678
def mysql_login_datastore
@@ -95,7 +97,7 @@ def mysql_query(sql)
9597
return nil
9698
end
9799

98-
vprint_status "#{mysql_conn.host}:#{mysql_conn.port} MySQL - querying with '#{sql}'"
100+
vprint_status "#{mysql_conn.peerhost}:#{mysql_conn.peerport} MySQL - querying with '#{sql}'"
99101
res
100102
end
101103

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def postgres_login(opts={})
121121
return :connection_refused
122122
end
123123
if self.postgres_conn
124-
print_good "#{self.postgres_conn.address}:#{self.postgres_conn.port} Postgres - Logged in to '#{db}' with '#{username}':'#{password}'" if verbose
124+
print_good "#{self.postgres_conn.peerhost}:#{self.postgres_conn.peerport} Postgres - Logged in to '#{db}' with '#{username}':'#{password}'" if verbose
125125
return :connected
126126
end
127127
end
@@ -130,8 +130,8 @@ def postgres_login(opts={})
130130
#
131131
# @return [void]
132132
def postgres_logout
133-
ip = self.postgres_conn.address
134-
port = self.postgres_conn.port
133+
ip = self.postgres_conn.peerhost
134+
port = self.postgres_conn.peerport
135135
verbose = datastore['VERBOSE']
136136

137137
if self.postgres_conn
@@ -158,7 +158,7 @@ def postgres_query(sql=nil,doprint=false)
158158

159159
if self.postgres_conn
160160
sql ||= datastore['SQL']
161-
vprint_status "#{self.postgres_conn.address}:#{self.postgres_conn.port} Postgres - querying with '#{sql}'"
161+
vprint_status "#{self.postgres_conn.peerhost}:#{self.postgres_conn.peerport} Postgres - querying with '#{sql}'"
162162
begin
163163
resp = self.postgres_conn.query(sql)
164164
rescue RuntimeError => e
@@ -196,7 +196,7 @@ def postgres_print_reply(resp=nil,sql=nil)
196196
return :error unless resp.kind_of? Connection::Result
197197

198198
if resp.rows and resp.fields
199-
print_status "#{postgres_conn.address}:#{postgres_conn.port} Rows Returned: #{resp.rows.size}" if verbose
199+
print_status "#{postgres_conn.peerhost}:#{postgres_conn.peerport} Rows Returned: #{resp.rows.size}" if verbose
200200
if resp.rows.size > 0
201201
tbl = Rex::Text::Table.new(
202202
'Indent' => 4,

lib/msf/core/post/common.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ def rhost
3030
session.sock.peerhost
3131
when 'shell', 'powershell'
3232
session.session_host
33-
when 'postgresql', 'mysql'
34-
session.address
3533
end
3634
rescue
3735
return nil
@@ -45,8 +43,6 @@ def rport
4543
session.sock.peerport
4644
when 'shell', 'powershell'
4745
session.session_port
48-
when 'postgresql', 'mysql'
49-
session.port
5046
end
5147
rescue
5248
return nil

lib/postgres/postgres-pr/connection.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,22 @@ def initialize(database, user, password=nil, uri = nil, proxies = nil)
121121
end
122122
end
123123

124-
def address
124+
def peerhost
125125
@conn.peerhost
126126
end
127127

128-
def port
128+
def peerport
129129
@conn.peerport
130130
end
131131

132132
def current_database
133133
@params['database']
134134
end
135135

136+
def peerinfo
137+
"#{peerhost}:#{peerport}"
138+
end
139+
136140
def close
137141
raise "connection already closed" if @conn.nil?
138142
@conn.shutdown

lib/rex/post/mssql/ui/console.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,6 @@ def initialize(session, opts={})
5454
# @return [MSSQL::Client]
5555
attr_reader :client
5656

57-
# @param [Object] val
58-
# @return [String]
59-
def format_prompt(val)
60-
# TODO: Once the client peerhost alignment is landed, extract this out to the generic SQL class.
61-
# prompt = "%und#{session.type} @ #{client.peerhost}:#{client.peerport} (#{current_database})%clr > "
62-
# or
63-
# prompt = "%und#{session.type} @ #{client.peerinfo} (#{current_database})%clr > "
64-
prompt = "%undMSSQL @ #{client.sock.peerinfo} (#{current_database})%clr > "
65-
substitute_colors(prompt, true)
66-
end
67-
6857
protected
6958

7059
attr_writer :session, :client # :nodoc:

lib/rex/post/mysql/ui/console.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ def initialize(session)
5151
# @return [MySQL::Client]
5252
attr_reader :client
5353

54-
# @param [Object] val
55-
# @return [String]
56-
def format_prompt(val)
57-
prompt = "%undMySQL @ #{client.socket.peerinfo} (#{current_database})%clr > "
58-
substitute_colors(prompt, true)
59-
end
60-
6154
protected
6255

6356
attr_writer :session, :client # :nodoc:

lib/rex/post/postgresql/ui/console.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ def initialize(session)
5353
# @return [PostgreSQL::Client]
5454
attr_reader :client # :nodoc:
5555

56-
def format_prompt(val)
57-
prompt = "%undPostgreSQL @ #{client.conn.peerinfo} (#{current_database})%clr > "
58-
substitute_colors(prompt, true)
59-
end
60-
6156
protected
6257

6358
attr_writer :session, :client # :nodoc:

lib/rex/post/sql/ui/console.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ def interact_with_client(client_dispatcher: nil)
9393
client.reset_ui
9494
end
9595

96+
# @param [Object] val
97+
# @return [String]
98+
def format_prompt(val)
99+
prompt = "%und#{session.type} @ #{client.peerinfo} (#{current_database})%clr > "
100+
substitute_colors(prompt, true)
101+
end
102+
96103
#
97104
# Log that an error occurred.
98105
#

lib/rex/proto/mssql/client.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,14 +661,18 @@ def initial_info_for_envchange(envchange: nil)
661661
self.initial_connection_info[:envs]&.select { |hash| hash[:type] == envchange }&.first || {}
662662
end
663663

664-
def address
664+
def peerhost
665665
rhost
666666
end
667667

668-
def port
668+
def peerport
669669
rport
670670
end
671671

672+
def peerinfo
673+
"#{peerhost}:#{peerport}"
674+
end
675+
672676
protected
673677

674678
def rhost

lib/rex/proto/mysql/client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def peerport
1818
io.remote_address.ip_port
1919
end
2020

21+
# @return [String] The remote peer information containing IP and port
22+
def peerinfo
23+
"#{peerhost}:#{peerport}"
24+
end
25+
2126
# @return [String] The database this client is currently connected to
2227
def current_database
2328
# Current database is stored as an array under the type 1 key.

0 commit comments

Comments
 (0)