文章更新于:2020-04-11

注:如何搭建环境参见:搭建DVWA Web渗透测试靶场

一、介绍

1.1、官方说明

The purpose of the command injection attack is to inject and execute commands specified by the attacker in the vulnerable application. In situation like this, the application, which executes unwanted system commands, is like a pseudo system shell, and the attacker may use it as any authorized system user. However, commands are executed with the same privileges and environment as the web service has.

Command injection attacks are possible in most cases because of lack of correct input data validation, which can be manipulated by the attacker (forms, cookies, HTTP headers etc.).

The syntax and commands may differ between the Operating Systems (OS), such as Linux and Windows, depending on their desired actions.

This attack may also be called “Remote Command Execution (RCE)”.

1.2、总结

总的来说就是如果Web服务器没有配置妥当,则可能产生命令注入漏洞。究其原因,是没有进行充分的验证访问者身份。

不同的操作系统比如(Linux 和 Windows)的命令格式是不相同的。

这种攻击方式也称为 远程命令执行 (RCE)

二、命令注入实践

2.1、安全级别:LOW

>> 查看源码

  1. 此对话框原意是输入一个 IP 或域名进行 PING 测试。

  1. 输入IP测试

  1. 点击页面右下角 View Source 得到如下源码:
<?php

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

在这些 php 代码中,我们可以看到,处理逻辑没有做任何过滤,只是针对不同的平台,调用了不同的 ping 命令,然后将结果原样返回到前端。

这是非常危险的,如果可以使用 &&&|; 等命令分隔符同事执行多条命令。则可任意执行命令,是有可能可以在系统上创建用户留下后门的。

比如我们执行 | net user add hello 1234

如果系统有安全软件,可能会有如下提示:

如果没有安全软件,命令执行成功将有可能在系统留下后门。

>>尝试注入

注1:常用 &|;作为命令分隔符。

注2:键盘左上角的反引号 ` ,会忽略掉外部的双引号先执行。

  1. 尝试用分号 ;分隔命令进行注入

    注:这里使用的是 Linux 环境下的 IP查看命令。

  1. 尝试获取主机密码文件内容

  1. 尝试用竖线 | 分隔命令进行注入

    注:这里使用的是 Windows 环境下的 IP查看命令。

  1. 尝试使用与符号 & 分割命令进行注入

2.2、安全级别:Medium

>>查看源码

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

上述代码可以看出,程序对 &&; 进行了替换成空字符串,但其他符号没有处理,所以其他分隔符依然有效。

>>尝试注入

  1. 尝试使用 | tree 命令

  1. 尝试创建文件 `| echo “hello” > hello.txt" | dir

2.3、安全级别:High

>>查看源码

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

这里可以看到程序对 &;|-$()、`、|| 进行了替换成空字符串,但同时可以注意到,|后面有一个空格。

>>尝试注入

  1. 尝试使用 | tree 进行注入

  1. 尝试使用 || tree

2.4、安全级别:Impossible

>>查看源码

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

上述代码可以看出,程序先对输入进行了点.分割成几部分,然后分别判断前四部分是否数字和是否总共四部分,如果是再进行拼接。那…这还命令注入个锤子!!

>> 可以尝试注入,但没必要

  1. 在上述几个代码的变化中,我们可以看到,如果没有任何过滤,那服务器将是十分不安全的。

  2. 即便使用了一些黑名单过滤手段,但你却无法保证你完全过滤掉了一些有害的信息。

  3. 所以比较稳妥的方法,还是使用白名单过滤手段,让经过严格过滤的数据通过,其他的一律拒绝。这样避免了一些未知的风险。

2.5、知识拓展

命令分隔符 说明
; 分号在linux命令执行的时候,可以分隔多条命令
& Windows顺序执行,Linux先执行后面的命令
&& 与,前面的命令执行成功后才可以执行下面的命令
| 管道符,前面的命令输出结果为后面的命令输入的内容
|| 或,前面的命令执行失败后才会执行后面的命令

三、Enjoy!

DVWA系列精品教程:2、命令注入的更多相关文章

  1. 【DVWA】Command Injection(命令注入)通关教程

    日期:2019-08-01 16:05:34 更新: 作者:Bay0net 介绍:利用命令注入,来复习了一下绕过过滤的方法,还可以写一个字典来 fuzz 命令注入的点. 0x01. 漏洞介绍 仅仅需要 ...

  2. DVWA靶场练习-Command Injection命令注入

    Command Injection 原理 攻击者通过构造恶意参数,破坏命令的语句结构,从而达到执行恶意命令的目的.

  3. 安全性测试入门:DVWA系列研究(二):Command Injection命令行注入攻击和防御

    本篇继续对于安全性测试话题,结合DVWA进行研习. Command Injection:命令注入攻击. 1. Command Injection命令注入 命令注入是通过在应用中执行宿主操作系统的命令, ...

  4. 《11招玩转网络安全》之第五招:DVWA命令注入

    首先还是将DVWA的安全级别设置为Low,然后单击DVWA页面左侧的Command Injection按钮. ​ 图5-1  Low级别的命令注入 这个就是最典型的命令注入接口.在文本框中输入一个IP ...

  5. dvwa——命令注入&文件包含

    命令注入 commond_injection 源码.分析.payload: low: <?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input ...

  6. DVWA命令注入扣动分析

    本周学习内容: 1.学习web应用安全权威指南: 2.观看安全学习视频: 实验内容: 进行DVWA命令注入漏洞 实验步骤: Low 1.打开DVWA,进入DVWA Security模块将 Level修 ...

  7. DVWA各等级命令注入漏洞

    漏洞描述 在web程序中,因为业务功能需求要通过web前端传递参数到后台服务器上执行,由于开发人员没有对输入进行严格过滤,导致攻击者可以构造一些额外的"带有非法目的的"命令,欺骗后 ...

  8. DVWA(四):Command Injection 全等级命令注入

    Command Injection : 命令注入(Command Injection),对一些函数的参数没有做好过滤而导致用户可以控制输入的参数,使其恶意执行系统命令或这cmd.bash指令的一种注入 ...

  9. Laravel 5 系列入门教程(一)【最适合中国人的 Laravel 教程】

    Laravel 5 系列入门教程(一)[最适合中国人的 Laravel 教程] 分享⋅ johnlui⋅ 于 2年前 ⋅ 最后回复由 skys215于 11个月前 ⋅ 17543 阅读   原文发表在 ...

随机推荐

  1. 交换机三种模式Access、Hybrid和Trunk

    [端口介绍] 种链路类型:access.trunk.hybird 个VLAN,一般用于连接计算机端口: Trunk类型端口:可以允许多个VLAN通过,可以接收和发送多个VLAN 报文, 一般用于交换机 ...

  2. Java 锁详解(转)

    转自 https://www.cnblogs.com/jyroy/p/11365935.html Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率.本文旨在对锁相 ...

  3. main.c(53): error: #268: declaration may not appear after executable statement in block

    这个问题是在编译STM32的程序时遇到的,这个错误的原因是对于变量的声明不能放在可执行语句后面,必须在主函数开头声明变量.在程序中声明一个变量时,需要在可执行语句之前声明,否则会出现以上错误.

  4. Nacos作为微服务注册中心,爱不释手的感觉

    我觉得Nacos用起来还不错 在使用SpringCloud做分布式微服务架构时,注册中心是必不可少的一个组件.目前可以用的主要有:Eureka.Consul.Zookeeper.今天,我们就来说一下A ...

  5. 通达OA rce复现

    通达OA下载:链接:https://pan.baidu.com/s/1c0P-M-IyY5VxfH5d0qKHsQ 提取码:l0pc 漏洞原因:未授权文件上传 + 文件包含(利用nginx日志也可以g ...

  6. AdFind

    C++实现(未开源),用于查询域内信息 http://www.joeware.net/freetools/tools/adfind/index.htm 常用命令如下: 列出域控制器名称: AdFind ...

  7. 题解 P5663 【加工零件【民间数据】】

    博客园体验更佳 讲讲我的做法 确定做法 首先,看到这道题,我直接想到的是递归,于是复杂度就上天了,考虑最短路. 如何用最短路 首先,看一张图 我们该如何解决问题? 问题:\(3\)做\(5\)阶段的零 ...

  8. 《数据库优化》- MySQL视图

    一.什么是视图 视图,是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改,视图基于的表称为基表.视图是存储在数据字典里的一条select语句. 通俗地讲,视图就 ...

  9. Javascript之封装运动函数

    @ 目录 阶段一.仅适用单位带px属性的匀速运动 阶段二.可适用单位不带px属性(如opacity)的匀速运动 阶段三.适用于多元素单一属性的匀速运动 阶段四.适用于多元素单一属性的匀速或缓冲运动 阶 ...

  10. AQS源码详细解读

    AQS源码详细解读 目录 AQS源码详细解读 基础 CAS相关知识 通过标识位进行线程挂起的并发编程范式 MPSC队列的实现技巧 代码讲解 独占模式 独占模式下请求资源 独占模式下的释放资源 共享模式 ...