<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ReversingLabs &#124; Blog &#187; Reversing</title>
	<atom:link href="http://blog.reversinglabs.com/tag/reversing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.reversinglabs.com</link>
	<description>Everything in reverse...</description>
	<lastBuildDate>Sat, 02 Jul 2011 10:53:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Sophos decodeme at AusCERT</title>
		<link>http://blog.reversinglabs.com/2010/05/decodeme/</link>
		<comments>http://blog.reversinglabs.com/2010/05/decodeme/#comments</comments>
		<pubDate>Sun, 23 May 2010 15:05:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Base64]]></category>
		<category><![CDATA[GIF]]></category>
		<category><![CDATA[GZIP]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=633</guid>
		<description><![CDATA[Being the huge file analysis geeks (you must be shocked by this, we know) that we are, we couldn't help solving the more than interesting #decodeme challenge from Sophos at this years AusCert. The challenge itself was printed on a T-Shirt and the puzzle looked exactly like this: %~~~~~~~~~~~~~~~~~~~~~~~~% &#124;H4sIAAAAAAACA3P3dLOwTOxh&#124; &#124;YGF4zsBg7tHJMApGwYgE////&#124; &#124;V/zJwsjF8I9BB8QH5QkGjhYG&#124; &#124;xj/MD' gULH&#124; &#124;JrY' [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Being the huge file analysis geeks (<em>you must be shocked by this, we know</em>) that we are, we couldn't help solving the more than interesting #<a href="http://www.sophos.com/blogs/duck/g/2010/05/16/decodeme-t-shirt-tex/" target="_blank">decodeme</a> challenge from <a href="http://www.sophos.com" target="_blank">Sophos</a> at this years <a href="http://conference.auscert.org.au/conf2010/" target="_blank">AusCert</a>. The challenge itself was printed on a <a href="http://www.sophos.com/blogs/duck/g/2010/05/15/sophos-auscert-decodeme/" target="_blank">T-Shirt</a> and the puzzle looked exactly like this:</p>
<blockquote>
<pre>%~~~~~~~~~~~~~~~~~~~~~~~~%
|H4sIAAAAAAACA3P3dLOwTOxh|
|YGF4zsBg7tHJMApGwYgE////|
|V/zJwsjF8I9BB8QH5QkGjhYG|
|xj/MD'              gULH|
|JrY'                BbVi|
|Tlx|   Y4NgmoOxWoxH4yL5d|
|VDR|   oTseHh8f6WK359lQU|
|qJy\              \YJOGt|
|xhN5I\              \dlr|
|qoJvnIznRDXvHjPWZ   |SY7|
|Lz31nKtYPklkV0F6w   |AKr|
|1E17                ,Vk5|
|afng              ,hp63R|
|VsvNzy8u9qpU670lon11hvnS|
|KNWuSS+vrvNf3HV05beU0NXB|
|p71kJQQYrAFt8kQCpwMAAA==|
%~~~~~~~~~~~~~~~~~~~~~~~~%
  D  E  C  O  D  E  M  E
</pre>
</blockquote>
<p style="text-align: justify;">We are pretty sure that "S" stands for Sophos not Superman. Now, the first thing that comes to mind when you look at the "picture" is that the data around the "S" is important. And if we look at the last two letters we see the <a href="http://en.wikipedia.org/wiki/Base64" target="_blank">base64</a> trademark signature. Which means that all that data is an encoded message or a file. To decode it, we must strip that "S" to form a proper base64 data chain. Once done, the data looks like this:</p>
<blockquote><p>H4sIAAAAAAACA3P3dLOwTOxhYGF4zsBg7tHJMApGwYgE////V/zJwsj<br />
F8I9BB8QH5QkGjhYGxj/MDgULHJrYBbViTlxY4NgmoOxWoxH4yL5dVD<br />
RoTseHh8f6WK359lQUqJyYJOGtxhN5IdlrqoJvnIznRDXvHjPWZSY7L<br />
z31nKtYPklkV0F6wAKr1E17Vk5afnghp63RVsvNzy8u9qpU670lon11<br />
hvnSKNWuSS+vrvNf3HV05beU0NXBp71kJQQYrAFt8kQCpwMAAA==</p></blockquote>
<p style="text-align: justify;">That data must be reverted to either text or binary to continue. First, we tried  an online <a href="http://www.motobit.com/util/base64-decoder-encoder.asp" target="_blank">base64 decoder</a> but it returns a very strange string. So then, we decoded the data to a binary file and opened that with a hex editor, where we see the well known 0x1F 0x8B signature, which indicates that the decoded data is in fact a <a href="http://en.wikipedia.org/wiki/Gzip" target="_blank">GZIP</a> file. Now, we know GZip files may or may not store a file name, so when we decompress the packed data we do another hex data inspection to discover that the decompressed file is a <a href="http://en.wikipedia.org/wiki/Gif" target="_blank">GIF</a> file. It's an image showing us this:<a href="http://blog.reversinglabs.com/wp-content/uploads/2010/05/base64.gif" rel="lightbox[633]"><img title="Base64" src="http://blog.reversinglabs.com/wp-content/uploads/2010/05/base64.gif" alt="" width="140" height="4" /></a> Not quite readable, but once you zoom in on it, and lower-case it, it points to: <a href="http://www.sophos.com/anz/sofarsogood.html" target="_blank">http://www.sophos.com/anz/sofarsogood.html</a> which holds the last piece of the puzzle.</p>
<p style="text-align: justify;">Sadly last piece of the puzzle has nothing to do with file analysis whatsoever. Its a crypto challenge requiring you to play with letter substitution crypto algorithms. And this isn't something we are really interested in. You are however more than welcome to fiddle with it if you like. For some help on solving it check <a href="http://community.websense.com/blogs/securitylabs/archive/2010/05/20/a-simple-n-gram-calculator-pyngram.aspx" target="_blank">this</a> out. Until next week...</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2010%2F05%2Fdecodeme%2F&amp;title=Sophos%20decodeme%20at%20AusCERT" id="wpa2a_2"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/05/decodeme/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Back to the basics</title>
		<link>http://blog.reversinglabs.com/2010/05/back-to-the-basics/</link>
		<comments>http://blog.reversinglabs.com/2010/05/back-to-the-basics/#comments</comments>
		<pubDate>Tue, 11 May 2010 21:33:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[LUA]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=606</guid>
		<description><![CDATA[Its been a really long time since we made an unpacker for... well anything. Sure we did a format converter and some archive format unpacker but our last PE unpacker was (checks the blog) in February. So, lets get back to the basics and create a dynamic unpacker for PackMan. We already have an unpacker [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Its been a really long time since we made an unpacker for... well anything. Sure we did a <a href="http://blog.reversinglabs.com/2010/04/archive-conversion/">format converter</a> and some <a href="http://blog.reversinglabs.com/2010/04/unpacking-archives-with-titanengine/">archive format unpacker</a> but our last PE unpacker was (<em>checks the blog</em>) in February. So, lets get back to the basics and create a dynamic unpacker for PackMan. We already have an unpacker for PackMan? Its in the TitanEngine package already, you say? Well we do, but what's stopping us from having a little fun with unpacker optimizations?</p>
<p style="text-align: justify;">There are a lot of optimizations one can do with the <em>TitanEngine </em>to make it work even faster then lightning. During the related unpacker execution timing research for our upcoming <a href="http://blog.reversinglabs.com/2010/04/caro-workshop/">CARO Workshop</a> talk we measured the impact that certain operations inside the engine itself have on the total unpacking time. We realized that there is significant space for performance improvement in certain unpacking areas which is especially important when we are processing large file volumes. Now, when unpacking files with unpackers built around the TitanEngine you get unpacker execution times quite similar to the sample execution time, except for cases where dynamic link library unpacking requires snapshots to correct the relocation table. in those cases we see a significant unpacking execution time increase. To counter this we can either do memory snapshots to memory or optimize relocation processing and avoid using snapshots at all.</p>
<p style="text-align: justify;">Generally when talking about fixing relocation table we refer to the easy snap-and-compare method. However there is another way of making the unpacked dynamic link library valid for loading on non default base. We can use RelocaterGrabRelocationTableEx function for cases when the packer uses non modified relocation table, defined as it is in the PECOFF document. Relocation data is still compressed and can only be accessed just before the file is relocated, which is why we need a function to inspect the memory and determine the relocation table size. And that is exactly what RelocaterGrabRelocationTableEx does. It determines the size of the relocation table at the provided address and copies it to the engine for later exporting. If we look at the following PackMan code snippet which does the image relocation:</p>
<pre class="asm">  <span style="color: #EE4A02;">OR</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">JE</span> L018
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EBX<span style="color: #ff0000;">+24</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">JMP</span> L013
L004:
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">OR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">JE</span> L011
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">AH</span>,0F
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDX</span>+<span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
L011:
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">JNZ</span> L004
L013:
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+8</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+4</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">JNZ</span> L011
L018:
  <span style="color: #EE4A02;">POPAD</span>
&nbsp;</pre>
<p style="text-align: justify;">We can see that the relocation table is stored at EBX+0x24 address. Therefore by reading that memory pointer before the actual relocation occurs we have all the parameters we need to fix the relocation table. Passing that parameter to the RelocaterGrabRelocationTableEx will result in the engine reading the relocation table and estimating its size. Therefore we can just use the pointer we read at the EBX+0x24 address and the return from RelocaterEstimatedSize to correct the PE header for the unpacked file. However RelocaterEstimatedSize doesn't return the accurate size due to the system design. It must be reduced by 8 to be correct for all cases.</p>
<p style="text-align: justify;">Since we are only updating the PE header data we can free the relocation table stored inside the engine with RelocaterCleanup. Once we dump the process relocation table fixing is as easy as updating the PE header fields. By doing the relocation table fixing this way we optimize the speed of execution by a significant percent. No actual data needs to be written to the file on the disk since it is already there and in the correct format. Furthermore you can start the debugging without the previously necessary DLL loading on the address other then default. If you choose to use that optimization as well packer execution time will be shorter since the file might not be relocated at all thus saving CPU cycles. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/05/PackMan.zip">RL!dePackMan</a><br />
(package contains the unpacker with source and the samples  used)</p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2010%2F05%2Fback-to-the-basics%2F&amp;title=Back%20to%20the%20basics" id="wpa2a_4"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/05/back-to-the-basics/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Attacking the cipher</title>
		<link>http://blog.reversinglabs.com/2010/01/attacking-the-cipher/</link>
		<comments>http://blog.reversinglabs.com/2010/01/attacking-the-cipher/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 05:00:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[BruteForce]]></category>
		<category><![CDATA[forensics]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[Recovery]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=245</guid>
		<description><![CDATA[Not too long ago we dedicated a blog post to removing executable password protections. In that post we said that we will eventually return to this topic to deal with much harder opponent. Well today is that day. This time we take a look at executable password protection named PEPasswordEncryptor As we have seen in [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Not too long ago we dedicated a <a href="http://blog.reversinglabs.com/2009/12/exe-password-protection/" target="_blank">blog post</a> to removing executable password protections. In that post we said that we will eventually return to this topic to deal with much harder opponent. Well today is that day. This time we take a look at executable password protection named <a href="http://smf.chat.ru/e_smf_release.htm" target="_blank">PEPasswordEncryptor</a></p>
<p style="text-align: justify;">As we have seen in our previous blog on this subject tools that provide this kind of protection are very often coded with major design flaws which enable us with quick and painless ways to work around the password protection. However today's password protection option doesn't have such flaws. And that is why we need to find an optimal way to quickly and accurately recover the password. Can it be done in this case?</p>
<p style="text-align: justify;">Quick analysis of the protected file shows us these interesting pieces of code:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">40</span>
  <span style="color: #EE4A02;">PUSH</span> 0D
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+20</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+402923</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">CALL</span> L025        <span style="color: #adadad; font-style: italic;">;Calculate the hash for input string</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,F492C2C1 <span style="color: #adadad; font-style: italic;">;Correct password hash</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">JNZ</span> L015
...
L025:              <span style="color: #adadad; font-style: italic;">;Slow hashing algorithm</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>      <span style="color: #adadad; font-style: italic;">;Hash initialization</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>         <span style="color: #adadad; font-style: italic;">;ESI holds the password pointer</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>      <span style="color: #adadad; font-style: italic;">;Reason why it executes 0xFFFF times</span>
  <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>          <span style="color: #adadad; font-style: italic;">;for every letter</span>
L031:
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">AH</span>,<span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
L033:
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #EE1802; font-weight:bold;">DL</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,434F4445 <span style="color: #adadad; font-style: italic;">;Constant</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">CL</span>,<span style="color: #EE1802; font-weight:bold;">AL</span>
  <span style="color: #EE4A02;">ROR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">CL</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,55AA5A5A <span style="color: #adadad; font-style: italic;">;Constant</span>
  <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">DX</span>
  <span style="color: #EE4A02;">JNZ</span> L033
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">JNZ</span> L031
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">Now just by looking at this piece of code we see that the author of the program thought of many things when it comes to protected file's security. Why? Algorithm in charge of hashing the string is really slow because it executes 0xFFFF times for every letter of the password. If it wasn't for this bruteforcing  this algorithm would be nice and quick. But before we go for that extreme we should always check for possible shortcuts that can enable us to skip the password necessity.</p>
<p style="text-align: justify;">Since we already know that password hash must be 0xF492C2C1 lets see if the memory content decryption has a weakness.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSHAD</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">48415348</span> <span style="color: #adadad; font-style: italic;">;Hash initialization</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> GetPasswordHash
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">AH</span>,<span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> GetPasswordHash
  <span style="color: #EE4A02;">SHR</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #ff0000;">2</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
L011:              <span style="color: #adadad; font-style: italic;">;Decrypt first section</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">CL</span>,<span style="color: #EE1802; font-weight:bold;">AL</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #ff0000;">4</span>
  <span style="color: #EE4A02;">ROL</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">CL</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>      <span style="color: #adadad; font-style: italic;">;Keys for decryption: EAX and EBX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">CL</span>,<span style="color: #EE1802; font-weight:bold;">BH</span>
  <span style="color: #EE4A02;">ROR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">CL</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">JNZ</span> L011
  <span style="color: #EE4A02;">POPAD</span>
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">Here is what happens here. Just before the program decrypts the first section it initializes two 32bit decryption keys. Both keys are initialized from the password string, more accurately the keys are password hashes. Second key is calculated fist. This key which is stored in EBX register is a direct product of password hash for algorithm initialize value 0x48415348. However this value isn't a constant, it is modified by XORing with the fist letter of the password. That is why the already calculated  password hash we have 0xF492C2C1 isn't enough to break this algorithm. Fist key is calculated last and its value is stored in EAX register. Hashing algorithm for this key calculation is a direct result of first key stored in EBX XORed with the fist letter of the password. Only by having the state of both keys for decryption beginning we can correctly decrypt the file. However we put it the password seems like a must for decryption. We are going to return to shortcuts a bit later, lets discuss the bruteforce option first.</p>
<p style="text-align: justify;">Building a bruteforcer for this algorithm is quite easy. We just need to go through the all possible combinations that a password can take in order to recover the 'lost' password. However that proves to be a much harder task then it sounds. Building the bruteforcer isn't a problem but the time its needed for recovering a simple password which is only 4 characters in length is very long. Take a look at this program log to see a basic log which shows the "slowness" of this algorithm. It seems that author of the program saw bruteforce as a potential risk and made it slow on purpose. But this isn't the only problem. Our bruteforce test shows that passwords collide meaning the multiple passwords have the same hash. Any of these passwords will unlock the program but it won't be decrypted correctly and therefore it will crash. Here is how the bruteforce program log looks like:</p>
<p style="text-align: justify;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/01/bForce1.png" rel="lightbox[245]"><img class="aligncenter size-medium wp-image-252" title="bForce1" src="http://blog.reversinglabs.com/wp-content/uploads/2010/01/bForce1-300x187.png" alt="" width="300" height="187" /></a></p>
<p style="text-align: justify;">Correct password for this sample is: "ap0x" but to recover it bruteforcer needed approximately 4 minutes. And as you can see in log file time needed to go through every four character password combination (a..z + A..Z + 0..9) is more then 2 hours. And even that isn't a guarantee because you have check every password since the protection doesn't have an additional validation to check if the code decrypted correctly. Since that is way too slow for any practical use on longer passwords we return to finding the algorithm weakness.</p>
<p style="text-align: justify;">But is there a weakness? If we look at the decryption algorithm we see that both EAX and EBX keys are needed for decryption to work correctly. However only one key, EAX, is used to decrypt data by XORing the file memory content. Therefore recovering the password would be easy if we knew the first four bytes unencrypted. Which we don't... Could it help if we need the unencrypted value of any random sequence of bytes in the file? It might but that would mean that we would also need one more information for that location, the value of ECX register because it is used to modify successive decryption keys. Is there just such a sequence of bytes? Sure, at the end of file whose sections are aligned to PE.FileAlignment (and its a must for this protection) we have at least 12 bytes which are zeroes. Last four bytes in that case are equal to EAX decryption key at that time, which leaves EBX and CL as unknowns. Four bytes before that we have the same story. In order to recover EAX, EBX and CL we must do the following:</p>
<ul>
<li>Reverse the decryption algorithm so it decrypts the memory backwards</li>
<li>Bruteforce that decryption to recover EAX, EBX and CL values</li>
<li>Make sure that those key values are correct since algorithm does collide</li>
<li>Decrypt the file backwards using the correct keys</li>
</ul>
<p style="text-align: justify;">Since keys are 32bit and we already know the value of one of those at one point we can bruteforce keys between 0x00000000 and 0xFFFFFFFF in order to get the missing EBX and CL values. That is much faster then trying to bruteforce the infinity of possible passwords. Reversed algorithm can be seen in the source code under the function named <em>PEPDecryptFile</em>.</p>
<div style="text-align: center;"><a href="http://www.youtube.com/watch?v=jJG-9l-Urmc"><img src="http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/inc/img.php?v=jJG-9l-Urmc"></a></div>
<p style="text-align: justify;">
<p style="text-align: justify;">Writing a bruteforcer for PEPasswordEncryptor is a nice reversing exercise especially when algorithm shortcut inspection is involved. As always binary, source code and the samples are included with the blog. Until next week...</p>
<p style="text-align: center;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/01/PEBrute.zip">PEBrute</a><br />
(package contains unpacker binary, source and samples used)</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2010%2F01%2Fattacking-the-cipher%2F&amp;title=Attacking%20the%20cipher" id="wpa2a_6"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/01/attacking-the-cipher/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Removing executable password protection</title>
		<link>http://blog.reversinglabs.com/2009/12/exe-password-protection/</link>
		<comments>http://blog.reversinglabs.com/2009/12/exe-password-protection/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 12:47:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[forensics]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[Protection]]></category>
		<category><![CDATA[Recovery]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=164</guid>
		<description><![CDATA[With the next TitanEngine release just around the corner we decided to do a light an interesting analysis of a simple executable password protection. Today's focus is on LCCrypto a simple example which will show us the general security and vulnerabilities of such and similar tools. Now it theory only one password should be able [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">With the next <em>TitanEngine </em>release just around the corner we decided to do a light an interesting analysis of a simple executable password protection. Today's focus is on <a href="http://www.luocong.com/myworks.htm" target="_blank">LCCrypto</a> a simple example which will show us the general security and vulnerabilities of such and similar tools.</p>
<p style="text-align: justify;">Now it theory only one password should be able to unlock such protected application and allow its execution without any possibility of removing the protection without the correct password. Well that's the theory behind such tools but they themselves have a weakness too because there still must be a way for the application to check the validity of the inputted password. So regardless of the protection executable password protection solution must do one of the following:</p>
<ul>
<li>Compare passwords or password hashes to determine whether or not to execute the file</li>
<li>Use the inputted password for code decryption and verify the decrypted content by hashing</li>
</ul>
<p style="text-align: justify;">So, not a lot of options to choose from. First model would be the least secure since no content is encrypted and the second most secure requiring password bruteforce to be removed. Lets see what we are dealing with in LCCrypto's case. Entry point of the protected file looks like this:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESP</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;Pointer to ExitProcess API</span>
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,FFFF0000
L002:
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,905A4D
  <span style="color: #EE4A02;">JE</span> L006
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">1000</span>
  <span style="color: #EE4A02;">JMP</span> L002
L006:
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBP</span></pre>
</blockquote>
<p style="text-align: justify;">Very simple way to get the kernel base most commonly used in malware which could flag this sample as malicious by some anti-virus vendors. Following this we have:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+3C<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EAX<span style="color: #ff0000;">+78</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+20</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
L007:
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">50746547</span>
  <span style="color: #EE4A02;">JNZ</span> L025
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EAX<span style="color: #ff0000;">+4</span><span style="color: #FFFFFF;">&#93;</span>,41636F72
  <span style="color: #EE4A02;">JNZ</span> L025
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EAX<span style="color: #ff0000;">+8</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">65726464</span>
  <span style="color: #EE4A02;">JNZ</span> L025
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+<span style="color: #DEE002;">C</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">7373</span>
  <span style="color: #EE4A02;">JNZ</span> L025
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+24</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+<span style="color: #EE1802; font-weight:bold;">EDX</span>*<span style="color: #ff0000;">2</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span>+1C<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+<span style="color: #EE1802; font-weight:bold;">EBX</span>*<span style="color: #ff0000;">4</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407408</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
L025:
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #ff0000;">4</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+18</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">JNZ</span> L007</pre>
</blockquote>
<p style="text-align: justify;">Another code which is used to locate GetProcAddress API pointer. This is crucial since that API and already located kernel base are all that this protection needs to locate all other needed APIs to create password input dialog and process it. Code that creates the window and its elements is located just after the code locates all needed APIs. Code that is of interest is here:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407004</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407410</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;FreeLibrary</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407000</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407410</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;FreeLibrary</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407881</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">JE</span> L008
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,004012C0 <span style="color: #adadad; font-style: italic;">;OriginalEntryPoint</span>
  <span style="color: #EE4A02;">JMP</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
L008:
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407414</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;ExitProcess</span></pre>
</blockquote>
<p style="text-align: justify;">This is what happens when the window message processing loop just above this exits, and that will happen when the created window associated with that message processing loop terminates by window closing. Which means that if the byte at address 0x00407881 isn't set to zero LCCrypto will pass the code execution to original entry point which in this case isn't encrypted at all. Patching this compare in memory and closing the window would do the trick. Removing the protection completely would be as easy as setting the entry point address in the PE header to 0x000012C0. Normally our work would be done here, but lets dig in just a little bit more.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">10</span>
  <span style="color: #EE4A02;">PUSH</span> 0040776E
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">64</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+8</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407460</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;GetDlgItemTextA</span>
  <span style="color: #EE4A02;">CALL</span> 00407FF9 <span style="color: #adadad; font-style: italic;">;Calculate CRC32</span>
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>40776E<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #ff0000;">00408021</span> <span style="color: #adadad; font-style: italic;">;Update CRC32</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>40775E<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span> <span style="color: #adadad; font-style: italic;">;Compare password CRC32</span>
<span style="color: #adadad; font-style: italic;">; DS:[0040775E]=0E1A88EF</span>
  <span style="color: #EE4A02;">SETE</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">407881</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;Set the correct password switch</span></pre>
</blockquote>
<p style="text-align: justify;">This code bit here processes the inputted password. As we can see from the code above inputted password is hashed with CRC32 and that value is compared with the value 0x0E1A88EF, and that means that hash for the password must be 0x0E1A88EF for protection to pass the control to original code. Now, leaving alone the fact that we can patch the file, think about what we can do to legitimately start the application. Bruteforce the hash? What about <a href="http://www.woodmann.com/fravia/crctut1.htm" target="_blank">make a string that has the same one</a>?</p>
<p style="text-align: justify;">CRC32 algorithm collides which means that we can create such a string. Without going into coding a program that can do that we use a <a href="http://www.peid.info/" target="_blank">PeID</a> plugin called CRC32 made by Gelios on a simple text file containing only the string "pwd". After the file's CRC32 has been set to 0x0E1A88EF our string "pwd" becomes "pwd€va" which is a string with the CRC32 hash we need. Entering such string into program will fool it into thinking that is the original password making LCCrypto execute the file normally.</p>
<p style="text-align: justify;">We didn't do any coding today because it wasn't necessary. However we did investigate the security of one executable password protection and saw all the flaws its has. Next time we return to this topic we will do some coding in order to make a bruteforce engine that will recover the password used to protect the file. Until next time...</p>
<p style="text-align: center;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/12/LCCrypted.zip"><img title="LCCrypto" src="http://blog.reversinglabs.com/wp-content/uploads/2009/12/LCCrypto.jpg" alt="LCCrypto" width="299" height="212" /></a><br />
<a href="http://blog.reversinglabs.com/wp-content/uploads/2009/12/LCCrypted.zip">Download protected sample</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F12%2Fexe-password-protection%2F&amp;title=Removing%20executable%20password%20protection" id="wpa2a_8"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/12/exe-password-protection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Complex static unpackers, may the force be with you!</title>
		<link>http://blog.reversinglabs.com/2009/11/complex-static-unpackers-may-the-force-be-with-you/</link>
		<comments>http://blog.reversinglabs.com/2009/11/complex-static-unpackers-may-the-force-be-with-you/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 13:49:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Unpacker]]></category>
		<category><![CDATA[y0da Crypter]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=156</guid>
		<description><![CDATA[Commonly targets chosen for demonstration of TitanEngine static unpacking functions were very simple and so the code that unpacks the target would be very short but still enough to understand the basic principal of static unpackers.  But today we do something very different. We decent into madness by testing the far limits of the TitanEngine [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Commonly targets chosen for demonstration of <em>TitanEngine </em>static unpacking functions were very simple and so the code that unpacks the target would be very short but still enough to understand the basic principal of static unpackers.  But today we do something very different. We decent into madness by testing the far limits of the <em>TitanEngine </em>and ourselves. Yoda's Crypter is one though nut to crack so may the force be with us on this journey.</p>
<p style="text-align: justify;">Proof of this thesis is found at the very beginning of our task. Entry point itself lays on a challenge. Polymorphic decryption is used to decrypt most of the crypter body. Since this code is random we must do something to handle it and all similar cases found in the crypter body.</p>
<blockquote>
<pre class="asm">L000:
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
<span style="color: #adadad; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">; Totally random decryption code</span>
<span style="color: #adadad; font-style: italic;">;</span>
  <span style="color: #EE4A02;">STOS</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">ES</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">LOOPD</span> L000</pre>
</blockquote>
<p style="text-align: justify;">Since both start and end patter can be defined with the LODS and STOS instructions code in between can be easily located. But what to do with it? Simple way of handling this would be extraction of this code and dynamic generation of decryption code with the following structure:</p>
<blockquote>
<pre class="asm"><span style="color: #adadad; font-style: italic;">; __stdcall function long Decrypt(EAX, ECX)</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #EE1802; font-weight:bold;">ESP</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span><span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+8</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span><span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+<span style="color: #DEE002;">C</span><span style="color: #FFFFFF;">&#93;</span>
<span style="color: #adadad; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">; Totally random decryption code pasted here</span>
<span style="color: #adadad; font-style: italic;">;</span>
  <span style="color: #EE4A02;">LEAVE</span>
  <span style="color: #EE4A02;">RET</span> <span style="color: #ff0000;">8</span></pre>
</blockquote>
<p style="text-align: justify;">Here ECX and EAX are input variables since they change. EAX is equal to byte pointed by ESI since its loaded with LODS, and ESI at start points to first byte after LOOPD instruction. Decryption size is static and its calculated and stored just before this decryption loop inside the crypter body. Since LOOPD decrements the ECX value it must be handled before every call to our decryption function. Return value of our decryption function is the value of decrypted byte. This is one way of dealing with polymorphic decryption functions and therefore this or similar approach will be used every time we encounter such obstacle while unpacking the crypter. If we were making a dynamic unpacker skipping this polymorphic decryption would be as easy as setting a hardware breakpoint on the first byte after LOOPD and waiting for it to hit.</p>
<p style="text-align: justify;">This first layer of encryption is the most important one since all the data needed for our unpacker is encrypted by it. If you remember when coding dynamic unpacker first logical step is to collect data about imports. Situation is a bit different when it comes to static unpackers. First thing to do is of course decrypt everything that needs decrypting. With the first layer already decrypted we move on to decrypting section content. Following code processes sections:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span>+3C<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,0F8
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
L005:
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">63727372</span> <span style="color: #adadad; font-style: italic;">;rsrc</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,7273722E <span style="color: #adadad; font-style: italic;">;.rsr</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,6F6C6572 <span style="color: #adadad; font-style: italic;">;relo</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,6C65722E <span style="color: #adadad; font-style: italic;">;.rel</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">4379</span>     <span style="color: #adadad; font-style: italic;">;yC</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,6164652E <span style="color: #adadad; font-style: italic;">;.eda</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,6164722E <span style="color: #adadad; font-style: italic;">;.rda</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,6164692E <span style="color: #adadad; font-style: italic;">;.ida</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,736C742E <span style="color: #adadad; font-style: italic;">;.tls</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>ESI<span style="color: #ff0000;">+14</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>ESI<span style="color: #ff0000;">+10</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">JE</span> L046
  <span style="color: #EE4A02;">PUSHAD</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>ESI<span style="color: #ff0000;">+10</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">OR</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">JNZ</span> L035
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>ESI<span style="color: #ff0000;">+14</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">CALL</span> 0040748B
  <span style="color: #EE4A02;">JMP</span> L038
L035:
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span>+<span style="color: #DEE002;">C</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">CALL</span> 0040744E  <span style="color: #adadad; font-style: italic;">;Decrypt content</span>
L038:
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,00402D3E
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">RET</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">INT</span> 0D
  <span style="color: #EE4A02;">POPAD</span>
L046:
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #ff0000;">28</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">DX</span>,<span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+6</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">JNZ</span> L005
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">All sections are processed by names. Every section except the ones named rsrc, idata, edata, rdata, tls and yC is decrypted. That kind of logic must be incorporated in our unpacker aswell. Decryption of content is done with another polymorphic decryption loop. Same procedure as described above can be applied. After that is done all that remains is that we fix imports and correct the entry point.</p>
<p style="text-align: justify;">Now for the imports... Not exactly a hard task once we locate yC's internal data. Scroll down to the end of the crypter code, until you find this:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #EE1802; font-weight:bold;">ESP</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+10</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+9C<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,crackme_<span style="color: #ff0000;">.00403393</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+B8<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+B4<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+9C<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">LEAVE</span>
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">Following this is a simple data structure containing the following:</p>
<blockquote>
<pre class="asm"><span style="color: #adadad; font-style: italic;">; DWORD - LoadedBase (default: ImageBase)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - OriginalEntryPoint address (RVA)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - yC protection options selected (All options: 0x3C)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - File checksum (custom algorithm)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Crypter body memory checksum (custom algorithm)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Reserved; Store place for a boolean variable</span>
<span style="color: #adadad; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">; Simplified IID (Image Import Descriptor)</span>
<span style="color: #adadad; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Pointer to name of the first DLL in the IAT (RVA)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Pointer to IAT for the first DLL(RVA)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Reserved; OriginalFirstTrunk</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Pointer to name of the second DLL in the IAT (RVA)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Pointer to IAT for the second DLL(RVA)</span>
<span style="color: #adadad; font-style: italic;">; DWORD - Reserved; OriginalFirstTrunk</span>
<span style="color: #adadad; font-style: italic;">; etc. for all DLLs</span></pre>
</blockquote>
<p style="text-align: justify;">As we can see all the data we need is decrypted with the first polymorphic decryption and easily located. What we need from this is to read the location of DLL names and API pointers for every DLL and rebuild IIDs linking this data. Additionally all strings are encrypted so we need to go through the API pointers and decrypt them with the following algorithm:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
L004:
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ROR</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #ff0000;">4</span>
  <span style="color: #EE4A02;">STOS</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">ES</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">JNZ</span> L004
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">Once that is done we have imports sorted and since we know the address of the original entry point we only need to write it to PE header and optionally strip to crypter section to complete the unpacker. Optionally because if crypter is used on programs with TLS table it will be moved to the crypter section and if we don't want to rebuild that as well we can just keep the crypter section.</p>
<p style="text-align: justify;">Writing an unpacker for Yoda's Crypter is a fairly complex task since there are few details to worry about. It provides an interesting challenge for any reverser. As always unpacker, source code and the samples are included with the blog. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/11/RLdeY0daCrypter.zip">RL!deY0daCrypter 1.x</a><span><br />
(package contains unpacker binary, source and samples used)</p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F11%2Fcomplex-static-unpackers-may-the-force-be-with-you%2F&amp;title=Complex%20static%20unpackers%2C%20may%20the%20force%20be%20with%20you%21" id="wpa2a_10"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/11/complex-static-unpackers-may-the-force-be-with-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From Russia with Love, nPack story</title>
		<link>http://blog.reversinglabs.com/2009/11/npack-unpacker/</link>
		<comments>http://blog.reversinglabs.com/2009/11/npack-unpacker/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 13:47:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[nPack]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=146</guid>
		<description><![CDATA[Dynamic unpacking has a couple of benefits and couple of drawbacks. Main benefit would of course be the quick unpacker writing and natural resilience to minor packer changes including multiple shell versions that use different compression and/or encryption algorithms. Our only real concern would be possibility of file malformation so that the file we are [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Dynamic unpacking has a couple of benefits and couple of drawbacks. Main benefit would of course be the quick unpacker writing and natural resilience to minor packer changes including multiple shell versions that use different compression and/or encryption algorithms. Our only real concern would be possibility of file malformation so that the file we are unpacking does a jail break. But since we can virtualize our unpackers with either system or application virtualization or sandboxing this isn't a huge issue any more. Also remember that <em>TitanEngine </em>now runs even under <a href="http://blog.reversinglabs.com/2009/10/titan-on-ubuntu/">Linux</a> making it as safe as possible to execute live application code. With this in mind we extend the functionality of our static unpacker functions to provide most help when it come to making such unpackers, since even though dynamic unpackers solve most of our problems static unpacking is still the best option. Further recommended reading on this topic can be found <a href="http://www.eecs.berkeley.edu/Pubs/TechRpts/2009/EECS-2009-133.pdf" target="_blank">here</a>.</p>
<p style="text-align: justify;">However this is a "code your own <em>dynamic </em>unpacker" Monday. Today we take a look at nPack a straight forward packer that comes to us from Russia. Since it supports compression of both dynamic link library and executable files it should give us a nice exercise on how to write dynamic unpackers. Shall we?</p>
<p style="text-align: justify;">Entry point of the packed file gives more useful information than we usually see with packers. Take a look.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E48<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>  <span style="color: #adadad; font-style: italic;">;File already decompressed check</span>
  <span style="color: #EE4A02;">JNZ</span> L003
  <span style="color: #EE4A02;">JMP</span> L004
L003:
  <span style="color: #EE4A02;">RET</span>
L004:
  <span style="color: #EE4A02;">CALL</span> 0040720A
  <span style="color: #EE4A02;">CALL</span> 0040723C
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E08<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E44<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">CALL</span> 0040727A
  <span style="color: #EE4A02;">CALL</span> 004073FD
  <span style="color: #EE4A02;">CALL</span> 004078B2
  <span style="color: #EE4A02;">CALL</span> <span style="color: #ff0000;">00407806</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E44<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E48<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">1</span>   <span style="color: #adadad; font-style: italic;">;Set file already decompressed flag</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E00<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span> <span style="color: #adadad; font-style: italic;">;Add loaded file base to OEP RVA</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E00<span style="color: #FFFFFF;">&#93;</span>	<span style="color: #adadad; font-style: italic;">;Entry point jump</span>
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">So, once again we solve the entry point location before the other pieces of the puzzle. Normally the first of our points of interest is import table processing. We find that part of the file by scrolling through the code and looking for the API call combination that utilizes GetProcAddress and LoadLibrary/GetModuleHandle. Since functions can be imported by name or ordinal number instruction TEST which does a logical compare with 0x80000000 is also a big clue on where this code is located. Most packers check for ordinal imports this way and this code part usually stands out on its own identifying the import processing part.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span>+<span style="color: #DEE002;">C</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;Part I - Loading new library</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E44<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">CALL</span> LoadLibraryA
...
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">80000000</span>            <span style="color: #adadad; font-style: italic;">;Part II - Is API ordinal?</span>
  <span style="color: #EE4A02;">JE</span> <span style="color: #DEE002;">SHORT</span> 004074A0
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,0FFFF
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>ESP<span style="color: #ff0000;">+18</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">AX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>ESP<span style="color: #ff0000;">+18</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> GetProcAddress          <span style="color: #adadad; font-style: italic;">;Find function via ordinal</span>
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
...
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E44<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">2</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESP</span>+1C<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>ESP<span style="color: #ff0000;">+18</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> GetProcAddress          <span style="color: #adadad; font-style: italic;">;Find function via name</span>
...
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>   <span style="color: #adadad; font-style: italic;">;Part III - Write function pointer</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #ff0000;">4</span>
  <span style="color: #EE4A02;">JMP</span> <span style="color: #DEE002;">SHORT</span> <span style="color: #ff0000;">00407469</span></pre>
</blockquote>
<p style="text-align: justify;">As we can see this code is segmented inside the function that processes imports. All three parts of this code have their role. First part load the necessary libraries, second one finds the functions inside the loaded libraries and the third writes the found API pointers to the import address table. Three breakpoints are needed in order to collect this data. One at the library loading part and two at function finding part. We need two breakpoints at the function finding part because only one of two GetProcAddress calls gets executed depending on whether the function is imported by ordinal or not. Similarly to this we have the following relocation code:</p>
<blockquote>
<pre class="asm">L000:
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">BP</span>,0F000
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #ff0000;">3000</span>
  <span style="color: #EE4A02;">JNZ</span> L010
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E44<span style="color: #FFFFFF;">&#93;</span>
L010:
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">BP</span>,0F000
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #ff0000;">1000</span>
  <span style="color: #EE4A02;">JNZ</span> L022
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">SHR</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #ff0000;">10</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">DX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E44<span style="color: #FFFFFF;">&#93;</span>
L022:
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">BP</span>,0F000
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #ff0000;">2000</span>
  <span style="color: #EE4A02;">JNZ</span> L032
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">SI</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>407E44<span style="color: #FFFFFF;">&#93;</span>
L032:
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">DEC</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>ESP<span style="color: #ff0000;">+10</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">JNZ</span> L000</pre>
</blockquote>
<p style="text-align: justify;">Yet again this is only the part of a really long code which can be easily identified. Test or compares with value 0x3000 indicates a 32 bit relocation is always a good clue, and if such test is a part of a loop there is a good chance that that code is a part of a relocation to new base function. We make two snapshots that fix relocations with ease. One at the beginning of this function and the other at the end of the same function. Memory to be snapshot is always the entire memory minus the packer section, which is in all cases from virtual address of the first section to virtual address of the last one. Since we already know where the entry point jump is this is the last piece of the puzzle needed to complete our unpacker.</p>
<p style="text-align: justify;">Writing an unpacker for nPack should be an easy task since there are just a few things to look out for. If you had no trouble writing an unpacker for <a href="http://blog.reversinglabs.com/2009/11/get-ready-for-packman/" target="_blank">PackMan</a> you shouldn't have a problem with this one. As always unpacker, source code and the samples are included with the blog. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/11/RLdenPack.zip">RL!denPack</a><span><br />
(package contains unpacker binary, source and samples used)</p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F11%2Fnpack-unpacker%2F&amp;title=From%20Russia%20with%20Love%2C%20nPack%20story" id="wpa2a_12"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/11/npack-unpacker/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Seek PackMan and press play on tape!</title>
		<link>http://blog.reversinglabs.com/2009/11/get-ready-for-packman/</link>
		<comments>http://blog.reversinglabs.com/2009/11/get-ready-for-packman/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 13:25:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[PackMan]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=134</guid>
		<description><![CDATA[After a few weeks we return to building unpackers with an interesting packer called Packman. Even though this is a pretty straight forward packer there are a few details that make us learn a trick or two while working on this unpacker. Most interesting detail about how one could find a loaded base for the [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">After a few weeks we return to building unpackers with an interesting packer called <a href="http://packman.cjb.net/" target="_blank">Packman</a>. Even though this is a pretty straight forward packer there are a few details that make us learn a trick or two while working on this unpacker. Most interesting detail about how one could find a loaded base for the module with just some simple math waits for us in the first few instructions, here:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSHAD</span>
  <span style="color: #EE4A02;">CALL</span> L002
L002:
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EBX-3A<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span></pre>
</blockquote>
<p style="text-align: justify;">That's is? This code can find on which base loaded module was loaded? It can and here is why. Once the first call and POP EBX execute EBX will be equal to address on which EBX is located. That doesn't get us closer to our loaded base address but next two instructions do. Once LEA executes EBX will be pointing to data inside the same section where the entry point resides. Data at that pointer is 0xFFFF8A30 which is equal to 0x004075D0 - 0x00400000 which is the EBX data minus the default image base. Since math for this is <em>EBX - ImageBase = Delta</em> reversing this would be <em>EBX + Delta = ImageBase</em>. And from this it is simple to figure out that as long as the EBX changes and delta remains the same with this simple math formula we can always calculate the loaded base of the file. Quite a neat trick.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,0E9
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span>+<span style="color: #DEE002;">C</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;0xFFFF9CB7</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>ESI<span style="color: #ff0000;">+1</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span></pre>
</blockquote>
<p style="text-align: justify;">What comes next is also interesting and very simple. Code above is a dynamic entry point jump generation. Since at that point ESI always point to the address of the packed entry point data that will be written to that address changes its code content. New instruction to be written instead of fist PUSHAD is a jump to the entry point. Since the relative distance between packed and original entry point will never change there is no need to recalculate this jump and we can always write the same data for that jump regardless of the file loaded base. This works of course because jumps are relative to their location. However in order  to get this location we can do several things:</p>
<ul>
<li>Set a breakpoint on the JUMP to packed entry point right after POPAD and single step twice to get to the entry point</li>
<li>Place a breakpoint at the packed entry point at some point and wait for it to hit and then single step to get to entry point</li>
<li>Read newly created entry point jump (or data used to write it) and recalculate the entry point jump placing a hardware breakpoint there</li>
</ul>
<p style="text-align: justify;">Any of the solutions above is a good choice and you can use any of those to place the final entry point breakpoint. But we are getting ahead of ourselves since we still need to work out relocations and imports before we even get to the entry point.</p>
<p style="text-align: justify;">First thing is first, imports. And so this code blob does everything we need to know about import handling in PackMan.</p>
<blockquote>
<pre class="asm">L000:
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;GetModuleHandleA</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">JMP</span> L017
L006:
  <span style="color: #EE4A02;">BTR</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,1F
  <span style="color: #EE4A02;">JB</span> L011
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
L011:
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+4</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;GetProcAddress</span>
  <span style="color: #EE4A02;">STOS</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">ES</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
L017:
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">JNZ</span> L006
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #ff0000;">10</span>
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">JNZ</span> L000</pre>
</blockquote>
<p style="text-align: justify;">One thing to notice is that PackMan uses GetModuleHandleA to get the base of the loaded DLL file. This is because it doesn't load libraries by itself, instead it lets Windows do the loading part and it just fills in the import address table with the correct API pointers. Its easy to place two breakpoints on these function calls and grab the data we need to fill in the imports correctly. Moving on to relocations.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span>+2C<span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;First snapshot</span>
  <span style="color: #EE4A02;">OR</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">JE</span> <span style="color: #DEE002;">SHORT</span> L015
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EBX<span style="color: #ff0000;">+24</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">JMP</span> L014
L005:
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">WORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">OR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">JE</span> L012
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">AH</span>,0F
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDX</span>+<span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
L012:
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">JNZ</span> L005
L014:
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+8</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<span style="color: #ff0000;">+4</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">JNZ</span> L012
L015:
  <span style="color: #EE4A02;">POPAD</span> <span style="color: #adadad; font-style: italic;">;Second snapshot</span></pre>
</blockquote>
<p style="text-align: justify;">This code blob relocates the file to newly loaded base. As always we can make two snapshots that fix relocations automatically. Question is which memory segment do we snapshot? Since Packman has two memory forms we apply a solution that works for both. Packman can have two or more PE sections with the packer code in the last section. So the memory to snapshot is always the entire memory minus the packer section, which is in all cases from virtual address of the first section to virtual address of the last one. Comparing those two snapshots fixes the relocation table with ease.</p>
<p style="text-align: justify;">Writing an unpacker for PackMan should be an easy task since there are just a few things to look out for. If you had no trouble writing an unpacker for <a href="http://upx.sourceforge.net/" target="_self">UPX</a> you shouldn't have a problem with this one. As always unpacker, source code and the samples are included with the blog. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/11/RLdePackMan.zip">RL!dePackMan 1.x</a><span><br />
(package contains unpacker binary, source and samples used)</p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F11%2Fget-ready-for-packman%2F&amp;title=Seek%20PackMan%20and%20press%20play%20on%20tape%21" id="wpa2a_14"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/11/get-ready-for-packman/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cloudy day with exeFog</title>
		<link>http://blog.reversinglabs.com/2009/10/cloudy-day-with-exefog/</link>
		<comments>http://blog.reversinglabs.com/2009/10/cloudy-day-with-exefog/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 10:53:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[exeFog]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=90</guid>
		<description><![CDATA[After one week detour to reversing tool coding field we return to what TitanEngine does best which making unpackers of course. This week we take a peek into what hides in the fog, exeFog. Being a relatively simple shell modifier it must be classified as a protector. I know that most of you wold disagree [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">After one week detour to reversing tool coding field we return to what <em>TitanEngine </em>does best which making unpackers of course. This week we take a peek into what hides in the fog, exeFog.</p>
<p style="text-align: justify;">Being a relatively simple shell modifier it must be classified as a protector. I know that most of you wold disagree because this shell modifier is a simple one but it has all the characteristics of an protector so we have no choice but to call it one. What does it have? It compresses the PE file with <a href="http://www.ibsensoftware.com/products_aPLib.html" target="_blank">aplib</a> algorithm, encrypts the file, processes the import table and makes use of simple anti debugging features. Maybe advanced crypter or just a packer?</p>
<p style="text-align: justify;">Can't wait to have some fun? Me neither. Lets load up some samples and make them sing our tune.</p>
<p style="text-align: justify;">Entry point looks like there is a loader decryption going on but its all an optical illusion. Take a closer look at:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
L001:
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">CL</span>
  <span style="color: #EE4A02;">ROR</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,<span style="color: #ff0000;">80</span>
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">NOP</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">JB</span> L009
  <span style="color: #EE4A02;">JNS</span> L009
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #ff0000;">0</span>
L009:
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">SI</span>,6C
  <span style="color: #EE4A02;">CLC</span>
  <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">STC</span>
  <span style="color: #EE4A02;">CLD</span>
  <span style="color: #EE4A02;">CMC</span>
  <span style="color: #EE4A02;">LOOPD</span> L001</pre>
</blockquote>
<p style="text-align: justify;">It doesn't do anything except execute 0xFF times trying to confuse us. Or possibly break an emulator or two? Never the less we trace some more until we find first and the only stub decryption code.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,00406E57
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,3C8
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,0A0
L003:
  <span style="color: #EE4A02;">XOR</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span>+<span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">AL</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span>+<span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">LOOPD</span> L003</pre>
</blockquote>
<p style="text-align: justify;">We simply place a hardware breakpoint on execution for the next "call" instruction and hit run. After this has been executed we see that our "call" was indeed a CALL but after it has been decrypted it lead to a more plausible location. What comes next is an interesting approach to an old anti debugging trick. Here we have to pay close attention to what is going on:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #ff0000;">10</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,77FFFFFF
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">FS</span>:<span style="color: #FFFFFF;">&#91;</span>EBX<span style="color: #ff0000;">+88000019</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;TEB</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span>+<span style="color: #EE1802; font-weight:bold;">ECX</span>*<span style="color: #ff0000;">2</span><span style="color: #ff0000;">+10</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;PEB</span>
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EAX<span style="color: #ff0000;">+2</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;BeingDebugged</span>
  <span style="color: #EE4A02;">NOT</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">1</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> 0C3FBF6
  <span style="color: #EE4A02;">CALL</span> L011
L011:
  <span style="color: #EE4A02;">SUB</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESP</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">33</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">ESP</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #ff0000;">4</span>
  <span style="color: #EE4A02;">JMP</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">ESI</span> <span style="color: #adadad; font-style: italic;">;Crash the debugger (safely)</span></pre>
</blockquote>
<p style="text-align: justify;">This is a simple PEB.BeingDebugged trick but if you trace the code and your debugger isn't hidden from this you will end up executing JMP ESI which leads to IDIV BL instruction. Why is this bad? Because if the program is being debugged processor will need to divide AL with BL which would be fine if they weren't set to NULL. In math we can handle such cases but processor doen't know how to do this and generates an exception which can't be passed making the target terminates itself. To skip this we can either hide our debugger with <a href="http://www.ollydbg.de/" target="_blank">OllyDBG</a> plugins or manually set EAX to NULL after the BeingDebugged line.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+1000</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #ff0000;">00407173</span> <span style="color: #adadad; font-style: italic;">; decompress aplib data</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,<span style="color: #ff0000;">8</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESP</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+1000</span><span style="color: #FFFFFF;">&#93;</span>
L012:
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDX</span>+<span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">; write decompressed data</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span>+<span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">AL</span>
  <span style="color: #EE4A02;">LOOPD</span> L012</pre>
</blockquote>
<p style="text-align: justify;">Further tracing leads us to this point where exeFog decompresses the main section and writes it to its original location. Following this is a code which frees up temporary memory and creates a unique mutex which ensure that only one copy of the protected file is running at the same time. This is an option set in exeFog before the file was protected, but that doesn't have an impact on what we are doing. However next code does:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,0040445C
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,3C
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #ff0000;">1B</span>
L003:
  <span style="color: #EE4A02;">XOR</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBX</span>+<span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">AL</span>
  <span style="color: #EE4A02;">LOOPD</span> L003</pre>
</blockquote>
<p style="text-align: justify;">What is interesting about this code is that it only decrypts a small piece of memory, and that piece of memory is the exact size of three IIDs. After the code is decrypted we take a look at the decrypted memory and find out that imports are  at their original location and that only the IIDs were encrypted meaning that we can just skip the import processing in our unpacker and read the data from the decryptor above in order to set the import table address and size. But for exercise purposes we decided to use our importer module and make this a full dynamic unpacker with the full use of importer module. Code that exeFog uses to process the import table is located just below this decryptor and you can easily analyze it to figure out where to set the breakpoints.</p>
<p style="text-align: justify;">Which only leaves the last piece of the puzzle, and that is always... How does it jump to entry point (and is it protected)?</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> 12C0 <span style="color: #adadad; font-style: italic;">;Entry point relative virtual address</span>
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">SI</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">NOP</span>
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">DL</span>,<span style="color: #ff0000;">0</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESP</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EBP</span>
  <span style="color: #EE4A02;">RET</span> <span style="color: #adadad; font-style: italic;">; Jump to OEP</span></pre>
</blockquote>
<p style="text-align: justify;">Just after the code for import filling exeFog store the code above which is in-charge of jumping to the entry point. As you can see the PUSH instruction hold the relative virtual address of the entry point, and once the last RET instruction is executed exeFog passes control to the original entry point and continues the execution of protected file.</p>
<p style="text-align: justify;">Writing an unpacker for exeFog should be an easy task since there are just a few things to look out for. As always unpacker, source code and the samples are included with the blog. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/10/RLdeExeFog.rar">RL!deExeFog</a><br />
<span>(package contains unpacker binary, source and samples used)</p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F10%2Fcloudy-day-with-exefog%2F&amp;title=Cloudy%20day%20with%20exeFog" id="wpa2a_16"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/10/cloudy-day-with-exefog/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Buggy Monday, PeX story</title>
		<link>http://blog.reversinglabs.com/2009/09/buggy-monday-pex-story/</link>
		<comments>http://blog.reversinglabs.com/2009/09/buggy-monday-pex-story/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 16:43:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[PeX]]></category>
		<category><![CDATA[Unpacker]]></category>
		<category><![CDATA[Unpacking]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=61</guid>
		<description><![CDATA[Its a beautiful Monday once again. What is special about this Monday is that it has its rather long introduction story. Here is what you don't know about ReversingLabs. At the end of each week we go through preparation for Monday blog. So the team decides and creates a sample code for our blog. This [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Its a beautiful Monday once again. What is special about this Monday is that it has its rather long introduction story. Here is what you don't know about ReversingLabs. At the end of each week we go through preparation for Monday blog. So the team decides and creates a sample code for our blog. This was also the case last week and we picked to do a blog about using <em>TitanEngine</em> as static library and creating a <a href="http://www.peid.info/" target="_blank">PeID</a> plugin for handling overlay. It a small plugin called <em>TitaniumOverlay </em>with options to add, remove, copy and extract overlay. But... in the last minute we found a rather obvious bug inside RemoveOverlay and decided not to use this in our blog. Then another team member decided to code an unpacker for <a href="http://packman.cjb.net/" target="_blank">PackMan</a> a simple PE32 packer which packs ExE and DLL files. Which seemed like a good idea until we found that with the current debugger design you can't SingleStep after hardware breakpoints. And that once again meant that this won't get on the blog. In another try to do a sample of using <em>TitanEngine </em>as a static library we made an <a href="http://www.ollydbg.de/">OllyDBG</a> plugin named <em>TitaniumHandles </em>which would display information about open handles and mutexes with the ability to close them. Which has proven useful on more then one occasion, and OllyDBG doen't have this as option so it seemed like a good idea... That is until we found a bug in handle filtering that prevented retrieving names of certain handles. Now its Sunday afternoon and we have three great samples on how you can use TitanEngine but since there are some bugs in the engine we can't use this code yet. So what do we do (after we fix them obviously)?</p>
<p style="text-align: justify;">We open our known formats list, place our hand over the eyes and pray that our next pick wont be an <a href="http://failblog.org/" target="_blank">epic fail</a>... And the pick is... <a href="http://www.openrce.org/reference_library/packer_database_view/14" target="_blank">PeX 0.99</a>. Nice and easy, it should be fun to do. Gentleman, start your debuggers.</p>
<p style="text-align: justify;">Once we start the analysis we find that the entry point has a nice message for us telling us that the sample is packed with PeX. How nice of bart^xt, the author of the packer. This can be used as a part of the signature for this format. After some tracing we find this code:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,<span style="color: #ff0000;">4</span> <span style="color: #adadad; font-style: italic;">; [EntryPoint + 0x254]</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">CS</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">RETF</span></pre>
</blockquote>
<p style="text-align: justify;">Which once executed leads us to level two of the packer. Formats who use multiple packing levels usually use them to process PE format specifics, such import table processing and file memory protection. In this case packer shell uses this level to fill and protect the import table. So by some more tracing we find:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span> <span style="color: #adadad; font-style: italic;">; kernel32.LoadLibraryA [Level2 + 0xF0]</span>
  <span style="color: #EE4A02;">TEST</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">JE</span> Level2 + 0x3BF</pre>
</blockquote>
<p style="text-align: justify;">Simple call the <em>LoadLibraryA</em> API call to load all needed libraries. Data we need is located as a pointer to string in EBX register which holds the name of DLL to be loaded. Some more tracing leads us to this point in code:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,<span style="color: #ff0000;">4</span> <span style="color: #adadad; font-style: italic;">; [Level2 + 0x1DF]</span>
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,7FFFFFFF
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+402A18<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CALL</span> +0x01	<span style="color: #adadad; font-style: italic;">;Redirected GetProcAddress call</span></pre>
</blockquote>
<p style="text-align: justify;">This is a call to <em>GetProcAddress </em>API which locates the functions inside the previously loaded DLL file. Parameters we need from here are EBX which is either API ordinal number or a pointer to string which is the name of the function to be found in loaded DLL, and EDI which holds the pointer to IAT. More specifically the place where the API pointer will be written. Normally that would be it for import fixing but PeX has a simple redirection code we must disable before continuing the unpacking process. That code is located here:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4025CB<span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">; [Level2 + 0x299]</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ECX</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0B</span><span style="color: #ff0000;">13</span>
  <span style="color: #EE4A02;">JNB</span> OverCodeThatProtectsTheImportTable
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span></pre>
</blockquote>
<p style="text-align: justify;">By simple patching we change <em>JNB </em>jump from above to <em>JMP </em>and ensure that import table protection code isn't executed. Last thing we have to find is the entry point location and set a breakpoint to dump and fix the file on it. We look a little bit down the code and find this:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> 4012BF <span style="color: #adadad; font-style: italic;">; [Level2 + 0x396]</span>
  <span style="color: #EE4A02;">JMP</span> L003
  ???
L003:
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">Code that will be written to packed entry point and used as a redirection to original entry point. We read this address (0x004012BF) and add one to it to get the original entry point address. Breakpoint we chose to set is a hardware breakpoint on execution, but we could have used a normal breakpoint as well since code at that address has been unpacked and written just before level switching. Once that breakpoint is hit our unpacker will finalize the unpacking in the well known manner.</p>
<p style="text-align: justify;">Since we were (and are) low on Delphi samples, this weeks unpacker was written for that programming language. Also note that all bugs that affected the three candidates for Monday blog were fixed and will be published very soon with the next <em>TitanEngine </em>release. In the mean time keep checking back at our blog and <a href="http://forum.reversinglabs.com" target="_blank">forums</a> for more info about the continuous <em>TitanEngine </em>development.</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/09/RLdePeX.rar">Download RL!dePeX unpacker</a><br />
<span>(package contains unpacker binary, source and samples used)</span></p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F09%2Fbuggy-monday-pex-story%2F&amp;title=Buggy%20Monday%2C%20PeX%20story" id="wpa2a_18"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/09/buggy-monday-pex-story/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Static unpackers, MEW5 story</title>
		<link>http://blog.reversinglabs.com/2009/09/static-unpackers-mew5-story/</link>
		<comments>http://blog.reversinglabs.com/2009/09/static-unpackers-mew5-story/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 05:59:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[MEW]]></category>
		<category><![CDATA[Unpacker]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=54</guid>
		<description><![CDATA[This is our last blog about MEW, we promise. We intend to keep that promise since this is the last known version of MEW. Whats so special about MEW anyway? The reason we chose to do MEW (again) is that in its version 5 it is a simple crypter which can be used as a [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">This is our last blog about MEW, we promise. We intend to keep that promise since this is the last known version of MEW. Whats so special about MEW anyway?</p>
<p style="text-align: justify;">The reason we chose to do MEW (<em>again</em>) is that in its version 5 it is a simple crypter which can be used as a perfect example on how to write static unpackers for these kind of crypters. That kind would be the kind that doesn't do anything to imports but only encrypts the executable code section. Next time we revisit static unpackers we will be talking about such cases. We are going to leave that aside for now because this Monday is all about simple and fast unpacker writing. Start your timers we will do in under 10 minutes.</p>
<p style="text-align: justify;"><strong>Minute 1 - 2:</strong></p>
<p style="text-align: justify;">We load our sample into Olly and see the entire MEW5 code at the entry point.</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,0040005B
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">XCHG</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">XCHG</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">XCHG</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
L010:
  <span style="color: #EE4A02;">LODS</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">ROL</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #ff0000;">29</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,BA
  <span style="color: #EE4A02;">ROR</span> <span style="color: #EE1802; font-weight:bold;">AL</span>,<span style="color: #ff0000;">50</span>
  <span style="color: #EE4A02;">STOS</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">ES</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">LOOPD</span> L010
  <span style="color: #EE4A02;">RET</span></pre>
</blockquote>
<p style="text-align: justify;">Yes, that it. The whole code. So what does it do? First it loads a pointer to all internal information into ESI register. We follow ESI in the hex dump to find that there are 3 DWORDs that have the data necessary for unpacking. First DWORD is 0x3000 which is the size of the first section, second DWORD is 0x004012c0 which is the address of the original entry point and third DWORD is 0x00401000 which is the virtual address of the first section. Code following this loads 0x3000 bytes one by one and decrypts them with a custom decryption algorithm. Here instruction sequence ROL, ADD and ROR is used to decrypt data.</p>
<p style="text-align: justify;"><strong>Minute 3 - 4:</strong></p>
<p style="text-align: justify;">We make a copy of existing <em>TitanEngine </em>SDK sample for DEF and use that as a template for our unpacker. We are making a Delphi unpacker since,... well since TitanEngine is low on Delphi samples and this is a nice and quick exercise.</p>
<p style="text-align: justify;"><strong>Minute 5 - 9:</strong></p>
<p style="text-align: justify;">We code the unpacker. First we need to read the ESI pointer and read the data from the file. Once we make that we convert third DWORD to physical address inside mapped file and we also convert the original entry point address to relative one. Simple call to <em>StaticMemoryDecryptEx </em>makes sure that our <em>StaticCallBack</em> decrypts the data by executing this custom decryption algorithm. Lastly we add the code to store the new entry point to PE32 header and we're done.</p>
<p style="text-align: justify;"><strong>Minute 9:47 - 10:</strong></p>
<p>We run the compiled unpacker to test if it works... Success!<br />
<!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/09/RLdeMEW5.rar">Download RL!deMEW5 unpacker</a><br />
<span>(package contains unpacker binary, source and samples used)</span></p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F09%2Fstatic-unpackers-mew5-story%2F&amp;title=Static%20unpackers%2C%20MEW5%20story" id="wpa2a_20"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/09/static-unpackers-mew5-story/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

