Computer and Network Security Lecture18
Computer and Network Security Lecture18
March 9, 2023
5:37pm
Goals:
2
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
interfaces, etc.), on the other. The core part of an OS is usually referred to as its kernel. Unless you
3
Computer and Network Security by Avi Kak Lecture 18
are using highly specialized hardware, access by a user program to the hardware in a general-purpose
computing platform must go through the kernel. By the same token, any new data made available by the
hardware in such general-purpose machines is likely to be seen first by the kernel. Therefore, when a new
data packet becomes available at a network interface, the kernel is in a position to immediately
determine its fate — provided the kernel has the TCP/IP capability built into it. Just imagine
how much slower it would be if a packet coming off a network interface had to be handed over
by the kernel to a user-level process for its processing. Kernel-level packet filtering is
particularly efficient in Linux because of the monolithic nature of the kernel. Linux is
monolithic despite the fact that much of its capability these days comes in the form of loadable
kernel modules. In general, a kernel is monolithic when its interaction with the hardware takes
place in the same address space in which the kernel itself is being executed. (The “loadable
kernel modules” of Linux that you can see with a command like lsmod are executed in the same
address space as the kernel itself.) The opposite of a monolithic kernel is a microkernel in which
the interaction with the hardware is delegated to different user-level processes (and, thus, is
subject to address-space translations required for process execution). Recall that each process
comes with its own address space that must be translated into actual memory addresses when
the process is executed. For a very fascinating discussion on monolithic kernels vs.
microkernels at the dawn of the Linux movement (in the early 90s), see
prophet of Linux, and Andrew Tanenbaum, the high-priest of operating systems in general. Even though this
]
discussion is now almost 30 years old, much of what you’ll find there remains relevant today.
4
Computer and Network Security by Avi Kak Lecture 18
The iptables tool inserts and deletes rules from the kernel’s
packet filtering tables. Ordinarily, these rules created by the
iptables command would be lost on reboot. However, you can
make the rules permanent with the commands iptables-save
and iptables-restore. The other way is to put the commands
required to set up your rules in an initialization script.
Despite its many advantages over iptables, there has not yet
5
Computer and Network Security by Avi Kak Lecture 18
If you would like to see how you can transition from iptables to
nftables, here is a wonderful document you can read:
https//www.sans.org/reading-room/whitepapers/firewalls/nftables-second-language-35937
6
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
The iptables command with all its options can appear at first
sight to be daunting to use. The “demo” presented in this
section illustrates how easy it is to use this command. Basically,
I will show how you can create a single-rule firewall to achieve
some pretty amazing protection for your computer.
– Demo Goal 2: How you can allow others to ssh into your
machine, but block it for every other access.
7
Computer and Network Security by Avi Kak Lecture 18
sudo iptables -L
you will see the following sort of output in the terminal window:
8
Computer and Network Security by Avi Kak Lecture 18
Later in this lecture, I will talk about the fact the iptables
supports four tables: filter, mangle, nat, and raw. I will also
mention later that the command ‘iptables -L’ is really a short
form for the more table-specific command ‘iptables -L -t
filter’ for examining the contents of the filter table. [So the
output shown previously tells us that there is currently nothing in only the filter
table. But note that the packets may still be subject to filtering by the rules in the
other tables. Later in this demo I will show an example in which the packets of a
certain kind will be denied entry into the Ubuntu laptop even when the filter table
has nothing in it.]
If the output you see for the ‘iptables -L’ command is different
from what I have shown on the previous slide, please flush the
filter table (meaning get rid of the rules in the filter table) by
sudo iptables -F
I should also add that for this demo to work in the manner
presented, ideally you should be flushing out all of the rules
(possibly after you have saved the rules by iptables-save using
the syntax I will show later) in all of the tables by
9
Computer and Network Security by Avi Kak Lecture 18
Let’s now address the first goal of this demo: You don’t
want others to be able to ping your Ubuntu laptop.
where the ‘-A INPUT’ option says to append a new rule to the
INPUT chain of the filter table. The ‘-p icmp’ option specifies
that the rule is to be applied to ICMP packets only. [The last part
of Section 16.2 of Lecture 16 mentions some of the more important types
of ICMP messages] The next option in the command line shown
above mentions what specific subtype of the ICMP packets this
rule applies to. Finally, ‘-j DROP’ specifies the action to be taken
10
Computer and Network Security by Avi Kak Lecture 18
for such packets. [As I will explain later, the above command enters a rule in
the INPUT chain of the filter table. This rule says to drop all incoming icmp
packets that are of the type echo-request. As stated in Section 18.10 of this lecture,
that is the type of ping ICMP packets.]
Now use the other machine to ping the Ubuntu laptop by using
either the ‘ping hostname’ syntax or the ‘ping xxx.xxx.xxx.xxx’ syntax
where the argument to ping is the IP address. You will notice
that you will not get back any echos from the Ubuntu machine.
If you had pinged the Ubuntu machine prior to the entry of the
above firewall rule, you would have received the normal echos
from that machine. [On some platforms, such as Solaris, you may have to use ‘ping -s’ to
get the same behavior as what you get with ‘ping’ in Ubuntu.]
To get ready for our second demo goal, now delete the rule you
entered above by
sudo iptables -F
Subsequently, if you execute ‘iptables -L’ again, you will see
again the empty chains of the filter table.
Recall that the objective now is to allow others to ssh into our
Ubuntu laptop, but we we do not want the Ubuntu laptop to
respond to any other service request coming from other
11
Computer and Network Security by Avi Kak Lecture 18
where the ‘-A INPUT’ option says to append the rules to the INPUT
chain of the filter table. The ‘-p tcp’ option says the rule is to
be applied to TCP packets. The next option mentions the
destination port on the local machine for these incoming
packets. [The meaning of “destination port” here corresponds to the field of the same
name in the TCP header you saw in Lecture 16. So if the value stored in that field of the TCP
header in the incoming packet is decimal 22, it will be accepted by the first rule shown
packets. Recall that 22 is the port registered for the SSH service.
To see that you have entered two new rules in the INPUTchain of
the filter table, execute the ‘sudo iptables -L’ command. You
should see the following:
12
Computer and Network Security by Avi Kak Lecture 18
Now when you use the other laptop to ssh into the Ubuntu
laptop with its firewall set as above, you should experience no
problems. However, if the other laptop makes any other type of
access (such as by ping) to the Ubuntu laptop, you will receive
“Port Unreachable” error message. If we had used DROP instead
of REJECT in the second rule we entered with the iptables
command, when the other laptop makes any access other than
ssh to the Ubuntu laptop, the other laptop would not receive
back any error messages. [When we entered the second iptables command
line, we did not specify the -reject-with option, yet it shows up in the second rule
in the filter table. Note that, as opposed to DROP, the job of REJECT is to send back
an error message. If you don’t specify what this error message should be, iptables
will by default use the icmp-port-unreachable option that sends back the Dest
Unreachable message.]
When you associate a “reject” decision with
a port, that means that you are closing that port. And when
you associate a “drop” decision with a port, that means that
you are not only closing the port, you are also making it
invisible to the rest of the network.
13
Computer and Network Security by Avi Kak Lecture 18
To see the effect of the second rule — the REJECT rule — try
pinging the Ubuntu laptop and see what happens. The machine
that is doing the pinging will receive and display a ‘Destination
Port Unreachable’ message.
To get ready for our third demo goal, now delete the two rules
you entered above by
sudo iptables -F
Recall that the goal of this part of the demo is to reject all
requests for new connections coming from other hosts in the
network. As mentioned in Lecture 16, when a host wants to
make a new connection with your machine, it sends your
machine a SYN packet. To block all such packets, we could use a
rule very similar to what we have shown so far. But, just to add
an interesting twist to the demo, we will use the mangle table for
the purpose. So go ahead and execute the following command
line as sudo:
sudo iptables -t mangle -A PREROUTING -p tcp -m tcp --tcp-flags SYN NONE -j DROP
14
Computer and Network Security by Avi Kak Lecture 18
The ‘-t’ option says that the new rule is meant for the mangle
table. We want the rule to be appended to the PREROUTING chain
(assuming that this chain was empty previously). You can check
that the rule is in the mangle table by executing the command
With the above rule in place in the mangle table, use the other
laptop to try to make any sort of connection with the Ubuntu
laptop. You could, for example, try to SSH into the Ubuntu
laptop. You will not be able to do. (You will still be able the
ping the Ubuntu laptop since ping packets do not have the SYN
flag set. More accurately speaking, the rule we entered is just for the TCP protocol packets. The ping
packets belong to a different protocol — the ICMP protocol, which resides at the Network Layer, as shown in
15
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
16
Computer and Network Security by Avi Kak Lecture 18
The filter table contains at least three rule chains: INPUT for
processing all incoming packets, OUTPUT for processing all
outgoing packets, and FORWARD for processing all packets being
routed through the machine. The INPUT, OUTPUT, and FORWARD
chains of the filter table are also referred to as the built-in
chains since they cannot be deleted (unlike the user-defined
chains we will talk about later).
allowed address range for such networks is 192.168.0.0 to 192.168.255.255. On the other hand,
when you are connected to the Purdue wireless network (PAL2 or PAL3), you are in a Class A
private network. The allowed address range for such a network is 10.0.0.0 to 10.255.255.255 —
unless dictated otherwise by the Subnet Mask used. When a packet in a private network is
routed out to the internet at large, it is subject to network address translation. The same
things happens when a packet from the internet at large is routed to your machine in a private
network; it is also subject to NAT, which would be the reverse of the address translation
17
Computer and Network Security by Avi Kak Lecture 18
From the discussion so far, it may seem that the four tables act
disjointly. That is, one may get the impression that a single
table decides the entire fate of a packet. That is not the case.
To illustrate how intertwined the tables are, a packet coming in
for the local host will first be seen by the mangle.PREROUTING,
nat.PREROUTING, and mangle.INPUT chains before it is seen by the
filter.INPUT chain. (By the notation ”xxxx.yyyy” I mean ”yyyy”
18
Computer and Network Security by Avi Kak Lecture 18
http://www.faqs.org/docs/iptables/index.html
19
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
20
Computer and Network Security by Avi Kak Lecture 18
Incoming Packets
Outgoing Packets
Routing FORWARD
Chain
Decision
Rules
OUTPUT
INPUT Chain
Chain Rules
Rules
21
Computer and Network Security by Avi Kak Lecture 18
22
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
lsmod | grep ip
iptable_raw 3328 0
ipt_REJECT 5760 0
iptable_mangle 3840 0
iptable_nat 8708 0
nf_nat 20140 1 iptable_nat
nf_conntrack_ipv4 19724 2 iptable_nat
nf_conntrack 65288 4 xt_state,iptable_nat,nf_nat,nf_conntrack_ipv4
nfnetlink 6936 3 nf_nat,nf_conntrack_ipv4,nf_conntrack
iptable_filter 3968 1
ip_tables 13924 4 iptable_raw,iptable_mangle,iptable_nat,iptable_filter
x_tables 16260 5 ipt_REJECT,xt_state,xt_tcpudp,iptable_nat,ip_tables
ipv6 273892 21
If you do not see all these modules, that does not mean that
iptables is not installed and running on your machine. Many of
the kernel modules are loaded in dynamically as they are
needed by the application programs.
sudo iptables -L
Note that these are the only four tables recognized by the
kernel. (Unlike user-defined chains in the tables, there are no
user-defined tables.)
24
Computer and Network Security by Avi Kak Lecture 18
For the filter table shown on the previous slide, note the policy
shown for each built-in chain right next to the name of the
chain. As mentioned earlier, only built-in chains have policies.
Policy is what is applied to a packet if it is not trapped by any
of the rules in a chain.
25
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
#!/bin/sh
# Create a new user-defined chain for the filter table: Make sure you first
# flush the previous rules by ’iptables -t filter F’ and delete any
# previously user-defined chains by ’iptables -t filter -X’
iptables -t filter -N myfirewall.rules
# You must not block packets that correspond to TCP/IP protocol numbers 50
# (ESP) and 51 (AH) for VPN to work. (See Lecture 20 for ESP and AH.). VPN
# also needs the UDP ports 500 (for IKE), UDP port 10000 (for IPSec
# encapsulated in UDP) and TCP port 443 (for IPSec encapsulated in
# TCP). [Note that if you are behind a NAT device, make sure it does not
# change the source port on the IKE (Internet Key Exchange) packets. If
26
Computer and Network Security by Avi Kak Lecture 18
# For multicast DNS (mDNS) --- allows a network device to choose a domain
# name in the .local namespace and announce it using multicast. Used by
# many Apple products. mDNS works differently from the unicast DNS we
# discussed in Lecture 17. In mDNS, each host stores its own information
# (for example its own IP address). If your machine wants to get the IP
# address of such a host, it sends out a multicast query to the multicast
# address 224.0.0.251.
iptables -A myfirewall.rules -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
# Accept all packets that are in the states ESTABLISHED and RELATED (See
# Section 18.11 for packet states):
iptables -A myfirewall.rules -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
# Drop all other incoming packets. Do not send back any ICMP messages for
# the dropped packets:
iptables -A myfirewall.rules -p all -j REJECT --reject-with icmp-host-prohibited
chmod +x myfirewall.sh
27
Computer and Network Security by Avi Kak Lecture 18
iptables -L -n -v --line-numbers
where the ‘-n’ switch suppresses address lookup and display all
IP address in the dot-decimal notation and the switch
‘–line-numbers’ displays a line number at the beginning of each
line in a rule chain. The switch ‘-v’ is for the verbose mode.
This command will generate the following display for the filter
table in your terminal window:
28
Computer and Network Security by Avi Kak Lecture 18
In the output shown above, note that the last column, with no
heading, contains ancillary information related to a rule. It may
mention a port (as in tcp dpt:443, where dpt stands for
“destination port”), the state of a packet, etc.
target :
The action part of a rule. The target can be one of the following: ACCEPT,
DROP, REJECT, REDIRECT, RETURN, or the name of the chain to
jump to. DROP means to drop the packet without sending an error message to
the originator of that packet. REJECT has the same effect as DROP, except that
the sender is sent an error message that depends on the argument supplied to this
target. REDIRECT means to send the packet to a new destination (used with
NAT). RETURN means to return from this chain to the calling chain and to
continue examining rules in the calling chain where you left off. When RETURN is
encountered in a built-in chain, the policy associated with the chain is executed.
proto :
The protocol associated with the packet to be trapped by this rule. The protocol
may be either named symbolically or specified by a number. Each standard
protocol has a number associated with it. The protocol numbers are assigned by
Internet Assigned Numbers Authority (IANA).
29
Computer and Network Security by Avi Kak Lecture 18
opt : optional
Note that when the fifth column (the proto column) mentions a
user-defined service as opposed to a protocol, then the last
column (without a title) must mention the port specifically. On
the other hand, for packets corresponding to standard services,
the system can figure out the ports from the entries in the file
/etc/services.
30
Computer and Network Security by Avi Kak Lecture 18
Since both the built-in INPUT and the built-in FORWARD chains
jump to the user-defined myfirewall.rules chain, let’s look at
the first rule in this user-defined chain in some detail. This rule
is:
num pkts bytes target prot opt in out source destination
1 327 34807 ACCEPT 0 -- lo * 0.0.0.0/0 0.0.0.0/0
Let’s now examine the rule in line 2 for the user-defined chain
myfirewall.rules shown in the display produced by the
command ‘iptables -L -n -v --line-numbers’ command:
Let’s now examine the OUTPUT chain in the filter table. [(See
the output shown earlier in this section that was produced by the command
‘iptables -L -n -v --line-numbers’ command.) There are no rules
in this chain. Therefore, for all outbound packets, the policy
associated with the OUTPUT chain will be used. This policy says
ACCEPT, implying that all outbound packets will be sent
directly, without further examination, to their intended
destinations.
32
Computer and Network Security by Avi Kak Lecture 18
33
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
iptables -t nat -n -L
we get
The nat table is used only for translating either the packet’s
source address field or its destination address field.
The ‘targets’ for the nat table (meaning, the actions that are
permitted for the rules) are
DNAT
SNAT
MASQUERADE
REDIRECT
36
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
iptables -t mangle -n -L
returns
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
38
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
iptables -t raw -L
This output shows that the raw table supports only two chains:
PREROUTING and OUTPUT.
39
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
iptables -h
Here are some other optional flags for the iptables command
and a brief statement of what is achieved by each flag:
40
Computer and Network Security by Avi Kak Lecture 18
After the first level flags shown above that name a chain, if this
flag calls for a new rule to be specified (such as for ‘-A’ flag) you
can have additional flags that specify the state of the packet
that must be true for the rule to apply and specify the action
part of the rule. We say that these additional flags describe the
filtering specifications for each rule.
-p args
for specifying the protocol (tcp, udp,
icmp, etc) You can also specify a protocol
by number if you know the numeric protocol
values for IP.
-s args
for specifying source address(es)
--sport args
41
Computer and Network Security by Avi Kak Lecture 18
-d args
for specifying destination address(es)
--dport args
for specifying destination port(s)
(For the port specifications, you can supply
a port argument by name, as by ‘www’, as
listed in /etc/services.)
--icmp-type typename
[ for spcifying the type of ICMP packet as
described in the standards documents RFC792
and RFC 4884. The icmp type names can be
found by the comamnd
42
Computer and Network Security by Avi Kak Lecture 18
TOS-network-redirect
TOS-host-redirect
echo-request (ping) (type 8)
router-advertisement (type 9)
router-solicitation (type 10)
time-exceeded (ttl-exceeded)(type 11)
ttl-zero-during-transit (code 0)
ttl-zero-during-reassembly (code 1)
parameter-problem (type 12)
ip-header-bad
required-option-missing
timestamp-request (type 13)
timestamp-reply (type 14)
address-mask-request (type 17)
address-mask-reply (type 18) ]
-j args
the name of the target to execute when
the rule matches; ‘j’ stands for ‘jump to’
-i args
for naming the input interface (when an
interface is not named, that means all
interfaces)
-o args
for specifying an output interface
43
Computer and Network Security by Avi Kak Lecture 18
44
Computer and Network Security by Avi Kak Lecture 18
Many rule specification flags (such as ‘-p’, ‘-s’, ‘-d’, ‘-f’ ‘–syn’,
etc.) can have their arguments preceded by ‘!’ (that is
pronounced ‘not’) to match values not equal to the ones given.
This is referred to as specification by inversion. For
example, to indicate all sources addresses but a specific address,
45
Computer and Network Security by Avi Kak Lecture 18
-s ! ip_address
For the ‘-f’ option flags, the inversion is done by placing ‘!’
before the flag, as in
! -f
The rule containing the above can only be matched with the
first fragment of a fragmented packet.
set. Both mask and comp are comma separated lists. The declaration shown above calls for the SYN,
RST, ACK, and FIN flag to be examined and, of these, the SYN flag must be set and the rest unset. Do
’man iptables-extensions’ and search for ’--tcp-flags mask comp’ to see this information in
greater detail. ] Note that ‘-d’, and ‘-s’ are also TCP extension flags.
46
Computer and Network Security by Avi Kak Lecture 18
These flags work only when the argument for the protocol flag
‘-p’ is ‘tcp’.
4. The fourth way uses the net mask directly to specify a group
of IP addresses. What was accomplished by 199.95.207.0/24
above is now accomplished by 199.95.207.0/255.255.255.0.
48
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
49
Computer and Network Security by Avi Kak Lecture 18
51
Computer and Network Security by Avi Kak Lecture 18
or as in
iptables -A INPUT -m mac --mac-source ! 00:60:08:91:CC:B7 DROP
The second rule will drop all incoming packets unless they are
from the specific machine with the MAC address shown.
52
Computer and Network Security by Avi Kak Lecture 18
53
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
54
Computer and Network Security by Avi Kak Lecture 18
--to-destination 10.0.0.1-10.0.0.25
This will now spread the load of the service over 25 machines,
including the gateway machine if its LAN address is 10.0.0.1.
So the basic idea in port forwarding is that you forward all the
traffic received at a given port on our firewall computer to the
designated machines in the LAN that is protected by the
firewall.
55
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
One can also use LOG as a target. So if you did not want to
drop a packet for some reason, you could go ahead and accept it
but at the same time log it to decide later if your current rule
for such packets is a good rule. Here is an example of a LOG
target in a rule for the FORWARD chain:
iptables -A FORWARD -p tcp -j LOG --log-level info
emerg
alert
crit
err
warning
notice
info
debug
56
Computer and Network Security by Avi Kak Lecture 18
57
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
Subsequently, when you reboot the machine, you can restore the
firewall by using the command iptables-restore as sudo:
58
Computer and Network Security by Avi Kak Lecture 18
59
Computer and Network Security by Avi Kak Lecture 18
/etc/network/interfaces
60
Computer and Network Security by Avi Kak Lecture 18
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.rules
post-down iptables-save > /etc/iptables.rules
Note that on Red Hat Linux and its variants, you can start and
stop iptables by
/etc/init.d/iptables start
/etc/init.d/iptables stop
/etc/init.d/iptables restart
Also on Red Hat Linux, if you are doing NAT, make sure you
turn on IP packet forwarding by setting
net.ipv4.ip_forward = 1
61
Computer and Network Security by Avi Kak Lecture 18
– The ‘-p tcp’ and ‘-p udp’ options load into the kernel the TCP
and UDP extension modules.
– Even if the condition part of a rule is matched, if the rule does not
specify a target, the next rule will be considered.
62
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
| | | LAN addresses:
\ | / 192.168.1.0/24
\ | /
\ | /
-----------------------------------
| ROUTER |
-----------------------------------
|
|
|
| interface eth1, IP adress: 192.168.1.1
-----------------------
| |
| Gateway | loopback: localhost
| Machine | addess: 127.0.0.1
| (firewall computer) | interface: lo
| |
-----------------------
| interface eth0, IP address: 123.45.67.89
|
|
internet
We will also assume that the gateway machine has its IP address
63
Computer and Network Security by Avi Kak Lecture 18
Allow for SSH access (port 22) to the firewall machine from
outside the LAN for external maintenance of this machine.
Let’s say that the LAN is hosting a web server (on behalf of the
whole LAN) and that this HTTPD server is running on the
machine 192.168.1.100 of the LAN. So the firewall must use
NAT to redirect the incoming TCP port 80 requests to
192.168.1.100.
64
Computer and Network Security by Avi Kak Lecture 18
We also want the firewall to accept the ICMP Echo requests (as
used by ping) coming from the outside.
tcp_services = "22,113"
icmp_types = "ping"
comp_httpd = "192.168.1.100"
# NAT/Redirect
modprobe ip_nat_ftp
iptables -t nat -A POSTROUTING -o $ext_if -j MASQUERADE
iptables -t nat -A PREROUTING -i $ext_if -p tcp --dport 80 \
-j DNAT --to-destination $comp_httpd
65
Computer and Network Security by Avi Kak Lecture 18
66
Computer and Network Security by Avi Kak Lecture 18
Back to TOC
3. What are the four tables maintained by the Linux kernel for
processing incoming and outgoing packets?
67
Computer and Network Security by Avi Kak Lecture 18
6. Show how you would use the iptables command to reject all
incoming SYN packets that seek to open a new connection with
your machine?
8. If you see the string ‘icmp type 255’ at the end of a line of
the output produced by the ‘iptables -L’ command, what
does that mean?
68
Computer and Network Security by Avi Kak Lecture 18
69