dvwa-command execution

command execution

大致浏览了一些博客,命令注入的关键是绕过过滤与对linux的命令的熟悉,只有熟悉才有可能对其进行注入

1、low

 <?php

if( isset( $_POST[ 'submit' ] ) ) {

    $target = $_REQUEST[ 'ip' ];

    // Determine OS and execute the ping command.
    if (stristr(php_uname('s'), 'Windows NT')) { 
    
        $cmd = shell_exec( 'ping  ' . $target );
        echo '<pre>'.$cmd.'</pre>';
        
    } else { 
    
        $cmd = shell_exec( 'ping  -c 3 ' . $target );
        echo '<pre>'.$cmd.'</pre>';
        
    }
    
}
?>
View Code

命令分隔符包括换行符( )、分号(;)、逻辑与(&&、&)、逻辑或(||、|),若在 win 批处理脚本中还能用 %1A

127.0.0.1;cat /proc/version

2、medium

 <?php

if( isset( $_POST[ 'submit'] ) ) {

    $target = $_REQUEST[ 'ip' ];

    // Remove any of the charactars in the array (blacklist).
    $substitutions = array(
        '&&' => '',
        ';' => '',
    );

    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
    
    // Determine OS and execute the ping command.
    if (stristr(php_uname('s'), 'Windows NT')) { 
    
        $cmd = shell_exec( 'ping  ' . $target );
        echo '<pre>'.$cmd.'</pre>';
        
    } else { 
    
        $cmd = shell_exec( 'ping  -c 3 ' . $target );
        echo '<pre>'.$cmd.'</pre>';
        
    }
}

?> 
View Code

只过滤了&&,;,可以用逻辑或(||、|),换行( )

 3、high

<?php

if( isset( $_POST[ 'submit' ] ) ) {

    $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')) { 
    
            $cmd = shell_exec( 'ping  ' . $target );
            echo '<pre>'.$cmd.'</pre>';
        
        } else { 
    
            $cmd = shell_exec( 'ping  -c 3 ' . $target );
            echo '<pre>'.$cmd.'</pre>';
        
        }
    
    }
    
    else {
        echo '<pre>ERROR: You have entered an invalid IP</pre>';
    }
    
    
}

?> 
View Code

将输入的ip地址分为了4部分,每部分检测是否为数字

百度的结果是这里不存在命令注入漏洞

4、工具的使用

Commix工具可以用来命令注入.

5、防护

参数过滤

白名单保护

如果命令的参数是有特征性的建议使用白名单对输入的参数进行保护

比如允许[a-z][A-Z][0-9] _- 等有限的字符

黑名单保护

|;&$><`! 可以将这些字符直接作为黑名单过滤

f u0000 这些字符需要作为黑名单过滤,特别是空字符截断 u0000 (这个在JVM6里是没有保护)

6、知识总结

利用的大致过程:

1)绕过过滤

是否使用多行模式修饰符(/foo/m)、是否遗漏匹配对象末尾的换行符(/^d+$/)、是否允许空白字符(s)、是否误写反斜杠匹配模式(//)。

引号逃逸(源代码里将输入的数据加上引号,使其不可以执行),逃逸方式大致有:闭合引号,注释后面的引号,或者利用的转义作用进行逃逸

2)命令注入

命令选项注入,那位仁兄tql,对Linux的命令要熟

参考链接:

https://www.cnblogs.com/Antiver/p/10322619.html

https://www.cnblogs.com/aeolian/p/11051361.html

原文地址:https://www.cnblogs.com/dx-yll/p/11963848.html