Loading

DVWA 之 Command Injection-命令注入

二、Command Injection-命令注入

原理

命令注入是指对一些函数的参数没有做过滤或过滤不严导致的,可以执行系统或者应用指令(CMD命令或者bash命令)的一种注入攻击手段。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一。

命令连接符
command1 && command2   先执行command1,成功后执行command2
command1 | command2    command1的输出作为command2的输入
command1 & command2    先执行command2后执行command1

1. Low

在文本框中随意输入一些数字123,发现乱码

解决办法:在DVWA-master\dvwa\includes 目录下找到 dvwaPage.inc.php 文件中所有的 ”charset=utf-8” ,修改为 ”charset=gb2312”,即可,最终结果如下

发现直接进行了ping命令,因此可以输入127.0.0.1 && dir,结果如下

发现可以直接执行系统命令,只需要在后面添加&&就可以执行任意系统命令。

Linux下输入127.0.0.1&&cat /etc/shadow甚至可以读取shadow文件

查看源代码

<?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
	$html .= "<pre>{$cmd}</pre>";
}

?>

发现直接针对操作系统进行ping,没有对输入的数据作出任何过滤,非常危险。

2. Medium

查看源代码,多了以下部分

// Set blacklist
$substitutions = array(
	'&&' => '',
	';'  => '',
);

// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );

对ip参数做了一定的过滤,即把&&; 删除,本质上采用黑名单机制,因此依旧存在安全问题。

由于只对&&; 进行过滤,其它的连接符不受影响,输入 127.0.0.1 | ipconfig,命令成功执行。127.0.0.1&ipconfig也可以成功执行

3. High

查看源代码,进一步完善了黑名单

	// Set blacklist
	$substitutions = array(
		'&'  => '',
		';'  => '',
		'| ' => '',
		'-'  => '',
		'$'  => '',
		'('  => '',
		')'  => '',
		'`'  => '',
		'||' => '',
	);

由于黑名单机制的局限性,仍然可以绕过,发现黑名单看似过滤了所有非法字符,但可以看到是把| 替换为空字符,|后有一个空格,因此|成为了漏网之鱼。

输入127.0.0.1|ipconfig

可用trim()函数解决这个问题,因为trim(str)可以删除字符串左右两边的空格。

防护方法

  • 白名单机制(只允许某种类型的输入)
  • 尽量不要使用系统执行命令
posted @ 2023-03-04 22:15  紫曜花  阅读(201)  评论(0编辑  收藏  举报