命令执行漏洞

定义:服务器端没有对用户提交的命令进行严格的过滤,而当用户可以控制命令执行函数里的参数时,便可以将恶意系统命令当成正常系统命令执行,从而形成系统攻击。

漏洞危害:

继承Web服务程序的权限去执行系统命令或读写文件
反弹shell
控制整个网站甚至控制服务器
进一步内网渗透

PHP中的危险函数:

  1. system:成功则返回命令输出的最后一行,失败则返回FALSE
  2. exec:命令执行结果的最后一行内容。
  3. shell_exec:命令执行的输出。如果执行过程中发生错误或者进程不产生输出,则返回NULL
  4. passthru:执行外部程序并且显示原始输出。
  5. eval:将输入的字符串参数当做PHP程序代码来执行。
  6. assertassert() 会检查指定的 assertion 并在结果为 FALSE 时采取适当的行动
  7. preg_replacepreg_replace 函数执行一个正则表达式的搜索和替换。
  8. call_user_func call_user_func 把第一个参数作为回调函数调用

①eval()函数
定义和用法:
eval() 函数把字符串按照 PHP 代码来计算。
该字符串必须是合法的 PHP 代码,且必须以分号结尾。
如果没有在代码字符串中调用 return 语句,则返回 NULL。如果代码中存在解析错误,则 eval() 函数返回 false。

语法

eval(phpcode)
phpcode 必需。规定要计算的 PHP 代码。

②assert()函数

定义和用法:
检查一个断言是否为 FALSE

语法

PHP 5
bool assert ( mixed $assertion [, string $description ] )
PHP 7
bool assert ( mixed $assertion [, Throwable $exception ] )
assert() 会检查指定的 assertion 并在结果为 FALSE 时采取适当的行动

例子

<?php
$a = $_GET['a'];
assert($a);
?>
http://127.0.0.1/oscommand/1.php?a=phpinfo();
http://127.0.0.1/oscommand/1.php?a=phpinfo()

eval()函数 VS assert()函数
注意:eval()函数正确执行需要满足php的代码规范,而assert()函数则不存在这个问题,对于php的代码规范要求不高

③preg_replace()函数

定义和语法:
preg_replace 函数执行一个正则表达式的搜索和替换。

例子

<?php
$a = $_GET['a'];
echo preg_replace("/test/e", $a, "just test!")
?>
http://127.0.0.1/oscommand/1.php?a=phpinfo()

ps: 在php5.4及以下版本中,preg_replace()可正常执行代码,而在php5.5及后续版本中会提醒”/e”修饰符已被弃用,要求用preg_replace_callback()函数来代替。

④call_user_func()函数
定义和用法:
call_user_func — 把第一个参数作为回调函数调用

语法

mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $… ]] )
第一个参数 callback 是被调用的回调函数,其余参数是回调函数的参数。

例子

<?php
call_user_func($_GET['a'],$_GET['b']);
?>
http://127.0.0.1/oscommand/1.php?a=assert&b=phpinfo()

命令连接符:

在windows和linux都支持,如果程序没有进行过滤,那么我们可以通过连接符来执行多条命令。

command1 && command2 先执行Command 1,执行成功后执行Command 2,否则不执行Command 2
command1 | command2 只执行command2
command1 & command2 先执行Command 1,不管是否成功,都会执行Command 2

靶场:DVWA

等级为:low

构造payload:#127.0.0.1&&dir
CTF 命令and代码执行 - 图1
我们可以通过dir命令,当前靶场所在的文件目录。

通过#127.0.0.1&ipconfig看到了本机IP
CTF 命令and代码执行 - 图2

查看源码:

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

看见了php_uname()函数,stristr()函数和shell_exec()函数

php_uname — 返回运行 PHP 的系统的有关信息
stristr() 函数搜索字符串在另一字符串中的第一次出现,该函数是不区分大小写的。
shell_exec — 通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回。

low等级只是用shell_exec和strietr函数对参数进行处理,没有对我们传入的IP进行严格的过滤。我们就给了我们命令攻击的机会。

等级为:medium

我们构造:#127.0.0.1&&dir
CTF 命令and代码执行 - 图3
我们的&&的被过滤掉了
新的payload:#127.0.0.1|dir
CTF 命令and代码执行 - 图4
也成功了

换个payload:#127.0.0.1&dir
CTF 命令and代码执行 - 图5
也成功了

看一下源码:

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

看到源码里对我们的&&和;进行了过滤;但是我们也可以使用&&&,&和|

等级为:high

试了几种payload,只有#127.0.0.1|dir成功了
CTF 命令and代码执行 - 图6
其他的结果
CTF 命令and代码执行 - 图7
程序应该把我们用的拼接字符全部都过滤了
我们应该用其他的方式绕过。
看一下源码:

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

把我们使用的拼接字符换成了空格,但是它过滤了|没有过滤|这就给了我们机会。

看了一下黑名单好像只有||它的旁边可以加个空格才可以绕过
CTF 命令and代码执行 - 图8
注意:#127.0.0.1|可以拼接很多的DOS命令,什么注册表啊,事件查看器啊等等DOS命令,细思极恐!

代码执行漏洞

靶场:DoraBox

代码执行漏洞的定义:代码执行通常指将可执行代码注入到当前页面中,比如PHP的eval函数,可以将字符串代表的代码作为PHP代码执行,当前用户能够控制这段字符串时,将产生代码执行漏洞。

常见的代码执行相关函数:
PHP: eval、assert、preg_replace()、+/e模式(PHP版本<5.5.0)

Javascript: eval

Python: exec

最简单的利用
输入框内:phpinfo()
CTF 命令and代码执行 - 图9
想上传个一句话木马,结果被发现了由于assert()函数的检查。
CTF 命令and代码执行 - 图10
试了几个靶场好像上传一句话都不行,就这样吧!!!