<?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>gj |</title>
	<atom:link href="https://blog.gaiterjones.com/category/exim4/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.gaiterjones.com/category/exim4/</link>
	<description>gaiterjones</description>
	<lastBuildDate>Fri, 25 Apr 2025 11:57:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>How I Beat the Bots with Exim4 Rate Limiting and an Auto-Ban Firewall Script</title>
		<link>https://blog.gaiterjones.com/how-i-beat-the-bots-with-exim4-rate-limiting-and-an-auto-ban-firewall-script/</link>
					<comments>https://blog.gaiterjones.com/how-i-beat-the-bots-with-exim4-rate-limiting-and-an-auto-ban-firewall-script/#respond</comments>
		
		<dc:creator><![CDATA[PAJ]]></dc:creator>
		<pubDate>Fri, 25 Apr 2025 11:57:11 +0000</pubDate>
				<category><![CDATA[Exim4]]></category>
		<category><![CDATA[Spam]]></category>
		<guid isPermaLink="false">https://blog.gaiterjones.com/?p=2487</guid>

					<description><![CDATA[Introduction Email servers are constant targets for bots: spam attempts, relay tests, fake login floods. When I noticed a huge increase in spam and attack attempts on my Exim4 mail...<a class="more-link" href="https://blog.gaiterjones.com/how-i-beat-the-bots-with-exim4-rate-limiting-and-an-auto-ban-firewall-script/" title="Continue reading">Continue reading</a>]]></description>
										<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Email servers are constant targets for bots: spam attempts, relay tests, fake login floods.<br />
  When I noticed a <strong>huge increase</strong> in spam and attack attempts on my Exim4 mail server, I decided to <strong>fight back</strong> — properly.</p>
<p>In this post, I&#8217;ll show you how I:</p>
<ul>
<li><img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f9f9.png" alt="🧹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Cleaned up my Exim4 logs</li>
<li><img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f6ab.png" alt="🚫" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Rate-limited abusive IPs at the SMTP level</li>
<li><img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f525.png" alt="🔥" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Automatically blocked offenders at the firewall level</li>
<li><img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f5a5.png" alt="🖥" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Built a slick web dashboard to monitor everything in real-time</li>
</ul>
<p><strong>The result:</strong> The bots <strong>gave up</strong> and moved on. My server is calm, responsive, and fully protected.</p>
<h2>Step 1: Activate Exim4 Rate Limiting</h2>
<p>Exim4 has powerful built-in ratelimit features. I added a simple rule to my <code>acl_check_rcpt</code> ACL:</p>
<pre style="background:#eee; padding: 1rem; border-radius: 5px;">
deny
  ratelimit = 10 / 1h / strict
  message = Too many messages, try later
  log_message = Rate limited: $sender_host_address
  </pre>
<ul>
<li><strong>Limit:</strong> Max 10 messages per hour per IP.</li>
<li><strong>Strict:</strong> Every violation resets the timer.</li>
<li><strong>Logging:</strong> Every rate-limited IP gets logged.</li>
</ul>
<p>Suddenly, bots hammering my server were getting temporary rejections like:</p>
<blockquote><p>&#8220;Too many messages, try later.&#8221;</p></blockquote>
<p>And all rate-limited attempts were logged in <code>/var/log/exim4/mainlog</code>.</p>
<h2>Step 2: Harvest Offenders Automatically</h2>
<p>I wrote a simple script to <strong>extract IPs</strong> from the Exim log:</p>
<pre style="background:#eee; padding: 1rem; border-radius: 5px;">
docker exec -i mail_exim4_1 tail -n 1000 /var/log/exim4/mainlog \
  | grep 'Rate limited:' \
  | awk '{print $NF}' \
  | sort | uniq -c | awk '$1 >= 3 { print $2 }' \
  > /tmp/exim-rate-limited-ips.txt
  </pre>
<p>This gave me a clean list of serious offenders.</p>
<h2>Step 3: Block Them at the Firewall</h2>
<p>I used <strong>ipset</strong> and <strong>iptables</strong> to <strong>ban</strong> these IPs at the host level:</p>
<pre style="background:#eee; padding: 1rem; border-radius: 5px;">
sudo ipset create ratelimited hash:ip timeout 86400 -exist

while read ip; do
    sudo ipset add ratelimited "$ip" timeout 86400 -exist
done &lt; /tmp/exim-rate-limited-ips.txt
  </pre>
<p>Then inserted a firewall rule into the Docker-specific <code>DOCKER-USER</code> chain:</p>
<pre style="background:#eee; padding: 1rem; border-radius: 5px;">
sudo iptables -I DOCKER-USER 1 -m set --match-set ratelimited src -j DROP
  </pre>
<p><strong>Effect:</strong> As soon as an IP hit my server after being rate-limited by Exim, it was instantly <strong>dropped at the firewall</strong>, before it could even touch my containers.</p>
<h2>Step 4: Build a Status Dashboard</h2>
<p>To keep an eye on things, I built a <strong>dynamic HTML dashboard</strong> showing:</p>
<ul>
<li>Number of IPs currently blocked</li>
<li>Number of packets dropped</li>
<li>Top offending IPs</li>
<li>Auto-refresh every 30 seconds</li>
</ul>
<h2>Result: <strong>Victory</strong></h2>
<p>After about 24 hours:</p>
<ul>
<li>Spam attempts dropped to almost zero</li>
<li>Bots kept getting blocked instantly</li>
<li>The attackers finally <strong>gave up</strong> and went away</li>
</ul>
<p>My Exim4 server runs lighter and faster than ever, with completely clean mail logs.</p>
<h2>Conclusion</h2>
<p>Sometimes, you don&#8217;t need complex external tools or expensive firewall appliances.</p>
<p><strong>Exim + Linux + a bit of scripting = total control.</strong></p>
<p>If you&#8217;re running your own mail server, I highly recommend setting up something like this.<br />
  It&#8217;s simple, effective, and once configured, it needs almost no babysitting.</p>
<p><strong>Bots only win if you let them. <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f6e1.png" alt="🛡" class="wp-smiley" style="height: 1em; max-height: 1em;" /></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.gaiterjones.com/how-i-beat-the-bots-with-exim4-rate-limiting-and-an-auto-ban-firewall-script/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
