Files
metasploit-gs/modules/auxiliary/scanner/postgres/postgres_version.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
4.0 KiB
Ruby
Raw Normal View History

##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::Postgres
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::Report
2024-02-12 11:52:48 +00:00
include Msf::OptionalSession::PostgreSQL
2013-08-30 16:28:54 -05:00
# Creates an instance of this module.
def initialize(info = {})
2025-06-20 13:20:44 +01:00
super(
update_info(
info,
'Name' => 'PostgreSQL Version Probe',
'Description' => %q{
Enumerates the version of PostgreSQL servers.
},
'Author' => [ 'todb' ],
'License' => MSF_LICENSE,
'References' => [
2022-01-23 15:28:32 -05:00
[ 'URL', 'https://www.postgresql.org/' ]
],
'Notes' => {
2025-06-23 12:43:46 +01:00
'Reliability' => UNKNOWN_RELIABILITY,
'Stability' => UNKNOWN_STABILITY,
'SideEffects' => UNKNOWN_SIDE_EFFECTS
}
2025-06-20 13:20:44 +01:00
)
)
2013-08-30 16:28:54 -05:00
register_options([ ]) # None needed.
2013-08-30 16:28:54 -05:00
deregister_options('SQL', 'RETURN_ROWSET')
end
2013-08-30 16:28:54 -05:00
# Loops through each host in turn. Note the current IP address is both
# ip and datastore['RHOST']
def run_host(ip)
2024-01-24 13:47:22 +00:00
self.postgres_conn = session.client if session
user = datastore['USERNAME']
pass = postgres_password
2025-06-20 13:20:44 +01:00
do_fingerprint(user, pass, datastore['DATABASE'])
end
2013-08-30 16:28:54 -05:00
# Alias for RHOST
def rhost
2024-03-22 16:50:01 +00:00
postgres_conn&.peerhost || datastore['RHOST']
end
2013-08-30 16:28:54 -05:00
2010-03-30 17:57:22 +00:00
# Alias for RPORT
def rport
2024-03-22 16:50:01 +00:00
postgres_conn&.peerport || datastore['RPORT']
end
2013-08-30 16:28:54 -05:00
def report_cred(opts)
service_data = {
address: opts[:ip],
port: opts[:port],
service_name: opts[:service_name],
protocol: 'tcp',
workspace_id: myworkspace_id
}
credential_data = {
origin_type: :service,
module_fullname: fullname,
username: opts[:user],
private_data: opts[:password],
private_type: :password
}.merge(service_data)
login_data = {
core: create_credential(credential_data),
status: Metasploit::Model::Login::Status::UNTRIED,
proof: opts[:proof]
}.merge(service_data)
create_credential_login(login_data)
end
2025-06-20 13:20:44 +01:00
def do_fingerprint(user = nil, pass = nil, database = nil)
begin
msg = "#{rhost}:#{rport} Postgres -"
password = pass || postgres_password
2024-01-24 13:47:22 +00:00
vprint_status("#{msg} Trying username:'#{user}' with password:'#{password}' against #{rhost}:#{rport} on database '#{database}'") unless postgres_conn
result = postgres_fingerprint(
:db => database,
:username => user,
:password => password
)
if result[:auth]
2024-03-22 16:50:01 +00:00
vprint_good "#{rhost}:#{rport} Postgres - Logged in to '#{database}' with '#{user}':'#{password}'" unless session
print_status "#{rhost}:#{rport} Postgres - Version #{result[:auth]} (Post-Auth)"
elsif result[:preauth]
2024-03-22 16:50:01 +00:00
print_good "#{rhost}:#{rport} Postgres - Version #{result[:preauth]} (Pre-Auth)"
else # It's something we don't know yet
2024-03-22 16:50:01 +00:00
vprint_status "#{rhost}:#{rport} Postgres - Authentication Error Fingerprint: #{result[:unknown]}"
print_status "#{rhost}:#{rport} Postgres - Version Unknown (Pre-Auth)"
end
2013-08-30 16:28:54 -05:00
# Reporting
report_service(
2024-03-22 16:50:01 +00:00
:host => rhost,
:port => rport,
:name => "postgres",
:info => result.values.first
)
2013-08-30 16:28:54 -05:00
if self.postgres_conn
report_cred(
2024-03-22 16:50:01 +00:00
ip: rhost,
port: rport,
service_name: 'postgres',
user: user,
password: password,
proof: "postgres_conn = #{self.postgres_conn.inspect}"
)
end
2013-08-30 16:28:54 -05:00
if result[:unknown]
report_note(
2024-03-22 16:50:01 +00:00
:host => rhost,
:proto => 'tcp',
2011-02-22 20:49:44 +00:00
:sname => 'postgres',
2024-03-22 16:50:01 +00:00
:port => rport,
2011-06-03 00:49:45 +00:00
:ntype => 'postgresql.fingerprint',
:data => { :unknown_pre_auth_fingerprint => result[:unknown] }
)
end
2013-08-30 16:28:54 -05:00
# Logout
2024-01-24 13:47:22 +00:00
postgres_logout if self.postgres_conn && session.blank?
rescue Rex::ConnectionError
vprint_error "#{rhost}:#{rport} Connection Error: #{$!}"
return :done
end
end
end