<?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>SoftLayer Blog &#187; allow</title>
	<atom:link href="http://blog.softlayer.com/tag/allow/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.softlayer.com</link>
	<description>A Behind the Scenes Look at the Best Hosting Provider in the World</description>
	<lastBuildDate>Tue, 04 Jun 2013 20:27:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>iptables Tips and Tricks: APF (Advanced Policy Firewall) Configuration</title>
		<link>http://blog.softlayer.com/2013/iptables-tips-and-tricks-apf-advanced-policy-firewall-configuration/</link>
		<comments>http://blog.softlayer.com/2013/iptables-tips-and-tricks-apf-advanced-policy-firewall-configuration/#comments</comments>
		<pubDate>Tue, 29 Jan 2013 22:40:14 +0000</pubDate>
		<dc:creator>Mark Ridlen</dc:creator>
				<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[allow]]></category>
		<category><![CDATA[APF]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[deny]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[ports]]></category>
		<category><![CDATA[rules]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[traffic]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10543</guid>
		<description><![CDATA[Let&#8217;s talk about APF. APF &#8212; Advanced Policy Firewall &#8212; is a policy-based iptables firewall system that provides simple, powerful control over your day-to-day server security. It might seem intimidating to be faced with all of the features and configuration tools in APF, but this blog should put your fears to rest. APF is an [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s talk about APF. APF &mdash; <a href="http://www.rfxn.com/projects/advanced-policy-firewall/">Advanced Policy Firewall</a> &mdash; is a policy-based iptables firewall system that provides simple, powerful control over your day-to-day server security. It might seem intimidating to be faced with all of the features and configuration tools in APF, but this blog should put your fears to rest. </p>
<p>APF is an iptables wrapper that works alongside iptables and extends its functionality. I personally don&#8217;t use iptables wrappers, but I have a lot of experience with them, and I&#8217;ve seen that they <em>do</em> offer some additional features that streamline policy management. For example, by employing APF, you&#8217;ll get several simple on/off toggles (set via configuration files) that make some complex iptables configurations available without extensive coding requirements. The flip-side of a wrapper&#8217;s simplicity is that you aren&#8217;t directly in control of the iptables commands, so if something breaks it might take longer to diagnose and repair. Before you add a wrapper like APF, be sure that you know what you are getting into. Here are a few points to consider:</p>
<ul>
<li>Make sure that what you&#8217;re looking to use adds a feature you need but cannot easily incorporate with iptables on its own.</li>
<li>You need to know how to effectively enable and disable the iptables wrapper (the correct way &#8230; read the manual!), and you should always have a trusted failsafe iptables ruleset handy in the unfortunate event that something goes horribly wrong and you need to disable the wrapper.
<li>Learn about the basic configurations and rule changes you can apply via the command line. You&#8217;ll need to understand the way your wrapper takes rules because it may differ from the way iptables handles rules.</li>
<li>You can&#8217;t manually configure your iptables rules once you have your wrapper in place (or at least you <em>shouldn&#8217;t</em>).</li>
<li>Be sure to know how to access your server via the IPMI management console so that if you completely lock yourself out beyond repair, you can get back in. You might even go so far as to have a script or set of instructions ready for tech support to run, in the event that you can&#8217;t get in via the management console.</li>
</ul>
<p><strong>TL;DR: Have a Band-Aid ready!</strong></p>
<h3>APF Configuration</h3>
<p>Now that you have been sufficiently advised about the potential challenges of using a wrapper (and you&#8217;ve got your Band-Aid ready), we can check out some of the useful APF rules that make iptables administration a lot easier. Most of the configuration for APF is in <code>conf.apf</code>. This file handles the default behavior, but not necessarily the specific blocking rules, and when we make any changes to the configuration, we&#8217;ll need to restart the APF service for the changes to take effect.</p>
<p>Let&#8217;s jump into <code>conf.apf</code> and break down what we see. The first code snippit is fairly self-explanatory. It&#8217;s another way to make sure you <a href="http://blog.softlayer.com/2012/iptables-tips-and-tricks-not-locking-yourself-out/">don&#8217;t lock yourself out</a> of your server as you are making configuration changes and testing them:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># !!! Do not leave set to (1) !!!</span>
<span style="color: #666666; font-style: italic;"># When set to enabled; 5 minute cronjob is set to stop the firewall. Set</span>
<span style="color: #666666; font-style: italic;"># this off (0) when firewall is determined to be operating as desired.</span>
<span style="color: #007800;">DEVEL_MODE</span>=<span style="color: #ff0000;">&quot;1&quot;</span></pre></div></div>

<p>The next configuration options we&#8217;ll look at are where you can make quick high-level changes if you find that legitimate traffic is being blocked and you want to make APF a little more lenient:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># This controls the amount of violation hits an address must have before it</span>
<span style="color: #666666; font-style: italic;"># is blocked. It is a good idea to keep this very low to prevent evasive</span>
<span style="color: #666666; font-style: italic;"># measures. The default is 0 or 1, meaning instant block on first violation.</span>
<span style="color: #007800;">RAB_HITCOUNT</span>=<span style="color: #ff0000;">&quot;1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This is the amount of time (in seconds) that an address gets blocked for if</span>
<span style="color: #666666; font-style: italic;"># a violation is triggered, the default is 300s (5 minutes).</span>
<span style="color: #007800;">RAB_TIMER</span>=<span style="color: #ff0000;">&quot;300&quot;</span>
<span style="color: #666666; font-style: italic;"># This allows RAB to 'trip' the block timer back to 0 seconds if an address</span>
<span style="color: #666666; font-style: italic;"># attempts ANY subsiquent communication while still on the inital block period.</span>
<span style="color: #007800;">RAB_TRIP</span>=<span style="color: #ff0000;">&quot;1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This controls if the firewall should log all violation hits from an address.</span>
<span style="color: #666666; font-style: italic;"># The use of LOG_DROP variable set to 1 will override this to force logging.</span>
<span style="color: #007800;">RAB_LOG_HIT</span>=<span style="color: #ff0000;">&quot;1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This controls if the firewall should log all subsiqent traffic from an address</span>
<span style="color: #666666; font-style: italic;"># that is already blocked for a violation hit, this can generate allot of logs.</span>
<span style="color: #666666; font-style: italic;"># The use of LOG_DROP variable set to 1 will override this to force logging.</span>
<span style="color: #007800;">RAB_LOG_TRIP</span>=<span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>Next, we have an option to adjust ICMP flood protection. This protection should be useful against some forms of DoS attacks, and the associated rules show up in your INPUT chain:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Set a reasonable packet/time ratio for ICMP packets, exceeding this flow</span>
<span style="color: #666666; font-style: italic;"># will result in dropped ICMP packets. Supported values are in the form of:</span>
<span style="color: #666666; font-style: italic;"># pkt/s (packets/seconds), pkt/m (packets/minutes)</span>
<span style="color: #666666; font-style: italic;"># Set value to 0 for unlimited, anything above is enabled.</span>
<span style="color: #007800;">ICMP_LIM</span>=<span style="color: #ff0000;">&quot;30/s&quot;</span></pre></div></div>

<p>If you wanted to add more ports to block for p2p traffic (which will show up in the P2P chain), you&#8217;ll update this code:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># A common set of known Peer-To-Peer (p2p) protocol ports that are often</span>
<span style="color: #666666; font-style: italic;"># considered undesirable traffic on public Internet servers. These ports</span>
<span style="color: #666666; font-style: italic;"># are also often abused on web hosting servers where clients upload p2p</span>
<span style="color: #666666; font-style: italic;"># client agents for the purpose of distributing or downloading pirated media.</span>
<span style="color: #666666; font-style: italic;"># Format is comma separated for single ports and an underscore separator for</span>
<span style="color: #666666; font-style: italic;"># ranges (4660_4678).</span>
<span style="color: #007800;">BLK_P2P_PORTS</span>=<span style="color: #ff0000;">&quot;1214,2323,4660_4678,6257,6699,6346,6347,6881_6889,6346,7778&quot;</span></pre></div></div>

<p>The next few lines let you designate the ports that you want to have closed at all times. They will be blocked for INPUT and OUTPUT chains:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># These are common Internet service ports that are understood in the wild</span>
<span style="color: #666666; font-style: italic;"># services you would not want logged under normal circumstances. All ports</span>
<span style="color: #666666; font-style: italic;"># that are defined here will be implicitly dropped with no logging for</span>
<span style="color: #666666; font-style: italic;"># TCP/UDP traffic inbound or outbound. Format is comma separated for single</span>
<span style="color: #666666; font-style: italic;"># ports and an underscore separator for ranges (135_139).</span>
<span style="color: #007800;">BLK_PORTS</span>=<span style="color: #ff0000;">&quot;135_139,111,513,520,445,1433,1434,1234,1524,3127&quot;</span></pre></div></div>

<p>The next important section to look at deals with conntrack. If you get &#8220;conntrack full&#8221; errors, this is where you&#8217;d increase the allowed connections. It&#8217;s not uncommon to need more connections than the default, so if you need to adjust that value, you&#8217;d do it here:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># This is the maximum number of &quot;sessions&quot; (connection tracking entries) that</span>
<span style="color: #666666; font-style: italic;"># can be handled simultaneously by the firewall in kernel memory. Increasing</span>
<span style="color: #666666; font-style: italic;"># this value too high will simply waste memory - setting it too low may result</span>
<span style="color: #666666; font-style: italic;"># in some or all connections being refused, in particular during denial of</span>
<span style="color: #666666; font-style: italic;"># service attacks.</span>
<span style="color: #007800;">SYSCTL_CONNTRACK</span>=<span style="color: #ff0000;">&quot;65536&quot;</span></pre></div></div>

<p>We&#8217;ve talked about the ports we want closed at all times, so it only makes sense that we&#8217;d specify which ports we want open for all interfaces:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Common inbound (ingress) TCP ports</span>
<span style="color: #007800;">IG_TCP_CPORTS</span>=<span style="color: #ff0000;">&quot;22&quot;</span>
<span style="color: #666666; font-style: italic;"># Common inbound (ingress) UDP ports</span>
<span style="color: #007800;">IG_UDP_CPORTS</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #666666; font-style: italic;"># Common outbound (egress) TCP ports</span>
<span style="color: #007800;">EG_TCP_CPORTS</span>=<span style="color: #ff0000;">&quot;21,25,80,443,43&quot;</span>
<span style="color: #666666; font-style: italic;"># Common outbound (egress) UDP ports</span>
<span style="color: #007800;">EG_UDP_CPORTS</span>=<span style="color: #ff0000;">&quot;20,21,53&quot;</span></pre></div></div>

<p>And when we want a special port allowance for specific users, we can declare it easily. For example, if we want port 22 open for user ID 0, we&#8217;d use this code:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Allow outbound access to destination port 22 for uid 0</span>
<span style="color: #007800;">EG_TCP_UID</span>=<span style="color: #ff0000;">&quot;0:22&quot;</span></pre></div></div>

<p>The next few sections on <code>Remote Rule Imports</code> and <code>Global Trust</code> are a little more specialized, and I encourage you to read a little more about them (since there&#8217;s so much to them and not enough space to cover them here on the blog). An important feature of APF is that it imports block lists from outside sources to keep you safe from some attackers, so the <code>Remote Rule Imports</code> can prove to be very useful. The <code>Global Trust</code> section is incredibly useful for multi-server deployments of APF. Here, you can set up your global allow/block lists and have them all pull from a central location so that you can make a single update to the source and have the update propogated to all servers in your configuration. These changes are synced to the <code>glob_allow/deny.rules</code> files, and they will be downloaded (and overwritten) on a regular basis from your specified source, so don&#8217;t make any manual edits in <code>glob_allow/deny.rules</code>.</p>
<p>As you can see, <code>apf.conf</code> is no joke. It has a lot of stuff going on, but it&#8217;s very straightforward and documented well. Once we&#8217;ve set up apf.conf with the configurations we need, it&#8217;s time to look at the more focused <code>allow_hosts.rules</code> and <code>deny_hosts.rules</code> files. These <code>.rules</code> files are where where you put your typical firewall rules in place. If there&#8217;s one piece of advice I can give you about these configurations, it would be to check if your traffic is already allowed or blocked. Having multiple rules that do the same thing (possibly in different places) is confusing and potentially dangerous. </p>
<p>The <code>deny_hosts.rules</code> configuration will look just like <code>allow_hosts.rules</code>, but it&#8217;s performing the opposite function. Let&#8217;s check out an <code>allow_hosts.rules</code> configuration that will allow the Nimsoft service to function:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">tcp:<span style="color: #000000; font-weight: bold;">in</span>:<span style="color: #007800;">d</span>=<span style="color: #000000;">48000</span>_48020:<span style="color: #007800;">s</span>=10.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span>
tcp:out:<span style="color: #007800;">d</span>=<span style="color: #000000;">48000</span>_48020:<span style="color: #007800;">d</span>=10.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span></pre></div></div>

<p>The format is somewhat simplistic, but the file gives a little more context in the comments:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># The trust rules can be made in advanced format with 4 options</span>
<span style="color: #666666; font-style: italic;"># (proto:flow:port:ip);</span>
<span style="color: #666666; font-style: italic;"># 1) protocol: [packet protocol tcp/udp]</span>
<span style="color: #666666; font-style: italic;"># 2) flow in/out: [packet direction, inbound or outbound]</span>
<span style="color: #666666; font-style: italic;"># 3) s/d=port: [packet source or destination port]</span>
<span style="color: #666666; font-style: italic;"># 4) s/d=ip(/xx) [packet source or destination address, masking supported]</span>
<span style="color: #666666; font-style: italic;"># Syntax:</span>
<span style="color: #666666; font-style: italic;"># proto:flow:[s/d]=port:[s/d]=ip(/mask)</span></pre></div></div>

<p>APF also uses <code>ds_hosts.rules</code> to load the DShield.org blocklist, and I assume the <code>ecnshame_hosts.rules</code> does something similar (can&#8217;t find much information about it), so you won&#8217;t need to edit these files manually. Additionally, you probably don&#8217;t need to make any changes to <code>log.rules</code>, unless you want to make changes to what exactly you log. As it stands, it logs certain dropped connections, which should be enough. Also, it might be worth noting that this file is a script, not a configuration file.</p>
<p>The last two configuration files are the <code>preroute.rules</code> and <code>postroute.rules</code> that (unsurprisingly) are used to make routing changes. If you have been following my articles, this corresponds to the <a href="http://blog.softlayer.com/2011/iptables-tips-and-tricks-port-redirection/">iptables chains for PREROUTING and POSTROUTING</a> where you would do things like port forwarding and other advanced configuration that you probably don&#8217;t want to do in most cases.</p>
<h3>APF Command Line Management</h3>
<p>As I mentioned in the &#8220;points to consider&#8221; at the top of this post, it&#8217;s important to learn the changes you can perform from the command line, and APF has some very useful command line tools:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># apf --help</span>
APF version <span style="color: #000000;">9.7</span> <span style="color: #000000; font-weight: bold;">&lt;</span>apf<span style="color: #000000; font-weight: bold;">@</span>r-fx.org<span style="color: #000000; font-weight: bold;">&gt;</span>
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>C<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2002</span>-<span style="color: #000000;">2011</span>, R-fx Networks <span style="color: #000000; font-weight: bold;">&lt;</span>proj<span style="color: #000000; font-weight: bold;">@</span>r-fx.org<span style="color: #000000; font-weight: bold;">&gt;</span>
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>C<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2011</span>, Ryan MacDonald <span style="color: #000000; font-weight: bold;">&lt;</span>ryan<span style="color: #000000; font-weight: bold;">@</span>r-fx.org<span style="color: #000000; font-weight: bold;">&gt;</span>
This program may be freely redistributed under the terms of the GNU GPL
&nbsp;
usage <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>apf <span style="color: #7a0874; font-weight: bold;">&#91;</span>OPTION<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #660033;">-s</span><span style="color: #000000; font-weight: bold;">|</span>--start ......................... load all firewall rules
<span style="color: #660033;">-r</span><span style="color: #000000; font-weight: bold;">|</span>--restart ....................... stop <span style="color: #7a0874; font-weight: bold;">&#40;</span>flush<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&amp;</span> reload firewall rules
<span style="color: #660033;">-f</span><span style="color: #000000; font-weight: bold;">|</span>--stop........ .................. stop <span style="color: #7a0874; font-weight: bold;">&#40;</span>flush<span style="color: #7a0874; font-weight: bold;">&#41;</span> all firewall rules
<span style="color: #660033;">-l</span><span style="color: #000000; font-weight: bold;">|</span>--list .......................... list all firewall rules
<span style="color: #660033;">-t</span><span style="color: #000000; font-weight: bold;">|</span>--status ........................ output firewall status log
<span style="color: #660033;">-e</span><span style="color: #000000; font-weight: bold;">|</span>--refresh ....................... refresh <span style="color: #000000; font-weight: bold;">&amp;</span> resolve dns names <span style="color: #000000; font-weight: bold;">in</span> trust rules
<span style="color: #660033;">-a</span> HOST CMT<span style="color: #000000; font-weight: bold;">|</span>--allow HOST COMMENT ... add host <span style="color: #7a0874; font-weight: bold;">&#40;</span>IP<span style="color: #000000; font-weight: bold;">/</span>FQDN<span style="color: #7a0874; font-weight: bold;">&#41;</span> to allow_hosts.rules and
                                     immediately load new rule into firewall
<span style="color: #660033;">-d</span> HOST CMT<span style="color: #000000; font-weight: bold;">|</span>--deny HOST COMMENT .... add host <span style="color: #7a0874; font-weight: bold;">&#40;</span>IP<span style="color: #000000; font-weight: bold;">/</span>FQDN<span style="color: #7a0874; font-weight: bold;">&#41;</span> to deny_hosts.rules and
                                     immediately load new rule into firewall
<span style="color: #660033;">-u</span><span style="color: #000000; font-weight: bold;">|</span>--remove HOST ................... remove host from <span style="color: #7a0874; font-weight: bold;">&#91;</span>glob<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span>_hosts.rules
                                     and immediately remove rule from firewall
<span style="color: #660033;">-o</span><span style="color: #000000; font-weight: bold;">|</span>--ovars ......................... output all configuration options</pre></div></div>

<p>You can use these command line tools to turn your firewall on and off, add allowed or blocked hosts and display troubleshooting information. These commands are very easy to use, but if you want more fine-tuned control, you&#8217;ll need to edit the configuration files directly (as we looked at above).</p>
<p>I know it seems like a lot of information, but to a large extent, that&#8217;s all you need to know to get started with APF. Take each section slowly and understand what each configuration file is doing, and you&#8217;ll master APF in no time at all.</p>
<p>-Mark</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/iptables-tips-and-tricks-apf-advanced-policy-firewall-configuration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
