DWVA-命令注入漏洞闯关(Command Injection)

前言

Vulnerability: Command Injection

LOW级别

代码:

<?php
if( isset( $_POST[ ‘Submit‘ ]  ) ) {
    // 几首一个变量为ip的参数
    $target = $_REQUEST[ ‘ip‘ ];
    // 判断系统
    if( stristr( php_uname( ‘s‘ ), ‘Windows NT‘ ) ) {
        // Windows
        $cmd = shell_exec( ‘ping  ‘ . $target );
    }
    else {
        // *nix
        $cmd = shell_exec( ‘ping  -c 4 ‘ . $target );
    }
    echo "<pre>{$cmd}</pre>";
}
?> 

我们分析这个靶场的代码可以看到$_REQUEST接受用户传过来的值 我们并没有看到有什么过滤机制的代码所以 可以输入任何东西。

测试执行ping127.0.0.1没问题 我们利用管道命令来 再加命令语句

Medium级别

代码:

<?php 

if( isset( $_POST[ ‘Submit‘ ]  ) ) { 

    $target = $_REQUEST[ ‘ip‘ ]; 

    // 黑名单过滤
    $substitutions = array(
        ‘&&‘ => ‘‘,
        ‘;‘  => ‘‘,
    ); 

    // 如果有黑名单字符则进行替换
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); 

    // 判断系统
    if( stristr( php_uname( ‘s‘ ), ‘Windows NT‘ ) ) {
        // Windows
        $cmd = shell_exec( ‘ping  ‘ . $target );
    }
    else {
        // *nix
        $cmd = shell_exec( ‘ping  -c 4 ‘ . $target );
    }
    echo "<pre>{$cmd}</pre>";
} 

?> 

我们注意6-9行 这里是个黑名单过滤 我们可以想办法绕过    这里虽然把&&和分号;加入了黑名单,但是我们还可以用逻辑或(||)、管道符(|)或(&)来命令执行  绕过

High级别

代码:

<?php 

if( isset( $_POST[ ‘Submit‘ ]  ) ) {
    // Get input
    $target = trim($_REQUEST[ ‘ip‘ ]); 

    // 白名单
    $substitutions = array(
        ‘&‘  => ‘‘,
        ‘;‘  => ‘‘,
        ‘| ‘ => ‘‘, //主义这一行 l后面是空格说明仅仅过滤了l+空格 没过滤单独的l
        ‘-‘  => ‘‘,
        ‘$‘  => ‘‘,
        ‘(‘  => ‘‘,
        ‘)‘  => ‘‘,
        ‘`‘  => ‘‘,
        ‘||‘ => ‘‘,
    ); 

    $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); 

    if( stristr( php_uname( ‘s‘ ), ‘Windows NT‘ ) ) {
        // Windows
        $cmd = shell_exec( ‘ping  ‘ . $target );
    }
    else {
        // *nix
        $cmd = shell_exec( ‘ping  -c 4 ‘ . $target );
    } 

    echo "<pre>{$cmd}</pre>";
} 

?> 

127.0.0.1|net user照样绕过

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 ); //stripslashes()过滤删除由 addslashes() 函数添加的反斜杠。

    // 以.分割命令
    $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 ) ) {
        // 以.拼接
        $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(); 

?> 

原文地址:https://www.cnblogs.com/xhds/p/12251389.html

时间: 2024-08-28 00:11:05

DWVA-命令注入漏洞闯关(Command Injection)的相关文章

Commix命令注入漏洞利用

介绍 项目地址:https://github.com/stasinopoulos/commix Commix是一个使用Python开发的漏洞测试工具,这个工具是为了方便的检测一个请求是否存在命令注入漏洞,并且对其进行测试,在其作者发布的最新版本中支持直接直接导入burp的历史记录进行检测,大大提高了易用性. 使用 选项:     -v VERBOSE详细程度级别(0-1,默认值:0).     --version 显示版本号并退出.     --output-dir=.. 设置自定义输出目录路径

命令注入漏洞总结

前言 漏洞本身原理很简单,用户的输入作为 要执行命令的一部分被 一些执行系统命令的函数去执行,如果不注意就能够让攻击者执行系统命令. 正文 相关的工具 https://github.com/ewilded/shelling https://github.com/commixproject/commix 测试环境 win 10 phpstudy https://github.com/commixproject/commix-testbed/ 部署在 http://test.commix.top 一

sqli-labs注入lesson3-4闯关秘籍

·lesson 3 与第一二关不同的是,这一关是基于错误的get单引号变形字符型注入 要使用 ') 进行闭合  (ps:博主自己理解为字符型注入,是不过是需要加括号进行闭合,适用于博主自己的方便记忆的法子,仅供参考 ) 1.判断是否存在注入 ?id=1') --+ 2.使用order by 猜测字段数 ?id=1') order by 3 --+ ?id=1') order by 4 --+ 3.确定显示的字段显示位 ?id=1') and 1=2 union select 1,2,3 --+

安全性测试入门(二):Command Injection命令行注入攻击和防御

安全性测试入门(二):Command Injection命令行注入攻击和防御 本篇继续对于安全性测试话题,结合DVWA进行研习. Command Injection:命令注入攻击. 1. Command Injection命令注入 命令注入是通过在应用中执行宿主操作系统的命令,来达到破坏目的的一种攻击方式.如果我们的应用程序将不安全的用户输入传递给了系统命令解析器(shell),那么命令攻击就有可能发生. 通常来说,由应用程序传递操作系统命令会赋有和应用一样的权限,所以如果没有合理防御机制会给系

DVWA系列(四)----Command Injection (命令行注入)

一.攻击模块2:Command Injection(命令注入) 命令注入攻击的常见模式为:仅仅需要输入数据的场合,却伴随着数据同时输入了恶意代码,而装载数据的系统对此并未设计良好的过滤过程,导致恶意代码也一并执行,最终导致信息泄露或者正常数据的破坏. PHP命令注入攻击漏洞是php应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!.DedeCMS等都曾经存在过该类型漏洞. 二.源码分析 1.Low(低)级别 [html] view plain copy <?php if( is

Command Injection(命令行注入)

实战部分: 说明:这里我用的是OWASP的一个平台和DVWA 下面简单说一下安装方法(windows下):     先下载webscarab-current.zip(这个自带tomcat,还有一个下载方式是war文件,需要自己安装tomcat,建议使用第一个),地址为http://www.owasp.org/index.php/Category:OWASP_WebGoat_Project,解压到一个文件夹,运行webgoat.bat即可启动其自带的tomcat,通过访问http://localh

PHP漏洞全解(二)-命令注入攻击

本文主要介绍针对PHP网站常见的攻击方式中的命令攻击.Command Injection,即命令注入攻击,是指这样一种攻击手段,黑客通过把HTML代码输入一个输入机制(例如缺乏有效验证限制的表格域)来改变网页的动态 生成的内容.使用系统命令是一项危险的操作,尤其在你试图使用远程数据来构造要执行的命令时更是如此.如果使用了被污染数据,命令注入漏洞就产生了. 命令注入攻击 PHP中可以使用下列5个函数来执行外部的应用程序或函数 system.exec.passthru.shell_exec.“(与s

命令注入工具Commix

命令注入工具Commix 命令注入(Command Injection)攻击是针对Web应用的一种攻击方式.很多Web应用会读取用户提交的数据,然后传递到系统Shell,执行特定的操作,如为用户创建单独的目录或文件.搜索特定的文件.如果对用户提交的数据缺少严格的验证,就会造成命令注入漏洞. Kali Linux提供针对该漏洞的专向工具Commix.该工具支持Results-Base和Blind两类注入方式.它以命令行方式运行.它会自动攻击漏洞,枚举目标主机信息,并允许用户上传恶意文件,达到控制主

【笔记】网易微专业-Web安全工程师-04.WEB安全实战-3.命令注入

命令注入(Command Injection):是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的. 前面的基础课程中,我们提到命令注入需要三个条件: 1. 是否调用系统命令? 2. 函数/参数是否可控? 3. 是否拼接输入? 具体怎么应用,我们在接下去的实战中学习和体会. DVWA实战: 1. 打开phpStudy或xampp,运行Apach和MySQL: 2. 浏览器进入DVWA主界面,在左侧栏选择DVWA Security安全等级为Low,然后进入Command Inj