在只须要数据的地方恶意插入了命令,而系统没有过滤时会形成命令行注入。dvwa提供了练习的地方php
正常状况下这是用来测试网址可否链接shell
1.Security:Low数组
然鹅当咱们插入恶意命令127.0.0.1&&net usersession
查看源代码:函数
<?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUEST[ 'ip' ]; // 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>"; } ?>
能够看出ip参数没有作任何的过滤就直接放在shell_exec函数中执行了,执行语句为shell_exec( 'ping 127.0.0.1&&net user ')。测试
函数:spa
stristr(string,search,before_searc)string是被搜索的字符串,search是搜索的字符串,before_search有true和false两种取值(默认为false,返回匹配点到以后的部分)如stristr(''hello world!","WORld")返回world!,若第三个值为true时返回hello操作系统
php_uname(mode)命令行
mode是单个字符,用于定义要返回什么信息:3d
2.Security:Medium
重复上述输入时返回Ping 请求找不到主机 127.0.0.1net。请检查该名称,而后重试。可知&&符号被替换为空了,尝试&与|均可成功。
查阅源代码:
<?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>"; } ?>
标红的地方将&&与;都替换为空,但并未过滤&与|
3.Security:High
调整为高级别后&与&&都返回Ping 请求找不到主机 127.0.0.1net。请检查该名称,而后重试。可知这两个都被过滤了,然而使用|依然能够顺利执行。
查看源码:
<?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = trim($_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>"; } ?>
能够看出这里先用trim去掉两边的空格,而后过滤了&,&&,与| 等符号,然而过滤的是|+空格,并无过滤|,也是很尴尬了。。。。。。让咱们再看看Impossible级别,开开眼
4.Security:Impossible
上面使用的全部符号都失效了,无尽的ERROR: You have entered an invalid IP.从返回结果也看不出过滤方式
查看源码:
<?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(); ?>
首先检查了token,这个我不懂。再看它先将ip按"."拆分为数组,而后判断位数是否为4位且每一位是否为数字。。。。。。。甘拜下风
再总结一下命令链接符:
& 顺序执行命令,无论是否执行成功
; 按顺序执行
&& 前面的命令执行成功后面的命令才执行
|| 前面的命令执行失败后面的命令才执行
| 后面的执行失败才执行前面的