<?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>Looking Out To Sea &#187; Security</title>
	<atom:link href="http://www.dougalstanton.net/blog/index.php/category/society/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dougalstanton.net/blog</link>
	<description></description>
	<lastBuildDate>Sat, 28 Jan 2012 17:49:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Part 2 of Cyber Security Challenge: Hexadecimal gibberish</title>
		<link>http://www.dougalstanton.net/blog/index.php/2010/08/02/part-2-of-cyber-security-challenge-hexadecimal-gibberish/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2010/08/02/part-2-of-cyber-security-challenge-hexadecimal-gibberish/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 22:27:40 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Maths & Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1136</guid>
		<description><![CDATA[This second part of the problem was more difficult, though the solution is much shorter to present. The input message looks like this, in part:


68edcdec4e2c8eae8d2c8e2dedcd6e04d2042fedae52ceac
04ccedaecd8c042ccd8c046cedad0e8dac8eac8c048e0dac


Under the assumption that each pair of characters represents a byte, we split the stream into pairs.


splitStream = splitEvery 2


Of course each pair isn&#8217;t actually a hexadecimal number, though it looks [...]]]></description>
			<content:encoded><![CDATA[<p>This second part of the problem was more difficult, though the solution is much shorter to present. The input message looks like this, in part:</p>

<p><div>
<pre>68edcdec4e2c8eae8d2c8e2dedcd6e04d2042fedae52ceac
04ccedaecd8c042ccd8c046cedad0e8dac8eac8c048e0dac</pre>
</div></p>

<p>Under the assumption that each pair of characters represents a byte, we split the stream into pairs.</p>

<p><div>
<pre class="haskell">splitStream = splitEvery <span style="color: red;">2</span></pre>
</div></p>

<p>Of course each pair isn&#8217;t actually a hexadecimal number, though it looks like one. It&#8217;s a string of characters, so we need to turn it into something the computer will recognise as a number.</p>

<p>Now, there&#8217;s a robust, I&#8217;m-a-serious-engineer way of doing this and then there&#8217;s the way I chose, which is to prefix all the strings with &#8220;0x&#8221; and use the <code>read</code> routine. The <code>Read</code> typeclass is <em>not</em> meant to represent a robust parsing mechanism but since this is a one-shot thing I think we&#8217;ll both just ignore it, yeah?</p>

<p><div>
<pre class="haskell">hexToInt :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
hexToInt = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:read"><span style="font-weight: bold;">read</span></a> . <span style="color: green;">&#40;</span><span style="background-color: #3cb371;">&quot;0x&quot;</span> ++<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">-- ouch!</span></pre>
</div></p>

<p>Having read in our number I tried to do what I did in the previous exercise and print it out, which produced complete nonsense. Then I decided that reversing the string before converting it to numbers would be helpful. I did that, but the result was still nonsense, just of a different shade.</p>

<p>Then I decided that my resulting numbers were a bit <em>large</em>. The number which represent capital A is 65, and I was getting numbers in the 200 range. Then I realised, &#8220;all these numbers are even, aren&#8217;t they?&#8221; So I divided the whole lot by two.</p>

<p>Outputting that message gave me a backwards English message. Aha! It&#8217;s close but there&#8217;s something wrong. I threw in another &#8220;reverse&#8221; statement and got an answer. At this point I noticed I had two reverse statements &#8212; one at the beginning and one at the end. This seemed like it would be the cause of my problems so I removed them both and, Robert&#8217;s your father&#8217;s brother, I was back to gibberish again.</p>

<p>What happened? What I had failed to realise was that when I had reverse my original message I had done so <em>before</em> splitting into pairs of characters. So ABCD becomes DCBA. After splitting this is [DC,BA]. If each pair is converted into a printable character, represented by f(XY), then the result is [f(DC),f(BA)]. I then reversed the message to get [f(BA),f(DC)].</p>

<p>If I didn&#8217;t do any reversing but went through the same splitting and conversion process, I got [f(AB),f(CD)]. Look &#8212; f(AB) is not the same as f(BA)! By reversing the list before splitting and again after conversion, I was implicitly reversing the order of the characters in each pair.</p>

<p>Obviously I had to reverse <em>only</em> the pairs after they&#8217;d been split, rather than the whole list at the beginning and end. If I do this then print the result I get an answer which is close to right.</p>

<p><div>
<pre class="haskell">byteToChar = chr . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip"><span style="font-weight: bold;">flip</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:div"><span style="font-weight: bold;">div</span></a> <span style="color: red;">2</span> . hexToInt . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>
message = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> byteToChar . splitStream
&nbsp;
loadfile = <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:lines"><span style="font-weight: bold;">lines</span></a><span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>` <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:readFile"><span style="font-weight: bold;">readFile</span></a> <span style="background-color: #3cb371;">&quot;hexstring.txt&quot;</span>
main = loadfile &gt;&gt;= <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> . message</pre>
</div></p>

<p>The resulting message is:</p>

<blockquote>
  <p>Congratulations  youve found and completed the REAL challenge. 
  Your win code is cyb3r=s3cur1ty*ch@ll3nge+26-07-2010.</p>
  
  <p>Please email this code to our team at media@cybersecuritychallenge.org.uk. If youre the first person to do so, and can prove you meet the eligibility criteria (British citizen currently resident in the UK) we will be in touch to advise how to claim your prize. Well done and good luck in the Cyber Security Challenge competitions taking place throughout the rest of the year.</p>
</blockquote>

<p>There are a few infelicities which are explained by the official solution. My divide-by-two technique is not the proper approach so I think some of the characters get lost in the decryption process. But as the saying goes, close enough for government work!</p>

<p>The solution states:</p>

<blockquote>
  <p>The challenge was based on a bitshift operation applied to a string, here each byte&#8217;s &#8220;3 least significant bits&#8221; have been added to the left side of the byte (making them the most significant bits respectively)</p>
</blockquote>

<p>Assuming we have bits one to eight:</p>

<p><div>
<pre>1 2 3 4 5 6 7 8</pre>
</div></p>

<p>what I did was divide everything by two, which we do in binary by shifting everything to one side so the least significant bit disappears. We fill the opposite edge with a zero here:</p>

<p><div>
<pre>0 1 2 3 4 5 6 7</pre>
</div></p>

<p>What the solution demands is to rotate the bits like so:</p>

<p><div>
<pre>6 7 8 1 2 3 4 5</pre>
</div></p>

<p>I am still not sure how it is that my solution matches so neatly with their own. Answers on a postcard.</p>

<p>If I hadn&#8217;t stumbled on a solution like this, the sensible approach might have been to produce a histogram of common characters. In this case, the most commonly used character encoded the space, followed closely by &#8220;e&#8221;, the most common letter in standard English texts. From there it would have been a bit of a slog to produce the result but it could be done.</p>

<p>The two other solutions I have found <a href="http://james.slaterspage.com/cyber-security-challenge-cipher-solution/">both use</a> <a href="http://blog.jimhi.com/2010/07/cyber-security-challenge.html">the official method</a> without problems. I suspect it&#8217;s got something to do with default word lengths (8 bit versus 32 bit) and signed integers, though I&#8217;ve not thought deeply on the issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2010/08/02/part-2-of-cyber-security-challenge-hexadecimal-gibberish/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Part 1 of Cyber Security Challenge: Decoding the image</title>
		<link>http://www.dougalstanton.net/blog/index.php/2010/07/31/part-1-of-cyber-security-challenge-decoding-the-image/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2010/07/31/part-1-of-cyber-security-challenge-decoding-the-image/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 22:37:17 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Maths & Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1126</guid>
		<description><![CDATA[The first part of the Cyber Security Challenge requires you to decode a long stream of letters, numbers and symbols into something coherent. This was by far the easiest part of the challenge. The symbols look like this:


/9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQMBAAUAAAABAAAASgMDAAEA
AAABAAAAAFEQAAEAAAABAQAAAFERAAQAAAABAAAOxFESAAQAAAABAAAOxAAAAAAAAYagAACxj//b
AEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAkLicgIiwjHBwoNyksMDE0NDQf


This is only the first three lines &#8212; there&#8217;s 362 lines of this. The most important bit is [...]]]></description>
			<content:encoded><![CDATA[<p>The first part of the <a href="https://cybersecuritychallenge.org.uk/cipher.html" title="Teaser challenge">Cyber Security Challenge</a> requires you to decode a long stream of letters, numbers and symbols into something coherent. This was by far the easiest part of the challenge. The symbols look like this:</p>

<p><div>
<pre>/9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQMBAAUAAAABAAAASgMDAAEA
AAABAAAAAFEQAAEAAAABAQAAAFERAAQAAAABAAAOxFESAAQAAAABAAAOxAAAAAAAAYagAACxj//b
AEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAkLicgIiwjHBwoNyksMDE0NDQf</pre>
</div></p>

<p>This is only the first three lines &#8212; there&#8217;s 362 lines of this. The most important bit is that the last line ends in a <code>=</code> sign. This symbol is used for padding out messages when they are encoded in Base 64 format, so if any string has an equals symbols on the end it&#8217;s worth seeing what happens if you decode it as if it were Base64. (The truth of the matter is that by the time my mate Rich had told me about this challenge he&#8217;d already decoded the image so I didn&#8217;t do any of this stuff!)</p>

<p>Thankfully there are plenty of programs that will do the decoding for you, and the resulting file is a JPEG file. Brilliant:</p>

<p><img src="/code/cybersecurity/part1/decode.jpg" class="show" /></p>

<p>This is <a href="http://xkcd.com/538/" title="XKCD #538">XKCD comic number 538</a>, with a subtle difference. Round the outside of the image there is an uneven dotted border. This isn&#8217;t in the original comic, and it looks irregular which suggests that it&#8217;s not just a pretty pattern but that it encodes some further information.</p>

<p>For the next stage I converted the image to PNG format because it was the easiest format to load and process.</p>

<p>The first part we need to do is load the image file and extract all the bytes from the actual image part, ignoring any metadata. The PNG file is represents a 24 bit colour image, with each component (red, green and blue) stored as an 8 bit number from 0&#8211;255. We just return the whole thing as a long list of bytes, starting at the top left and scanning left to right, top to bottom. It&#8217;s not efficient but it&#8217;s very simple to reason about because we don&#8217;t need to think about array locations.</p>

<p><div>
<pre class="haskell">getimage :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#91;</span>Word8<span style="color: green;">&#93;</span>
getimage = <span style="color: #06c; font-weight: bold;">do</span>
  <span style="color: green;">&#40;</span>Right img<span style="color: green;">&#41;</span> &lt;- loadPNGFile <span style="background-color: #3cb371;">&quot;decode.png&quot;</span>
  getElems <span style="color: green;">&#40;</span>imageData img<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The long list may be conceptually simple but it&#8217;s not representative of the structure of the image. Since each pixel is represented by 3 consecutive numbers we want to gather those numbers together, so we can start dealing with them as pixels. Thus we convert [r,g,b,r,g,b,r,g &#8230;] into [[r,g,b],[r,g,b],&#8230;]</p>

<p><div>
<pre class="haskell">splitIntoBytes = splitEvery <span style="color: red;">3</span></pre>
</div></p>

<p>Even though each pixel can represent many colours we&#8217;re really only dealing with black and white here so we can represent each component as fully-on or fully-off. Each pixel can really be stored as boolean values like [True, False, False]. Since it&#8217;s possible that some values are not absolutely 0 or absolutely 255 I&#8217;ve divided them up the middle &#8212; anything darker than 128 is 0, and anything lighter is white.</p>

<p>Once each pixel is a list of booleans we can collapse that down into a single black/white value by taking the conjunction. If all values are True then the pixel is True (ie, black) otherwise False (white).</p>

<p><div>
<pre class="haskell">normalise = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:and"><span style="font-weight: bold;">and</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <span style="color: green;">&#40;</span>&lt; <span style="color: red;">128</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>At this stage our image is no longer a list of integers but a list of booleans. The original list had height-times-width-times-3 elements. Since we&#8217;ve collapsed all those three elements into a single value representing each pixel we now have height-times-width elements. The image is 350x175 pixels so we can split it into rows by cutting the list every 350 elements. This gives us 175 rows.</p>

<p><div>
<pre class="haskell">tomatrix = splitEvery <span style="color: red;">350</span> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> normalise . splitIntoBytes</pre>
</div></p>

<p>The next stage is, I think, the most beautiful aspect of representing the image as a list of lists. We have a full image but we only want the wavy lines of pixels which make up the border. How do we extract those pixels?</p>

<p>The first thing to notice is that the border is not continuous. The top and bottom edges go to the edge of the screen at both sides, but the two sides don&#8217;t meet at the top or bottom. To illustrate:</p>

<p><div>
<pre>#################
&nbsp;
#               #
#               #
#               #
&nbsp;
#################</pre>
</div></p>

<p>So the first question, &#8220;where do we start and end?&#8221; is answered for us. Each segment is discrete. It was my colleague Rich which pointed out the slight gaps in the border which makes this simplification possible. If the border were complete we&#8217;d have to determine whether the corner pieces were part of both the horizontal and the vertical (like a crossword where Across and Down share letters) or not.</p>

<p>The second question we ask is, &#8220;which direction does the sequence go in?&#8221;. Do we go clockwise from top left, following this alphabetical sequence?</p>

<p><div>
<pre>a b c d e
n       f
m       g
l k j i h</pre>
</div></p>

<p>Or do we maybe scan left-to-right, ignoring gaps?</p>

<p><div>
<pre>a b c d e
f       g
h       i
j k l m n</pre>
</div></p>

<p>What about anticlockwise? A mixture of the above? I took the approach which seemed obvious to me, clockwise from top left, and it turned out to be correct. But interestingly the final message is repeated in part so choosing the wrong start point would not have mattered, and it would have been obvious that the output was almost right.</p>

<p>Each row is a list, stored in order from top to bottom. This means the top border is just the first list:</p>

<p><div>
<pre class="haskell">topedge = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a></pre>
</div></p>

<p>The bottom border is the last list, but because we&#8217;ve chosen clockwise we need to reverse the list too.</p>

<p><div>
<pre class="haskell">bottomedge = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:last"><span style="font-weight: bold;">last</span></a></pre>
</div></p>

<p>The right edge is the last element of each list, ignoring the first 3 rows and the last three rows, because as mentioned above, the side patterns are shorter. These two look slightly complicated but it&#8217;s mostly dealing with trimming the top and bottom edges off.</p>

<p><div>
<pre class="haskell">rightedge = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop"><span style="font-weight: bold;">drop</span></a> <span style="color: red;">3</span>  . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:last"><span style="font-weight: bold;">last</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop"><span style="font-weight: bold;">drop</span></a> <span style="color: red;">3</span>
leftedge = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop"><span style="font-weight: bold;">drop</span></a> <span style="color: red;">3</span> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop"><span style="font-weight: bold;">drop</span></a> <span style="color: red;">3</span></pre>
</div></p>

<p>Now we can extract each edge of the border we can join them all together into our encoded message. This converts our list of lists, which is a complete picture, into a list of bits encoded in the border.</p>

<p><div>
<pre class="haskell">msgstream :: <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> -&gt; <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a><span style="color: green;">&#93;</span>
msgstream bits = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concatMap"><span style="font-weight: bold;">concatMap</span></a> <span style="color: green;">&#40;</span>$ bits<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span>topedge, rightedge, bottomedge, leftedge<span style="color: green;">&#93;</span></pre>
</div></p>

<p>Now we have a long list of bits. And it just so happens the number of bits we have is divisible by eight! This is a good sign because binary information is typically grouped into sets of eight. For the next stage we group our list into sets of 8 bits and turn each 8 into a single number.</p>

<p>One of the tricky aspects about any number is that you can&#8217;t tell in advance which end to start from. You and I know that 12 is &#8220;twelve&#8221; not &#8220;twenty-one&#8221; but that&#8217;s because we know to read numbers from left to right. Computer formats have used both in the past so I wasn&#8217;t sure which one would be important to me &#8212; is the biggest number the first digit or the last digit? The actual question is, &#8220;is the leading digit the <em>most</em> significant bit or the <em>least</em> significant bit?&#8221;. I calculated both to see what would happen, and it turned out that most-significant-bit was the way to go. Thankfully, MSB and LSB are just the reverse of each other, so by implementing one we get the other for free!</p>

<p><div>
<pre class="haskell">msb,lsb :: <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a><span style="color: green;">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
msb = lsb . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>
lsb = bitsToInt . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromIntegral"><span style="font-weight: bold;">fromIntegral</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromEnum"><span style="font-weight: bold;">fromEnum</span></a><span style="color: green;">&#41;</span></pre>
</div></p>

<p>We calculate least significant bit by converting all the True values to 1 and all the False values to zero, and then multiplying element-wise by a stream of powers of two. To illustrate:</p>

<p><div>
<pre>  1 2 4 8 16
* 0 1 1 0 1
= 0 2 4 0 16</pre>
</div></p>

<p>Then we just add that list up, so &#8220;0&#160;1 1&#160;0 1&#8221; is &#8220;0+2+4+0+16&#8221; or 22.</p>

<p><div>
<pre class="haskell">bitsToInt = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum"><span style="font-weight: bold;">sum</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zipWith"><span style="font-weight: bold;">zipWith</span></a> <span style="color: green;">&#40;</span>*<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:iterate"><span style="font-weight: bold;">iterate</span></a> <span style="color: green;">&#40;</span><span style="color: red;">2</span>*<span style="color: green;">&#41;</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>After all that prelude we can put this segment together, dividing the image up into its matrix, extracting the bits from the border of the image, splicing them up into sets of eight and converting each 8 bits into a single number:</p>

<p><div>
<pre class="haskell">msgbits = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> msb . splitEvery <span style="color: red;">8</span> . msgstream . tomatrix</pre>
</div></p>

<p>Now what do we do with our numbers? Readable characters are represented internally as numbers, so it is a simple thing to convert between number and printable characters. If we convert each number to a character we get nonsense, but consistent and tantalising nonsense:</p>

<blockquote>
  <p>Cyrnfr sbyybj guvf yvax:
  uggcf://plorefrphevglpunyyratr.bet.hx/834wgc.ugzy
  uggcf://plorefrphevglpunyyratr.bet.hx/834wgc.ugzy</p>
</blockquote>

<p>Look at those sequences &#8220;uggcf://&#8221; repeated twice. That looks so much like a web address, &#8220;https://&#8221;. If we assume that it <em>is</em> a web address how has it been altered? Each letter in a pair, h/u, t/g, c/p, is 13 characters apart from its partner. But the punctuation symbols aren&#8217;t any different.</p>

<p>This looks like the encoding called &#8220;rot13&#8221; where each character is shifted 13 characters along in the alphabet, and if we reach the end we wrap back to the start. Also, since the first letter of the nonsense message is a capital and none of the rest are it seems like case is being preserved by this encoding.</p>

<p>We convert back to sensible words by rot13 encoding again, since 13+13=26 so any two applications of this transformation will undo each other.</p>

<p>Uppercase and lowercase are treated separately but by the same process. We&#8217;re processing some character which we call <em>c</em> and we want to know how many characters it is away from the start of the alphabet. The letter &#8216;a&#8217; (or &#8216;A&#8217;) is 0 characters away from the start, &#8216;b&#8217; is 1 character and so on.</p>

<p>If we assume an infinite stream of letters &#8220;a b c &#8230; x y z a b c&#8230;&#8221; repeating the alphabet, then the Nth letter in that stream is the one which is N away from the start. The 0th letter is &#8216;a&#8217;, the first letter &#8216;b&#8217;. But if we chop the first 13 characters off this stream, so it&#8217;s &#8220;n o p &#8230; y z a b c &#8230;&#8221; then the 0th letter is &#8216;n&#8217;, the 1st letter &#8216;o&#8217; and so on. Each offset now directly maps one letter onto its partner letter. And because the sequences of letters is endless we don&#8217;t have to worry about falling off the end. The mapping just loops back on itself:</p>

<p><div>
<pre>0 1 2 3 4 5 6 ...
a b c d e f g ...
n o p q r s t ...</pre>
</div></p>

<p>Thus we can find the alternate character in our pair by checking first of all how many characters our current letter is from the start, and then looking for the equivalent character in the list which has had its head chopped off. We do the same for the upper case characters and just pass through unchanged anything which isn&#8217;t upper or lower case &#8212; all the punctuation and spaces.</p>

<p><div>
<pre class="haskell">rot13 :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span style="color: #cccc00; font-weight: bold;">Char</span></a> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span style="color: #cccc00; font-weight: bold;">Char</span></a>
rot13 c | isLower c = lowercase!!<span style="color: green;">&#40;</span>ord c - ord 'a'<span style="color: green;">&#41;</span>
        | isUpper c = uppercase!!<span style="color: green;">&#40;</span>ord c - ord 'A'<span style="color: green;">&#41;</span>
        | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a> = c
  <span style="color: #06c; font-weight: bold;">where</span> lowercase = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop"><span style="font-weight: bold;">drop</span></a> <span style="color: red;">13</span> $ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cycle"><span style="font-weight: bold;">cycle</span></a> <span style="color: green;">&#91;</span>'a'..'z'<span style="color: green;">&#93;</span>
        uppercase = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> toUpper lowercase</pre>
</div></p>

<p>By now I think you&#8217;re dying to know what the message says, so let&#8217;s finish up here. We decipher a message by converting each integer to a character and performing a rot13 transformation &#8212; then we print it.</p>

<p><div>
<pre class="haskell">decipher = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <span style="color: green;">&#40;</span>rot13 . chr<span style="color: green;">&#41;</span>
main = getimage &gt;&gt;= decipher . msgbits</pre>
</div></p>

<p>The whole thing is pulled together so we can run it from the command line and we receive the sensible output of:</p>

<blockquote>
  <p>Please follow this link:<br />

https://cybersecuritychallenge.org.uk/834jtp.html


https://cybersecuritychallenge.org.uk/834jtp.html</p>

</blockquote>

<p>Next time I&#8217;ll look at what happens when we follow that link, and how we complete the next phase of the challenge.</p>

<p>If any of this doesn&#8217;t make sense or is confusing please ask questions in the comments. If you want to look at the code in full you can read the solutions for part 1 and part 2 online at <a href="http://www.dougalstanton.net/code/cybersecurity/">http://www.dougalstanton.net/code/cybersecurity/</a>.</p>

<p>As I mentioned in my introductory post I was incredibly impressed by the easy exploratory power of the Haskell code I wrote. Writing little segments to splice, decode, convert and transform made it simple to try out different ways of getting sensible output. I had no idea when I started which way the answer would take me, but putting them together in different combinations in the interpreter was easy and provided instant feedback. (That being said, Rich kinda floored me with his ability to wrangle Excel of all things into solving this problem, though that doesn&#8217;t mean it&#8217;s the right tool for the job!) Tune in next time for more exciting cryptographic games!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2010/07/31/part-1-of-cyber-security-challenge-decoding-the-image/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Self-certification</title>
		<link>http://www.dougalstanton.net/blog/index.php/2008/11/15/self-certification/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2008/11/15/self-certification/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 14:21:34 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=508</guid>
		<description><![CDATA[There&#8217;s been a really interesting discussion going on in various circles about a bug report that was filed against the Firefox 3 browser. The user who reported the problem had been annoyed that Firefox seemed to reject the security certificates of every major website she visited &#8212; Paypal, Facebook, Amazon. Each time the browser put [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s been a <a href="http://www.mail-archive.com/dev-tech-crypto@lists.mozilla.org/msg04900.html">really interesting discussion</a> going on in various circles about <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=460374">a bug report that was filed against the Firefox 3 browser</a>. The user who reported the problem had been annoyed that Firefox seemed to reject the security certificates of every major website she visited &#8212; Paypal, Facebook, Amazon. Each time the browser put up an error message the user would press the button that said &#8220;this is all okay, accept this as valid and secure&#8221;.</p>

<p>Of course, the sting in the tail is that it was not Firefox that was in error. This user was being subjected to repeated Man-In-The-Middle attacks &#8212; there was someone else between her and the desired website, intercepting all her traffic and putting her privacy in serious jeopardy.</p>

<p>The interesting aspect is that every single error message which Firefox displayed to the user was valid and pertinent. She <em>really was under attack</em>. But the user, savvy enough to report this bug, didn&#8217;t realise that these messages weren&#8217;t in error. Clearly there is something very wrong with the usability of this system.</p>

<p><span id="more-508"></span></p>

<h3>Piggy in the middle</h3>

<p>The security mechanism built in to modern browsers, called SSL, can encrypt messages between you and whoever you are talking to. If the messages are being encrypted the browser will often show a padlock icon or some other sign that encryption is being used. But this does not mean you are not at risk &#8212; in fact, as this story shows, the feeling of security is all an illusion.</p>

<p>Let&#8217;s assume that there is an encrypted channel (<code>&lt;===&gt;</code>) between Alice and Bob.</p>

<p><div>
<pre>Alice &lt;====&gt; Bob</pre>
</div></p>

<p>What you and Alice don&#8217;t know is where that encrypted channel ends. If there is some eavesdropper in the middle she could easily be talking, securely, with them!</p>

<p><div>
<pre>Alice &lt;==== Mallory ----&gt; Bob</pre>
</div></p>

<p>This is essentially what was happening to the Firefox user mentioned above. The way around this problem is called &#8220;certification&#8221;. Each domain has a certificate which is unique to that domain. No-one else can pretend to be Bob, since no-one else has Bob&#8217;s certificate. This allows Alice to say, with some measure of certainty, not <em>I am talking securely</em> but <em>I am talking securely to Bob</em>.</p>

<p>Certificates are an interesting commodity now. They&#8217;re the stamp of approval that says &#8220;I am Bob&#8221;. What&#8217;s to stop someone else claiming to be Bob with their own certificate? Certificates are issued by certification authorities, many of whom charge money for the privilege. But it&#8217;s also possible to make your own &#8212; so Mallory can make a certificate &#8220;proving&#8221; he is Bob. What can Alice do to spot this?</p>

<p>As I said, certificates are issued by certification authorities. There are not many of these, so it&#8217;s easy for a browser to carry details on all of them. Alice can look at the Mallory&#8217;s certificate and notice that it wasn&#8217;t signed by any certification authority she knows. In fact, it was signed <em>by Mallory himself</em>. A little bit suspicious, no?</p>

<p>If you have to apply for a passport you need a signature from someone like a doctor who knows who you are. This is the equivalent of a certification authority. Signing your own passport application is just as insecure as signing your own digital certificate.</p>

<p>And that is exactly where the above user tripped up. They received a certificate from Mallory, pretending to be Bob, and the certificate was signed by &#8220;Bob&#8221;. There was no third party to say &#8220;yes, this is definitely Bob&#8221;.</p>

<p>The really pernicious danger is that self-signed certificates are not rare. Many smaller websites have self-signed certificates. Browsers will flag up a warning and users will click Ignore and carry on anyway. This behaviour becomes ingrained. Popup cancelling is a horrible danger of having too many messages flashing up with useless messages in the first place.</p>

<h3>Nuke it from orbit</h3>

<p>This problem is slow and insidious, a slow choking danger. Every time another website appears with a self-signed certificate there&#8217;s another reason for users to assume this is normal. Before you know it people don&#8217;t know there <em>is</em> a difference between the padlock icon and the authenticated padlock icon. Which is how we end up here, because chances are that you didn&#8217;t know what the difference was either. And the problem is not going to go away without several changes, some of them quite drastic.</p>

<div style='width:240; float:right; text-align:right; font-size:xx-small; border-width:1px; border-color:#444444; border-style:solid; padding:3px; margin-bottom:30px; margin-left:30px;'>
<img width='240' height='144' alt='Bridge to Nowhere' src='http://farm1.static.flickr.com/4/5742304_cdf04ad1b6_m.jpg'>
<br/>
<a href='http://flickr.com/photos/martinb/5742304/'>Bridge to Nowhere</a>
<br/>&copy;
<a href='http://flickr.com/people/martinb'>Martin Burns</a>
<br/><a href='http://creativecommons.org/licenses/by-nc-nd/2.0/'><img src='http://i.creativecommons.org/l/by-nc-nd/2.0/80x15.png' title='used under a Creative Commons Attribution-NonCommercial-NoDerivs License' width='80' height='15' border='0'/></a>
</div>

<ul>
<li><p>The first problem, and the easiest to solve by an internet mile, is that browsers treat self-signed certificates as secure when they are obviously not. The padlock icon that appears in the browser is meaningless for the reasons outlined above &#8212; it&#8217;s a secure bridge to nowhere.</p>

<p>So the first step is not to show a padlock if the session is not properly secured.</p></li>
<li><p>All those lame websites which use self-signed certificates for commercial purposes are then going to appear as unsecure as they were all along. This will annoy many people and be regarded as a very bad move. But it&#8217;s not the job of browsers to lie about how secure things are, so web designers shouldn&#8217;t expect this.</p>

<p>The next step will be to force websites to pony up for a proper digital certificate<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, or abandon secure connections. (I hope that modern web users are savvy enough not to transmit their bank details in the clear. Maybe I hope too much.)</p></li>
<li><p>Systems which bother users with useless messages cost lives. The history of &#8220;human error&#8221; in nuclear power, avionics and the military speaks volumes. Unfortunately this simple lesson is repeatedly ignored.</p>

<p>Firefox already does a good job of not bothering the user. It rarely does anything unexpected (I regard 404/connection down messages as expected for a network application) but not everyone plays as nice. Windows itself is notorious for alerting the user to endless inconsequential changes of circumstances &#8212; hardware found, driver found, driver installed &#8212; and trains users to ignore error messages in the end.</p>

<p>So, the impossible step in the three step solution is to (a) stop all those useless error messages and (b) stop users from ignoring error messages. You can see why I don&#8217;t hold out much hope for this part.</p></li>
</ul>

<p>As terribly unlikely as these steps are, they are <em>still</em> not actually enough to get the security people expect to get when they see the padlock icon on their browser.</p>

<h3>Not out of the woods yet</h3>

<p>I still haven&#8217;t touched on issues of phishing (how do you know Bob is trustworthy?) which certification does very little to help. Maybe another day. But for now, pay close attention to what your browser says, because it may be saying something important.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>I&#8217;ve been ignoring the case where people have legitimate reason for signing their own certificates, but that&#8217;s for good reason. If you&#8217;re working on a corporate LAN then you can become your own certification authority. But even then, don&#8217;t use it for interaction with the public.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2008/11/15/self-certification/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Islamofascists vandalise ethical shopping website</title>
		<link>http://www.dougalstanton.net/blog/index.php/2008/03/08/islamofascists-vandalise-ethical-shopping-website/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2008/03/08/islamofascists-vandalise-ethical-shopping-website/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 00:11:46 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Politics]]></category>
		<category><![CDATA[Religion]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/index.php/2008/03/08/islamofascists-vandalise-ethical-shopping-website</guid>
		<description><![CDATA[Helen just pointed out that &#8216;The Green Apple&#8217;, an ethical/Fairtrade craft store online, has been hacked by some Islamic fundamentalists. The main page currently has a &#8220;closed for maintenance notice&#8221; but if you click straight through to the store you see a protest page from some nutty religious group:



After thirty seconds the page directs you [...]]]></description>
			<content:encoded><![CDATA[<p>Helen just pointed out that &#8216;The Green Apple&#8217;, an ethical/Fairtrade craft store online, has been hacked by some Islamic fundamentalists. The main page currently has a &#8220;closed for maintenance notice&#8221; but if you <a href="http://www.the-green-apple.co.uk/store" title="Store">click straight through to the store</a> you see a protest page from some nutty religious group:</p>

<p><img src="/blog/wp-content/uploads/2008/03/greenapplehacked.png" alt="Screenshot of the protest" title="Green Apple store hacked" /></p>

<p>After thirty seconds the page directs you to some other site which is about the wonderful prophet.</p>

<p>From a quick look at the guy they&#8217;re protesting &#8212; <a href="http://en.wikipedia.org/wiki/Geert_Wilders">Geert Wilders</a> &#8212; I don&#8217;t really have any sympathy for either side. He seems like the Dutch equivalent of Robert Kilroy Silk (he even has the same daft haircut&#8230;):</p>

<blockquote>
  <p>Take a walk down the street and see where this is going. You no longer feel like you are living in your own country. There is a battle going on and we have to defend ourselves. Before you know it there will be more mosques than churches!</p>
</blockquote>

<p>Oh no! More mosques than churches!</p>

<p>On the other side, the Islamofascists are no better. I feel quite happy denouncing someone who would hijack a third party website for their own pointless protest and then claim &#8220;sorry for the inconvenience. Our aim is not to harm your system&#8221;. Er, yes it is. It was an effective online shop before and now it doesn&#8217;t sell anything &#8212; what other meaning of harm do you want to use?</p>

<p>Bunch of nutters, the lot of them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2008/03/08/islamofascists-vandalise-ethical-shopping-website/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jeremy Clarkson outwitted by self</title>
		<link>http://www.dougalstanton.net/blog/index.php/2008/01/07/jeremy-clarkson-outwitted-by-self/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2008/01/07/jeremy-clarkson-outwitted-by-self/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 14:15:32 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Humour]]></category>
		<category><![CDATA[Politics]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/index.php/2008/01/07/jeremy-clarkson-outwitted-by-self</guid>
		<description><![CDATA[It couldn&#8217;t have happened to a nicer guy:


  The Top Gear host [Jeremy Clarkson] revealed his account numbers [in his newspaper column] after rubbishing the furore over the loss of 25 million people&#8217;s personal details on two computer discs.
  
  He wanted to prove the story was a fuss about nothing.
  [...]]]></description>
			<content:encoded><![CDATA[<p>It couldn&#8217;t have happened to a <a href="http://news.bbc.co.uk/1/hi/entertainment/7174760.stm">nicer guy</a>:</p>

<blockquote>
  <p>The Top Gear host [Jeremy Clarkson] revealed his account numbers [in his newspaper column] after rubbishing the furore over the loss of 25 million people&#8217;s personal details on two computer discs.</p>
  
  <p>He wanted to prove the story was a fuss about nothing.</p>
  
  <p>But Clarkson admitted he was &#8220;wrong&#8221; after he discovered a reader had used the details to create a £500 direct debit to the charity Diabetes UK.</p>
</blockquote>

<p>I&#8217;m pretty disappointed that they choice Diabetes UK and not, say, Friends of the Earth or some anti-motoring charity. That would have been the icing on the cake.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2008/01/07/jeremy-clarkson-outwitted-by-self/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

