命令注入(Command Injection)

一、Low

1、服务端代码

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

2、函数介绍:

  • php_uname(mode):返回运行 PHP 的系统的有关信息,mode 是单个字符,用于定义要返回什么信息,其中’s’返回操作系统名称,比如FreeBSD,Windows NT;‘n’返回主机名,比如localhost.example.com;‘r’返回版本名称,比如 5.1.2-RELEASE;‘v’返回版本信息,比如操作系统之间有很大的不同;‘m’返回机器类型,比如i386;‘’a’为默认,包含 “s n r v m” 的所有信息。
  • stristr(string,search,before_search):如果参数before_search为false,搜索参数“search”在参数“string”中第一次出现的位置,并返回该位置后面剩余的字符串,如果参数before_search为true,返回该位置前面剩余的字符串。
  • shell_exec:创建一个新的进程,执行系统命令
    3、漏洞分析:
    4、漏洞利用:
    • 输入 “127.0.0.1&&cat /etc/shadow”,“cat /etc/shadow”命令并未返回结果,这是因为shell_exec的权限有限,无法越权执行安全级别较高的命令。
      命令注入(Command Injection)_第1张图片
      命令注入(Command Injection)_第2张图片
  • 解决方法:
    1)输入“127.0.0.1&&whoami”,获取开启新进程的用户
    命令注入(Command Injection)_第3张图片
    2)修改/etc/sudoers配置文件,赋予www-data用户超级权限
    命令注入(Command Injection)_第4张图片
    1. 输入 “127.0.0.1&&sudo cat /etc/shadow”,获取所有用户密码,危害之大可见一斑。
      命令注入(Command Injection)_第5张图片

二、Medium

1、服务器端代码

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

2、相关函数

  • str_replace:字符串替换

3、漏洞分析

  • 服务端会将用户输入中的“&&”和“;”替换为“ ”,可以使用“&”执行多条命令,也可以使用“&;&”进行绕过,达到“&&”的效果

4、漏洞利用

  • 输入“127.0.0.1&sudo cat /etc/shadow”(comand1 && comand2:comand1和comand2都会执行,comand1 & comand2:comand1执行成功后,comand2才会执行)
    命令注入(Command Injection)_第6张图片

  • 输入“127.0.0.1&;&sudo cat /etc/shadow”
    命令注入(Command Injection)_第7张图片

三、High

1、服务器端代码

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

2、漏洞分析

  • 相比于上一个安全级别的代码,High级别的代码对用户输入黑名单进一步扩充,注意到’| ‘并不是’|’,所以可以利用’|‘进行绕过,在linux系统中,’|'表示管道, comand1 | comand2 表示将comand1命令的执行结果作为命令2的输入参数,然后执行命令2,并且只打印命令2的执行结果。

3、漏洞利用

  • 输入“127.0.0.1|sudo echo “hello””
    命令注入(Command Injection)_第8张图片

三、impossible

1、服务器端代码

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 "
{$cmd}
"; } else { // Ops. Let the user name theres a mistake echo '
ERROR: You have entered an invalid IP.</pre>';
    }
}

// Generate Anti-CSRF token
generateSessionToken();

?> 

2、相关函数

  • stripslashes(string):删除字符串“string”中的反斜杠,并返回剩余字符串。
    3、代码分析
    impossible安全级别的代码加入了Anti-CSRF token,一定程度阻止频繁的命令提交,同时对用户输入进行了严格的限制,只有“数字.数字.数字.数字”的输入才会被执行,因此不存在安全漏洞。

你可能感兴趣的:(命令注入(Command Injection))