php命令注入函数及dvwa命令注入实践

命令注入漏洞

注:命令注入漏洞的分析,及含有命令注入漏洞的函数解析php

含有命令注入漏洞的函数:system()、exec()、passthru()、shell_exec()、``(与shell_exec()功能相同)linux

1、 基于DVWA环境的命令注入漏洞(shell_exec)

一、 函数用法
String shell_exec(string command)
command 要执行的命令
二、 low级别
源码:shell

<?php

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

?>

源码分析:
函数首先判断环境下的系统,若是是win则执行第一个命令,如果linux执行的命令加上-c选项,觉得linux中ping命令是一直执行的。只有加了-c指定发送的跳数才能中止。
能够看到在接收用户输入的地方,对用户的输入没有作任何的处理。不难看出这就是一个典型的命令注入漏洞。并且孩子是最轻易。
咱们正常测试一下:
php命令注入函数及dvwa命令注入实践
能够看到,正常返回的是ping返回的数据。
咱们经过这个命令执行漏洞进行测试一下:
构造咱们的语句:10.39.1.4 | net user
解释: | 的意思是前面命令的输出结果做为后面命令的输入。
net user 查看当前系统中存在哪些用户
测试:
php命令注入函数及dvwa命令注入实践
能够看到当前系统中存在三个用户。若是做为×××去利用的话就可使用命令去建立一个用户。就不在演示
漏洞分析:不处理用户的任何输入就直接执行函数中的命令。数组

知识扩展:
; - 分号在linux命令执行的时候,能够直接执行几条命令,命令与命令之间用分号隔开。
& 前面的命令执行后接着执行候命的命令
&& 前面的命令执行成功后才能够执行下面的命令
| 前面的命令输出结果最为后面命令输入的内容
|| 前面的命令执行失败后才会执行后面的命令浏览器

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

?>

源码分析:
Str_replace()函数,以其余字符替换字符串中的一些字符(区分大小写)。
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );将用户输入的内容,含有&&或;的替换为空。
其余的部分基本和low相差不大。
这里的源码对用户的输入进行了初步的过滤,过滤掉了一些可以同时执行命令的符号,可是咱们知道,拥有一样做用的符号不止&&和;。因此依然能够进行命令注入。
命令注入测试:
构造语句: 10.39.1.4 & net user
& 前面命令执行后接着执行后面的命令
测试:
php命令注入函数及dvwa命令注入实践
依然得到了执行的结果session

漏洞分析:此级别下的源码虽然对用户的输入设置了过滤,可是没有将特殊符号过滤彻底,仅仅设置黑名单是不够的,你不知道用户会输入什么,形成有心者亦能够利用此漏洞。 ide

四、 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级别的源码相差不大,只是将更多的符号加入黑名单。
经过这样的确实可以有效的防护以前的诸多思路。
测试:
输入10.39.1.4 | net user
php命令注入函数及dvwa命令注入实践
已经不能用那些方法了。具体的利用我也没有找到合适的方法。

漏洞分析:只是作黑名单的话,老是不够安全的,只要黑名单不够完整,就不是很安全。即便你认为名单已经很完整了。可能还有你不知道的存在能够利用。

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

源码分析:
Explode()函数,将字符串变为数组。这里就是将咱们输入的ip,变成数组
而后判断数组的前四组数是否为数字,而且数组中有四个对象。不知足就会报错提醒。也就是说这里只容许你输入四组数字。
若果判断为true的话,就会再将这四组数经过点链接起来再就行ping命令。
就不测试了,这样的源码已经杜绝了你的全部命令注入×××。

2、eval()函数形成的命令注入漏洞

一、函数用法
eval(phpcode)
Phpcode 规定要计算的php代码。一般用分号结束每句代码的执行。
二、环境源码

<?php
$var = "var";
if(isset($_GET["name"])){
    $arg = $_GET["name"];
    eval("\$var=$arg;");
    echo "\$var = ".$var;
}

?>

构造语句:

name=phpinfo()

测试效果:
php命令注入函数及dvwa命令注入实践
三、 ctf题目实例
题目地址bugku中的本地包含:http://120.24.86.145:8003/
题目解析:
源码:

<?php 
    include "flag.php"; 
    $a = @$_REQUEST['hello']; 
    eval( "var_dump($a);"); 
    show_source(__FILE__); 
?>

构造语句:
Hello = file(‘flag.php’)
解析,当参数接收到的构造的语句的时候,代码就会变为
Eval(var_dump(file(‘flag.php’)))
Eval函数,执行函数体没的php代码;file()函数把整个文件读到一个数组中。Var_dump()函数 输出。
因此执行结果就是将flag.php中的内容以数组的形式输出出来。
php命令注入函数及dvwa命令注入实践
获得flag

3、system()函数形成的漏洞

一、函数用法:
System(string command,int &return_var)
Command 要执行的命令
Return_var 存放命令的执行后的状态值
二、环境源码:

<?php
$cmd = $_GET['cmd'];
if(isset($cmd)){
    echo system("dir".$cmd);
}
?>

构造语句:
Cmd=| net user

测试:
php命令注入函数及dvwa命令注入实践
经过漏洞咱们得到了系统中存在哪些用户,一样的咱们也能够经过这样的方法在系统中建立咱们本身的用户。并能够加入到管理员组中。这里就不在说了。

4、shell_exec()函数形成的漏洞

一、函数用法:
shell_exec(string command)
command 要执行的命令
二、环境源码:

<?php
$cmd = $_GET['cmd'];
if(isset($cmd)){
    echo "<h3>";
    echo shell_exec("dir".$cmd);
    echo "<h3>";
}
?>

四、 测试:
构造语句: | net user

php命令注入函数及dvwa命令注入实践
实现方法和上一个函数是同样的。一样的函数还有exec()和符号

5、passthru()函数形成的漏洞
一、函数用法:
void passthru (string command, int &return_var)
command 要执行的命令
return_var 存放执行命令后的状态值

同 exec() 函数相似, passthru() 函数 也是用来执行外部命令(command)的。 当所执行的 Unix 命令输出二进制数据, 而且须要直接传送到浏览器的时候, 须要用此函数来替代 exec() 或 system() 函数。
二、环境源码:

<?php
$cmd = $_GET['cmd'];
if(isset($cmd)){

    echo passthru($cmd);
}
?>

三、测试
构造语句 cmd=net user
php命令注入函数及dvwa命令注入实践
得到用户列表

6、总结

总结以上全部函数漏洞形成的命令注入漏洞,每个例子都是由于没有对用户的输入进行处理。在防护漏洞的时候,必定明白一个道理,全部用户的输入都是有害的。全部的输入都是不值得相信的。
相关文章
相关标签/搜索