Virtual Private Networks and Firewalls
CMPT 471: Networking II
VPN: Use Cases
2
Protect yourself from hackers in untrustworthy Wi-Fi hotspots
VPN: Use Cases
3
Bypass geographic restrictions
VPN: Use Cases
4
Bypassing egress filtering at firewalls
VPN: Use Cases
5
Extend private network (e.g., enterprise, home, etc.)
Many VPN Options
6
Motivation
7
Region 3
Region 2
Region 1
Region 4
Should act as a single private network
Internet
Old Solution
8
Region 3
Region 2
Region 1
Region 4
Other Options?
9
Guarantees of a Private Network
10
What is a VPN?
11
Internet
Client
VPN server
Private Network
Encrypted link
Authenticating clients
What is a VPN?
🡪 IP packets need to be encrypted (including headers)
12
IP hdr
Payload
A
B
Encrypted IP pkt
Routers cannot read/modify IP headers
IPsec Tunneling: Tunnel Mode
13
Kernel
IP pkt
IP layer
Kernel
IP pkt
IP layer
A
B
Encrypted IP pkt
New IP hdr
IPsec hdr
TLS Tunneling
14
Kernel
IP pkt
Kernel
IP pkt
A
B
Encrypted IP pkt
New IP hdr
TCP/UDP hdr
User space
User space
Application
Application
Overview of TLS VPN
15
TLS-based VPN
16
TLS Tunnel
VPN Client
VPN Server
IP hdr
Payload
IP hdr
Payload
Private Network
A
Private Network
B
Encrypted IP pkt
New hdr
Original pkt: A🡪B
Three steps:
1
2
3
Creating a Tunnel
17
TLS Tunnel
1
VPN Client
VPN Server
Forwarding IP Packets
18
VPN Client
IP hdr
Payload
A
TUN/TAP device
Modify routing
TLS Tunnel
2
Releasing IP Packets
19
VPN Server
IP hdr
Payload
B
TLS Tunnel
3
TLS VPN Details
20
Virtual Network Interfaces
21
Application
Application
Transport Layer
Network Layer
Link Layer
Physical NIC
TUN
TAP
TUN/TAP interface
Creating a TUN Interface
22
int createTunDevice()
{
int tunfd;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
tunfd = open("/dev/net/tun", O_RDWR);
ioctl(tunfd, TUNSETIFF, &ifr);
return tunfd;
}
Create a TUN device
Register the device with the kernel
No additional info sent by the driver
Configuring the TUN Interface
23
$ sudo ifconfig tun0 10.0.7.99/24 up
VPN: Big Picture
24
Data to Apps
Protocol Stack
VPN Server
Physical NIC
TUN
VPN Client
Physical NIC
TUN
Routing
Data from Apps
Protocol Stack
tun
Data from other hosts
Routing
tun
Data to other hosts
Network
socket
socket
TLS Tunnel
IP Pkt
IP Pkt
Hosts need to be configured to route traffic to TUN interfaces
10.0.7.0/24
10.0.8.0/24
Routing Packets
25
$ sudo route add -net 10.0.8.0/24 tun0
Details of Building a VPN Tunnel
26
Overview of A Simplified VPN Program
27
Create a TUN interface
Establish the tunnel with other end using sockets
Monitor both TUN and socket interfaces
TUN/socket
Data from TUN, send it to the tunnel
TUN
Data from socket, send it to TUN
socket
Overview of A Simplified VPN Program
28
IP Tunnel
Encrypt
IP Tunnel
Decrypt
TUN
TUN
Simple VPN Tunnel over UDP (in C)
29
Monitoring Multiple Connections (File Descriptors)
30
Program
fd1
fd3
fd2
fd0
fd4
Monitoring File Descriptors
31
while (1) {
fd_set readFDSet;
FD_ZERO(&readFDSet);
FD_SET(sockfd, &readFDSet);
FD_SET(tunfd, &readFDSet);
select(FD_SETSIZE, &readFDSet, NULL, NULL, NULL);
if (FD_ISSET(tunfd, &readFDSet)) tunSelected(tunfd, sockfd);
if (FD_ISSET(sockfd, &readFDSet)) socketSelected(tunfd, sockfd);
}
Register the fds
Register the fds
IO multiplexing
From TUN to Socket
32
void tunSelected(int tunfd, int sockfd){
int len;
char buff[BUFF_SIZE];
bzero(buff, BUFF_SIZE);
len = read(tunfd, buff, BUFF_SIZE);
sendto(sockfd, buff, len, 0, (struct sockaddr *) &peerAddr,
sizeof(peerAddr));
}
Pkt is encrypted before sending
IP Tunnel
Encrypt
TUN
From Socket to TUN
33
void socketSelected (int tunfd, int sockfd){
int len;
char buff[BUFF_SIZE];
bzero(buff, BUFF_SIZE);
len = recvfrom(sockfd, buff, BUFF_SIZE, 0, NULL, NULL);
write(tunfd, buff, len);
}
Pkt should be decrypted
IP Tunnel
Decrypt
TUN
Simple Demo of Tunneling using Python
34
#!/usr/bin/env python3
import fcntl
import struct
import os
import time
from scapy.all import *
�TUNSETIFF = 0x400454ca
IFF_TUN = 0x0001
IFF_TAP = 0x0002
IFF_NO_PI = 0x1000
�# Create the tun interface
tun = os.open("/dev/net/tun", os.O_RDWR)
ifr = struct.pack('16sH', b'tun%d', IFF_TUN | IFF_NO_PI)
ifname_bytes = fcntl.ioctl(tun, TUNSETIFF, ifr)
�# Get the interface name
ifname = ifname_bytes.decode('UTF-8')[:16].strip("\x00")
print("Interface Name: {}".format(ifname))
while True:
# read a packet from tun
pkt = os.read(tun, 2048)
if pkt:
ip = IP(pkt) #create an IP object using scapy
#now, we are supposed to encapsulate,� # encrypt, and send it to VPN Server
# instead, we just print it
print(ip.summary())
put this code in tun.py and run it:
$ chmod a+x ./tun.py
$ sudo ./tun.py
Simple Demo of Tunneling using Python
35
sudo ./tun.py # create tun0
ifconfig –a # check it exists, but DOWN (in another terminal)
sudo ifconfig tun0 10.0.2.99/24 up #assign IP address to tun0 and bring it up (check the ip address)
ifconfig -a # check again
sudo route add -net 10.0.8.0/24 tun0 #add route to the remote network 10.0.8.0/24 �
route # check the routing table
ping 10.0.8.1 -c2 #ping a host in 10.0.8.0/24 network (packet should go to tun0)
ping 10.0.9.1 -c2 #ping another network: tun0 should not receive it
Firewalls
CMPT 471: Networking II
What is a Firewall?
37
Firewall
Protected Network
Network
Requirements of a Firewall [Bellovin and Cheswick’94]
38
Firewall Policy
39
Firewall Policy
40
Firewall Actions
41
Ingress and Egress Filtering
42
Home Network
Home Network
Other Functions
43
Header
Payload
Header
Payload
Types of Firewalls
44
Link
Network
Transport
Application
Physical
End-to-End transport connection
End-to-End transport connection
Types of Firewalls
45
Internal transport connection
Link
Network
Transport
Application
Physical
External transport connection
Link
Network
Transport
Application
Physical
App. Firewall
Packet Filtering Firewalls
46
netfilter
47
netfilter
INPUT
Hooks
Network Drivers
Network Services
kernel
user
OUTPUT
Netfilter: Five Hooks
48
NF_IP_POST_ROUTING
NF_IP_PRE_ROUTING
Routing
NF_IP_FORWARD
NF_IP_LOCAL_IN
Routing
Network Stack
NF_IP_LOCAL_OUT
Is the pkt destined for this host, or another host?
1
2
Consumed by the host
Generated by the host
netfilter Calling Order
49
Example: Block Outgoing Telnet Packets
50
unsigned int telnetFilter(void *priv, struct sk_buff *skb,
const struct nf_hook_state *state)
{
struct iphdr *iph;
struct tcphdr *tcph;
iph = ip_hdr(skb);
tcph = (void *) iph+iph->ihl*4; // ihl: IP header length
if (iph->protocol == IPPROTO_TCP && tcph->dest == htons(23)) {
return NF_DROP;
} else {
return NF_ACCEPT;
}
}
Example: Block Outgoing Telnet Packets
51
static struct nf_hook_ops telnetFilterHook;
int setUpFilter(void) {
telnetFilterHook.hook = telnetFilter;
telnetFilterHook.hooknum = NF_INET_POST_ROUTING;
telnetFilterHook.pf = PF_INET;
telnetFilterHook.priority = NF_IP_PRI_FIRST;
// Register the hook
nf_register_hook(&telnetFilterHook);
return 0;
}
iptables
52
netfilter
INPUT
Hooks
Network Drivers
Network Services
kernel
user
OUTPUT
iptables
iptables Structure
53
Table
Table
Table
Chain 1
Rule 1
Rule N
…
Chain 2
Rule 1
Rule N
…
iptables: Built-in Tables
54
raw
mangle
nat
filter
INPUT
Rule 1
…
FORWARD
Rule 1
…
OUTPUT
Rule 1
…
The filter Table
55
POST_ROUTING
PRE_ROUTING
Routing
FORWARD
LOCAL_IN
Routing
Network Stack
LOCAL_OUT
The nat Table
56
POST_ROUTING
PRE_ROUTING
Routing
FORWARD
LOCAL_IN
Routing
Network Stack
LOCAL_OUT
The mangle Table
57
POST_ROUTING
PRE_ROUTING
Routing
FORWARD
LOCAL_IN
Routing
Network Stack
LOCAL_OUT
The raw Table
58
POST_ROUTING
PRE_ROUTING
Routing
FORWARD
LOCAL_IN
Routing
Network Stack
LOCAL_OUT
Table/Chain Traversal Order (simplified)
59
Routing
Routing
Routing
Network Stack
mangle
PREROUTING
nat
PREROUTING
PREROUTING
mangle
POSTROUTING
nat
POSTROUTING
POSTROUTING
mangle
FORWARD
filter
FORWARD
FORWARD
mangle
OUTPUT
nat
OUTPUT
filter
OUTPUT
OUTPUT
mangle
INPUT
nat
INPUT
filter
INPUT
INPUT
Packet
Building a Simple Firewall
60
Checking Rules
61
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
No rules yet!
No rules yet!
No rules yet!
$ sudo iptables –t filter -F
To flush filter table
List all entries in the filter table
Use –t to show other tables
iptables notes:
62
General format of the command:
Our sFW
63
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
Iptables –A OUTPUT –p tcp all –j ACCEPT
Allow SSH, HTTP, and all outgoing TCP
iptables -A INPUT -p all -i lo -j ACCEPT
Allow loopback interface
Our sFW
64
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
Allow DNS
Our sFW
65
iptables -P OUTPUT ACCEPT
Allow outgoing traffic
iptables -P INPUT DROP
Iptables –P OUTPUT DROP
iptables -P FORWARD DROP
Drop all other traffic
Note:
-P: Policy
-p: Protocol
66
#!/bin/bash
# to run this script (sFW.sh):
## chmod a+x sFW.sh.
## sudo ./sFW.sh
## remember to cleanup the rules (run cleanup.sh) after you are done
# Allow incoming SSH, HTTP and all outgoing TCP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp -j ACCEPT
# Allow loopback interface.
# -I INPUT 1: insert a rule at 1 first position of the INPUT chain
# -i lo: local interface
iptables -I INPUT 1 -i lo -j ACCEPT
# Allow DNS in both ways. –A: appends rule to the chain
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
# Set default filter policy (-P) to DROP on the three chains.
# note: rules are applied in order. Thus, the DROP will apply only if none #of the above ACCEPT rules matches.
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
Let us try a few things
$ sudo iptables –L
$ sudo iptables –t nat –L
$ ping –c1 google.com (before fw)
$ sudo ./sFW.sh (apply rules. Note: they do not allow ICMP)
$ ping –c1 google.com (after fw. Should fail)
# add rule to FW to allow ICMP
$ sudo iptables -A INPUT –p icmp --icmp-type any –j ACCEPT
$sudo iptables –L
$ ping –c1 google.com (try again, works? No. Why? )
# allow ICMP in the other direction as well
$ sudo iptables -A OUTPUT –p icmp --icmp-type any –j ACCEPT
Demo of Simple Firewall
67
#!/bin/bash
# to run this script (cleanup.sh):
## chmod a+x cleanupsh.sh
## sudo ./cleanup.sh
# Setup all the default policies to ACCEPT packets
iptables -P OUTPUT ACCEPT
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
# Flush all exiting configurations
iptables -F
Demo of Simple Firewall
# restore all default policies
$ sudo ./cleanup.sh
Evading Firewalls: Rationale
68
pkt
Regular route
pkt
Tunneled route
Summary
69