国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP整站防注入程序

2024-05-04 21:58:48
字體:
供稿:網(wǎng)友

這是一個(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如下:

  1. <?PHP  
  2. //PHP整站防注入程序,需要在公共文件中require_once本文件  
  3. //判斷magic_quotes_gpc狀態(tài) 
  4.  
  5.  代碼如下 復(fù)制代碼  
  6. if (@get_magic_quotes_gpc ()) {  
  7. $_GET = sec ( $_GET );  
  8. $_POST = sec ( $_POST );  
  9. $_COOKIE = sec ( $_COOKIE );  
  10. $_FILES = sec ( $_FILES );  
  11. }  
  12. $_SERVER = sec ( $_SERVER );  
  13. function sec(&$array) {  
  14. //如果是數(shù)組,遍歷數(shù)組,遞歸調(diào)用  
  15. if (is_array ( $array )) {  
  16. foreach ( $array as $k => $v ) {  
  17. $array [$k] = sec ( $v );  
  18. //開源代碼Vevb.com 
  19. else if (is_string ( $array )) {  
  20. //使用addslashes函數(shù)來處理  
  21. $array = addslashes ( $array );  
  22. else if (is_numeric ( $array )) {  
  23. $array = intval ( $array );  
  24. }  
  25. return $array;  
  26. }?> 

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ù),代碼如下:

  1. function num_check($id) {  
  2. if (! $id) {  
  3. die ( '參數(shù)不能為空!' );  
  4. //是否為空的判斷  
  5. else if (inject_check ( $id )) {  
  6. die ( '非法參數(shù)' );  
  7. //注入判斷  
  8. else if (! is_numetic ( $id )) {  
  9. die ( '非法參數(shù)' );  
  10. }  
  11. //數(shù)字判斷  
  12. $id = intval ( $id );  
  13. //整型化  
  14. return $id;  
  15.  
  16.  
  17. //字符過濾函數(shù)  
  18. function str_check($str) {  
  19. if (inject_check ( $str )) {  
  20. die ( '非法參數(shù)' );  
  21. }  
  22. //注入判斷  
  23. $str = htmlspecialchars ( $str );  
  24. //轉(zhuǎn)換html  
  25. return $str;  
  26. }  
  27. function search_check($str) {  
  28. $str = str_replace ( "_""_"$str );  
  29. //把"_"過濾掉  
  30. $str = str_replace ( "%""%"$str );  
  31. //把"%"過濾掉  
  32. $str = htmlspecialchars ( $str );  
  33. //轉(zhuǎn)換html  
  34. return $str;  
  35. }  
  36. //表單過濾函數(shù)  
  37. function post_check($str$min$max) {  
  38. if (isset ( $min ) && strlen ( $str ) < $min) {  
  39. die ( '最少$min字節(jié)' );  
  40. else if (isset ( $max ) && strlen ( $str ) > $max) {  
  41. die ( '最多$max字節(jié)' );  
  42. }  
  43. 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注入漏洞,代碼如下:

  1. //防注入函數(shù)  
  2. function inject_check($sql_str) {  
  3. return eregi ( 'select|inert|update|delete|'|/*|*|../|./|UNION|into|load_file|outfile', $sql_str );  
  4. // m.survivalescaperooms.com 進(jìn)行過濾,防注入  
  5.  
  6. function stripslashes_array(&$array) {  
  7. if (is_array ( $array )) {  
  8. foreach ( $array as $k => $v ) {  
  9. $array [$k] = stripslashes_array ( $v );  
  10. }  
  11. else if (is_string ( $array )) {  
  12. $array = stripslashes ( $array );  
  13. }  
  14. return $array;  

好了文章介紹到的防注入也算是比較全面的,大家可以測試或更出更好的方法.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 淅川县| 永修县| 蓝田县| 安阳县| 富裕县| 新化县| 海林市| 泾源县| 伊川县| 宁陕县| 安丘市| 黄石市| 普兰店市| 华阴市| 寿阳县| 通榆县| 顺平县| 综艺| 府谷县| 永善县| 鹤壁市| 榆林市| 嘉祥县| 上饶县| 延吉市| 内乡县| 九龙坡区| 云和县| 台湾省| 固阳县| 金坛市| 英山县| 雅江县| 鄂托克旗| 广丰县| 河南省| 壶关县| 海丰县| 鄯善县| 泽普县| 晋中市|