Agenda: excercises-firewalls.txt

File excercises-firewalls.txt, 5.7 KB (added by admin, 9 years ago)
Line 
1Advanced Registry Operations Curriculum
2
3Building a Host-Based Firewall (Ubuntu/Debian)
4----------------------------------------------
5
61. Creating an initial firewall
7   ----------------------------
8
9Let's create the first firewall to understand how this is done. We'll create a very simple rule. Let's block access to ping to your box. It's a good example, but we don't want to do this in real life. Ping is too valuable to block.
10
11To do this do the following:
12
13        $ sudo iptables -A INPUT -p icmp --icmp-type echo-request -i lo -j DROP
14
15See if the new rule that is now in place is working:
16
17        $ ping localhost
18
19This should now fail. Press ctrl-c to exit from the ping.
20
21If you wanted to make this rule be permanent you would do:
22
23        $ sudo iptables-save > /etc/iptables.rules
24        $ sudo vi /etc/network/interfaces
25
26In this file you will see something like:
27
28>>>
29
30# The primary network interface
31auto eth0
32iface eth0 inet static
33        address 67.218.55.101
34        netmask 255.255.255.192
35        network 67.218.55.64
36        broadcast 67.218.55.127
37        gateway 67.218.55.65
38        # dns-* options are implemented by the resolvconf package, if installed
39        dns-nameservers 67.218.55.67
40        dns-search pacnog.bluesky.as
41
42<<<<
43
44At the end of this, on a separate line just after "dns-search..." you should a line that looks like:
45
46        pre-up iptables-restore < /etc/iptables.rules
47
48Then save and exit from the file (":wq" in vi).
49
50Now each time your machine boots the iptables rule will be applied.
51
522. Removing the initial iptables ping blocking rule
53   ------------------------------------------------
54
55To remove the rule is simple. There are two ways to do this. You can do:
56
57        $ sudo iptables -D INPUT -p icmp --icmp-type echo-request -i lo -j DROP
58
59Now try pinging your local machine:
60
61        $ ping localhost
62
63It should be working again. But, you saved the old rule to /etc/iptables.rules. This means that if you were to reboot or
64restart your network interface the ping blocking rule would come back. You can do:
65
66        $ sudo iptables -F
67
68to flush all rules, or you can leave things as they are. In either case, run:
69
70        $ sudo iptables-save > /etc/iptables.rules
71
72and you will have a file with no iptables in it that gets loaded next time you reboot.
73
74
753. Creating an initial, restrictive iptables ruleset
76   -------------------------------------------------
77
78To test this you may wish to do the following:
79
80        $ su -                  [enter in the root password]
81        # cd
82        # vi firewall.sh
83
84In this file add the following:
85
86>>>>
87
88#!/bin/bash
89
90iptables -F
91iptables -P INPUT DROP
92iptables -P FORWARD DROP
93
94iptables -A INPUT -i lo -j ACCEPT
95
96iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
97
98iptables -A INPUT -p tcp --dport 22 -j ACCEPT
99iptables -A INPUT -p tcp --dport 80 -j ACCEPT
100iptables -A INPUT -p tcp --dport 443 -j ACCEPT
101iptables -A INPUT -p udp --dport 53 -j ACCEPT
102iptables -A INPUT -p tcp --dport 53 -j ACCEPT
103iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
104
105iptables -A INPUT -j REJECT
106iptables -A FORWARD -j REJECT
107
108<<<<
109
110Now save and exit from the file (":wq" in vi).
111
112Make the file executable:
113
114        # chmod 755 firewall.sh
115       
116execute the firewall rules
117
118        # ./firewall.sh
119
120Do some testing. Can you to the services on your box from another machine (ssh, web, ping, anything else?).
121
122If you have problems try to figure out what is blocking the service and add a rule in to iptables to let the
123packets through.
124
125There are endless possible iptables rules you can add - including dynamic rules to deal with potential
126DDoS attacks, port scanning on the ports you do open, allowing access from certain addresses or ranges
127only, etc., etc.
128
129Here are some good web pages with more in-depth iptables rulesets:
130
131        https://help.ubuntu.com/community/IptablesHowTo
132        http://www.shanghaiwebhosting.com/ssh-hosting/typical-iptables-firewall-rules-for-a-server-that-hosts-websites
133        http://forcespike.altervista.org/articles/setting-firewall-with-iptables.php
134        http://blogs.techrepublic.com.com/10things/?p=539
135        http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch14_:_Linux_Firewalls_Using_iptables
136        http://wiki.vpslink.com/HOWTO:_Building_IPTables_rules
137        http://www.pizon.org/articles/building-a-linux-firewall-with-iptables.html
138
139You can view your current iptables ruleset by typing:
140
141        # iptables -L
142
143To make the current firewall rules permanent remember you must do:
144
145        # iptables-save > /etc/iptables.rules
146
147Below is a more in-depth description of each rule in our iptables ruleset:
148
149# Flush the current iptables ruleset in memory
150iptables -F
151
152# drop all packets on the INPUT chain in the Filter table
153iptables -P INPUT DROP
154
155# drop all packets on the FORWARD chain on the Filter tables
156iptables -P FORWARD DROP
157
158# accept all packets on our local loopback interface
159iptables -A INPUT -i lo -j ACCEPT
160
161# allow us to connect out from our box
162iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
163
164# allow incoming tcp connection on port 22 (ssh)
165iptables -A INPUT -p tcp --dport 22 -j ACCEPT
166
167# allow incomding tcp connections on port 80 (http)
168iptables -A INPUT -p tcp --dport 80 -j ACCEPT
169
170# allow incoming tcp connections on port 443 (https)
171iptables -A INPUT -p tcp --dport 443 -j ACCEPT
172
173# allow incoming udp connections on port 53 (dns)
174iptables -A INPUT -p udp --dport 53 -j ACCEPT
175
176# allow incoming tcp connections on port 53 (dns)
177iptables -A INPUT -p tcp --dport 53 -j ACCEPT
178
179# allow icmp requests of type 8 (ECHO or ping)
180iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
181
182# reject anything else on these Chains that gets to here. Do this explicitly even though it is implied.
183iptables -A INPUT -j REJECT
184iptables -A FORWARD -j REJECT
185
186
187You are now running your server with a firewall that allows you to get out, but which only allows access to your currently running services.
188
189
190