這是一個(gè)考慮比較全面的php和sql結(jié)合的防注入程序,在php方便主要對get,post,cooke,files進(jìn)行了過濾,在sql中我們就對delete,update一些查詢命令進(jìn)行檢測過濾.
SQL注入攻擊的總體思路
·發(fā)現(xiàn)SQL注入位置;·判斷后臺數(shù)據(jù)庫類型;·確定XP_CMDSHELL可執(zhí)行情況·發(fā)現(xiàn)WEB虛擬目錄·上傳ASP,php,jsp木馬;·得到管理員權(quán)限;
PHP整站防注入程序?qū)嵗a如下:
- <?PHP
- //PHP整站防注入程序,需要在公共文件中require_once本文件
- //判斷magic_quotes_gpc狀態(tài)
- 代碼如下 復(fù)制代碼
- if (@get_magic_quotes_gpc ()) {
- $_GET = sec ( $_GET );
- $_POST = sec ( $_POST );
- $_COOKIE = sec ( $_COOKIE );
- $_FILES = sec ( $_FILES );
- }
- $_SERVER = sec ( $_SERVER );
- function sec(&$array) {
- //如果是數(shù)組,遍歷數(shù)組,遞歸調(diào)用
- if (is_array ( $array )) {
- foreach ( $array as $k => $v ) {
- $array [$k] = sec ( $v );
- } //開源代碼Vevb.com
- } else if (is_string ( $array )) {
- //使用addslashes函數(shù)來處理
- $array = addslashes ( $array );
- } else if (is_numeric ( $array )) {
- $array = intval ( $array );
- }
- return $array;
- }?>
1、整型參數(shù)的判斷
當(dāng)輸入的參數(shù)YY為整型時(shí),通常abc.asp中SQL語句原貌大致如下:
select * from 表名 where 字段=YY,所以可以用以下步驟測試SQL注入是否存在。
①HTTP://xxx.xxx.xxx/abc.asp?p=YY’(附加一個(gè)單引號),此時(shí)abc.ASP中的SQL語句變成了
select * from 表名 where 字段=YY’,abc.asp運(yùn)行異常;
②HTTP://xxx.xxx.xxx/abc.asp?p=YY and 1=1,abc.asp運(yùn)行正常,而且與HTTP://xxx.xxx.xxx/abc.asp?p=YY運(yùn)行結(jié)果相同;
③HTTP://xxx.xxx.xxx/abc.asp?p=YY and 1=2,abc.asp運(yùn)行異常;
如果以上三步全面滿足,abc.asp中一定存在SQL注入漏洞.
綜合上面我們寫一個(gè)整型過濾函數(shù),代碼如下:
- function num_check($id) {
- if (! $id) {
- die ( '參數(shù)不能為空!' );
- } //是否為空的判斷
- else if (inject_check ( $id )) {
- die ( '非法參數(shù)' );
- } //注入判斷
- else if (! is_numetic ( $id )) {
- die ( '非法參數(shù)' );
- }
- //數(shù)字判斷
- $id = intval ( $id );
- //整型化
- return $id;
- }
- //字符過濾函數(shù)
- function str_check($str) {
- if (inject_check ( $str )) {
- die ( '非法參數(shù)' );
- }
- //注入判斷
- $str = htmlspecialchars ( $str );
- //轉(zhuǎn)換html
- return $str;
- }
- function search_check($str) {
- $str = str_replace ( "_", "_", $str );
- //把"_"過濾掉
- $str = str_replace ( "%", "%", $str );
- //把"%"過濾掉
- $str = htmlspecialchars ( $str );
- //轉(zhuǎn)換html
- return $str;
- }
- //表單過濾函數(shù)
- function post_check($str, $min, $max) {
- if (isset ( $min ) && strlen ( $str ) < $min) {
- die ( '最少$min字節(jié)' );
- } else if (isset ( $max ) && strlen ( $str ) > $max) {
- die ( '最多$max字節(jié)' );
- }
- return stripslashes_array ( $str );
- }
當(dāng)輸入的參數(shù)YY為字符串時(shí),通常abc.asp中SQL語句原貌大致如下:
select * from 表名 where 字段='YY',所以可以用以下步驟測試SQL注入是否存在。
①HTTP://xxx.xxx.xxx/abc.asp?p=YY’(附加一個(gè)單引號),此時(shí)abc.ASP中的SQL語句變成了
select * from 表名 where 字段=YY’,abc.asp運(yùn)行異常;
②HTTP://xxx.xxx.xxx/abc.asp?p=YY&;nb ... 39;1'='1',abc.asp運(yùn)行正常,而且與HTTP://xxx.xxx.xxx/abc.asp?p=YY運(yùn)行結(jié)果相同;
③HTTP://xxx.xxx.xxx/abc.asp?p=YY&;nb ... 39;1'='2',abc.asp運(yùn)行異常;
如果以上三步全面滿足,abc.asp中一定存在SQL注入漏洞,代碼如下:
- //防注入函數(shù)
- function inject_check($sql_str) {
- return eregi ( 'select|inert|update|delete|'|/*|*|../|./|UNION|into|load_file|outfile', $sql_str );
- // m.survivalescaperooms.com 進(jìn)行過濾,防注入
- }
- 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;
- }
好了文章介紹到的防注入也算是比較全面的,大家可以測試或更出更好的方法.
新聞熱點(diǎn)
疑難解答