过分相信用户的输入,直接拼接到ping 命令中,会造成命令注入。注意到常用的bash命令拼接的方式有||.&&,|,&,;这五个,所以由于没有过滤完全,我们直接进行命令拼接,然后执行任意命令,如127.0.0.1;cat /etc/passwd。
(2) Medium level
- <?php
-
- if( isset( $_POST[ 'Submit' ] ) ) {
- // Get input
- $target = $_REQUEST[ 'ip' ];
-
- // Set blacklist
- $substitutions = array(
- '&&' => '',
- ';' => '',
- );
-
- // Remove any of the charactars in the array (blacklist).
- $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
-
- // Determine OS and execute the ping command.
- if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
- // Windows
- $cmd = shell_exec( 'ping ' . $target );
- }
- else {
- // *nix
- $cmd = shell_exec( 'ping -c 4 ' . $target );
- }
-
- // Feedback for the end user
- echo "<pre>{$cmd}</pre>";
- }
-
- ?>
这里采用黑名单过滤的方式,注意到黑名单的办法存在的通病就是过滤不完全。可以看到这里也一样,没有把之前提到的东西给过滤完全。其实highlevel也是一样的,过滤语句写得不严谨,多加了空格,造成绕过,这里就不再展开叙述了。同过这个例子可以直观的看到黑名单式过滤方式是不安全的,容易出岔子。接着我们将看到Impossible等级下的白名单试想方式。直接指定只接受num.num.num.num型的输入,也就是我们期望的输入,从而避免了命令执行。
(3) Impossible level
- <?php
-
- if( isset( $_POST[ 'Submit' ] ) ) {
- // Check Anti-CSRF token
- checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
-
- // Get input
- $target = $_REQUEST[ 'ip' ];
- $target = stripslashes( $target );
-
- // Split the IP into 4 octects
- $octet = explode( ".", $target );
-
- // Check IF each octet is an integer
- if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
- // If all 4 octets are int's put the IP back together.
- $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
-
- // Determine OS and execute the ping command.
- if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
- // Windows
- $cmd = shell_exec( 'ping ' . $target );
- }
- else {
- // *nix
- $cmd = shell_exec( 'ping -c 4 ' . $target );
- }
-
- // Feedback for the end user
- echo "<pre>{$cmd}</pre>";
- }
- else {
- // Ops. Let the user name theres a mistake
- echo '<pre>ERROR: You have entered an invalid IP.</pre>';
- }
- }
-
- // Generate Anti-CSRF token
- generateSessionToken();
-
- ?>
4. Brute Force (编辑:威海站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|