php Simple parameter Filter code learning

Source: Internet
Author: User
    1. /**
    2. * Parameter Filter code
    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. If it is an array, iterate through the array, calling recursively
    14. if (Is_array ($array)) {
    15. foreach ($array as $k = = $v) {
    16. $array [$k] = sec ($v);
    17. }
    18. } else if (is_string ($array)) {
    19. Use the Addslashes function to handle
    20. $array = Addslashes ($array);
    21. } else if (Is_numeric ($array)) {
    22. $array = Intval ($array);
    23. }
    24. return $array;
    25. }
    26. ?>
Copy Code

1, the parameters of the integer parameter when the input parameter YY is an integer, the SQL statement in abc.asp usually looks as follows: SELECT * from table name where field =yy, so you can use the following steps to test whether SQL injection exists. ①http://xxx.xxx.xxx/abc.asp?p=yy ' (append a single quotation mark), at this time ABC. The SQL statement in ASP becomes a select * from table name where Field =yy ', abc.asp run exception, ②http://xxx.xxx.xxx/abc.asp?p=yy and 1=1, abc.asp is working properly, and http:/ /xxx.xxx.xxx/abc.asp?p=yy run the same result; ③http://xxx.xxx.xxx/abc.asp?p=yy and 1=2, Abc.asp run exception, if the above three steps are fully satisfied, there must be a SQL injection vulnerability in abc.asp.

An integer filter function with the following code:

  1. function Num_check ($id) {

  2. if (! $id) {
  3. Die (' parameter cannot be empty! ' );
  4. }//Is null-judged
  5. else if (Inject_check ($id)) {
  6. Die (' illegal parameters ');
  7. }//Injection judgment
  8. else if (! is_numetic ($id)) {
  9. Die (' illegal parameters ');
  10. }
  11. Digital judgment
  12. $id = Intval ($id);
  13. The whole type of
  14. return $id;
  15. }

  16. Character Filter function

  17. function Str_check ($STR) {
  18. if (Inject_check ($STR)) {
  19. Die (' illegal parameters ');
  20. }
  21. Injection judgment
  22. $str = Htmlspecialchars ($STR);
  23. Convert HTML
  24. return $str;
  25. }
  26. function Search_check ($STR) {
  27. $str = Str_replace ("_", "_", $str);
  28. Filter Out "_"
  29. $str = str_replace ("%", "%", $str);
  30. Filter out "%"
  31. $str = Htmlspecialchars ($STR);
  32. Convert HTML
  33. return $str;
  34. }
  35. Form Filter function
  36. function Post_check ($str, $min, $max) {
  37. if (Isset ($min) && strlen ($STR) < $min) {
  38. Die (' minimum $min bytes ');
  39. } else if (Isset ($max) && strlen ($STR) > $max) {
  40. Die (' Up to $max bytes ');
  41. }
  42. Return Stripslashes_array ($STR);
  43. }
  44. ?>

Copy Code

When the input parameter YY is a string, the SQL statement in abc.asp usually looks as follows: SELECT * from table name where field = ' YY ', so you can use the following steps to test whether SQL injection exists. ①http://xxx.xxx.xxx/abc.asp?p=yy ' (append a single quotation mark), at this time ABC. The SQL statement in ASP becomes a select * from table name where Field =yy ', abc.asp run exception; ②http://xxx.xxx.xxx/abc.asp?p=yy&;nb 39;1 ' = ' 1 ', abc.asp run normally, and with HTTP://xxx.xxx.xxx/abc.asp?p=YY run the same result; ③http://xxx.xxx.xxx/abc.asp?p=yy&;nb ... 39;1 ' = ' 2 ', abc.asp run exception, if the above three steps are fully satisfied, there must be a SQL injection vulnerability in abc.asp.

Attach a function to prevent SQL injection:

    1. Anti-injection function
    2. function Inject_check ($sql _str) {
    3. Return eregi (' select|inert|update|delete| ' | /*|*|.. /|. /| Union|into|load_file|outfile ', $sql _str);
    4. Filtration, anti-injection 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. ?>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.