DVWA(Damn Vulnerable Web App)是一个基于PHP/MySql搭建的Web应用程序,旨在为安全专业人员测试自己的专业技能和工具提供合法的 环境,帮助Web开发者更好的理解Web应用安全防范的过程。
DVWA一共包含十个模块分别是:
1.Bruce Force //暴力破解
2.Command Injection //命令注入
3.CSRF //跨站请求伪造
4.File Inclusion //文件包含
5.File Upload //文件上传漏洞
6.Insecure CAPTCHA //不安全的验证
7.SQL Injection //sql注入
8.SQL Injection(Blind) //sql注入(盲注)
9.XSS(Reflected) //反射型XSS
10.XSS(Stored) //存储型XSS
我们从原理上学习一下Command Injection命令执行漏洞。
先来看源代码,点击右下脚 View Source即可查看源代码。切换漏洞利用难度级别请点击DVWA Security。懂得都懂
初级
Low Command Injection Source
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
echo "<pre>{$cmd}</pre>";
}
看页面是让输入一个IP地址,源码是把ip通过变量$target传入,这里产生漏洞的是shell_exec函数和exec相同也是命令执行的函数,然后传入变量cmd并最终打印。这里并没有对target输入的内容进行过滤,因此通过命令拼接的办法执行命令,&&后面是想要执行的命令:ping 127.0.0.1 && ls /tmp
中级
Command Injection Source
<?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 );
// 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
echo "<pre>{$cmd}</pre>";
}
?>
通过查看源代码发现函数shell_exec没有做过滤但是对于命令链接符“&&”和“;”做了过滤,因此只需要考虑替换代替&&的连接符就可以了。这里使用|进行连接。
其他命令连接符可以参考文章:https://blog.csdn.net/weixin_43847838/article/details/111602811
‘&’‘&&’‘||’‘|’
高级
High Command Injection Source
<?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
echo "<pre>{$cmd}</pre>";
}
?>
这回看到的是过滤了更多的拼接符,但是仍然使用了shell_exec函数。
单个的拼接符都被过滤了,多加几个拼接符,导致只过滤一部分拼接符,执行了其他拼接符。
不可能的(修复)
Impossible Command Injection Source
Impossible Command Injection Source
<?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
echo "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
先监测了token,使用了stripslashes()和explode()安全函数做过滤,先通过stripslashes()对返回数据做了清理explode()对输入的字符串进行了定义,定义为4组数组。虽然修复方案里并没有禁用shell_exec()函数,但由于对于输入的过滤足够严格因此漏洞得到了风险降级,漏洞也没有办法进行利用了。
欢迎关注“天大天财”
关于天大天财
内蒙古天大天财信息技术有限责任公司,成立于1999年,是集网络安全服务、安全集成、计算机系统集成、公共安全技术防范系统设计|施工|维修为一体的综合性高科技信息公司
专注产生价值,天大天财致力于做内蒙古网络安全设计、实施、运维服务综合实力最强的本土品牌,我们潜心了解客户业务需求及痛点,注重提升与客户的粘合度,赢得了客户与市场的认可,享有很好的商誉;我们坚持以技术实施、服务为基础,注重技术团队的培养与提升,拥有一支技术过硬的服务团队,能够为全区客户提供及时的应急支援服务,同时,为了能够为客户做更优质更有水准的安全服务,我们成立了天大天财安全实验室,推出了自己的安全服务产品,收到客户的高度认可。
2018年被评为内蒙古网络安全行业协会理事长单位,获得内蒙古自治区科技型企业资质以及高新技术企业证书,涉密资质由原来的三项(集成、运维、安防)新增一项国家秘密载体印制资质证书.
安服团队连续三年参加省级攻防演练,并取得不错的成绩,在安全服务方面有全套网络安全解决方案。
服务能力图示:
服务热线:0471-2658551/552/553
地址:内蒙古自治区呼和浩特市赛罕区昭乌达路世纪六路鸿博大厦4层
原文始发于微信公众号(天大天财):代码审计基础DVWA命令执行
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论