<?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; APF</title>
	<atom:link href="http://blog.softlayer.com/tag/apf/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>Wed, 15 May 2013 15:33:34 +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: CSF Configuration</title>
		<link>http://blog.softlayer.com/2013/iptables-tips-and-tricks-csf-configuration/</link>
		<comments>http://blog.softlayer.com/2013/iptables-tips-and-tricks-csf-configuration/#comments</comments>
		<pubDate>Tue, 19 Mar 2013 18:10:39 +0000</pubDate>
		<dc:creator>Mark Ridlen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[APF]]></category>
		<category><![CDATA[blocking]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[CSF]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[iptab]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10812</guid>
		<description><![CDATA[In our last &#8220;iptables Tips and Tricks&#8221; installment, we talked about Advanced Policy Firewall (APF) configuration, so it should come as no surprise that in this installment, we&#8217;re turning our attention to ConfigServer Security &#38; Firewall (CSF). Before we get started, you should probably run through the list of warnings I include at the top [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://blog.softlayer.com/2013/iptables-tips-and-tricks-apf-advanced-policy-firewall-configuration/">our last &#8220;iptables Tips and Tricks&#8221; installment</a>, we talked about Advanced Policy Firewall (APF) configuration, so it should come as no surprise that in this installment, we&#8217;re turning our attention to <a href="http://configserver.com/cp/csf.html">ConfigServer Security &amp; Firewall</a> (CSF). Before we get started, you should probably run through the list of warnings I include at the top of the APF blog post and make sure you have your Band-Aid ready in case you need it.</p>
<p>To get the ball rolling, we need to download CSF and install it on our server. In this post, we&#8217;re working with a CentOS 6.0 32-bit server, so our (root) terminal commands would look like this to download and install CSF:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.configserver.com<span style="color: #000000; font-weight: bold;">/</span>free<span style="color: #000000; font-weight: bold;">/</span>csf.tgz <span style="color: #666666; font-style: italic;">#Download CSF using wget.</span>
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> zxvf csf.tgz <span style="color: #666666; font-style: italic;">#Unpack it.</span>
$ yum <span style="color: #c20cb9; font-weight: bold;">install</span> perl-libwww-perl <span style="color: #666666; font-style: italic;">#Make sure perl modules are installed ...</span>
$ yum <span style="color: #c20cb9; font-weight: bold;">install</span> perl-Time-HiRes  <span style="color: #666666; font-style: italic;">#Otherwise it will generate an error.</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> csf
$ .<span style="color: #000000; font-weight: bold;">/</span>install.sh <span style="color: #666666; font-style: italic;">#Install CSF.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#MAKE SURE YOU HAVE YOUR BAND-AID READY</span>
&nbsp;
$ <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>csf start <span style="color: #666666; font-style: italic;">#Start CSF. (Note: You can also use '$ service csf start')</span></pre></div></div>

<p>Once you start CSF, you can see a list of the default rules that load at startup. CSF defaults to a DROP policy:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ iptables <span style="color: #660033;">-nL</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> policy
Chain INPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy DROP<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Chain FORWARD <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy DROP<span style="color: #7a0874; font-weight: bold;">&#41;</span>
Chain OUTPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy DROP<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Don&#8217;t ever run &#8220;<code>iptables -F</code>&#8221; unless you want to lock yourself out. In fact, you might want to add &#8220;This server is running CSF &#8211; do not run &#8216;iptables -F&#8217;&#8221;  to your <code>/etc/motd</code>, just as a reminder/warning to others.</p>
<p>CSF loads on startup by default. This means that if you get locked out, a simple reboot probably won&#8217;t fix the problem. Runlevels 2, 3, 4, and 5 are all on:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ chkconfig <span style="color: #660033;">--list</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> csf
csf             <span style="color: #000000;">0</span>:off   <span style="color: #000000;">1</span>:off   <span style="color: #000000;">2</span>:on    <span style="color: #000000;">3</span>:on    <span style="color: #000000;">4</span>:on    <span style="color: #000000;">5</span>:on    <span style="color: #000000;">6</span>:off</pre></div></div>

<p>Some features of CSF will not work unless you have certain iptables modules installed. I believe they are installed by default in CentOS, but if you custom-built your iptables, they might not all be installed. Run this script to see if all modules are installed:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf<span style="color: #000000; font-weight: bold;">/</span>csftest.pl
Testing ip_tables<span style="color: #000000; font-weight: bold;">/</span>iptable_filter...OK
Testing ipt_LOG...OK
Testing ipt_multiport<span style="color: #000000; font-weight: bold;">/</span>xt_multiport...OK
Testing ipt_REJECT...OK
Testing ipt_state<span style="color: #000000; font-weight: bold;">/</span>xt_state...OK
Testing ipt_limit<span style="color: #000000; font-weight: bold;">/</span>xt_limit...OK
Testing ipt_recent...OK
Testing xt_connlimit...OK
Testing ipt_owner<span style="color: #000000; font-weight: bold;">/</span>xt_owner...OK
Testing iptable_nat<span style="color: #000000; font-weight: bold;">/</span>ipt_REDIRECT...OK
Testing iptable_nat<span style="color: #000000; font-weight: bold;">/</span>ipt_DNAT...OK
&nbsp;
RESULT: csf should <span style="color: #000000; font-weight: bold;">function</span> on this server</pre></div></div>

<p>As I mentioned, this is the default iptables installation on a minimal CentOS 6.0 image, so chances are good that these modules are already installed on your system. It never hurts to check, though.</p>
<h3>The CSF Configuration File</h3>
<p>The primary CSF configuration is stored in the well-documented <code>/etc/csf/csf.conf</code> file. CSF is <em>extremely</em> configurable, so there are a lot of options to read over. Let&#8217;s take a look over some of the more important features:</p>
<p><span id="more-10812"></span></p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Testing</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">TESTING = <span style="color: #ff0000;">&quot;1&quot;</span>
TESTING_INTERVAL = <span style="color: #ff0000;">&quot;5&quot;</span></pre></div></div>

<p>This TESTING cron job runs every &#8220;5&#8243; minutes so you don&#8217;t lock yourself out when you&#8217;re testing your rules. When you are satisfied with your rules (and confident that you won&#8217;t lock yourself out), you can set TESTING to &#8220;0&#8243;.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Globally Allowed Ports</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Allow incoming TCP ports</span>
TCP_IN = <span style="color: #ff0000;">&quot;20,21,22,25,53,80,110,143,443,465,587,993,995&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Allow outgoing TCP ports</span>
TCP_OUT = <span style="color: #ff0000;">&quot;20,21,22,25,53,80,110,113,443&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Allow incoming UDP ports</span>
UDP_IN = <span style="color: #ff0000;">&quot;20,21,53&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Allow outgoing UDP ports</span>
<span style="color: #666666; font-style: italic;"># To allow outgoing traceroute add 33434:33523 to this list</span>
UDP_OUT = <span style="color: #ff0000;">&quot;20,21,53,113,123&quot;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:5px;"><strong>Incoming Ping Requests</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Allow incoming PING</span>
ICMP_IN = <span style="color: #ff0000;">&quot;1&quot;</span></pre></div></div>

<p>Allowing ping is usually a good option for diagnostic purposes, so I don&#8217;t recommend turning it off. Disallowing ping is an example of &#8220;security through obscurity,&#8221; and it will not typically dissuade your attackers.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Ethernet Device</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ETH_DEVICE = <span style="color: #ff0000;">&quot;&quot;</span>
ETH6_DEVICE = <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>

<p>Here, you can configure iptables to ONLY use one Ethernet adapter. You might want to only guard your public network adapter in some situations.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>IP Limit in Permanent &#8220;Deny&#8221; File</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">DENY_IP_LIMIT = <span style="color: #ff0000;">&quot;200&quot;</span></pre></div></div>

<p>A higher number here will obviously screen out more IP addresses in <code>csf.deny</code>, but higher numbers also may cause slowdowns.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>IP Limit in Temporary &#8220;Deny&#8221; File</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">DENY_TEMP_IP_LIMIT = <span style="color: #ff0000;">&quot;100&quot;</span></pre></div></div>

<p>Similar to DENY_IP_LIMIT, the DENY_TEMP_IP_LIMIT represents the maximum number of IPs that can be stored in the temporary ban list.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>SMTP Blocking</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">SMTP_BLOCK = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>When set to &#8220;1&#8243;, SMTP_BLOCK does not completely block outbound SMTP, but it does block it for most users. This will prevent malicious scripts and compromised users from making outbound connections from unauthorized mail clients on the server. SMTP_BLOCK doesn&#8217;t stop those scripts from running, but it does stop them from functioning. Mail sent through the proper channels will still be delivered normally.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Allowing SMTP on localhost</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">SMTP_ALLOWLOCAL = <span style="color: #ff0000;">&quot;1&quot;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:5px;"><strong>Custom Mail Port Designation</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">SMTP_PORTS = <span style="color: #ff0000;">&quot;25,465,587&quot;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:5px;"><strong>Allowing SMTP Access to Users/Groups</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">SMTP_ALLOWUSER = <span style="color: #ff0000;">&quot;&quot;</span>
SMTP_ALLOWGROUP = <span style="color: #ff0000;">&quot;mail,mailman&quot;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:5px;"><strong>SYN Flood Protection</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">SYNFLOOD = <span style="color: #ff0000;">&quot;0&quot;</span>
SYNFLOOD_RATE = <span style="color: #ff0000;">&quot;100/s&quot;</span>
SYNFLOOD_BURST = <span style="color: #ff0000;">&quot;150&quot;</span></pre></div></div>

<p>Per the documentation, you should only enable SYN flood protection (SYNFLOOD= &#8220;1&#8243;) if you are currently under a SYN flood attack.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Concurrent Connections Limit</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CONNLIMIT = <span style="color: #ff0000;">&quot;22;5,80;20&quot;</span>
PORTFLOOD = <span style="color: #ff0000;">&quot;22;tcp;5;300,80;tcp;20;5</span></pre></div></div>

<p>These options allow you to add customized DoS protection. CONNLIMIT handles the number of concurrent connections, and in this example, we&#8217;re limiting port 22 to 5 connections and port 80 to 20 connections.</p>
<p>PORTFLOOD on deals with connections per second. In this example, we&#8217;re limiting the TCP connection on port 22 to 5 connections/second with a quiet period of 300 seconds before the connection is unblocked. Additonally, we&#8217;re limiting the TCP connection on port 80 to 20 connections/second with a quiet period of 5 seconds before the connection is unblocked.</p>
<p>Check the <code>readme.txt</code> file for more information about the syntax.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Logging to Syslog</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">SYSLOG = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>When enabled, this option logs lfd (Login Failure Daemon) messages to syslog as well as to <code>/var/log/lfd.log</code>.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Dropping v. Rejecting Packets</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">DROP = <span style="color: #ff0000;">&quot;DROP&quot;</span></pre></div></div>

<p>This configuration allows you to either DROP or REJECT packets. REJECT tells the sender that the packet has been blocked by the firewall. DROP just drops the packet and does not send a response. I like DROP better for regular use, but REJECT might be more helpful if you need to diagnose a connectivity issue.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Logging Dropped Connections</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">DROP_LOGGING = <span style="color: #ff0000;">&quot;1&quot;</span></pre></div></div>

<p>This option logs dropped connections to syslog. I don&#8217;t see any reason to turn this off unless your hard drive is getting full.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Port Exceptions When Logging Dropped Connections</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">DROP_NOLOG = <span style="color: #ff0000;">&quot;67,68,111,113,135:139,445,500,513,520&quot;</span></pre></div></div>

<p>These ports are specifically blocked from being logged either to conserve hard drive space or make the log file easier to read.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>&#8220;Watch Mode&#8221;</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">WATCH_MODE = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>If you are ever stuck trying to troubleshoot a large ruleset, you might consider turning this option on. You can use it to track the actions to watched IP addresses to see where they are getting blocked or accepted.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Login Failure Daemon Alert</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_ALERT_TO = <span style="color: #ff0000;">&quot;&quot;</span>
LF_ALERT_FROM = <span style="color: #ff0000;">&quot;&quot;</span>
LF_ALERT_SMTP = <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>

<p>You can specify an email address to report errors from the Login Failure Daemon, which tracks and automatically blocks brute force login attempts.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Permanent Blocks and NetBlocks</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_PERMBLOCK = <span style="color: #ff0000;">&quot;1&quot;</span>
LF_PERMBLOCK_INTERVAL = <span style="color: #ff0000;">&quot;86400&quot;</span>
LF_PERMBLOCK_COUNT = <span style="color: #ff0000;">&quot;4&quot;</span>
LF_PERMBLOCK_ALERT = <span style="color: #ff0000;">&quot;1&quot;</span>
LF_NETBLOCK = <span style="color: #ff0000;">&quot;0&quot;</span>
LF_NETBLOCK_INTERVAL = <span style="color: #ff0000;">&quot;86400&quot;</span>
LF_NETBLOCK_COUNT = <span style="color: #ff0000;">&quot;4&quot;</span>
LF_NETBLOCK_CLASS = <span style="color: #ff0000;">&quot;C&quot;</span>
LF_NETBLOCK_ALERT = <span style="color: #ff0000;">&quot;1&quot;</span></pre></div></div>

<p>These settings control the permanent block and netblock blocking. You probably don&#8217;t need to touch these settings, but you might want some additional security or less security depending on your company needs. If something gets permablocked, it will require your intervention to clear it, which might create downtime for your clients. Likewise, if a legitimate IP address happens to be part of a netblock which has an attacking IP address on it, it will get blocked if you have that feature turned on. A class C network encompasses 256 IP addresses. You can set this to class B or A, but that could block thousands or millions of IP addresses, respectively. Unless you find yourself under constant attack, I would advise you to leave that LF_NETBLOCK off.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Additional Protection During Updates</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Safe Chain Update. If enabled, all dynamic update chains (GALLOW*, GDENY*,</span>
<span style="color: #666666; font-style: italic;"># SPAMHAUS, DSHIELD, BOGON, CC_ALLOW, CC_DENY, ALLOWDYN*) will create a new</span>
<span style="color: #666666; font-style: italic;"># chain when updating, and insert it into the relevant LOCALINPUT/LOCALOUTPUT</span>
<span style="color: #666666; font-style: italic;"># chain, then flush and delete the old dynamic chain and rename the new chain.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># This prevents a small window of opportunity opening when an update occurs and</span>
<span style="color: #666666; font-style: italic;"># the dynamic chain is flushed for the new rules.</span>
SAFECHAINUPDATE = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>Activating this option will increase your system resource usage and will require more rules to be running at one time, but it provides an additional layer of protection during updates. Without this option turned on, your rules will be flushed for a short amount of time, leaving your server vulnerable.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Multi-Server Deployment Options</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_GLOBAL = <span style="color: #ff0000;">&quot;0&quot;</span>
GLOBAL_ALLOW = <span style="color: #ff0000;">&quot;&quot;</span>
GLOBAL_DENY = <span style="color: #ff0000;">&quot;&quot;</span>
GLOBAL_IGNORE = <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>

<p>Like APF, you can configure global lists for multiple server deployments. You&#8217;ll need to specify a URL of the text file with the IP addresses for the global lists.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>SPAMHAUSE Blocklist</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_SPAMHAUS = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>This option enables the SPAMHAUS blocklist. Specify the number of seconds between refreshes. Recommended setting is 86400 (1 day).</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Blocking TOR Exit IP Addresses</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_TOR = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>Enabling this option will block TOR exit IP addresses. If you are not familiar with <a href="https://www.torproject.org/">TOR</a>, it is a completely anonymous proxy network. This could block some legitimate users who are trying to protect their anonymity, so I would recommend only turning this on if you are already under attack from a TOR exit address.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Blocking Bogon Addresses</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_BOGON = <span style="color: #ff0000;">&quot;0&quot;</span>
LF_BOGON_URL = <span style="color: #ff0000;">&quot;http://www.cymru.com/Documents/bogon-bn-agg.txt&quot;</span>
LF_BOGON_SKIP = <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>

<p>Blocking bogon addresses (addresses that should not be possible) is usually a good decision. To enable, set the number of seconds between refreshes. I recommend enabling this option and setting the refresh at 86400 (1 day). If you do so, be sure to add your private network adapters to the skip list.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Country-Specific Access to Your Server</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CC_DENY = <span style="color: #ff0000;">&quot;&quot;</span>
CC_ALLOW = <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>

<p>With these options, you can block or allow entire countries from accessing your server. To do so, enter the country codes in a comma separated list. Even though this generates a lot of additional rules, it&#8217;s valuable to some sysadmins.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CC_ALLOW_FILTER = <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>

<p>Alternatively, you can set your server to exclusively accept traffic from a list of country codes. All other countries not listed will have their traffic dropped. There are many other settings related to these options that I don&#8217;t have time to cover in this blog.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Blocking Login Failures</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_TRIGGER = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>This enables blocking of login failures (per service). There are a lot of great customization options in this section.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Scanning Directories for Malicious Files</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_DIRWATCH = <span style="color: #ff0000;">&quot;300&quot;</span></pre></div></div>

<p>This feature scans <code>/tmp</code> and <code>/dev/shm</code> for potentially malicious files and alerts you to their presence based on the interval you designate. You can also have CSF automatically quarantine malicious files with this option:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_DIRWATCH_DISABLE = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:5px;"><strong>Distributed Attack Protection</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LF_DISTATTACK = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>By enabling this option, you activate additional protection against distributed attacks.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Blocking Based on Abusive Email Usage</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LT_POP3D = <span style="color: #ff0000;">&quot;0&quot;</span>
LT_IMAPD = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>If a user checks email too many times per hour (more than the non-zero value specified), the user&#8217;s IP address is blocked.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Email Alert Following Block</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">LT_EMAIL_ALERT = <span style="color: #ff0000;">&quot;1&quot;</span></pre></div></div>

<p>This will send you email when something is blocked. I&#8217;d recommend leaving it on.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Blocking IP Addresses Based on Number of Connections</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CT_LIMIT = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>This feature tracks connections and blocks the IP if the number of connections is too high. Use caution because if you enable this option and set this value too low, it will block legitimate traffic.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Application-Level Protection</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">PT_LIMIT = <span style="color: #ff0000;">&quot;60&quot;</span></pre></div></div>

<p>This feature provides application level protection against malicious scripts that take a long time to execute.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Blocking Port Scanners</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">PS_INTERVAL = <span style="color: #ff0000;">&quot;300&quot;</span>
PS_LIMIT = <span style="color: #ff0000;">&quot;10&quot;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:5px;"><strong>Enabling HTML User Interface for CSF</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">UI = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>CSF has a built-in HTML user interface. You can enable this by setting UI = &#8220;1&#8243;. There are a list of prerequisites for this option in the <code>readme.txt</code>.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Notifying Blocked IP Addresses</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">MESSENGER = <span style="color: #ff0000;">&quot;0&quot;</span></pre></div></div>

<p>This option will notify blocked IP addresses when they have been blocked by the firewall.</p>
<p style="margin-bottom:0; padding-bottom:5px;"><strong>Port Knocking</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">PORTKNOCKING = <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>

<p>CSF supports port knocking, which is a technique that provides an additional layer of security. See <a href="http://www.portknocking.org/">http://www.portknocking.org/</a> for details. </p>
<h3>Allow and Deny Lists</h3>
<p>As we walked through the CSF configuration file, you saw that I referenced the <code>csf.deny</code> file, so it should come as no surprise that CSF also includes <code>csf.allow</code> to customize &#8220;allow&#8221; rules as well. If you are familiar with APF, these files have a very similar syntax &#8230; Each entry is made up of the same four components: <code>protocol|flow|port|IP</code>. The only real difference being that APF uses the colon as a delimiter while CSF uses the pipe:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#APF Version</span>
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>
&nbsp;
<span style="color: #666666; font-style: italic;">#CSF Version</span>
tcp<span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000; font-weight: bold;">in</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #007800;">d</span>=<span style="color: #000000;">48000</span>_48020<span style="color: #000000; font-weight: bold;">|</span><span style="color: #007800;">s</span>=10.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span></pre></div></div>

<p>Fortunately, replacing your colon with a pipe is a minimally invasive procedure that can be automated with a tool like vi.</p>
<h3>CSF Command Line Tool</h3>
<p>The command line tool for CSF is much more robust than the one for APF:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ csf <span style="color: #660033;">--help</span>
csf: v5.79 <span style="color: #7a0874; font-weight: bold;">&#40;</span>cPanel<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
ConfigServer Security <span style="color: #000000; font-weight: bold;">&amp;</span> Firewall
<span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000;">2006</span>-<span style="color: #000000;">2013</span>, Way to the Web Limited <span style="color: #7a0874; font-weight: bold;">&#40;</span>http:<span style="color: #000000; font-weight: bold;">//</span>www.configserver.com<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
Usage: <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>csf <span style="color: #7a0874; font-weight: bold;">&#91;</span>option<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>value<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
Option              Meaning
-h, <span style="color: #660033;">--help</span>          Show this message
-l, <span style="color: #660033;">--status</span>        List<span style="color: #000000; font-weight: bold;">/</span>Show iptables configuration
-l6, <span style="color: #660033;">--status6</span>      List<span style="color: #000000; font-weight: bold;">/</span>Show ip6tables configuration
-s, <span style="color: #660033;">--start</span>         Start firewall rules
-f, <span style="color: #660033;">--stop</span>          Flush<span style="color: #000000; font-weight: bold;">/</span>Stop firewall rules <span style="color: #7a0874; font-weight: bold;">&#40;</span>Note: lfd may restart csf<span style="color: #7a0874; font-weight: bold;">&#41;</span>
-r, <span style="color: #660033;">--restart</span>       Restart firewall rules
-q, <span style="color: #660033;">--startq</span>        Quick restart <span style="color: #7a0874; font-weight: bold;">&#40;</span>csf restarted by lfd<span style="color: #7a0874; font-weight: bold;">&#41;</span>
-sf, <span style="color: #660033;">--startf</span>       Force CLI restart regardless of LF_QUICKSTART setting
-a, <span style="color: #660033;">--add</span> ip        Allow an IP and add to <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.allow
-ar, <span style="color: #660033;">--addrm</span> ip     Remove an IP from <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.allow and delete rule
-d, <span style="color: #660033;">--deny</span> ip       Deny an IP and add to <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.deny
-dr, <span style="color: #660033;">--denyrm</span> ip    Unblock an IP and remove from <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.deny
-df, <span style="color: #660033;">--denyf</span>        Remove and unblock all entries <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.deny
-g, <span style="color: #660033;">--grep</span> ip       Search the iptables rules <span style="color: #000000; font-weight: bold;">for</span> an IP match <span style="color: #7a0874; font-weight: bold;">&#40;</span>incl. CIDR<span style="color: #7a0874; font-weight: bold;">&#41;</span>
-t, <span style="color: #660033;">--temp</span>          Displays the current list of temp IP entries and their TTL
-tr, <span style="color: #660033;">--temprm</span> ip    Remove an IPs from the temp IP ban and allow list
-td, <span style="color: #660033;">--tempdeny</span> ip ttl <span style="color: #7a0874; font-weight: bold;">&#91;</span>-p port<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>-d direction<span style="color: #7a0874; font-weight: bold;">&#93;</span>
                    Add an IP to the temp IP ban list. ttl is how long to
                    blocks <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>default:seconds, can use one suffix of h<span style="color: #000000; font-weight: bold;">/</span>m<span style="color: #000000; font-weight: bold;">/</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.
                    Optional port. Optional direction of block can be one of:
                    <span style="color: #000000; font-weight: bold;">in</span>, out or inout <span style="color: #7a0874; font-weight: bold;">&#40;</span>default:<span style="color: #000000; font-weight: bold;">in</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
-ta, <span style="color: #660033;">--tempallow</span> ip ttl <span style="color: #7a0874; font-weight: bold;">&#91;</span>-p port<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>-d direction<span style="color: #7a0874; font-weight: bold;">&#93;</span>
                    Add an IP to the temp IP allow list <span style="color: #7a0874; font-weight: bold;">&#40;</span>default:inout<span style="color: #7a0874; font-weight: bold;">&#41;</span>
-tf, <span style="color: #660033;">--tempf</span>        Flush all IPs from the temp IP entries
-cp, <span style="color: #660033;">--cping</span>        PING all members <span style="color: #000000; font-weight: bold;">in</span> an lfd Cluster
-cd, <span style="color: #660033;">--cdeny</span> ip     Deny an IP <span style="color: #000000; font-weight: bold;">in</span> a Cluster and add to <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.deny
-ca, <span style="color: #660033;">--callow</span> ip    Allow an IP <span style="color: #000000; font-weight: bold;">in</span> a Cluster and add to <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.allow
-cr, <span style="color: #660033;">--crm</span> ip       Unblock an IP <span style="color: #000000; font-weight: bold;">in</span> a Cluster and remove from <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf.deny
-cc, <span style="color: #660033;">--cconfig</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>name<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>value<span style="color: #7a0874; font-weight: bold;">&#93;</span>
                    Change configuration option <span style="color: #7a0874; font-weight: bold;">&#91;</span>name<span style="color: #7a0874; font-weight: bold;">&#93;</span> to <span style="color: #7a0874; font-weight: bold;">&#91;</span>value<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">in</span> a Cluster
-cf, <span style="color: #660033;">--cfile</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Send <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">in</span> a Cluster to <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>csf<span style="color: #000000; font-weight: bold;">/</span>
-crs, <span style="color: #660033;">--crestart</span>    Cluster restart csf and lfd
-w, <span style="color: #660033;">--watch</span> ip      Log SYN packets <span style="color: #000000; font-weight: bold;">for</span> an IP across iptables chains
-m, <span style="color: #660033;">--mail</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>addr<span style="color: #7a0874; font-weight: bold;">&#93;</span>   Display Server Check <span style="color: #000000; font-weight: bold;">in</span> HTML or email to <span style="color: #7a0874; font-weight: bold;">&#91;</span>addr<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">if</span> present
-lr, <span style="color: #660033;">--logrun</span>       Initiate Log Scanner report via lfd
-c, <span style="color: #660033;">--check</span>         Check <span style="color: #000000; font-weight: bold;">for</span> updates to csf but <span style="color: #000000; font-weight: bold;">do</span> not upgrade
-u, <span style="color: #660033;">--update</span>        Check <span style="color: #000000; font-weight: bold;">for</span> updates to csf and upgrade <span style="color: #000000; font-weight: bold;">if</span> available
<span style="color: #660033;">-uf</span>                 Force an update of csf
-x, <span style="color: #660033;">--disable</span>       Disable csf and lfd
-e, <span style="color: #660033;">--enable</span>        Enable csf and lfd <span style="color: #000000; font-weight: bold;">if</span> previously disabled
-v, <span style="color: #660033;">--version</span>       Show csf version</pre></div></div>

<p>The command line tool will also tell you if the testing mode is enabled (which is a very useful feature). If TESTING were enabled, we&#8217;d see this line at the bottom of the output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">*</span>WARNING<span style="color: #000000; font-weight: bold;">*</span> TESTING mode is enabled - <span style="color: #000000; font-weight: bold;">do</span> not forget to disable it <span style="color: #000000; font-weight: bold;">in</span> the configuration</pre></div></div>

<p>Did you make it all the way through?! Great! I know it&#8217;s a lot to take in, but it&#8217;s not terribly complicated when we break it down and understand how each piece works. Next time, I&#8217;ll be back with some tips on integrating CSF into cPanel. </p>
<p>-Mark</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/iptables-tips-and-tricks-csf-configuration/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<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>
