<?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>Jonathan's Blog &#187; shell scripts</title>
	<atom:link href="http://blog.exeko.com/category/linux/shell-scripts/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.exeko.com</link>
	<description>un blog pas comme les autres ...</description>
	<lastBuildDate>Fri, 30 Oct 2009 21:33:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Stop spam flood attack with postfix and iptables</title>
		<link>http://blog.exeko.com/2008/06/stop-spam-flood-postfix-iptables/</link>
		<comments>http://blog.exeko.com/2008/06/stop-spam-flood-postfix-iptables/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 12:01:47 +0000</pubDate>
		<dc:creator>eCliPs</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[shell scripts]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[postfix]]></category>
		<category><![CDATA[spam flood]]></category>

		<guid isPermaLink="false">http://blog.exeko.com/?p=30</guid>
		<description><![CDATA[For about 2 or 3 months, I constantly get attacked by an army of robots trying to spam my mailbox for the domain exeko.com  that by the way is participating in the busby seo challenge and a few of my clients domains. Yesterday night the spam flood attack was so violent that my poor [...]]]></description>
			<content:encoded><![CDATA[<p>For about 2 or 3 months, I constantly get attacked by an army of robots trying to spam my mailbox for the domain exeko.com  that by the way is participating in the <a href="http://exeko.com/busby-seo-challenge/">busby seo challenge</a> and a few of my clients domains. Yesterday night the spam flood attack was so violent that my poor little server hosted by <a href="http://www.ovh.com">OVH</a> started to swap like a crazy cow !</p>
<p>I finally decided to write a little shell script to read the mail log file and extract the last minute of log to count the number of rejected attempts to send mail to my server by the same host and ban this host using iptables if a defined limit is reached.</p>
<p>Here is a version of my script:<br />
<code>#!/bin/bash<br />
IPT=/sbin/iptables<br />
LIMIT=10<br />
cd /root/Filters<br />
# first get one minute of log<br />
grep "`date +"%b %d %H:%M:" --date="1 minutes ago"`" /var/log/mail.info > minutelog<br />
# now extract the rejected attempts, sort and count uniq ip<br />
cat minutelog | grep "reject:" | cut -d" " -f10 | cut -d"[" -f2 | cut -d"]" -f 1 | sort | uniq -c | sort -n | sed 's/^[ \t]*//' > tmp1<br />
# for each line in result<br />
while read line<br />
do<br />
  MYCOUNT=`echo $line | cut -d" " -f1`<br />
  MYIP=`echo $line | cut -d" " -f2`<br />
  if  [ $MYCOUNT -lt $LIMIT ] ;<br />
  then<br />
    echo $MYIP is ok: $MYCOUNT attempts<br />
  else<br />
    echo blocking the spammer at $MYIP with $MYCOUNT attempts<br />
    $IPT -I INPUT -i eth0 --proto tcp -s $MYIP --destination-port 25 -j DROP<br />
    echo $MYIP >> blocked.smtp<br />
  fi<br />
done < tmp1<br />
rm -f minutelog<br />
rm -f tmp1</code><br />
You can also download this script here: <a href='http://blog.exeko.com/wp-content/uploads/2008/06/filter_smtp.zip'>filter_smtp.zip</a></p>
<p>This shell script is running for about 12 hours on my mail server and it already stopped 701 spamming machines. Any feedback on the script is welcome ! </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.exeko.com/2008/06/stop-spam-flood-postfix-iptables/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
