强大的PHP防SQL注入类,可以过滤敏感参数
這是一個考慮比較全面的php和sql結合的防注入程序,在php方便主要對get,post,cooke,files進行了過濾,在sql中我們就對delete,update一些查詢命令進行檢測過濾。
-
SQL注入攻擊的總體思路
-
發現SQL注入位置;
-
判斷后臺數據庫類型;
-
確定XP_CMDSHELL可執行情況
-
發現WEB虛擬目錄
-
上傳ASP,php,jsp木馬;
-
得到管理員權限;
貼代碼:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //PHP整站防注入程序,需要在公共文件中require_once本文件?? //判斷magic_quotes_gpc狀態? if (@get_magic_quotes_gpc ()) {?? ??? $_GET = sec ( $_GET );?? ??? $_POST = sec ( $_POST );?? ??? $_COOKIE = sec ( $_COOKIE );?? ??? $_FILES = sec ( $_FILES );?? }?? $_SERVER = sec ( $_SERVER );?? function sec(&$array) {?? ??? //如果是數組,遍歷數組,遞歸調用?? ??? if (is_array ( $array )) {?? ??????? foreach ( $array as $k => $v ) {?? ??????????? $array [$k] = sec ( $v );?? ??????? }?? ??? } else if (is_string ( $array )) {?? ??????? //使用addslashes函數來處理?? ??????? $array = addslashes ( $array );?? ??? } else if (is_numeric ( $array )) {?? ??????? $array = intval ( $array );?? ??? }?? ??? return $array;?? } |
?
1、整型參數的判斷
當輸入的參數YY為整型時,通常abc.asp中SQL語句原貌大致如下:
select * from 表名 where 字段=YY,所以可以用以下步驟測試SQL注入是否存在。
①HTTP://xxx.xxx.xxx/abc.asp?p=YY’(附加一個單引號),此時abc.ASP中的SQL語句變成了
select * from 表名 where 字段=YY’,abc.asp運行異常;
②HTTP://xxx.xxx.xxx/abc.asp?p=YY and 1=1, abc.asp運行正常,而且與HTTP://xxx.xxx.xxx/abc.asp?p=YY運行結果相同;
③HTTP://xxx.xxx.xxx/abc.asp?p=YY and 1=2, abc.asp運行異常;
如果以上三步全面滿足,abc.asp中一定存在SQL注入漏洞。
綜合上面我們寫一個整型過濾函數:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | function num_check($id) {? ??? if (! $id) {? ??????? die ( '參數不能為空!' );? ??? } //是否為空的判斷? ??? else if (inject_check ( $id )) {? ??????? die ( '非法參數' );? ??? } //注入判斷? ??? else if (! is_numetic ( $id )) {? ??????? die ( '非法參數' );? ??? }? ??? //數字判斷? ??? $id = intval ( $id );? ??? //整型化? ??? return $id;? } ?????? //字符過濾函數? function str_check($str) {? ??? if (inject_check ( $str )) {? ??????? die ( '非法參數' );? ??? }? ??? //注入判斷? ??? $str = htmlspecialchars ( $str );? ??? //轉換html? ??? return $str;? }? function search_check($str) {? ??? $str = str_replace ( "_", "_", $str );? ??? //把"_"過濾掉? ??? $str = str_replace ( "%", "%", $str );? ??? //把"%"過濾掉? ??? $str = htmlspecialchars ( $str );? ??? //轉換html? ??? return $str;? }? //表單過濾函數? function post_check($str, $min, $max) {? ??? if (isset ( $min ) && strlen ( $str ) < $min) {? ??????? die ( '最少$min字節' );? ??? } else if (isset ( $max ) && strlen ( $str ) > $max) {? ??????? die ( '最多$max字節' );? ??? }? ??? return stripslashes_array ( $str );? } |
當輸入的參數YY為字符串時,通常abc.php中SQL語句原貌大致如下:
select * from 表名 where 字段='YY',所以可以用以下步驟測試SQL注入是否存在。
①HTTP://xxx.xxx.xxx/abc.php?p=YY’(附加一個單引號),此時abc.ASP中的SQL語句變成了
select * from 表名 where 字段=YY’,abc.asp運行異常;
②HTTP://xxx.xxx.xxx/abc.php?p=YY&;nb ... 39;1'='1', abc.php運行正常,而且與HTTP://xxx.xxx.xxx/abc.asp?p=YY運行結果相同;
③HTTP://xxx.xxx.xxx/abc.php?p=YY&;nb ... 39;1'='2', abc.php運行異常;
如果以上三步全面滿足,abc.asp中一定存在SQL注入漏洞。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //防注入函數? function inject_check($sql_str) {? ??? return eregi ( 'select|inert|update|delete|'|/*|*|../|./|UNION|into|load_file|outfile', $sql_str );? ??? // 進行過濾,防注入 } function stripslashes_array(&$array) {? ??? if (is_array ( $array )) {? ??????? foreach ( $array as $k => $v ) {? ??????????? $array [$k] = stripslashes_array ( $v );? ??????? }? ??? } else if (is_string ( $array )) {? ??????? $array = stripslashes ( $array );? ??? }? ??? return $array;? } |
轉載于:https://www.cnblogs.com/fx2008/archive/2013/03/22/2974880.html
總結
以上是生活随笔為你收集整理的强大的PHP防SQL注入类,可以过滤敏感参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BI推荐8款优秀的app
- 下一篇: 浏览器的内核及版本的判断