<?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>PSEiBlog &#187; Iptables</title>
	<atom:link href="http://www.pseiko.nl/blog/tag/iptables/feed" rel="self" type="application/rss+xml" />
	<link>http://www.pseiko.nl/blog</link>
	<description>There are ten types of people... Those who understand binary and those who don't... Uh... Wait a minute...</description>
	<lastBuildDate>Fri, 07 May 2010 14:02:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Automatically remove duplicate IP&#8217;s in Iptables</title>
		<link>http://www.pseiko.nl/blog/375/automatically-remove-duplicate-ips-in-iptables</link>
		<comments>http://www.pseiko.nl/blog/375/automatically-remove-duplicate-ips-in-iptables#comments</comments>
		<pubDate>Wed, 09 Dec 2009 15:01:58 +0000</pubDate>
		<dc:creator>Remco</dc:creator>
				<category><![CDATA[Computer & Technology Related]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[clean]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[Iptables]]></category>

		<guid isPermaLink="false">http://www.pseiko.nl/blog/?p=375</guid>
		<description><![CDATA[Many scripts like my Persistent Iptables bans from Fail2Ban script add a bunch of DROP statements to Iptables chains.
I noticed that quite a lot of people ask questions on mailing lists and forums requesting a method to automatically remove any duplicate IP&#8217;s that might exist within a given chain.
Well, that&#8217;s quite easy to accomplish really, [...]]]></description>
			<content:encoded><![CDATA[<p>Many scripts like my <a href="http://www.pseiko.nl/blog/288/persistent-iptables-bans-from-fail2ban">Persistent Iptables bans from Fail2Ban script</a> add a bunch of DROP statements to Iptables chains.</p>
<p>I noticed that quite a lot of people ask questions on mailing lists and forums requesting a method to automatically remove any duplicate IP&#8217;s that might exist within a given chain.</p>
<p>Well, that&#8217;s quite easy to accomplish really, just run this little PHP script I created as root, and your Iptables is once again clean as a whistle!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Configuration
 */</span>
<span style="color: #000088;">$chain</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Blocklist&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$safelist</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x.x.x.x&quot;</span>
                 <span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;y.y.y.y&quot;</span>
                 <span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;z.z.z.z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">shell_exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'iptables -S '</span><span style="color: #339933;">.</span><span style="color: #000088;">$chain</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$iparr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ref</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span>sizeof<span style="color: #009900;">&#40;</span><span style="color: #000088;">$iparr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iparr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$ref</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$iparr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ref</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span>sizeof<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ref</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ref</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$ref</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stristr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;0.0.0.0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$ip</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #000088;">$safelist</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #000088;">$ref</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Duplicate IP found: <span style="color: #006699; font-weight: bold;">$ip</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #000088;">$ref</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #990000;">shell_exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'iptables -D '</span><span style="color: #339933;">.</span><span style="color: #000088;">$chain</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' -s '</span><span style="color: #339933;">.</span><span style="color: #000088;">$ip</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' -j DROP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$ref</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">array_search</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #000088;">$ref</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.pseiko.nl/blog/375/automatically-remove-duplicate-ips-in-iptables/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Persistent Iptables bans from Fail2Ban</title>
		<link>http://www.pseiko.nl/blog/288/persistent-iptables-bans-from-fail2ban</link>
		<comments>http://www.pseiko.nl/blog/288/persistent-iptables-bans-from-fail2ban#comments</comments>
		<pubDate>Tue, 18 Aug 2009 13:53:23 +0000</pubDate>
		<dc:creator>Remco</dc:creator>
				<category><![CDATA[Blogs & Hersenspinsels]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Fail2Ban]]></category>
		<category><![CDATA[Firewall]]></category>
		<category><![CDATA[Iptables]]></category>

		<guid isPermaLink="false">http://www.pseiko.nl/blog/?p=288</guid>
		<description><![CDATA[On my servers I use the nifty program Fail2Ban to perform logbased automatic fire walling of &#8216;bad&#8217; ip&#8217;s.
The idea behind this is easy: Some IP performs an action I don&#8217;t approve of. This can be any number of things, e.g. requesting pages in Apache that are commonly accessed by bots and/or scanners, or trying to [...]]]></description>
			<content:encoded><![CDATA[<p>On my servers I use the nifty program <a href="http://www.fail2ban.org/wiki/index.php/Main_Page">Fail2Ban</a> to perform logbased automatic fire walling of &#8216;bad&#8217; ip&#8217;s.</p>
<p>The idea behind this is easy: Some IP performs an action I don&#8217;t approve of. This can be any number of things, e.g. requesting pages in Apache that are commonly accessed by bots and/or scanners, or trying to log in to SSH with accounts that do not exist on the system. This bad behavior gets logged, and Fail2Ban keeps tabs on those logs, and using a number of rules it determines if a host is &#8216;bad&#8217; enough to temporarily or permanently ban all access to the server. It does so by adding a few chains to Iptables (one for each thing it checks for), and dynamically adding/removing IP&#8217;s to/from these chains.</p>
<p>This all works perfectly. However, there&#8217;s one issue; When Iptables gets reloaded, it restores its default rules, removing the Fail2Ban chains and all the rules they contain, even if the ip&#8217;s in the chain were marked as permanent.<br />
<span id="more-288"></span></p>
<p>I created a workaround for this problem, consisting of two simple steps:<br />
- When a &#8216;bad&#8217; ip gets banned, it&#8217;s added to the Iptables chain, but also written to a file, containing all collected &#8216;bad&#8217; ip&#8217;s. (I use /etc/shitlist for this purpose).<br />
- Whenever Iptables gets reloaded, I run a PHP script that checks the /etc/shitlist file for &#8217;safe&#8217; and duplicate ip&#8217;s, and writes all other ip&#8217;s to the permanent Blocklist chain. (The checking for &#8217;safe&#8217; ip&#8217;s might be a bit unneeded, but with my Fail2Ban rules it&#8217;s possible that one of my own ip&#8217;s gets banned for 10 minutes if a SSH login attempt fails for 5 times. Though it&#8217;s a temporary ban, the ip will still get written to the shitlist, and would end up in the permanent Blocklist).</p>
<p>To make this work, I made the following changes:</p>
<p>Every &#8216;jail&#8217; in Fail2Ban uses an &#8216;action.d&#8217; script to perform (un)banning. I defaulted all actions to an action script called &#8216;iptables-allports.conf&#8217;. Basically this action drops everything in Iptables if a package originates from the &#8216;bad&#8217; IP.<br />
I updated the ban action such that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">actionban = iptables <span style="color: #660033;">-I</span> fail2ban-<span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">1</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">&lt;</span>ip<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #660033;">-j</span> DROP
                 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">&lt;</span>ip<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>shitlist</pre></td></tr></table></div>

<p>After that I created a PHP script that updates Iptables with the ip&#8217;s contained in the shitlist:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/** script that loads a shitlist file into iptables
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//CONFIG</span>
<span style="color: #000088;">$shitlists</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/root/list.txt&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;/etc/shitlist&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$chain</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Blocklist&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$safelist</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x.x.x.x&quot;</span>
                 <span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;y.y.y.y&quot;</span>
                 <span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;z.z.z.z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Reading current IPTABLES state<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">shell_exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'iptables -S '</span><span style="color: #339933;">.</span><span style="color: #000088;">$chain</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$iparr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ref</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span>sizeof<span style="color: #009900;">&#40;</span><span style="color: #000088;">$iparr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iparr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$ref</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$iparr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ref</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shitlists</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$shitlist</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Reading shitlist at <span style="color: #006699; font-weight: bold;">$shitlist</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//READ FILE</span>
        <span style="color: #000088;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shitlist</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$itt</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$iparr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_search</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #000088;">$iparr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span>false<span style="color: #339933;">&amp;&amp;</span>array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #000088;">$safelist</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span>false<span style="color: #339933;">&amp;&amp;</span>array_search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/32&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$ref</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                        <span style="color: #000088;">$iparr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ip</span><span style="color: #339933;">;</span>
                                        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Now adding <span style="color: #006699; font-weight: bold;">$ip</span> to <span style="color: #006699; font-weight: bold;">$chain</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                                        <span style="color: #000088;">$ins</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">18</span><span style="color: #339933;">+</span><span style="color: #000088;">$itt</span><span style="color: #339933;">;</span>
                                        <span style="color: #990000;">shell_exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;iptables -I &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$chain</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ins</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; -s &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ip</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/32 -j DROP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #000088;">$itt</span><span style="color: #339933;">++;</span>
                                <span style="color: #009900;">&#125;</span>
                        <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Finished adding <span style="color: #006699; font-weight: bold;">$itt</span> ip's from list <span style="color: #006699; font-weight: bold;">$shitlist</span> to chain <span style="color: #006699; font-weight: bold;">$chain</span> . Bye!<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$itt</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Could not open shitlist file <span style="color: #006699; font-weight: bold;">$shitlist</span> . Skipping this list<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Finished adding <span style="color: #006699; font-weight: bold;">$total</span> ip's to chain <span style="color: #006699; font-weight: bold;">$chain</span> from &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$shitlists</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; shitlists.<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You can run the script from the commandline (as root!) simply by stating &#8220;php shitlist.php&#8221;, or add it to the startup script of your Iptables installation.</p>
<p>Hope this helps keeping your NIC&#8217;s available for VALID traffic!</p>
<p><b>Update:</b> Made some changes to the script to check for already existing bans, to keep your chains clean!<br />
<b>Update 2:</b> Little tweak to the script so it now loads an array of lists, in case you have various sources.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pseiko.nl/blog/288/persistent-iptables-bans-from-fail2ban/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
