![]() |
Now serving 106 guides. |
|
FreeBSD as a Gateway/Firewall with IPFilter
The original guide is found at www.derrickl.net/guides.
General Information The purpose of this guide is to provide the reader with enough knowledge to configure FreeBSD with a basic NAT/Firewall setup, allowing you to share your internet connection between all your computers with only one IP address, and to protect your network from hackers.This is not intended to be an exhaustive guide, merely a reasonably in depth introduction to get you off on the right foot as painlessly as possible. You will have to modify this guide somewhat to match your particular network configuration. Here are the settings I use for this guide: xl0 - External (internet facing) interface. Change to suit your configuration. xl1 - Internal (inside network) interface. Change to suit your configuration. 192.168.0.0/16 - Internal network address space/netmask. Change to suit your configuration. 0.0.0.0/32 - Generic internet address space/netmask. Leave this alone, unless you really know what you're doing. Requirements
Configuration Section A - Configuring rc.conf There are several options that you must insert into your /etc/rc.conf to tell your system at boot time to enable itself to route packets between interfaces, to load the ipnat and ipfilter software you compiled into your kernel, and where to find the rules for ipfilter and ipnat. Open up your /etc/rc.conf with your favorite editor and insert the following options:
Section B - Configuring NAT (ipnat.rules) Now you must configure your rules for NAT (Network Address Translation). NAT allows many computers to access the Internet with only one IP address by mapping the internal IP addresses to the external address, sending the data out, and routing replies from the internet back to the internal system. All the work is done on the system running NAT. All you have to do on the client machine is set its default gateway to the internal IP of the system that you are setting up as a router/firewall.The configuration file for the IPNAT software on your system will be /etc/ipnat.rules, as set above in /etc/rc.conf. The command syntax is pretty straightforward. For the purposes of this guide I will use a very simple ruleset. You can learn more advanced rules by reading the information linked to in the "Further Reading" section of this guide. Below is a sample ipnat.conf file. Keep in mind that xl0 is my EXTERNAL (Internet Facing) interface, and that 192.168.1.0/16 is my internal subnet. You must change the xl0 and the 192.168.1.0/16 to match your hardware and network configuration. The first two lines handle FTP support, and must be in this precise order to allow FTP to work properly. The third line will translate everything else.
You may use the ipnat command to control the ipnat software, view active mappings and statistics after you have completed this guide and rebooted (however, do not reboot yet). Section C - Building your firewall ruleset (ipf.rules) Now, you must build the rules for your firewall. This will be responsible for blocking or passing data through your network. IPFILTER also handles the state table. If it accepts a packet, and the rule that accepts it tells the firewall to keep state, an entry will be placed into your systems state table. Keeping state is essential to keeping connections from your client computers to the outside world alive. Any connection kept open in the state table will bypass the entire firewall ruleset. Don't worry, it will have to have been accepted to be placed in the state table in the first place.So, open up /etc/ipf.rules in you editor of choice, and we shall jump in. IPFILTER rules are pretty easy to understand. They are pretty much in plain english, and make sense if you have half a clue about what your doing. The first rules I will show you will handle keeping connection state.
The quick keyword means that if the packet matches the rule, it stops comparing the packet against the rest of the rules. Without the quick keyword, IPFILTER will continue to compare the packet against the remaining rules, with the last one matching being the one that counts. If the packet didn't match the first rule, it would be compared against the next one in sequence. If the packet happens to be a udp packet passing out on the xl0 interface, it would match the second rule, and that rule would be applied without comparing against the next rule(s). Unless you happen to know better, these three rules are rather essential as they are, and should (must) be at the beginning of your ipf.rules. Next, you will want to block packets coming in from the Internet from address ranges they shouldn't be coming from. Address ranges they shouldn't be coming from are those that are non-routable (set aside for networks not on the net). People could use spoofed packets from these ranges to DoS your machine. Some, but not all of these ranges are: 192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8, 127.0.0.0/8, and 192.0.2.0/24. An example rule blocking one of these addresses is:
Now, if your the system you are setting up as a gateway/firewal is hosting services (http, ftp, etc) you will want to allow others on the Internet to access them. First you will need to know what port(s) and protocol the service uses and add a rule to pass those packets in. Let's say you are running apache to serve webpages. It uses port 80 and the tcp protocol. Therefore, the rule to let others access it will look like this:
You will now want to block anything that hasn't matched any rules so far. Otherwise, IPFILTER will just pass these packets.
Now that you have everything configured, reboot your system and things should work fairly well if you are good at following instructions. A couple IPFILTER specific command line utilities are now in order. First, ipf is the control program for IPFILTER. It can do a lot, but the most important (in my opinion) is reloading the /etc/ipf.rules when you make a change. To do this, simply execute
Once again, there are a lot more sophisticated things you could do, but they are outside the scope of this guide. I strongly suggest reading through the additional material referenced below. Further Reading Compiling a custom kernel - By Derrick LewisIPFILTER Howto - By Brendan Conoboy and Erik Fichtner This guide is © 2003 - 2008 Derrick Lewis. Author: Derrick Lewis 1 Comment Posted by monkeywrencher on October 25, 2005 at 9:20:31 pm EEST
You can do everything in this guide automatically and in less than 1 minute with this script: http://defcon1.org/html/Networking_Articles/ipfilterscript-v2.tar.gz
Enjoy |
Copyright 2003 - 2008 BSD Guides. All rights reserved. |
About | Terms of Use | Privacy | Contact |

