【Web安全】DVWA之Command injection(命令执行漏洞)探索
文章目录
1 命令行注入
命令执行漏洞的原理:在操作系统中, & 、&& 、| 、 || 都可以作为命令连接符使用,用户通过浏览器提交执行命令,由于服务器端没有针对执行函数做过滤,导致在没有指定绝对路径的情况下就执行命令.
2 low
<?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>"; } ?>
可以看到,low级别的代码接收了用户输入的ip,然后根据服务器是否是Windows NT系统,对目标ip进行不同的ping测试。但是这里对用户输入的ip并没有进行任何的过滤,所以我们可以进行命令执行漏洞
我们ping一下百度的ip地址看看,可以看到能ping通
我们尝试输入 61.135.169.125 & ipconfig ,在操作系统中," & 、&& 、| 、 || "都可以作为命令连接符使用,我们在ping完后再执行ipconfig 命令查看ip信息
可以看到,成功执行。然后我们就可以继续执行我们的命令了。把ipconfig换成其他的系统命令
比如,我们执行 61.135.169.125 & net user xie /add ,尝试ping完后新建一个用户,
3 Medium
<?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>"; } ?>
可以看到,medium级别的代码在low级别的代码上增加量了对 && 和 ;的过滤,但是这并没有什么软用。
我们根本就不用 && ,我们直接用 &
&&和&的区别在于,&&是执行完前面的命令然后执行后面的命令,&是不管前面的命令是否值执行,后面的都执行
可以看到,我们直接按照low级别的命令就可以绕过执行。
4 Middle
<?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>"; } ?>
可以看到,High级别的代码进行了黑名单过滤,把一些常见的命令连接符给过滤了。黑名单过滤看似安全,但是如果黑名单不全是话,是很容易进行绕过的。我们仔细看黑名单过滤中的| ,|后面还有一个空格,所以我们可以进行绕过,输入下面命令
61.135.169.125|ipconfig
可以看到,成功执行了!
5 Impossible
<?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(); ?>
stripslashes(string) : 该函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。
explode(separator,string,limit): 该函数把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。
is_numeric(string): 该检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。
可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。
- 点赞
- 收藏
- 分享
- 文章举报
![](https://g.csdnimg.cn/static/user-reg-year/1x/1.png)
- 通过DVWA学习命令执行漏洞(Command Execution)
- DVWA 之命令注入(Command Injection)
- DVWA系列之7 low级别命令执行漏洞
- DVWA命令注入(Command_Injection)出现乱码
- DVWA系列之8 medium级别命令执行漏洞
- DVWA系列之10 命令执行漏洞的挖掘与防御
- file include 文件包括漏洞,毒化日志,并生成木马,执行系统命令 dvwa低安全
- DVWA系列之8 medium级别命令执行漏洞
- DVWA系列之10 命令执行漏洞的挖掘与防御
- DVWA系列之9 high级别命令执行漏洞
- DVWA系列之9 high级别命令执行漏洞
- Struts 2命令执行漏洞
- Mac系统终端命令行不执行命令 总出现command not found解决方法
- WordPress <= 4.6 命令执行漏洞(PHPMailer)复现分析
- CVE-2018-18320 - 华硕路由器 Merlin.php 命令执行漏洞
- Mac系统终端命令行不执行命令 总出现command not found解决方法
- 命令执行漏洞
- Apache shiro 1.2.4版本远程命令执行漏洞详解
- jenkins执行xctool命令出现command not found问题解决方法
- Linux下执行一些命令前加sudo时出现command not found的原因