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();

?>
相关推荐
板栗妖怪2 天前
命令执行漏洞
渗透·命令执行
玥轩_5213 天前
网络安全 DVWA通关指南 Cross Site Request Forgery (CSRF)
安全·web安全·网络安全·靶场·csrf·dvwa·dvwa通关指南
qq_585532285 天前
【sqlmap命令学习及测试dvwa_SQL_Injection】
dvwa·sqlmap
kali-Myon21 天前
ctfshow-web入门-命令执行(web30-web36)
前端·学习·web安全·php·web·命令执行
kali-Myon23 天前
ctfshow-web入门-命令执行(web37-web40)
前端·学习·安全·web安全·php·web·命令执行
七天啊1 个月前
HackTheBox-Machines--Cronos
sql注入·命令执行·hack the box·计划任务写入shell提权
I_WORM3 个月前
CTFHUB RCE作业
rce·命令执行·ctfhub·作业
mlws19004 个月前
DVWA靶场-暴力破解
安全·web安全·网络安全·php·dvwa
Ryongao4 个月前
命令执行讲解和函数
网络安全·命令执行
补天阁5 个月前
CTFshow web(文件上传158-161)
前端·web安全·文件上传·文件包含·命令执行