<?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>Thu, 01 Mar 2012 19:46:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>httpBL Middleware for 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 &#8230; <a href="http://iamtgc.com/2008/02/11/httpbl-middleware-for-django/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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;.</p>
<pre class="brush:plain;">
HTTPBLKEY = 'opqrstuvwxyz'
</pre>
<p>Next you&#8217;ll need to include your httpbl middleware in <strong>MIDDLEWARE_CLASSES</strong></p>
<pre class="brush:plain;">
MIDDLEWARE_CLASSES = (
    ....
    'projectname.middleware.httpbl.HttpBLMiddleware',
    ....
)
</pre>
<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></p>
<pre class="brush:py;">
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]) <= self.age and int(resultlist[2]) >= self.threat and int(resultlist[3]) &#038; self.classification > 0):
            if settings.HTTPBLREDIRECT:
               return HttpResponsePermanentRedirect(settings.HTTPBLREDIRECT)
            else:
               return HttpResponseNotFound('
<h1>Not Found</h1>

')

      return None
</pre>
<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>.</p>
<pre class="brush:shell;">
# 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 &#038; Harvester &#038; Comment Spammer
HTTPBLCLASS = 7
</pre>
<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>.</p>
<pre class="brush:shell;">
# 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'
</pre>
]]></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>

