Security ❀ Command Injection 命令注入

Posted 国家级干饭型选手°

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Security ❀ Command Injection 命令注入相关的知识,希望对你有一定的参考价值。

1、low


源码解析:

<?php

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

    // Determine OS and execute the ping command.确定操作系统并执行ping命令
    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>";
}

?>
源码未限制输入内容,因此直接使用命令进行注入攻击;

结果验证:

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

?>

源码将&& 与;过滤,&&为Windows命令分隔符,;为linux命令分隔符,可以使用&与|进行命令注入(| - 或,& - 与)

结果验证:

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

?>

源码内过滤内容增加,但是过滤项中存在一项为“| ”,去掉空格即为不过来项,因此可以使用其进行注入;

结果验证:

4、impossible


源码解析:源码将输入定义为4个整数,完全防护输入为一个IP地址;

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Check Anti-CSRF token 检查Anti-CSRF令牌
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

    // Get input
    $target = $_REQUEST[ 'ip' ];
    $target = stripslashes( $target );

    // Split the IP into 4 octects 将IP分割为4个字节
    $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 生成Anti-CSRF令牌
generateSessionToken();

?>

以上是关于Security ❀ Command Injection 命令注入的主要内容,如果未能解决你的问题,请参考以下文章

如何在url上实现spring security

javax.inject.Inject是哪个jar包里的

lib / javax.inject-1.jar中的javax / inject / Inject.class被lib / javax.inject-2.5.0-b42.jar隐藏在onejar可执行

Java 类型 `javax.inject.Inject` 由多个托管类型生成。请更改 [注册] 属性

SpringBoot启动提示JSR-330 javax.inject.Inject

My life of Honker Security Commando