<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ServerAdmins.NET &#187; ipfw</title>
	<atom:link href="http://serveradmins.net/tag/ipfw/feed/" rel="self" type="application/rss+xml" />
	<link>http://serveradmins.net</link>
	<description>Stuff for Server Admins...</description>
	<lastBuildDate>Wed, 07 Dec 2011 23:39:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Basic IPFW firewall HowTo for FreeBSD&#8230;</title>
		<link>http://serveradmins.net/a-basic-ipfw-firewall-howto-for-freebsd/</link>
		<comments>http://serveradmins.net/a-basic-ipfw-firewall-howto-for-freebsd/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 01:42:27 +0000</pubDate>
		<dc:creator>chrism</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Utility]]></category>
		<category><![CDATA[allow]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[ipfw]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://serveradmins.net/?p=65</guid>
		<description><![CDATA[How to setup and configure a basic firewall for FreeBSD using ipfw.]]></description>
			<content:encoded><![CDATA[<p>Of all the different OS&#8217;s and the multitude of firewall implementations for each, my favorite has to be FreeBSD&#8217;s IPFW ruleset.  It&#8217;s powerful, super efficient, and most of all, *EASY* to read! </p>
<p>So I&#8217;m just going to paste a sample firewall below, notate it heavily for you, and show you how to get it enabled.   </p>
<p>For newer FreeBSD builds, it&#8217;s enough to add <code>firewall_enable="YES"</code> to your /etc/rc.conf file and reboot.  This will load all the necessary kernel modules, and get you in shape.   You should know that by default FreeBSD defaults to a &#8216;default deny&#8217; policy.  This is *VERY* important to know.   What it means is that by default, your server isn&#8217;t going to open up any ports. You *WILL* be locked out if you do not have a firewall in place.  Don&#8217;t say I didn&#8217;t warn you!</p>
<p>So, with that being said, let&#8217;s get our firewall in built and in place, and then we&#8217;ll go about adding in the necessary options to ensure it comes up properly on boot.  It should noted here that you can build in a &#8220;Default to accept&#8221; option into the kernel or &#8220;firewall_type=&#8221;open&#8221; to the /etc/rc.conf to start up wide open.  This is the suggested practice as the chances are, you&#8217;re not close enough to your server to console it. <img src='http://serveradmins.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let&#8217;s get our basic firewall going.   We&#8217;re going to open up 22 for SSH, 80 for web traffic, 25 for SMTP and 110/143 for POP3/IMAP mail.   The syntax is simple enough you should be able to customize it for your own needs, but always make sure your SSH port is accessible.  Also if you run SSH on an alternative port, you should modify that here.  You don&#8217;t want to lock yourself out now, do you?</p>
<p>So go ahead, login as root to your server, open up your text editor of choice.  For the purpose of this post, I&#8217;m going to assume you&#8217;re creating the file /etc/firewall.sh.  If not, modify where appropriate. <img src='http://serveradmins.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   You should also make sure this file is executable by root, otherwise it won&#8217;t fire off properly on reboot.</p>
<p><code><br />
#!/bin/sh</p>
<p>set -e</p>
<p>#First, let's clear out any chance of conflicting with other FreeBSD firewall configurations<br />
# and make sure we're starting from a fresh slate.</p>
<p>/sbin/sysctl net.inet.ip.forwarding=0 >/dev/null<br />
/sbin/ipfw -q list >/dev/null 2>&#038;1 || /sbin/kldload ipfw<br />
(/sbin/ipf -D) >/dev/null 2>&#038;1 || true<br />
(/sbin/kldunload ipl) >/dev/null 2>&#038;1 || true<br />
(/sbin/pfctl -d) >/dev/null 2>&#038;1 || true<br />
(/sbin/kldunload pf) >/dev/null 2>&#038;1 || true<br />
/sbin/ipfw -q /dev/stdin < < EOF<br />
flush<br />
delete set 31</p>
<p>#Open up our Loopback device.  There's almost never any reason to filter this.<br />
add allow ip from any to any via lo0</p>
<p>#Allow checking/maintenance of stateful rulesets<br />
add check-state</p>
<p>#Kill off any active/open sessions, pre-fw init.  We do this<br />
#to ensure that any connection to unauthorized ports is dealt with<br />
#and that all connections adhere to the policy...</p>
<p>add reset tcp from any to any established</p>
<p>    ##########################<br />
## Add Inbound Service Allowances ##<br />
    ##########################<br />
#Port 80: www<br />
add allow tcp from any to me 80 setup in<br />
#Port 22: SSH - Stateful connection (it's going to maintain a connection, not come and go)<br />
add allow tcp from any to me 22 setup in keep-state</p>
<p>#Port 21, and 30000-50000, FTP and Passive port rolloff.<br />
add allow tcp from any to me 21 setup in<br />
add allow tcp from any to me 30000-50000 setup in keep-state</p>
<p>#Port 53: DNS<br />
add allow udp from any to me 53 in </p>
<p>#Port 25, 110, 143.  SMTP, POP3 and IMAP<br />
add allow tcp from any to me 25 in<br />
add allow tcp from any to me 110 in<br />
add allow tcp from any to me 143 in</p>
<p>#ICMP/ping requests should be allowed through, fun stuff happens<br />
#if you don't allow this.  (see ptmu)<br />
add icmp from any to me icmptypes 8 in</p>
<p> #################################<br />
## Lets Deny some Packets. WOOOT ##<br />
 #################################</p>
<p>#These rules denies everything else, not explicitly listed above<br />
add deny tcp from any to me setup in<br />
add deny ip from any to me in</p>
<p> ##################<br />
## Outbound Ahoy! ##<br />
 ##################</p>
<p>#Wide open outbound filtering.  You could modify this section to default to deny<br />
#And then allow specific ports out.  I'm not going to do that here, but you should<br />
#be able to figure it out. <img src='http://serveradmins.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>add allow tcp from me to any setup out keep-state<br />
add allow ip from me to any out keep-state</p>
<p> ###########################<br />
## Last chain to make sure ##<br />
 ###########################</p>
<p>#One more time, just to be positive...<br />
add deny tcp from any to any setup<br />
add deny ip from any to any<br />
enable firewall<br />
EOF</p>
<p>#I set this because the default FreeBSD behavior is to keep a table open<br />
#for a session for 1 hour.  That's a LONG time on a production server.  We<br />
#Go for 10 mins here, but could be lowered drastically.</p>
<p>#Set TTL on Dynamic Rules to 10 Mins.  Formerly 1 hour.<br />
/sbin/sysctl net.inet.ip.fw.dyn_ack_lifetime=600 >/dev/null</p>
<p></code></p>
<p>Done!</p>
<p>Save your /etc/firewall.sh file, then make sure permissions are right on it&#8230;</p>
<p><code><br />
chmod 755 /etc/firewall.sh &#038;&#038; chown root:wheel /etc/firewall.sh<br />
</code></p>
<p>Open up your handy text editor again, and add the following lines to /etc/rc.conf at the bottom.</p>
<p><code><br />
firewall_enable="YES"<br />
firewall_script="/etc/firewall.sh"<br />
</code></p>
<p>Done!   </p>
<p>Please keep in mind that if you set &#8220;firewall_type=open&#8221; above, it may overrule this firewall_script variable, so it should be removed.</p>
<p>Congrats, reboot and enjoy your new firewall. <img src='http://serveradmins.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If this is something you&#8217;re interested in, leave a comment and let me know how and I can write a few more articles on more complex configurations quite easily. <img src='http://serveradmins.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://serveradmins.net/a-basic-ipfw-firewall-howto-for-freebsd/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
