加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长资讯 > 评论 > 正文

浅谈PHP安全规范

发布时间:2018-10-15 10:42:58 所属栏目:评论 来源:littlepotato
导读:【新品产上线啦】51CTO播客,随时随地,碎片化学习 一、前言 php因天生支持web应用的开发,以其简单易学,开发效率高而备受喜爱。使其占据了大片的市

过分相信用户的输入,直接拼接到ping 命令中,会造成命令注入。注意到常用的bash命令拼接的方式有||.&&,|,&,;这五个,所以由于没有过滤完全,我们直接进行命令拼接,然后执行任意命令,如127.0.0.1;cat /etc/passwd。

(2) Medium level

  1. <?php  
  2.  
  3. if( isset( $_POST[ 'Submit' ]  ) ) {  
  4.     // Get input  
  5.     $target = $_REQUEST[ 'ip' ];  
  6.  
  7.     // Set blacklist  
  8.     $substitutions = array(  
  9.         '&&' => '',  
  10.         ';'  => '',  
  11.     );  
  12.  
  13.     // Remove any of the charactars in the array (blacklist).  
  14.     $target = str_replace( array_keys( $substitutions ), $substitutions, $target );  
  15.  
  16.     // Determine OS and execute the ping command.  
  17.     if( stristr( php_uname( 's' ), 'Windows NT' ) ) {  
  18.         // Windows  
  19.         $cmd = shell_exec( 'ping  ' . $target );  
  20.     }  
  21.     else {  
  22.         // *nix  
  23.         $cmd = shell_exec( 'ping  -c 4 ' . $target );  
  24.     }  
  25.  
  26.     // Feedback for the end user  
  27.     echo "<pre>{$cmd}</pre>";  
  28. }  
  29.  
  30. ?> 

这里采用黑名单过滤的方式,注意到黑名单的办法存在的通病就是过滤不完全。可以看到这里也一样,没有把之前提到的东西给过滤完全。其实highlevel也是一样的,过滤语句写得不严谨,多加了空格,造成绕过,这里就不再展开叙述了。同过这个例子可以直观的看到黑名单式过滤方式是不安全的,容易出岔子。接着我们将看到Impossible等级下的白名单试想方式。直接指定只接受num.num.num.num型的输入,也就是我们期望的输入,从而避免了命令执行。

(3) Impossible level

  1. <?php  
  2.  
  3. if( isset( $_POST[ 'Submit' ]  ) ) {  
  4.     // Check Anti-CSRF token  
  5.     checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );  
  6.  
  7.     // Get input  
  8.     $target = $_REQUEST[ 'ip' ];  
  9.     $target = stripslashes( $target );  
  10.  
  11.     // Split the IP into 4 octects  
  12.     $octet = explode( ".", $target );  
  13.  
  14.     // Check IF each octet is an integer  
  15.     if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {  
  16.         // If all 4 octets are int's put the IP back together.  
  17.         $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];  
  18.  
  19.         // Determine OS and execute the ping command.  
  20.         if( stristr( php_uname( 's' ), 'Windows NT' ) ) {  
  21.             // Windows  
  22.             $cmd = shell_exec( 'ping  ' . $target );  
  23.         }  
  24.         else {  
  25.             // *nix  
  26.             $cmd = shell_exec( 'ping  -c 4 ' . $target );  
  27.         }  
  28.  
  29.         // Feedback for the end user  
  30.         echo "<pre>{$cmd}</pre>";  
  31.     }  
  32.     else {  
  33.         // Ops. Let the user name theres a mistake  
  34.         echo '<pre>ERROR: You have entered an invalid IP.</pre>';  
  35.     }  
  36. }  
  37.  
  38. // Generate Anti-CSRF token  
  39. generateSessionToken();  
  40.  
  41. ?> 

4. Brute Force

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读