php簡單的參數過濾代碼學習

來源:互聯網
上載者:User
  1. /**
  2. * 參數過濾代碼
  3. * edit bbs.it-home.org
  4. */
  5. if (@get_magic_quotes_gpc ()) {
  6. $_GET = sec ( $_GET );
  7. $_POST = sec ( $_POST );
  8. $_COOKIE = sec ( $_COOKIE );
  9. $_FILES = sec ( $_FILES );
  10. }
  11. $_SERVER = sec ( $_SERVER );
  12. function sec(&$array) {
  13. //如果是數組,遍曆數組,遞迴調用
  14. if (is_array ( $array )) {
  15. foreach ( $array as $k => $v ) {
  16. $array [$k] = sec ( $v );
  17. }
  18. } else if (is_string ( $array )) {
  19. //使用addslashes函數來處理
  20. $array = addslashes ( $array );
  21. } else if (is_numeric ( $array )) {
  22. $array = intval ( $array );
  23. }
  24. return $array;
  25. }
  26. ?>
複製代碼

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. function num_check($id) {

  2. if (! $id) {
  3. die ( '參數不可為空!' );
  4. } //是否為空白的判斷
  5. else if (inject_check ( $id )) {
  6. die ( '非法參數' );
  7. } //注入判斷
  8. else if (! is_numetic ( $id )) {
  9. die ( '非法參數' );
  10. }
  11. //數字判斷
  12. $id = intval ( $id );
  13. //整型化
  14. return $id;
  15. }

  16. //字元過濾函數

  17. function str_check($str) {
  18. if (inject_check ( $str )) {
  19. die ( '非法參數' );
  20. }
  21. //注入判斷
  22. $str = htmlspecialchars ( $str );
  23. //轉換html
  24. return $str;
  25. }
  26. function search_check($str) {
  27. $str = str_replace ( "_", "_", $str );
  28. //把"_"過濾掉
  29. $str = str_replace ( "%", "%", $str );
  30. //把"%"過濾掉
  31. $str = htmlspecialchars ( $str );
  32. //轉換html
  33. return $str;
  34. }
  35. //表單過濾函數
  36. function post_check($str, $min, $max) {
  37. if (isset ( $min ) && strlen ( $str ) < $min) {
  38. die ( '最少$min位元組' );
  39. } else if (isset ( $max ) && strlen ( $str ) > $max) {
  40. die ( '最多$max位元組' );
  41. }
  42. return stripslashes_array ( $str );
  43. }
  44. ?>

複製代碼

當輸入的參數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&;nb … 39;1'='1', abc.asp運行正常,而且與HTTP://xxx.xxx.xxx/abc.asp?p=YY運行結果相同;③HTTP://xxx.xxx.xxx/abc.asp?p=YY&;nb … 39;1'='2', abc.asp運行異常;如果以上三步全面滿足,abc.asp中一定存在SQL注入漏洞。

附上一個防止sql注入的函數:

  1. //防注入函數
  2. function inject_check($sql_str) {
  3. return eregi ( 'select|inert|update|delete|'|/*|*|../|./|UNION|into|load_file|outfile', $sql_str );
  4. // 進行過濾,防注入 bbs.it-home.org
  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;
  15. }
  16. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.