命令注入,即 Command Injection。是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。

在Web应用中,有时候会用到一些命令执行的函数,如php中system、exec、shell_exec等,当对用户输入的命令没有进行限制或者过滤不严导致用户可以执行任意命令时,就会造成命令执行漏洞。

Low

服务端核心代码如下:


<?php if( isset( $_POST[ 'submit' ] ) ) { $target = $_REQUEST[ 'ip' ]; // Determine OS and execute the ping command.
if (stristr(php_uname('s'), 'Windows NT')) { $cmd = shell_exec( 'ping ' . $target );
echo '<pre>'.$cmd.'</pre>'; } else { $cmd = shell_exec( 'ping -c 3 ' . $target );
echo '<pre>'.$cmd.'</pre>'; } }
?>

函数首先判断环境下的系统,如果是win则执行第一个命令,若是linux执行的命令加上-c选项,以为linux中ping命令是一直执行的。只有加了-c指定发送的跳数才能停止。

可以看到在接收用户输入的地方,对用户的输入没有做任何的处理。不难看出这就是一个典型的命令注入漏洞,输入恶意构造的语句 www.baidu.com && ifconfig会把后面的语句也一并执行,如果把命令换成其他恶意的攻击语句,就会造成严重后果

执行结果:

Medium

服务端核心代码如下:

<?php

if( isset( $_POST[ 'submit'] ) ) {

    $target = $_REQUEST[ 'ip' ];

    // Remove any of the charactars in the array (blacklist).
$substitutions = array(
'&&' => '',
';' => '',
); $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); // Determine OS and execute the ping command.
if (stristr(php_uname('s'), 'Windows NT')) { $cmd = shell_exec( 'ping ' . $target );
echo '<pre>'.$cmd.'</pre>'; } else { $cmd = shell_exec( 'ping -c 3 ' . $target );
echo '<pre>'.$cmd.'</pre>'; }
} ?>

Str_replace()函数的作用是以其他字符替换字符串中的一些字符(区分大小写)。

$target = str_replace( array_keys( $substitutions ), $substitutions, $target );将用户输入的内容,含有“&&”或“;”的替换为空。

其他的部分基本和low相差不大。

这里的源码对用户的输入进行了初步的过滤,过滤掉了一些能够同时执行命令的符号,但是我们知道,拥有同样作用的符号不止“&&”和“;”。所以依然可以进行命令注入。

构造语句: www.baidu.com & ifconfig

& 是前面命令执行后,接着执行后面的命令

执行结果:

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

这个级别的源码和medium级别的源码相差不大,只是将更多的符号加入黑名单。通过这样的确实能够有效的防御之前的诸多思路。但是只是做黑名单的话,总是不够安全的,只要黑名单不够完整,就不是很安全。即使你认为名单已经很完整了。可能还有你不知道的存在可以利用。

黑名单看似过滤了所有的非法字符,但仔细观察到是把”| ”(注意这里|后有一个空格)替换为空字符,但是并没有过滤“|”。

构造语句:127.0.0.1|net user

“|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。

执行结果:

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

?>

stripslashes(string)

stripslashes函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。

explode(separator,string,limit)

把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。

is_numeric(string)

检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。

可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

DVWA Command Injection 解析的更多相关文章

  1. DVWA Command Injection 通关教程

    Command Injection 介绍 命令注入(Command Injection),对一些函数的参数没有做过滤或过滤不严导致的,可以执行系统或者应用指令(CMD命令或者bash命令)的一种注入攻 ...

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

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

  3. DVWA之命令注入(command injection)

    Command injection就是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的 LOW 无论是Windows还是Linux,都可以使用&&连接多个命令 执行 ...

  4. DVWA之Command Injection

    Command Injection Command Injection,即命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的.PHP命令注入攻击漏洞是PHP应用程序中常见 ...

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

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

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

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

  7. DVWA靶场之Command Injection(命令行注入)通关

    Command Injection Low: <?php if( isset( $_POST[ 'Submit' ]  ) ) { // Get input $target = $_REQUES ...

  8. Linux Command Line 解析

    Linux Command Line 解析 0 处理模型 Linux kernel的启动包括很多组件的初始化和相关配置,这些配置参数一般是通过command line进行配置的.在进行后续分析之前,先 ...

  9. RT-SA-2019-005 Cisco RV320 Command Injection Retrieval

    Advisory: Cisco RV320 Command Injection RedTeam Pentesting discovered a command injection vulnerabil ...

随机推荐

  1. 二十一世纪计算 | John Hopcroft:AI革命

    编者按:信息革命的浪潮浩浩汤汤,越来越多的人将注意力转向人工智能,想探索它对人类生产生活所产生的可能影响.人工智能的下一步发展将主要来自深度学习,在这个领域中,更多令人兴奋的话题在等待我们探讨:神经网 ...

  2. History of AI

    人工智能的历史源远流长.在古代的神话传说中,技艺高超的工匠可以制作人造人,并为其赋予智能或意识.[1]现代意义上的AI始于古典哲学家用机械符号处理的观点解释人类思考过程的尝试.20世纪40年代基于抽象 ...

  3. 空间数据导入Oracle数据库备忘

  4. LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解

    题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一 ...

  5. percona-toolkit 之 【pt-query-digest】使用

    背景 关于pt-query-digest的使用场景和方法在percona-toolkit 之 [pt-query-digest]介绍文章里已经做了详细说明,现在开始介绍下如何使用,以及常用的命令. 使 ...

  6. 设计模式-11享元模式(Flyweight Pattern)

    1.模式动机 在面向对象程序设计过程中,有时会面临要创建大量相同或相似对象实例的问题.创建那么多的对象将会耗费很多的系统资源,它是系统性能提高的一个瓶颈. 享元模式就是把相同或相似对象的公共部分提取出 ...

  7. 使用timeit测试Python函数的性能

    timeit是Python标准库内置的小工具,可以快速测试小段代码的性能. 认识timeit timeit 函数: timeit.timeit(stmt, setup,timer, number) 参 ...

  8. clientWidth offsetWidth等视窗尺寸

    clientWidth和offsetWidth clientWidth 是一个只读属性,返回元素的内部宽度,该属性包括内边距,但不包括垂直滚动条(如果有).边框和外边距. offsetWidth 是一 ...

  9. 前端每日实战:7# 视频演示如何用纯 CSS 创作一个 3D 文字跑马灯特效

    效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/GdrrZq 可交互视频教程 此视频是可以交 ...

  10. 025.掌握Service-SVC基础使用

    一 Service简介 1.1 Service概念 Service是Kubernetes的核心概念,通过创建Service,可以为一组具有相同功能的容器应用提供一个统一的入口地址,并且将请求负载分发到 ...