Skip to content

Commit

Permalink
Revert remote/postgres verbosity changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Jan 22, 2024
1 parent 9de20d3 commit 77108b7
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions lib/msf/core/exploit/remote/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def postgres_login(opts={})
db = opts[:database] || datastore['DATABASE']
username = opts[:username] || datastore['USERNAME']
password = opts[:password] || datastore['PASSWORD']
ip = opts[:server] || rhost
port = opts[:port] || rport
ip = opts[:server] || datastore['RHOST']
port = opts[:port] || datastore['RHOST']
uri = "tcp://#{ip}:#{port}"

if Rex::Socket.is_ipv6?(ip)
Expand All @@ -110,21 +110,21 @@ def postgres_login(opts={})
rescue RuntimeError => e
case e.to_s.split("\t")[1]
when "C3D000"
vprint_status "#{ip}:#{port} Postgres - Invalid database: #{db} (Credentials '#{username}:#{password}' are OK)"
print_status "#{ip}:#{port} Postgres - Invalid database: #{db} (Credentials '#{username}:#{password}' are OK)" if verbose
return :error_database # Note this means the user:pass is good!
when "C28000", "C28P01"
vprint_error "#{ip}:#{port} Postgres - Invalid username or password: '#{username}':'#{password}'"
print_error "#{ip}:#{port} Postgres - Invalid username or password: '#{username}':'#{password}'" if verbose
return :error_credentials
else
vprint_error "#{ip}:#{port} Postgres - Error: #{e.inspect}"
print_error "#{ip}:#{port} Postgres - Error: #{e.inspect}" if verbose
return :error
end
rescue ::Rex::ConnectionRefused => e
print_error "#{ip}:#{port} Postgres - Connection Refused: #{e}"
print_error "#{ip}:#{port} Postgres - Connection Refused: #{e}" if verbose
return :connection_refused
end
if self.postgres_conn
vprint_good "#{ip}:#{port} Postgres - Logged in to '#{db}' with '#{username}':'#{password}'"
print_good "#{ip}:#{port} Postgres - Logged in to '#{db}' with '#{username}':'#{password}'" if verbose
return :connected
end
end
Expand All @@ -133,17 +133,20 @@ def postgres_login(opts={})
#
# @return [void]
def postgres_logout
ip = datastore['RHOST']
port = datastore['RPORT']
verbose = datastore['VERBOSE']
# Don't log out if we are using a session.
if defined?(session) && session
vprint_status "#{rhost}:#{rport} Postgres - Skipping disconnecting from the session"
print_status "#{ip}:#{port} Postgres - Skipping disconnecting from the session" if verbose
return
end

if self.postgres_conn
self.postgres_conn.close if(self.postgres_conn.kind_of?(Connection) && self.postgres_conn.instance_variable_get("@conn"))
self.postgres_conn = nil
end
vprint_status "#{rhost}:#{rport} Postgres - Disconnected"
print_status "#{ip}:#{port} Postgres - Disconnected" if verbose
end

# If not currently connected, attempt to connect. If an
Expand All @@ -154,6 +157,8 @@ def postgres_logout
# @param doprint [Boolean] Whether the result should be printed
# @return [Hash]
def postgres_query(sql = nil, doprint = false)
ip = datastore['RHOST']
port = datastore['RPORT']
unless self.postgres_conn
result = postgres_login
unless result == :connected
Expand All @@ -163,7 +168,7 @@ def postgres_query(sql = nil, doprint = false)

if self.postgres_conn
sql ||= datastore['SQL']
vprint_status "#{rhost}:#{rport} Postgres - querying with '#{sql}'"
vprint_status "#{ip}:#{port} Postgres - querying with '#{sql}'"
begin
resp = self.postgres_conn.query(sql)
rescue RuntimeError => e
Expand Down Expand Up @@ -197,9 +202,12 @@ def postgres_query(sql = nil, doprint = false)
# Otherwise, create a rowset using Rex::Text::Table (if there's
# more than 0 rows) and return :complete.
def postgres_print_reply(resp=nil,sql=nil)
ip = datastore['RHOST']
port = datastore['RPORT']
verbose = datastore['VERBOSE']
return :error unless resp.kind_of? Connection::Result
if resp.rows and resp.fields
vprint_status "#{rhost}:#{rport} Rows Returned: #{resp.rows.size}"
print_status "#{ip}:#{port} Rows Returned: #{resp.rows.size}" if verbose
if resp.rows.size > 0
tbl = Rex::Text::Table.new(
'Indent' => 4,
Expand All @@ -226,14 +234,15 @@ def postgres_fingerprint(args={})
db = args[:database] || datastore['DATABASE']
username = args[:username] || datastore['USERNAME']
password = args[:password] || datastore['PASSWORD']
ip = args[:server] || rhost
port = args[:port] || rport
rhost = args[:server] || datastore['RHOST']
rport = args[:port] || datastore['RPORT']

uri = "tcp://#{ip}:#{port}"
if Rex::Socket.is_ipv6?(ip)
uri = "tcp://[#{ip}]:#{port}"
uri = "tcp://#{rhost}:#{rport}"
if Rex::Socket.is_ipv6?(rhost)
uri = "tcp://[#{rhost}]:#{rport}"
end

verbose = args[:verbose] || datastore['VERBOSE']
begin
self.postgres_conn = Connection.new(db,username,password,uri)
rescue RuntimeError => e
Expand Down

0 comments on commit 77108b7

Please sign in to comment.