<?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>Intrepid Blog &#187; reversing</title>
	<atom:link href="http://blog.affien.com/archives/tag/reversing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 01 Mar 2010 00:58:01 +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>Reversing CRC</title>
		<link>http://blog.affien.com/archives/2005/07/15/reversing-crc/</link>
		<comments>http://blog.affien.com/archives/2005/07/15/reversing-crc/#comments</comments>
		<pubDate>Fri, 15 Jul 2005 11:44:05 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[crc]]></category>
		<category><![CDATA[reversing]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/?p=113</guid>
		<description><![CDATA[Cyclic Redundancy Code
CRC is a hash which is frequently used as a checksum for data in for instance archives. Who hasn&#8217;t had bad CRC errors once when opening corrupted zips. CRC is a very old algorithm and over time it changed a lot from the original idea.
The original idea behind CRC was representing the data [...]]]></description>
			<content:encoded><![CDATA[<h3>Cyclic Redundancy Code</h3>
<p>CRC is a <a href="http://en.wikipedia.org/wiki/Hash_function">hash</a> which is frequently used as a checksum for data in for instance archives. Who hasn&#8217;t had <cite>bad CRC</cite> errors once when opening corrupted zips. CRC is a very old algorithm and over time it changed a lot from the original idea.</p>
<p>The original idea behind CRC was representing the data that you wanted the hash from as a big number and dividing it by a certain number called the <em>polynomial</em> and taking the remainder of the division as the hash. For instance: <code>23 % 3 = 2</code> (<code>%</code> is the <em>modulus</em> operator, which is the remainder of a division)</p>
<p>The initial problem was that dividing is a rather intensive operation. They wanted to simplify CRC to make it easier to implement in hardware and make it faster. They did this by getting rid of the <em>carry</em> used in the substraction of the division:</p>
<p>Normal substraction (binary): <code>10101 - 01100 = 01001</code><br />
Without carry: <code><strong>10</strong>101 - <strong>01</strong>100 = <strong>11</strong>001</code></p>
<p>Substraction without a carry is basicly a <em>eXclusive bitwise OR</em> (XOR), which returns only 1 when one of the operands is 1 and the other 0.</p>
<p>The algorithm required was faster bit still worked bit by bit, which isn&#8217;t really what a computer likes. A computer works best with one to four bytes. To make CRC faster they cached 8 operations at the time by precomputing the results for a certain start value and put it in a table called a XOR Table.</p>
<p>The required code for the CRC calculation itself now became very simple:</p>
<p><code>hash = (hash &gt;&gt; 8 ) ^ table[data ^ (0xff &#038; hash)]</code></p>
<p>They changed the CRC algorithm once more by making it <em>reflected</em>. This means that the input data is reversed bitwise: <code>011101011</code> &lt;-&gt; <code>110101110</code>. This was done because most of the hardware chips at the time reversed data bitwise. For it was too much work to <em>reflect</em> each byte of incoming data they changed the algorithm that generates the Crc table to create a table which has the effect of reflected data.</p>
<p>This is by the way not totally correct; the result still was different for a reflected than a non-reflected algorithm for they wouldn&#8217;t cache the whole piece of data to reverse it but did it per byte at calculation.</p>
<p>At this moment CRC barely resembles the original idea of a modulus.</p>
<h3>Reversing CRC</h3>
<p>First off, credits for the original idea of CRC patching go to anarchriz. </p>
<p>CRC is a cryptographicly very weak algorithm. It can be easily <em>reversed</em> for it has got the property that with 4 bytes you append to the current computed hash you can get every required hash. You can change the whole message and add 4 <em>patch</em> bytes to patch the hash to the one you like.</p>
<p>The ability to patch a CRC also makes it possible to very efficiently generate all possible source data of a checksum. Although it still is a bruteforce method you got 4 bytes freely and patching is faster than calculating.</p>
<p>Patching is basicly going back the way CRC works. Crc basicly takes the hash, moves it 1 byte to the right (dropping one byte) and xor-ring it with the table entry. The nice thing about normal CRC is that the first byte of a table entry is unique for that entry. </p>
<p>For the first byte of the entry is unique for that entry and it is put in the hash xor-red with 0 for that is what is shifted in from the right you can work back the whole entry used.</p>
<p>For instance:</p>
<p>My is: <code>0x012345678</code>, this means that it was xorred with the entry in the CRC table that starts with <code>0x12</code>. When you xor the hash with that full entry the only thing that the next byte was xorred with was the start of a table entry too.</p>
<p>When reversing the current hash you know what will be xorred on the patch you&#8217;ll give. Xorring this with your wanted hash is enough.</p>
<p>The resulting algorithm is suprisingly simple:</p>
<p>- Put the current hash byte wise reversed at the start of a buffer. Put the wanted hash byte wise reversed at the end of the current hash in the same buffer.<br />
- Look up the entry in the table that starts with byte 7 in the buffer. Xor this value of position 4, and Xor the entry number on position 3. Repeat this 4 times with the positions each time one decremented. (thus 7,6,5,4 and 4,3,2,1 and 3,2,1,0)</p>
<p>When you&#8217;ve done this the required patch bytes are the first 4 bytes in the buffer.</p>
<p><ins>Some Crc variants tend to use duplicates in the crc-table which means there could be more than one original table entry used. You should just branch and try all of them.</ins></p>
<p>I&#8217;ve made a simple python script to work with crc32 and to patch a hash.</p>
<p>You can download it <a href="http://w-nz.com/~darkshines/projects/crc-rev-v0.01b.tar">Here</a>.  And there is a C implementation <a href="http://cvs.codeyard.net/svn/icrc/trunk/src/">here</a>.</p>
<p><ins>Update</ins> Fixed a bug in the example.</p>
<p><ins>Update</ins> Andrea Pannitti wrote an <a href="http://andreapannitti.blogspot.com/2008/01/crc-reverser-java-version.html">implementation in java</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2005/07/15/reversing-crc/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>
