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

?>
相关推荐
编程小白呀8 天前
【DVWA 靶场通关】 File Inclusion(文件包含漏洞)
靶场·dvwa
编程小白呀2 个月前
【XSS】DVWA靶场XSS攻击
靶场·xss·dvwa
H轨迹H3 个月前
DVWA靶场XSS漏洞通关教程及源码审计
网络安全·渗透测试·xss·dvwa·web漏洞
H轨迹H3 个月前
渗透测试必刷靶场包含基础靶场以及渗透真实环境靶场
网络安全·渗透测试·dvwa·web漏洞
H轨迹H3 个月前
DVWA靶场Authorisation Bypass (未授权绕过) 漏洞通关教程及源码审计
网络安全·渗透测试·dvwa·web漏洞·未授权
轨迹H3 个月前
DVWA靶场CSRF漏洞通关教程及源码审计
网络安全·渗透测试·csrf·dvwa
H轨迹H4 个月前
DVWA靶场JavaScript Attacks漏洞low(低),medium(中等),high(高),impossible(不可能的)所有级别通关教程
javascript·网络安全·渗透测试·dvwa·web漏洞
轨迹H4 个月前
DVWA靶场Insecure CAPTCHA(不安全验证)漏洞所有级别通关教程及源码审计
网络安全·渗透测试·dvwa·web漏洞
H轨迹H4 个月前
DVWA靶场Open HTTP Redirect (重定向) 漏洞所有级别通关教程及源码审计
网络安全·渗透测试·dvwa·web漏洞
轨迹H4 个月前
DVWA靶场Open HTTP Redirect (重定向) 漏洞所有级别通关教程及源码审计
网络协议·渗透测试·dvwa·重定向·web漏洞