命令注入
命令注入(Command Injection)是指通过提交恶意结构的参数粉碎命令语句布局。从而到达执行恶意命令的目的。
检察命令注入的流程:
1;检察是否挪用系统命令。
2;函数以及函数的参数是否可控。
3;是否拼接命令注入。
下面我们使用dvwa来做测试。
A;我们先将安适品级调解为“low”
1;检察源代码
<?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>"; } ?>
2;代码分析:
核心函数shell_exec(),该函数的成果就是通过shell执行命令并将完整输出作为字符串返回。整个代码看下来并没有对输入的参数做一些过滤。几乎可以判定存在命令注入的缝隙。
3;缝隙操作:
使用&&来执行多条命令(Window和linux都可以使用)输入命令“127.0.0.1 && ifconfig ”这个时候我们发明ifconfig 命令也告成被执行且返回效果。
B;下面我们将安适品级调解为“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>"; } ?>
2;代码分析:
这里我们发明代码中给与了黑名单的要领过滤了字符串“&&”,“;”也就说类似于127.0.0.1 && ifconfg 这样使用被过滤的连接标记就无法在告成执行。
我们都知道,给与黑名单并不是绝对安适的,好比上面代码傍边就没有过滤“&”那么下面我们就可以操作这点。
3;缝隙操作:
输入127.0.0.1 & ifconfig
C;安适品级“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>"; } ?>
2;缝隙操作:
注入代码:127.0.0.1|ifconfig 我们可以发明命令ifconfig可以被告成执行。
C;Impossible
1;检察代码
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30802.html