<?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>iamtgc &#187; Announcements</title>
	<atom:link href="http://iamtgc.com/category/announcements/feed/" rel="self" type="application/rss+xml" />
	<link>http://iamtgc.com</link>
	<description></description>
	<lastBuildDate>Wed, 21 Jul 2010 12:13:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>httpBL Middleware for&#160;Django</title>
		<link>http://iamtgc.com/2008/02/11/httpbl-middleware-for-django/</link>
		<comments>http://iamtgc.com/2008/02/11/httpbl-middleware-for-django/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 17:36:54 +0000</pubDate>
		<dc:creator>tgc</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://iamtgc.com/2008/02/11/httpbl-middleware-for-django/</guid>
		<description><![CDATA[Inspired by Project Honeypot and the http:BL WordPress Plugin, I decided to write a similar plugin for Django. The Http:BL API is well documented here and there are implementations for Joomla, Drupal and numerous others. As my framework of choice is Django, we&#8217;ll focus on that. To beign, you&#8217;ll need to request your http:BL API [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by <a href="http://www.projecthoneypot.org/">Project Honeypot</a> and the <a href="http://wordpress.org/extend/plugins/httpbl/">http:BL WordPress Plugin</a>, I decided to write a similar plugin for Django.  The Http:BL API is well documented <a href="http://www.projecthoneypot.org/httpbl_api">here</a> and there are implementations for <a href="http://code.google.com/p/joomla-httpbl/">Joomla</a>, <a href="http://drupal.org/project/httpbl">Drupal</a> and <a href="http://www.projecthoneypot.org/httpbl_implementations.php">numerous others.</a></p>
<p>As my framework of choice is Django, we&#8217;ll focus on that.<br />
<span id="more-11"></span><br />
To beign, you&#8217;ll need to request your http:BL API Access Key <a href="http://www.projecthoneypot.org/httpbl_configure.php">here</a>.  It is also suggested, but not required, that you <a href="http://www.projecthoneypot.org/manage_quicklink.php">configure your QuickLink</a> (more on this below).</p>
<p>Now, in your <strong>settings.py</strong> you&#8217;ll want to add your http:BL API Key &#8216;HTTPBLKEY&#8217;.<br />
<code>HTTPBLKEY = 'opqrstuvwxyz'</code></p>
<p>Next you&#8217;ll need to include your httpbl middleware in <strong>MIDDLEWARE_CLASSES</strong><br />
<code>MIDDLEWARE_CLASSES = (
    ....
    'projectname.middleware.httpbl.HttpBLMiddleware',
    ....
)</code></p>
<p>You&#8217;ll want to place the middleware file, named <strong>httpbl.py</strong> in your <strong>project/middleware</strong> directory. If this does not exist, you will need to create it and place an empty __init__.py file in it.</p>
<p><strong>project/middleware/httpbl.py</strong><a href="http://django-httpbl-middleware.googlecode.com/files/httpbl.py"><i>(download)</i></a><br />
<code>from django.conf import settings
from django.http import HttpResponseNotFound, HttpResponsePermanentRedirect
import socket

class HttpBLMiddleware(object):
   """
   "HttpBL" Middleware by iamtgc@gmail.com
   """
   def __init__(self, age=None, threat=None, classification=None):
      if age is None:
         self.age = getattr(settings, 'HTTPBLAGE', 14)
      else:
         self.age = age
      if threat is None:
         self.threat = getattr(settings, 'HTTPBLTHREAT', 30)
      else:
         self.threat = threat
      if classification is None:
         self.classification = getattr(settings, 'HTTPBLCLASS', 7)
      else:
         self.classification = classification

   def process_request(self, request):

      if settings.HTTPBLKEY:
         ip = request.META.get('REMOTE_ADDR')
         iplist = ip.split('.')
         iplist.reverse()

         domain = 'dnsbl.httpbl.org'

         query = settings.HTTPBLKEY + "." + ".".join(iplist) + "." + domain

         try:
            result = socket.gethostbyname(query)
         except socket.gaierror:
            return None

         resultlist = result.split('.')

         if (int(resultlist[1]) &lt;= self.age and int(resultlist[2]) &gt;= self.threat and int(resultlist[3]) &amp; self.classification &gt; 0):
            if settings.HTTPBLREDIRECT:
               return HttpResponsePermanentRedirect(settings.HTTPBLREDIRECT)
            else:
               return HttpResponseNotFound('&lt;h1&gt;Not Found&lt;/h1&gt;')

      return None</code></p>
<p>This should be all you need to be on your way and protecting your Django site from suspicious hosts, email harvesters, and comment spammers.  But who am I to tell you what your settings should be?  Here are the additional settings you can define in <strong>settings.py</strong>.  The octets that these variables correspond with are fully documented in <a href="http://www.projecthoneypot.org/httpbl_api.php">Http:BL API Specification &#8211; Query Responses</a>.<br />
<code># HTTBLAGE - represents the number of days since activity was seen on the Honey Pot network.  Defaults to 14
HTTPBLAGE = 14
# HTTPBLTHREAT = threat score assigned by Project Honey Pot, higher number is more of a threat.  Defaults to 30
HTTPBLTHREAT = 30
# HTTPBLCLASS = bitset category, see API doc for more details.  Defaults to 7 = Suspicious &amp; Harvester &amp; Comment Spammer
HTTPBLCLASS = 7</code></p>
<p>As mentioned above, it is suggested that you configure a QuickLink. If configured, you should set HTTPBLREDIRECT to your QuickLink URL to redirect any &#8220;bad&#8221; traffic away from your site and into a honeypot.  Again, this would be defined in <strong>settings.py</strong>.<br />
<code># HTTPBLREDIRECT = QuickLink Honey Pot URL that we direct the bad traffic to.  Default = "Not Found" response, no redirection.
HTTPBLREDIRECT = 'http://some.honeypot.url/goes/here'</code></p>
]]></content:encoded>
			<wfw:commentRss>http://iamtgc.com/2008/02/11/httpbl-middleware-for-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
