PHP代码审计入门-DVWA靶场命令注入篇

0x00  写在前面

从零学习php,最终目的实现代码审计入门,软件采用sublime text,环境使用phpstudy搭建,数据库是navicat,需要有基本的前端基础、简单的php+mysql后端基础、渗透知识和漏洞原理,文章跟随流沙前辈学习记录,看看曾经遥不可及的代码审计能不能慢慢啃下来。

本章为代码审计入门第六篇-DVWA靶场篇,对DVWA靶场漏洞进行代码审计。

0x01  常用的命令连接符

Windows下常用的命令连接符

&,以test、whoami、dir命令测试

1.前面为假,执行后面命令

2.前面为真,前后命令都执行

通俗解释: 前面命令执行后接着去执行后面命令

 &&,以test、whoami、dir命令测试

1.前面为假,前后都不执行命令

2.前面为真,前后都执行命令

通俗解释:前面命令执行成功了才会去执行后面命令。

 | 和 || ,以ping和whoami为例

| ,浅浅的讲:直接执行后面命令

|| ,前面为假才执行后面命令

Linux下常用的命令连接符和windows一致,唯一区别的是 ;  的使用,;的作用连接多条命令,每条命令都执行这里以vps为例。

常用的命令连接符就差不多了,还有一个常用的但不是命令连接符的东东,基本就这么多。

>      写入并覆盖

>>   追加

0x02  Command Injection

命令注入模块,注释写在代码里,尽量用详细的语言描述代码含义。

level-low

命令执行,但没有作任何限制

low.php

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {		//isset函数检测变量非空
	// 判断post过来的数据是否被提交过来
	// Get input
	$target = $_REQUEST[ 'ip' ];
	// 得到接收的'ip'赋值给变量$target
	// Determine OS and execute the ping command.
	if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
		// php_uname 返回运行 PHP 的系统的有关信息 (mode:'s')  返回操作系统
		$cmd = shell_exec( 'ping  ' . $target );
		// Windows 系统 ,用 shell_exec 运行拼接命令 ping.$target,例如ping 127.0.0.1
	}
	else {
		// *nix
		$cmd = shell_exec( 'ping  -c 4 ' . $target );
		// Linux系统,shell_exec 运行拼接命令 ping -c  4.$target,原因是linux下如果不指定ping次数会一直ping下去
	}

	// Feedback for the end user
	// 输出操作命令后的结果
	$html .= "<pre>{$cmd}</pre>";
}

?>

level-medium

相比于low,增加一个黑名单机制

medium.php
<?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 );
	// 用str_replace对变量 $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>";
}

?>

level-high

相比于medium,黑名单内容增加

high.php

<?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>";
}

?>

level-impossible

既然是impossibale,那以咱水平肯定绕不过去,直接看代码。这里先把 $target 输出,看看代码是如何防止命令注入的。

impossiable.php

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
	// Check Anti-CSRF token
	checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
	//检测token
	// Get input
	$target = $_REQUEST[ 'ip' ];
	$target = stripslashes( $target );
	//接收数据,使用stripslashes防注入
	// Split the IP into 4 octects
	$octet = explode( ".", $target );
	var_dump($octet);		// --调试--
	exit();
	// explode() 函数使用一个字符串分割另一个字符串,并返回由字符串组成的数组。
	// 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.
		// 先拆为数组,分别为0、1、2、3、4,is_numeric检测是否为数字,也就是说在这里数组0、1、2、3、4都必须为数字
		$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();

?>

调试也可以看出上面代码的数组分组

这里虽然没用禁用所谓的危险函数shell_exec,但对于用户的输入内容过滤严格从而避免命令注入漏洞。

0x03  总结

沙雕考试暂且结束,暂获一周躺平权。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值