DVWA系列(四)----Command Injection (命令行注入)

一、攻击模块2:Command Injection(命令注入)

 

      命令注入攻击的常见模式为:仅仅需要输入数据的场合,却伴随着数据同时输入了恶意代码,而装载数据的系统对此并未设计良好的过滤过程,导致恶意代码也一并执行,最终导致信息泄露或者正常数据的破坏。

       PHP命令注入攻击漏洞是php应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。

 

 

 

二、源码分析

 

 

1.Low(低)级别

 

 

[html] view plain copy
 
  1. <?php   
  2.   
  3. if( isset( $_POST[ 'Submit' ]  ) ) {   
  4.     // Get input   
  5.     $target = $_REQUEST[ 'ip' ];   
  6.   
  7.     // Determine OS and execute the ping command.   
  8.     if( stristr( php_uname( 's' ), 'Windows NT' ) ) {   
  9.         // Windows   
  10.         $cmd = shell_exec( 'ping  ' . $target );   
  11.     }   
  12.     else {   
  13.         // *nix   
  14.         $cmd = shell_exec( 'ping  -c 4 ' . $target );   
  15.     }   
  16.   
  17.     // Feedback for the end user   
  18.     echo "<pre>{$cmd}</pre>";   
  19. }   
  20.   
  21. ?>   

 

 

相关函数介绍


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”(返回机器类型)
可以看到,服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。

漏洞利用


window和Linux系统都可以用&&来执行多条命令


127.0.0.1&&net user

 

 

命令连接符


command1 && command2   先执行command1后执行command2
command1 | command2     只执行command2
command1 & command2    先执行command2后执行command1


以上三种连接符在windows和linux环境下都支持


如果程序没有进行过滤,那么我们就可以通过连接符执行多条系统命令。

 

 

 

2.Medium(中)级别

 

 

[html] view plain copy
 
  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. ?>   

相比Low级别的代码,服务器端对ip参数做了一定过滤,即把”&&” ,”;”删除,本质上采用的是黑名单机制,因此依旧存在安全问题。

 

 

漏洞利用


1、127.0.0.1&net user


因为被过滤的只有”&&”与”    ;”,所以”&”不会受影响。

 

 

这里需要注意的是”&&”与”    &”的区别:


Command 1&&Command 2
先执行Command 1,执行成功后执行Command 2,否则不执行Command 2

 

Command 1&Command 2
先执行Command 1,不管是否成功,都会执行Command 2

 

 

2、由于使用的是str_replace把”&&”,”;”替换为空字符,因此可以采用以下方式绕过:


127.0.0.1&;&ipconfig

 

 

这是因为”127.0.0.1&;&ipconfig”中的” ;”会被替换为空字符,这样一来就变成了”127.0.0.1&& ipconfig” ,会成功执行。

 

 

 

3.High(高)级别 

 

 

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

相比Medium级别的代码,High级别的代码进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。

 

 

漏洞利用


黑名单看似过滤了所有的非法字符,但仔细观察到是把”| ”(注意这里|后有一个空格)替换为空字符,于是    ”|” 就有用了。

 

127.0.0.1|net user

 

 

Command 1 | Command 2


“|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。



4.Impossible(不可能的)级别

 

 

[html] view plain copy
 
  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. ?>   

相关函数介绍

 


stripslashes(string)


stripslashes函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。


explode(separator,string,limit)
把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。


is_numeric(string)
检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。


可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

 
 
posted @ 2017-06-15 11:30  寂地沉  阅读(837)  评论(0编辑  收藏  举报