1 | Advanced Registry Operations Curriculum |
---|
2 | |
---|
3 | Building a Host-Based Firewall (Ubuntu/Debian) |
---|
4 | ---------------------------------------------- |
---|
5 | |
---|
6 | 1. Creating an initial firewall |
---|
7 | ---------------------------- |
---|
8 | |
---|
9 | Let'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 | |
---|
11 | To do this do the following: |
---|
12 | |
---|
13 | $ sudo iptables -A INPUT -p icmp --icmp-type echo-request -i lo -j DROP |
---|
14 | |
---|
15 | See if the new rule that is now in place is working: |
---|
16 | |
---|
17 | $ ping localhost |
---|
18 | |
---|
19 | This should now fail. Press ctrl-c to exit from the ping. |
---|
20 | |
---|
21 | If 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 | |
---|
26 | In this file you will see something like: |
---|
27 | |
---|
28 | >>> |
---|
29 | |
---|
30 | # The primary network interface |
---|
31 | auto eth0 |
---|
32 | iface 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 | |
---|
44 | At 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 | |
---|
48 | Then save and exit from the file (":wq" in vi). |
---|
49 | |
---|
50 | Now each time your machine boots the iptables rule will be applied. |
---|
51 | |
---|
52 | 2. Removing the initial iptables ping blocking rule |
---|
53 | ------------------------------------------------ |
---|
54 | |
---|
55 | To 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 | |
---|
59 | Now try pinging your local machine: |
---|
60 | |
---|
61 | $ ping localhost |
---|
62 | |
---|
63 | It should be working again. But, you saved the old rule to /etc/iptables.rules. This means that if you were to reboot or |
---|
64 | restart your network interface the ping blocking rule would come back. You can do: |
---|
65 | |
---|
66 | $ sudo iptables -F |
---|
67 | |
---|
68 | to flush all rules, or you can leave things as they are. In either case, run: |
---|
69 | |
---|
70 | $ sudo iptables-save > /etc/iptables.rules |
---|
71 | |
---|
72 | and you will have a file with no iptables in it that gets loaded next time you reboot. |
---|
73 | |
---|
74 | |
---|
75 | 3. Creating an initial, restrictive iptables ruleset |
---|
76 | ------------------------------------------------- |
---|
77 | |
---|
78 | To test this you may wish to do the following: |
---|
79 | |
---|
80 | $ su - [enter in the root password] |
---|
81 | # cd |
---|
82 | # vi firewall.sh |
---|
83 | |
---|
84 | In this file add the following: |
---|
85 | |
---|
86 | >>>> |
---|
87 | |
---|
88 | #!/bin/bash |
---|
89 | |
---|
90 | iptables -F |
---|
91 | iptables -P INPUT DROP |
---|
92 | iptables -P FORWARD DROP |
---|
93 | |
---|
94 | iptables -A INPUT -i lo -j ACCEPT |
---|
95 | |
---|
96 | iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
---|
97 | |
---|
98 | iptables -A INPUT -p tcp --dport 22 -j ACCEPT |
---|
99 | iptables -A INPUT -p tcp --dport 80 -j ACCEPT |
---|
100 | iptables -A INPUT -p tcp --dport 443 -j ACCEPT |
---|
101 | iptables -A INPUT -p udp --dport 53 -j ACCEPT |
---|
102 | iptables -A INPUT -p tcp --dport 53 -j ACCEPT |
---|
103 | iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT |
---|
104 | |
---|
105 | iptables -A INPUT -j REJECT |
---|
106 | iptables -A FORWARD -j REJECT |
---|
107 | |
---|
108 | <<<< |
---|
109 | |
---|
110 | Now save and exit from the file (":wq" in vi). |
---|
111 | |
---|
112 | Make the file executable: |
---|
113 | |
---|
114 | # chmod 755 firewall.sh |
---|
115 | |
---|
116 | execute the firewall rules |
---|
117 | |
---|
118 | # ./firewall.sh |
---|
119 | |
---|
120 | Do some testing. Can you to the services on your box from another machine (ssh, web, ping, anything else?). |
---|
121 | |
---|
122 | If you have problems try to figure out what is blocking the service and add a rule in to iptables to let the |
---|
123 | packets through. |
---|
124 | |
---|
125 | There are endless possible iptables rules you can add - including dynamic rules to deal with potential |
---|
126 | DDoS attacks, port scanning on the ports you do open, allowing access from certain addresses or ranges |
---|
127 | only, etc., etc. |
---|
128 | |
---|
129 | Here 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 | |
---|
139 | You can view your current iptables ruleset by typing: |
---|
140 | |
---|
141 | # iptables -L |
---|
142 | |
---|
143 | To make the current firewall rules permanent remember you must do: |
---|
144 | |
---|
145 | # iptables-save > /etc/iptables.rules |
---|
146 | |
---|
147 | Below is a more in-depth description of each rule in our iptables ruleset: |
---|
148 | |
---|
149 | # Flush the current iptables ruleset in memory |
---|
150 | iptables -F |
---|
151 | |
---|
152 | # drop all packets on the INPUT chain in the Filter table |
---|
153 | iptables -P INPUT DROP |
---|
154 | |
---|
155 | # drop all packets on the FORWARD chain on the Filter tables |
---|
156 | iptables -P FORWARD DROP |
---|
157 | |
---|
158 | # accept all packets on our local loopback interface |
---|
159 | iptables -A INPUT -i lo -j ACCEPT |
---|
160 | |
---|
161 | # allow us to connect out from our box |
---|
162 | iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
---|
163 | |
---|
164 | # allow incoming tcp connection on port 22 (ssh) |
---|
165 | iptables -A INPUT -p tcp --dport 22 -j ACCEPT |
---|
166 | |
---|
167 | # allow incomding tcp connections on port 80 (http) |
---|
168 | iptables -A INPUT -p tcp --dport 80 -j ACCEPT |
---|
169 | |
---|
170 | # allow incoming tcp connections on port 443 (https) |
---|
171 | iptables -A INPUT -p tcp --dport 443 -j ACCEPT |
---|
172 | |
---|
173 | # allow incoming udp connections on port 53 (dns) |
---|
174 | iptables -A INPUT -p udp --dport 53 -j ACCEPT |
---|
175 | |
---|
176 | # allow incoming tcp connections on port 53 (dns) |
---|
177 | iptables -A INPUT -p tcp --dport 53 -j ACCEPT |
---|
178 | |
---|
179 | # allow icmp requests of type 8 (ECHO or ping) |
---|
180 | iptables -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. |
---|
183 | iptables -A INPUT -j REJECT |
---|
184 | iptables -A FORWARD -j REJECT |
---|
185 | |
---|
186 | |
---|
187 | You 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 | |
---|