zoukankan      html  css  js  c++  java
  • 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(); 
    
    ?> 
  • 相关阅读:
    关于Spring和Struts2的整合
    有关Transaction not successfully started问题解决办法
    两种页面跳转
    jdbc连接mysql
    change和modify
    Navicat Premium 15连接Oracle时提示oracle library is not loaded的解决方案
    Oracle创建本地数据库实例及配置监听
    下载Visual Studio 2019离线安装包
    简单的数据库
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序
  • 原文地址:https://www.cnblogs.com/xhds/p/12251389.html
Copyright © 2011-2022 走看看