<?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>Weblog of Michael Cutler &#187; Apache</title>
	<atom:link href="http://blog.lobstertechnology.com/category/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.lobstertechnology.com</link>
	<description>"I felt a great disturbance in the Force, as if millions of peers suddenly cried out in terror and were suddenly silenced."</description>
	<lastBuildDate>Tue, 17 Oct 2006 14:40:43 +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>Patch to mod_evasive to enhance reporting</title>
		<link>http://blog.lobstertechnology.com/2006/03/29/patch-to-mod_evasive-to-enhance-reporting/</link>
		<comments>http://blog.lobstertechnology.com/2006/03/29/patch-to-mod_evasive-to-enhance-reporting/#comments</comments>
		<pubDate>Wed, 29 Mar 2006 09:27:58 +0000</pubDate>
		<dc:creator>Michael Cutler</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://blog.lobstertechnology.com/2006/03/29/patch-to-mod_evasive-to-enhance-reporting/</guid>
		<description><![CDATA[This morning I took the opportunity to install mod_evasive on my Apache Web Server after being hammered by zombies last night. It appears to work well, I tested it out by loading it up with small scale DoS attacks. It blocked the offending addresses as expected and produced the relevant syslog entires &#038; triggered my external reporting script. I was however a little disappointed with its script execution functionality, it basically did a "system" call allowing you to pass only one argument - the offending IP address.]]></description>
			<content:encoded><![CDATA[<p>This morning I took the opportunity to install mod_evasive on my Apache Web Server after being hammered by zombies last night. Quote from [<a href="http://www.nuclearelephant.com/projects/mod_evasive/">www.nuclearelephant.com</a>]:</p>
<blockquote><p>mod_evasive is an evasive maneuvers module for Apache to provide evasive action in the event of an HTTP DoS or DDoS attack or brute force attack. It is also designed to be a detection and network management tool, and can be easily configured to talk to ipchains, firewalls, routers, and etcetera. mod_evasive presently reports abuses via email and syslog facilities.</p></blockquote>
<p>It appears to work well, I tested it out by loading it up with small scale DoS attacks. It blocked the offending addresses as expected and produced the relevant syslog entires &#038; triggered my external reporting script. I was however a little disappointed with its script execution functionality, it basically did a "system" call allowing you to pass only one argument - the offending IP address.</p>
<p>I already have <a href="http://www.modsecurity.org/">mod_security</a> installed which also executes an external reporting script. However mod_security has a neat little feature which I took for granted, it passes all the 'environment' variables from the request to the script allowing you to see the request itself &#038; any headers passed.</p>
<p>For example, a typical mod_security email alert for me would contain:</p>
<p><code>DOCUMENT_ROOT=/usr/local/apache/vhosts/www.domain.com<br />
GATEWAY_INTERFACE=CGI/1.1<br />
HTTP_ACCEPT=*/*<br />
HTTP_ACCEPT_ENCODING=gzip, x-gzip<br />
HTTP_CONNECTION=close<br />
HTTP_HOST=www.domain.com<br />
HTTP_MOD_SECURITY_ACTION=500<br />
HTTP_MOD_SECURITY_EXECUTED=/usr/local/scripts/modsec_alert.pl<br />
HTTP_MOD_SECURITY_MESSAGE=Access denied with code 500. Error normalizing REQUEST_URI: Invalid URL encoding detected: not enough characters<br />
HTTP_USER_AGENT=Mozilla/4.0<br />
PATH=/bin:/sbin...<br />
PATH_INFO=/search.cgi<br />
PATH_TRANSLATED=/usr/local/scripts/modsec_alert.pl<br />
QUERY_STRING=q='object+levels%<br />
REDIRECT_STATUS=302<br />
REMOTE_ADDR=XXX.XXX.XXX.XXX<br />
REMOTE_PORT=45852<br />
REQUEST_METHOD=GET<br />
REQUEST_URI=/cgi-bin/search.cgi?q='object+levels%<br />
SCRIPT_FILENAME=/usr/local/apache/vhosts/www.domain.com/cgi-bin<br />
SCRIPT_NAME=/cgi-bin<br />
SERVER_ADDR=XXX.XXX.XXX.XXX<br />
SERVER_ADMIN=foo@bar<br />
SERVER_NAME=www.domain.com<br />
SERVER_PORT=80<br />
SERVER_PROTOCOL=HTTP/1.1<br />
SERVER_SIGNATURE=<br />
SERVER_SOFTWARE=Apache</code></p>
<p>This shows me detailed information about the request which was declined and why. I wanted to get similar functionality out of mod_evasive and I achieved this with the following additional code (butchered from mod_security).</p>
<div class="igBar"><span id="lcpp-1"><a href="#" onclick="javascript:showPlainTxt('cpp-1'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-1">
<div class="cpp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>sys_command != <span style="color: #0000ff;">NULL</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">char</span> **env = <span style="color: #0000ff;">NULL</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> *args<span style="color: #000000;">&#91;</span><span style="color: #0000dd;color:#800000;">5</span><span style="color: #000000;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; ap_add_cgi_vars<span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; ap_add_common_vars<span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; env = <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span> **<span style="color: #000000;">&#41;</span>ap_create_environment<span style="color: #000000;">&#40;</span>r-&gt;pool, r-&gt;subprocess_env<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; ap_cleanup_for_exec<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; args<span style="color: #000000;">&#91;</span><span style="color: #0000dd;color:#800000;">0</span><span style="color: #000000;">&#93;</span> = filename;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; args<span style="color: #000000;">&#91;</span><span style="color: #0000dd;color:#800000;">1</span><span style="color: #000000;">&#93;</span> = text_add;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; args<span style="color: #000000;">&#91;</span><span style="color: #0000dd;color:#800000;">2</span><span style="color: #000000;">&#93;</span> = <span style="color: #0000ff;">NULL</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; execve<span style="color: #000000;">&#40;</span>sys_command, <span style="color: #000000;">&#40;</span><span style="color: #0000ff;">char</span> ** <span style="color: #0000ff;">const</span><span style="color: #000000;">&#41;</span>&amp;args, env<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The original mod_evasive code is expecting a sprintf format string as the parameter 'sys_command' allowing you to define a position with '%s' where the IP address should be inserted. My code above does not to this, it expects 'sys_command' to be the path to the executable which takes a single argument of the IP address.</p>
<p>This change can be applied automagically - to the Apache 1.3.x version of mod_evasive.c only - with the following patch: <a href="http://svn.lobstertechnology.com/mod_evasive/mod_evasive_execve.patch">mod_evasive_execve.patch</a></p>
<p>Assuming mod_evasive_1.10.1.tar.gz &#038; mod_evasive_execve.patch have already been downloaded to the same directory:</p>
<p><code>[foo@bar ~]$ <strong>tar zxf mod_evasive_1.10.1.tar.gz</strong><br />
[foo@bar ~]$ <strong>cd mod_evasive</strong><br />
[foo@bar mod_evasive]$ <strong>patch -p1 &lt; ../mod_evasive_execve.patch</strong><br />
patching file mod_evasive.c<br />
[foo@bar mod_evasive]$ <strong>$APACHE_ROOT/bin/apxs -iac mod_evasive.c</strong><br />
gcc -DLINUX=22 -DEAPI -I/usr/include/gdbm -DUSE_HSREGEX -fpic  -DEAPI -DSHARED_MODULE -I/usr/local/apache/include  -c mod_evasive.c<br />
gcc -shared -o mod_evasive.so mod_evasive.o<br />
[activating module `evasive' in /usr/local/apache/conf/httpd.conf]<br />
cp mod_evasive.so /usr/local/apache/libexec/mod_evasive.so<br />
chmod 755 /usr/local/apache/libexec/mod_evasive.so<br />
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bak<br />
cp /usr/local/apache/conf/httpd.conf.new /usr/local/apache/conf/httpd.conf<br />
rm /usr/local/apache/conf/httpd.conf.new<br />
[foo@bar mod_evasive]$ </code></p>
<p>Now create a simple shell/perl/something script to use this info. My example emails myself and the address listed as the SERVER_ADMIN, because each VirtualHost on my server has a 'ServerAdmin' entry with the owners email address, my customers get a copy of the email too.</p>
<div class="igBar"><span id="lperl-2"><a href="#" onclick="javascript:showPlainTxt('perl-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PERL:</span>
<div id="perl-2">
<div class="perl">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#!/usr/bin/perl</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># /usr/local/scripts/mod_evasive_alert.pl</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">$IP</span>=<span style="color: #0000ff;">$ARGV</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">$MSG</span>=<span style="color: #ff0000;">"mod_evasive has blacklisted the IP $IP.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">foreach</span> <span style="color: #0000ff;">$key</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.perldoc.com/perl5.6/pod/func/sort.html"><span style="color: #000066;">sort</span></a> <a href="http://www.perldoc.com/perl5.6/pod/func/keys.html"><span style="color: #000066;">keys</span></a> <span style="color: #0000ff;">%ENV</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0000ff;">$MSG</span> .= <span style="color: #0000ff;">$key</span> . <span style="color: #ff0000;">"="</span> . <span style="color: #0000ff;">$ENV</span><span style="color: #66cc66;">&#123;</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">&#125;</span> . <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/open.html"><span style="color: #000066;">open</span></a><span style="color: #66cc66;">&#40;</span>SENDMAIL, <span style="color: #ff0000;">"|/usr/sbin/sendmail -t"</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://www.perldoc.com/perl5.6/pod/func/die.html"><span style="color: #000066;">die</span></a> <span style="color: #ff0000;">"Cannot open sendmail: $!"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span style="color: #000066;">print</span></a> SENDMAIL <span style="color: #ff0000;">"Reply-To: foo<span style="color: #000099; font-weight: bold;">\@</span>bar<span style="color: #000099; font-weight: bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span style="color: #000066;">print</span></a> SENDMAIL <span style="color: #ff0000;">"Subject: [lobstertechnology.com] mod_evasive alert $IP<span style="color: #000099; font-weight: bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span style="color: #000066;">print</span></a> SENDMAIL <span style="color: #ff0000;">"To: "</span> . <span style="color: #0000ff;">$ENV</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'SERVER_ADMIN'</span><span style="color: #66cc66;">&#125;</span> . <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span style="color: #000066;">print</span></a> SENDMAIL <span style="color: #ff0000;">"Cc: foo<span style="color: #000099; font-weight: bold;">\@</span>bar<span style="color: #000099; font-weight: bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span style="color: #000066;">print</span></a> SENDMAIL <span style="color: #ff0000;">"Content-type: text/plain<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/print.html"><span style="color: #000066;">print</span></a> SENDMAIL <span style="color: #0000ff;">$MSG</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.perldoc.com/perl5.6/pod/func/close.html"><span style="color: #000066;">close</span></a><span style="color: #66cc66;">&#40;</span>SENDMAIL<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now configure mod_evasive to execute your script when it is triggered, add the following to your $APACHE_ROOT/conf/httpd.conf:</p>
<div class="igBar"><span id="lcode-3"><a href="#" onclick="javascript:showPlainTxt('code-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-3">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;ifmodule mod_evasive.<span style="">c</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; DOSSystemCommand&nbsp; &nbsp; <span style="color:#CC0000;">"/usr/local/scripts/mod_evasive_alert.pl"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/ifmodule&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now restart Apache:</p>
<p><code>[foo@bar mod_evasive]$ <strong>$APACHE_ROOT/bin/apachectl restart</strong><br />
/usr/local/apache/bin/apachectl restart: httpd restarted</code></p>
<p>Tada! You're done. Use the 'test.pl' script provided by mod_evasive to trigger a blocking of your IP and see the email generated.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lobstertechnology.com/2006/03/29/patch-to-mod_evasive-to-enhance-reporting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
