实验环境:
DVWA靶机:172.16.12.10
windos攻击机:172.16.12.7
kali攻击机:172.16.12.30
实验步骤:
一、Low级
1、源码分析
<?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input //获取IP字段 $target = $_REQUEST[ 'ip' ]; // Determine OS and execute the ping command. //确定操作系统并执行ping命令 // stristr(string,search,before_search) //stristr函数搜索字符串在另一字符串中的第一次出现,返回字符串的剩余部分(从匹配//点),如果未找到所搜索的字符串,则返回FALSE。参数string规定被搜索的字符串,参//数search规定要搜索的字符串(如果该参数是数字,则搜索匹配该数字对应的ASCII值//的字符),可选参数before_true为布尔型,默认为“false”,如果设置为“true”,函//数将返回search参数第一次出现之前的字符串部分。 // php_uname(mode) //这个函数会返回运行php的操作系统的相关描述,参数mode可取值”a” (此为默认,包//含序列”s n r v m”里的所有模式),”s ”(返回操作系统名称),”n”(返回主机名),” //r”(返回版本名称),”v”(返回版本信息), ”m”(返回机器类型)。 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>"; } ?>
相关函数介绍
stristr(string,search,before_search):
stristr函数搜索字符串在另一字符串中的第一次出现,返回字符串的剩余部分(从匹配点),如果未找到所搜索的字符串,则返回FALSE。参数string规定被搜索的字符串,参数search规定要搜索的字符串(如果该参数是数字,则搜索匹配该数字对应的ASCII值的字符),可选参数before_true为布尔型,默认为“false”,如果设置为“true”,函数将返回search参数第一次出现之前的字符串部分。
php_uname(mode):
这个函数会返回运行php的操作系统的相关描述,参数mode可取值”a”(此为默认,包含序列”snrvm”里的所有模式),”s”(返回操作系统名称),”n”(返回主机名),”r”(返回版本名称),”v”(返回版本信息),”m”(返回机器类型)。
可以看到,服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。
2、漏洞复现
ping自己 127.0.0.1 ,可以通
那么我们就是用&& ,输入 127.0.0.1&&net user
注:
&& Usage:第一条命令 && 第二条命令 [&& 第三条命令…]
当碰到执行出错的命令后将不执行后面的命令,如果一直没有出错则一直执行完所有命令;
可以执行命令net user
输入 127.0.0.1&&net user cuiyi /add 来新建一个账户
然后输入 127.0.0.1&&net user ,发现新增cuiyi账户
二、Medium级
1、源码分析
<?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>"; } ?>
可以看到,相比Low级别的代码,服务器端对ip参数做了一定过滤,即把”&&”、”;”删除,本质上采用的是黑名单机制,因此依旧存在安全问题。可以使用&或者|之类的连接符
2、漏洞复现
方法一:使用& 或者 | 连接两个命令
输入 127.0.0.1&net user,成功查看账户
注:
”&&”与” &”的区别:
Command 1&&Command 2
先执行Command 1,执行成功后执行Command 2,否则不执行Command 2
Command 1&Command 2
先执行Command 1,不管是否成功,都会执行Command 2
”||”与”|”的区别:
Command1||Command2
当Command1执行成功,就不执行Command2,只有当Command1执行
失败才会执行Command2。
Command1|Command2
不管Command1是否可以执行成功,都会写很执行Command2。
方法二:双写逃逸
当然有其它的方法去绕过这个防护,由于代码中是将“&&”换成“”,将“;”也换成“”,所以可以写成下图样子
输入 127.0.0.1&;&net user,成功查看账户
三、High级
1、源码分析
<?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>"; } ?>
相比Medium级别的代码,High级别的代码进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。
2、漏洞复现
黑名单看似过滤了所有的非法字符,但仔细观察到是把“|”(注意这里|后有一个空格)替换为空字符,于是“|”成了“漏网之鱼”。而且"||“也是能用的,可以当作”|"来执行
输入 127.0.0.1 |net user #注意“|”和“net”之间无空格才能执行成功
四、Impossible级
1、源码分析
<?php if( isset( $_POST[ 'Submit' ] ) ) { // Check Anti-CSRF token checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' ); // Get input $target = $_REQUEST[ 'ip' ]; // stripslashes函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。 $target = stripslashes( $target ); // Split the IP into 4 octects //将具体的IP以.分隔 // explode(separator,string,limit) 把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。 $octet = explode( ".", $target ); // Check IF each octet is an integer //检查是否分成了4块,并且每一块是否都为数字 // s_numeric(string) 检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。 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(); ?>
可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。