<?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>Made of Everything You&#039;re Not &#187; bad behavior</title>
	<atom:link href="http://blog.ericlamb.net/tag/bad-behavior/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ericlamb.net</link>
	<description>Thoughts on programming, people and life</description>
	<lastBuildDate>Thu, 27 Oct 2011 01:29:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Advanced Bad Behavior</title>
		<link>http://blog.ericlamb.net/2009/05/advanced-bad-behavior/</link>
		<comments>http://blog.ericlamb.net/2009/05/advanced-bad-behavior/#comments</comments>
		<pubDate>Mon, 18 May 2009 13:43:01 +0000</pubDate>
		<dc:creator>Eric Lamb</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[bad behavior]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spam]]></category>

		<guid isPermaLink="false">http://blog.ericlamb.net/?p=1668</guid>
		<description><![CDATA[I really wanted to move away from Bad Behavior; there&#8217;s only so much I&#8217;m interested in this topic. But the first two posts didn&#8217;t cover everything I wanted to talk about so I wasn&#8217;t left with that warm fuzzy feeling of completeness.  If, like me, you&#8217;re over the whole Bad Behavior series I&#8217;m really sorry. This [...]]]></description>
			<content:encoded><![CDATA[<p>I really wanted to move away from <a title="Bad Behavior" href="http://www.bad-behavior.ioerror.us/" onclick="return TrackClick('http%3A%2F%2Fwww.bad-behavior.ioerror.us%2F','Bad+Behavior')" target="_blank">Bad Behavior</a>; there&#8217;s only so much I&#8217;m interested in this topic. But the <a title="The Bad Behavior Spam Blocker Part 1" href="http://blog.ericlamb.net/2009/05/the-bad-behavior-spam-blocker-part-1/" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2F2009%2F05%2Fthe-bad-behavior-spam-blocker-part-1%2F','The+Bad+Behavior+Spam+Blocker+Part+1')" target="_blank">first</a> <a title="More Bad Behavior" href="http://blog.ericlamb.net/2009/05/more-bad-behavior/" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2F2009%2F05%2Fmore-bad-behavior%2F','More+Bad+Behavior')">two</a> posts didn&#8217;t cover everything I wanted to talk about so I wasn&#8217;t left with that warm fuzzy feeling of completeness.  If, like me, you&#8217;re over the whole Bad Behavior series I&#8217;m really sorry.</p>
<div id="attachment_1669" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.ericlamb.net/wp-content/uploads/2009/05/more_bad_behavior_again.jpg" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F05%2Fmore_bad_behavior_again.jpg','More+Bad+Behavior%3B+Again')"><img class="size-medium wp-image-1669" title="More Bad Behavior; Again" src="http://blog.ericlamb.net/wp-content/uploads/2009/05/more_bad_behavior_again-300x261.jpg" alt="More Bad Behavior; Again" width="300" height="261" /></a><p class="wp-caption-text">More Bad Behavior; Again</p></div>
<p>This time I&#8217;m going to go over the last little tid-bits so you can protect your sites and make sure the &#8220;bad&#8221; people stay away. For the <a title="Not Perfect Comment" href="http://blog.ericlamb.net/2009/05/the-bad-behavior-spam-blocker-part-1/comment-page-1/#comment-946" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2F2009%2F05%2Fthe-bad-behavior-spam-blocker-part-1%2Fcomment-page-1%2F%23comment-946','Not+Perfect+Comment')" target="_blank">most part</a> anyway.</p>
<h3>White Listing</h3>
<p>You can white list IP addresses and user-agents. IP addresses can be white listed using ranges (in the CIDR format) or single IP by editing the file &#8216;whitelist.inc.php&#8217;. Open it up and edit the  below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Includes four examples of whitelisting by IP address and netblock.</span>
<span style="color: #000088;">$bb2_whitelist_ip_ranges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span>
	<span style="color: #0000ff;">&quot;64.191.203.34&quot;</span><span style="color: #339933;">,</span>	<span style="color: #666666; font-style: italic;">// Digg whitelisted as of 2.0.12</span>
	<span style="color: #0000ff;">&quot;208.67.217.130&quot;</span><span style="color: #339933;">,</span>	<span style="color: #666666; font-style: italic;">// Digg whitelisted as of 2.0.12</span>
	<span style="color: #0000ff;">&quot;10.0.0.0/8&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">&quot;172.16.0.0/12&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">&quot;192.168.0.0/16&quot;</span><span style="color: #339933;">,</span>
<span style="color: #666666; font-style: italic;">//	&quot;127.0.0.1&quot;,</span>
<span style="color: #000;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>User-agents can be white listed in the same file but require an exact match to what you add to the array.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>37
38
39
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bb2_whitelist_user_agents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span>
<span style="color: #666666; font-style: italic;">//	&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) It's me, let me in&quot;,</span>
<span style="color: #000;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It&#8217;s important to use white listing conservatively. Extremely so. You don&#8217;t want to use white listing unless you positively, absolutely, have no other option.</p>
<h3>Black Listing</h3>
<p>There are two different ways to use black listing in Bad Behavior; manually adding entries to your black lists and using the http:BL feature. </p>
<p>To use Bad Behavior&#8217;s http:BL features you must have an <a href="http://www.projecthoneypot.org/httpbl_configure.php?rf=24694" onclick="return TrackClick('http%3A%2F%2Fwww.projecthoneypot.org%2Fhttpbl_configure.php%3Frf%3D24694','http%3ABL+Access+Key')" title="http:BL Access Key" target="_blank">http:BL Access Key</a>. It&#8217;s a fairly simple process that requires registration with Project Honeypot. They&#8217;ll give you a BL Access Key and you place it in the settings array of &#8216;bad-behavior-generic.php&#8217;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bb2_settings_defaults</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span>
	<span style="color: #0000ff;">'log_table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'bb_logs'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'display_stats'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'strict'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'verbose'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'logging'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'httpbl_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'PLACE_YOUR_KEY_HERE'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'httpbl_threat'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'25'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'httpbl_maxage'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'30'</span><span style="color: #339933;">,</span>
<span style="color: #000;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once that&#8217;s done your install of Bad Behavior will use your local black lists as well as the http:BL lists.</p>
<p>On the other hand, you may encounter some rare cases where your site is being spammed by a new agent. In this case you might want to manually add entries to your local black lists.</p>
<p>The black lists are placed within &#8216;blacklist.inc.php&#8217;. It only accepts user-agents, probably because IP address blocking is essentially useless. You&#8217;ll need to edit 3 different areas of the script:</p>
<p>The first is for strings that occur at the beginning of the user-agent.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bb2_spambots_0</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span>
	<span style="color: #0000ff;">&quot;&lt;sc&quot;</span><span style="color: #339933;">,</span>			<span style="color: #666666; font-style: italic;">// XSS exploit attempts</span>
	<span style="color: #0000ff;">&quot;8484 Boston Project&quot;</span><span style="color: #339933;">,</span>	<span style="color: #666666; font-style: italic;">// video poker/porn spam</span>
	<span style="color: #0000ff;">&quot;adwords&quot;</span><span style="color: #339933;">,</span>		<span style="color: #666666; font-style: italic;">// referrer spam</span>
	<span style="color: #0000ff;">&quot;autoemailspider&quot;</span><span style="color: #339933;">,</span>	<span style="color: #666666; font-style: italic;">// spam harvester</span>
	<span style="color: #666666; font-style: italic;">//etc...</span></pre></td></tr></table></div>

<p>The next is for strings that occur anywhere within the user-agent string.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>57
58
59
60
61
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bb2_spambots</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span>
	<span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">,</span>			<span style="color: #666666; font-style: italic;">// A really dumb bot</span>
	<span style="color: #0000ff;">&quot;; Widows &quot;</span><span style="color: #339933;">,</span>		<span style="color: #666666; font-style: italic;">// misc comment/email spam</span>
	<span style="color: #0000ff;">&quot;a href=&quot;</span><span style="color: #339933;">,</span>		<span style="color: #666666; font-style: italic;">// referrer spam</span>
	<span style="color: #666666; font-style: italic;">//etc...</span></pre></td></tr></table></div>

<p>And, best of all, there&#8217;s also a regular expression (regex) array for the really difficult user-agents.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>87
88
89
90
91
92
93
94
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// These are regular expression matches.</span>
<span style="color: #000088;">$bb2_spambots_regex</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #000;">&#40;</span>
	<span style="color: #0000ff;">&quot;/^[A-Z]{10}$/&quot;</span><span style="color: #339933;">,</span>	<span style="color: #666666; font-style: italic;">// misc email spam</span>
	<span style="color: #0000ff;">&quot;/^Mozilla...[05]$/i&quot;</span><span style="color: #339933;">,</span>	<span style="color: #666666; font-style: italic;">// fake user agent/email spam</span>
	<span style="color: #0000ff;">&quot;/[bcdfghjklmnpqrstvwxz ]{8,}/&quot;</span><span style="color: #339933;">,</span>
<span style="color: #666666; font-style: italic;">//		&quot;/(;\){1,2}$/&quot;,		// misc spammers/harvesters</span>
<span style="color: #666666; font-style: italic;">//		&quot;/MSIE.*Windows XP/&quot;,	// misc comment spam</span>
<span style="color: #000;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It&#8217;s also possible to add your own blacklists into your Bad Behavior install. This is pretty helpful if you have multiple installs and are sane enough to recognize the absurdity in maintaining multiple lists. Just build a blacklist service and add the info to &#8216;blackhole.inc.php&#8217;.</p>
<p>Yeah, just build a blacklist server. Ummm&#8230; it&#8217;s easy?</p>
<h3>Customizing the Template</h3>
<p>Every time a request gets blocked the system doesn&#8217;t <em>really</em> know, 100%, that the request is bad; it just can&#8217;t. So, instead of just dying, Bad Behavior displays a page with instructions on how to &#8220;unblock&#8221; yourself usually by just clicking on a link.</p>
<p>Unfortunately, the page looks like ass.</p>
<div id="attachment_1682" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.ericlamb.net/wp-content/uploads/2009/05/bb_blocked_screenshot.jpg" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F05%2Fbb_blocked_screenshot.jpg','Bad+Behavior+Blocked+Screenshot')" title="Bad Behavior Blocked Screenshot" target="_blank"><img src="http://blog.ericlamb.net/wp-content/uploads/2009/05/bb_blocked_screenshot-300x128.jpg" alt="Bad Behavior Blocked Screenshot" title="Bad Behavior Blocked Screenshot" width="300" height="128" class="size-medium wp-image-1682" /></a><p class="wp-caption-text">Bad Behavior Blocked Screenshot</p></div>
<p>The idea, I&#8217;m sure, is that the page should have as small a footprint on the server as possible. One of the selling points is to lower bandwidth by blocking spam requests. No images, CSS or pretty allowed at all. </p>
<p>The flip side of this argument is that legitimate users who get this page will have an experience that&#8217;s lacking in, ahem, quality.</p>
<p>You can change the look of the page by editing &#8216;banned.inc.php&#8217;. It should be pretty self explanatory once you open the file what needs to be done. </p>
<p>If you want, you can also change the response messages. Those are stored in &#8216;responses.inc.php&#8217;.</p>
<p>Well, that about does it; there&#8217;s more to Bad Behavior but this series pretty well covered all the good parts. </p>
<p>This <em>will</em> be my last post on Bad Behavior; I swear.</p>
<div><a class="addthis_button" href="http://blog.ericlamb.net//addthis.com/bookmark.php?v=250" addthis:url='http://blog.ericlamb.net/2009/05/advanced-bad-behavior/' addthis:title='Advanced Bad Behavior '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.ericlamb.net/2009/05/advanced-bad-behavior/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More Bad Behavior</title>
		<link>http://blog.ericlamb.net/2009/05/more-bad-behavior/</link>
		<comments>http://blog.ericlamb.net/2009/05/more-bad-behavior/#comments</comments>
		<pubDate>Fri, 15 May 2009 13:24:27 +0000</pubDate>
		<dc:creator>Eric Lamb</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[bad behavior]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spam]]></category>

		<guid isPermaLink="false">http://blog.ericlamb.net/?p=1613</guid>
		<description><![CDATA[In the first post in this &#8220;series&#8221; (wtf? when did I start doing &#8220;series&#8221;? Oh, right&#8230;) I went over the basics of what Bad Behavior is and how to get it installed. Bad Behavior&#8217;s advanced setup required some investigation and forethought in order to work out it so it was best to break the post [...]]]></description>
			<content:encoded><![CDATA[<p>In the first post in this &#8220;<a title="The Bad Behavior Spam Blocker Part 1" href="http://blog.ericlamb.net/2009/05/the-bad-behavior-spam-blocker-part-1/" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2F2009%2F05%2Fthe-bad-behavior-spam-blocker-part-1%2F','The+Bad+Behavior+Spam+Blocker+Part+1')">series</a>&#8221; (wtf? when did I start doing &#8220;series&#8221;? Oh, <a title="Arc90 Twitter API Service Part 1" href="http://blog.ericlamb.net/2009/04/arc90-twitter-api-service-part-1/" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2F2009%2F04%2Farc90-twitter-api-service-part-1%2F','Arc90+Twitter+API+Service+Part+1')">right&#8230;</a>) I went over the basics of what <a title="Bad Behavior" href="http://www.bad-behavior.ioerror.us/" onclick="return TrackClick('http%3A%2F%2Fwww.bad-behavior.ioerror.us%2F','Bad+Behavior')" target="_blank">Bad Behavior</a> is and how to get it installed. Bad Behavior&#8217;s advanced setup required some investigation and forethought in order to work out it so it was best to break the post up; so, you know, here you go.</p>
<div id="attachment_1614" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.ericlamb.net/wp-content/uploads/2009/05/more_bad_behavior.jpg" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F05%2Fmore_bad_behavior.jpg','More+Bad+Behavior')"><img class="size-medium wp-image-1614" title="More Bad Behavior" src="http://blog.ericlamb.net/wp-content/uploads/2009/05/more_bad_behavior-300x224.jpg" alt="More Bad Behavior" width="300" height="224" /></a><p class="wp-caption-text">More Bad Behavior</p></div>
<p>By default, if all you do is follow the instructions laid out in the first post you&#8217;ll have a working setup. For some of the cooler logging functionality you&#8217;re going to have to edit the included file &#8216;bad-behavior-generic.php&#8217;.</p>
<p>I&#8217;ve been feeling kind of down on Bad Behavior about this process for a few days now. It seemed kind of lame that they came up with such a great idea, wrote a really cool script but then killed the implementation. After working with the script for a couple days it&#8217;s starting to make <em>some</em> sense but not really enough to convince me it shouldn&#8217;t have been done.</p>
<p>To help others who&#8217;ve had this delima, I&#8217;ve compiled a list of steps to get Bad Behavior logging up and running.</p>
<h3>Install Bad Behavior</h3>
<p>The first thing you&#8217;re going to want to do is create a database and add the connection code to your version of &#8216;bad-behavior-generic.php&#8217;. </p>
<p>Here&#8217;s the SQL:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #22f;">CREATE</span> <span style="color: #22f;">TABLE</span> <span style="color: #22f;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> <span style="color: #008000;">`bb<span style="color: #008080; font-weight: bold;">_</span>logs`</span> <span style="color: #000;">&#40;</span>
  <span style="color: #008000;">`id`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #000;">&#40;</span><span style="color: #008080;">11</span><span style="color: #000;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">auto_increment</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`ip`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`date`</span> <span style="color: #999900; font-weight: bold;">datetime</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">default</span> <span style="color: #008000;">'0000-00-00 00:00:00'</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`request<span style="color: #008080; font-weight: bold;">_</span>method`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`request<span style="color: #008080; font-weight: bold;">_</span>uri`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`server<span style="color: #008080; font-weight: bold;">_</span>protocol`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`http<span style="color: #008080; font-weight: bold;">_</span>headers`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`user<span style="color: #008080; font-weight: bold;">_</span>agent`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`request<span style="color: #008080; font-weight: bold;">_</span>entity`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`key`</span> <span style="color: #999900; font-weight: bold;">text</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #22f;">PRIMARY KEY</span>  <span style="color: #000;">&#40;</span><span style="color: #008000;">`id`</span><span style="color: #000;">&#41;</span><span style="color: #000033;">,</span>
  <span style="color: #22f;">KEY</span> <span style="color: #008000;">`ip`</span> <span style="color: #000;">&#40;</span><span style="color: #008000;">`ip`</span><span style="color: #000;">&#40;</span><span style="color: #008080;">15</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #000033;">,</span>
  <span style="color: #22f;">KEY</span> <span style="color: #008000;">`user<span style="color: #008080; font-weight: bold;">_</span>agent`</span> <span style="color: #000;">&#40;</span><span style="color: #008000;">`user<span style="color: #008080; font-weight: bold;">_</span>agent`</span><span style="color: #000;">&#40;</span><span style="color: #008080;">10</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span>
<span style="color: #000;">&#41;</span></pre></td></tr></table></div>

<p>For this simple demo I&#8217;m going to use the native php functions but it&#8217;s more than likely you&#8217;ll have a database class. I put the below directly past the comments above any function declaration:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$link</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
   <span style="color: #990000;">die</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Not connected : '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// make foo the current db</span>
<span style="color: #000088;">$db_selected</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_select_db</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'bad_behavior'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$link</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$db_selected</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
   <span style="color: #990000;">die</span> <span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Can\'t use foo : '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span></pre></td></tr></table></div>

<p>Next, you need to populate the skeletal functions with their appropriate code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Return current time in the format preferred by your database.</span>
<span style="color: #000000; font-weight: bold;">function</span> bb2_db_date<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #22f;">return</span> <span style="color: #990000;">gmdate</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">'Y-m-d H:i:s'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// Example is MySQL format</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Return affected rows from most recent query.</span>
<span style="color: #000000; font-weight: bold;">function</span> bb2_db_affected_rows<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #22f;">return</span> <span style="color: #990000;">mysql_affected_rows</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Escape a string for database usage</span>
<span style="color: #000000; font-weight: bold;">function</span> bb2_db_escape<span style="color: #000;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #22f;">return</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Return the number of rows in a particular query.</span>
<span style="color: #000000; font-weight: bold;">function</span> bb2_db_num_rows<span style="color: #000;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #22f;">if</span> <span style="color: #000;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #000;">&#41;</span>
		<span style="color: #22f;">return</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #22f;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Run a query and return the results, if any.</span>
<span style="color: #666666; font-style: italic;">// Should return FALSE if an error occurred.</span>
<span style="color: #666666; font-style: italic;">// Bad Behavior will use the return value here in other callbacks.</span>
<span style="color: #000000; font-weight: bold;">function</span> bb2_db_query<span style="color: #000;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #22f;">return</span> <span style="color: #990000;">mysql_query</span><span style="color: #000;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Return all rows in a particular query.</span>
<span style="color: #666666; font-style: italic;">// Should contain an array of all rows generated by calling mysql_fetch_assoc()</span>
<span style="color: #666666; font-style: italic;">// or equivalent and appending the result of each call to an array.</span>
<span style="color: #000000; font-weight: bold;">function</span> bb2_db_rows<span style="color: #000;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #22f;">return</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Return emergency contact email address.</span>
<span style="color: #000000; font-weight: bold;">function</span> bb2_email<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// return &quot;example@example.com&quot;;	// You need to change this.</span>
	<span style="color: #22f;">return</span> <span style="color: #0000ff;">&quot;badbots@ioerror.us&quot;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// You need to change this.</span>
<span style="color: #000;">&#125;</span></pre></td></tr></table></div>

<p>Then you&#8217;ll also need to place a call to close the database connection at the bottom of the script. Place the below on the very last line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>160
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_close</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Doing that will make sure you don&#8217;t have any rogue connections eating up your queue. </p>
<p>Once the above is complete you should have a fully setup and working install of Bad Behavior. </p>
<p>In case anyone has any issues with the above I&#8217;ve prepared a stand alone version of the script anyone can <a href="http://blog.ericlamb.net/wp-content/uploads/2009/05/bad-behavior-generic.zip" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F05%2Fbad-behavior-generic.zip','download+bad-behavior-generic')" title="download bad-behavior-generic">download bad-behavior-generic</a>.</p>
<p>It should also be noted that incorporating a database into your Bad Bahavior installation ups the load on every request (which may be why it&#8217;s not in there by default). You just have to choose whether the need for logging out weighs the increased load. For me, it did.</p>
<div><a class="addthis_button" href="http://blog.ericlamb.net//addthis.com/bookmark.php?v=250" addthis:url='http://blog.ericlamb.net/2009/05/more-bad-behavior/' addthis:title='More Bad Behavior '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.ericlamb.net/2009/05/more-bad-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Bad Behavior Spam Blocker Part 1</title>
		<link>http://blog.ericlamb.net/2009/05/the-bad-behavior-spam-blocker-part-1/</link>
		<comments>http://blog.ericlamb.net/2009/05/the-bad-behavior-spam-blocker-part-1/#comments</comments>
		<pubDate>Fri, 08 May 2009 13:38:54 +0000</pubDate>
		<dc:creator>Eric Lamb</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[bad behavior]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spam]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.ericlamb.net/?p=1504</guid>
		<description><![CDATA[Anyone with a blog has seen comment spam. This is the stuff that shows up talking about Viagra, written in Russia and are usually stuffed with links. There are a couple tactics for combating this sort of thing; some sites require registration to comment, some people manually delete the stuff and some sites use technology [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone with a blog has seen comment spam. This is the stuff that shows up talking about Viagra, written in Russia and are usually stuffed with links. </p>
<p>There are a couple tactics for combating this sort of thing; some sites require registration to comment, some people manually delete the stuff and some sites use technology to help. </p>
<div id="attachment_1591" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.ericlamb.net/wp-content/uploads/2009/04/bad_behavior.jpg" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2Fwp-content%2Fuploads%2F2009%2F04%2Fbad_behavior.jpg','Bad+Behavior')"><img src="http://blog.ericlamb.net/wp-content/uploads/2009/04/bad_behavior-300x199.jpg" alt="Bad Behavior" title="Bad Behavior" width="300" height="199" class="size-medium wp-image-1591" /></a><p class="wp-caption-text">Bad Behavior</p></div>
<p>What to do? What to do&#8230;?</p>
<p>Well, I don&#8217;t think it&#8217;s a good idea to add barriers in front of users participating in a discussion, so registration is out. I&#8217;m pretty lazy and don&#8217;t want to manually delete comment spam so moderation isn&#8217;t going to work. I am a programmer though so I have an innate confidence in technology to deal with this (mostly anyway). To that end I like to use 2 different services to deal with comment spam; Akismet, which I&#8217;m not going to talk about now, and <a href="http://www.bad-behavior.ioerror.us/" onclick="return TrackClick('http%3A%2F%2Fwww.bad-behavior.ioerror.us%2F','Bad+Behavior')" title="Bad Behavior" target="_blank">Bad Behavior</a>. </p>
<p>According to the official site:</p>
<blockquote><p>
Bad Behavior complements other link spam solutions by acting as a gatekeeper, preventing spammers from ever delivering their junk, and in many cases, from ever reading your site in the first place. This keeps your site’s load down, makes your site logs cleaner, and can help prevent denial of service conditions caused by spammers.
</p></blockquote>
<p>Thankfully, there are already WordPress plugins for both Akismet and Bad Behavior, so my blog is pretty well protected, but I also work on custom programs and need to protect them too. This got me thinking about how to to get Bad Behavior up and running on your systems; which is why you&#8217;re here I&#8217;m sure.</p>
<p>Like most things php, installing Bad Behavior is pretty easy. To install just <a href="http://www.bad-behavior.ioerror.us/download/" onclick="return TrackClick('http%3A%2F%2Fwww.bad-behavior.ioerror.us%2Fdownload%2F','Download+Bad+Behavior')" title="Download Bad Behavior" target="_blank">download the files</a>, unzip and place the files somewhere in your applications include path. Then just include the below preferably in a file included in all your pages after you upload the files. Using the below will only protect your site</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$path_to_bb</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/path/to/'</span><span style="color: #339933;">;</span>
<span style="color: #22f;">require_once</span><span style="color: #000;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$path_to_bb</span>/bad-behavior-generic.php&quot;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The above is nice and all; your site&#8217;s pretty well protected from there but it would be nice to know what was happening behind the scenes. Just how many spam attempts are being blocked?</p>
<p>Bad Behavior does include a logging system but, oddly, at the time of this writing using 2.0.26, they don&#8217;t include any sort of install script. The instructions state:</p>
<blockquote><p>
If you just can’t live without logging, you will need to provide a database connection. Bad Behavior uses callbacks whenever it needs to run a database query; in order to provide this functionality, you will need to provide the appropriate hooks into your PHP-based software’s database and add them into the bad-behavior-generic.php file. The code has stub functions which show what is needed, and you can use the bad-behavior-wordpress.php file as an example to work from, though your implementation will necessarily be different.
</p></blockquote>
<p>I&#8217;ll go into detail in the next post; I&#8217;m still <a href="http://blog.ericlamb.net/2009/05/yeah-yeah-yeah-ive-been-sick/" onclick="return TrackClick('http%3A%2F%2Fblog.ericlamb.net%2F2009%2F05%2Fyeah-yeah-yeah-ive-been-sick%2F','Yeah%2C+Yeah%2C+Yeah%3B+I%E2%80%99ve+been+Sick%E2%80%A6')" title="Yeah, Yeah, Yeah; I’ve been Sick…">sick</a> so I can&#8217;t write anymore. </p>
<div><a class="addthis_button" href="http://blog.ericlamb.net//addthis.com/bookmark.php?v=250" addthis:url='http://blog.ericlamb.net/2009/05/the-bad-behavior-spam-blocker-part-1/' addthis:title='The Bad Behavior Spam Blocker Part 1 '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.ericlamb.net/2009/05/the-bad-behavior-spam-blocker-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

