dvwa命令执行漏洞分析

dvwa靶场命令执⾏漏洞

high难度的源码:

target = trim(_REQUEST[ 'ip' ]);是一个接收id值的变量

array_keys()函数功能是返回包含原数组中所有键名的一个新数组。

str_replace() 函数如下,把字符串 "Hello world!" 中的字符 "world" 替换为 "Shanghai":

复制代码
str_replace("world","Shanghai","Hello world!");

shell_exec()函数是执行Linux命令函数,可以获取全部数据

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

?>

这段源码中的substitutions是一个php关联数组,它交互过滤了所有的特殊字符,将他们置为空,但是经过观察'| ' => ''有一段空格,没有成功过滤:

复制代码
$substitutions = array(
		'&'  => '',
		';'  => '',
		'| ' => '',
		'-'  => '',
		'$'  => '',
		'('  => '',
		')'  => '',
		'`'  => '',
		'||' => '',
	);

impossible难度的源码:

这段代码中加入了CSRF token,如果user的token值与更改密码时的token值不相等或者不存在seesion_token值,便会报错,这样就防止了伪造攻击

stripslashes()函数:过滤字符串中的反斜杠。

explode()函数:将所有的字符串打散成为数组。

is_numeric() 函数:用于检测变量是否为数字或数字字符串。

Anti-CSRF token:同时对参数ip进行严格的限制,只有"数字.数字.数字.数字"的输入才会被接受,因此不存在命令注入漏洞。

防范措施:获取要测试的IP,利用函数根据.将其分割成4个数值,再重新拼接后进行测试。

复制代码
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];

限制了只有数字.数字.数字.数字才会接受,所以杜绝了命令注入漏洞

复制代码
<?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
		$html .= "<pre>{$cmd}</pre>";
	}
	else {
		// Ops. Let the user name theres a mistake
		$html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
	}
}

// Generate Anti-CSRF token
generateSessionToken();

?>
相关推荐
Le_ee4 天前
dvwa7——SQL Injection
数据库·sql·网络安全·靶场·token·dvwa
Le_ee4 天前
dvwa5——File Upload
网络安全·靶场·php·靶机·dvwa
Le_ee4 天前
dvwa4——File Inclusion
网络安全·靶场·dvwa
Le_ee4 天前
dvwa6——Insecure CAPTCHA
android·安全·网络安全·靶场·dvwa
泪不是Web妳而流1 个月前
【CTFSHOW_Web入门】命令执行
web安全·网络安全·php·linux命令·rce·命令执行·ctfshow命令执行wp题解
旺旺仙贝1 个月前
SQL手工注入(DVWA)
dvwa
摸鱼也很难1 个月前
[GXYCTF2019]Ping Ping Ping
ctf·命令执行
编程小白呀2 个月前
【DVWA 靶场通关】 File Inclusion(文件包含漏洞)
靶场·dvwa
编程小白呀3 个月前
【XSS】DVWA靶场XSS攻击
靶场·xss·dvwa
H轨迹H5 个月前
DVWA靶场XSS漏洞通关教程及源码审计
网络安全·渗透测试·xss·dvwa·web漏洞