<?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; Unpacker</title>
	<atom:link href="http://blog.reversinglabs.com/tag/unpacker/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>Everything in one go</title>
		<link>http://blog.reversinglabs.com/2010/07/everything-in-one-go/</link>
		<comments>http://blog.reversinglabs.com/2010/07/everything-in-one-go/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 10:37:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[Unpacker]]></category>
		<category><![CDATA[UPX]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=710</guid>
		<description><![CDATA[When talking about new concepts, its always best to demonstrate them on something everyone is familiar with. In our case that's of-course UPX with which we are fairly familiar. It almost feels like we write one UPX unpacker each week, doesn't it? Today we are presenting an optimization concept that enables us to unpack everything [...]]]></description>
			<content:encoded><![CDATA[<div style="text-align: center;"><a href="http://www.youtube.com/watch?v=g_dQ1xp7AfE"><img src="http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/inc/img.php?v=g_dQ1xp7AfE"></a></div>
<p style="text-align: justify;">When talking about new concepts, its always best to demonstrate them on something everyone is familiar with. In our case that's of-course <a href="http://upx.sourceforge.net/" target="_blank">UPX</a> with which we are fairly familiar. It almost feels like we write one UPX unpacker each week, doesn't it?</p>
<p style="text-align: justify;">Today we are presenting an optimization concept that enables us to unpack everything in a single go. Now, when talking about file unpacking we always unpack everything in one go, but we never unpack both the main executable module and all of its packed dependencies in a single run. Normally, you wold do this by batching through individual files.  But from a speed perspective, the best optimization imaginable comes from unpacking the main module and all of its dependencies at once. Since <em>TitanEngine </em>wasn't really designed to do that out-of-the-box, it needs just a little bit of help to pull it off.</p>
<p style="text-align: justify;">The problem is the existence of multiple relocation tables, and more importantly multiple import tables. Since TitanEngine was designed to unpack files one at the time, we must do some additional coding around these boundaries to achieve our goal. Compared to a traditional TitanEngine dynamic unpacker, the only difference is the need to collect import table data for modules in one place, and use that data for any module that has reached its entry point jump. The UPX is a special case because it always imports packed file dependencies through the import table. This is, of course, a static way of importing libraries but our approach must be flexible enough to cover both dynamic and static importing.</p>
<p style="text-align: justify;">To achieve our goal we have to scan the main module and all loaded libraries and try to find  the appropriate patterns. Once the patterns are found, we set breakpoints and store info about them so we know which module triggered which callback event. Normally we have three callbacks for UPX unpackers (LoadLibrary, GetProcAddress and EP jump) but since we are doing transverse unpacking we need one more: the load library event custom handler, which determines whether the loaded dependencies are packed with UPX by trying to find the neccessary breakpoint patterns. Even though it is impossible to have more than one module loading at a time, we still need to store the import data because the import tables for the main executable and dependencies might overlap if the modules are loaded dynamically. Once stored, the import info for each module is retrieved when it hits its entry point callback. Relocations aren't really a problem since there is just one module loading at a time, so we can use our "snapshot and compare" model, provided that modules load on non-default image bases. This can be done in numerous ways - one of the easiest is to compile the sample files so that they do that by default (which is considered cheating in the unpacking game), alternatively, we can pre-allocate the memory so that the modules have no choice but to pick another base address. For the purpose of this blog we cheated, in a real world application of this approach you mustn't.</p>
<p style="text-align: justify;">In the real world you will hardly ever see this kind of case but if you do, you now know how to get everything in one go. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table border="0" cellspacing="0" cellpadding="0" width="600" align="center">
<tbody>
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;amp; 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;amp; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle"><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/07/RL!deUPX_oneGo.rar">RL!deUPX</a><br />
(package contains the unpacker with source and the samples  used)</td>
</tr>
</tbody>
</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%2F07%2Feverything-in-one-go%2F&amp;title=Everything%20in%20one%20go" 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/07/everything-in-one-go/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unpacking by hooking?</title>
		<link>http://blog.reversinglabs.com/2010/06/unpacking-by-hooking/</link>
		<comments>http://blog.reversinglabs.com/2010/06/unpacking-by-hooking/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 13:31:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[Hooks]]></category>
		<category><![CDATA[Unpacker]]></category>
		<category><![CDATA[UPX]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=676</guid>
		<description><![CDATA[Lets try something totally crazy. Lets try dynamic unpacking without total unpacking control, without breakpoints, without any kind of debugging whatsoever. Lets merge our unpacking process with the packer itself, binding them into one unique work-flow that collects information while the packer is executing. It's similar to what we do with debugging - just without [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Lets try something totally crazy. Lets try dynamic unpacking without total unpacking control, without breakpoints, without any kind of debugging whatsoever. Lets merge our unpacking process with the packer itself, binding them into one unique work-flow that collects information while the packer is executing. It's similar to what we do with debugging - just without the debugger. How do we do this? Can we for that matter?</p>
<p style="text-align: justify;">We can, with a little help from TitanEngine's hooking library. The idea is to use our unpacker as a library which will be injected into the packed file during its execution. Such a library would place hooks inside the packer code, redirecting the control flow to our unpacker wherever data collection or execution handling is needed. Those places are usually spots where the packer processes the import table or relocations, jumps to the original entry point, or just switches execution from one layer to another.</p>
<p style="text-align: justify;">What are the benefits of such an approach? Even though its <em>slightly</em> harder to create and test such unpackers, the most notable benefit of unpacking by hooking is total immunity to various anti-debugging tricks used to detect the unpacking process. The only detection applicable to this unpacking scenario is anti-hooking and memory checksumming. The first is hardly ever used in modern protections due to the large number of false positives it gives, which are triggered by the operating system itself, security software and various window skinning applications. The second one is rarely present, and when it is it only covers specific memory regions that correspond to a single protection layer. In conclusion this method of implementing the unpacking process should result in fewer things to worry about.</p>
<p style="text-align: justify;">Implementing this kind of hooking requires building custom functions to process the hook events. This is necessary to maintain the packed program work flow, and is exactly why we preserve the register state with PUSHAD, and if there is a jump affected by our hook, even EFLAGS with PUSHFD. These ASM instructions are embedded in our C code and with the help of naked pre-processor instruction they become the prologue and epilogue of the function. To apply the hooks we use the DLL_PROCESS_ATTACH event. For example if we were to hook the UPX code which loads libraries the hook code flow would look like this:</p>
<p style="text-align: justify;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/06/hooking.png" rel="lightbox[676]"><img class="aligncenter size-full wp-image-597" title="Hook Flow" src="http://blog.reversinglabs.com/wp-content/uploads/2010/06/hooking.png" alt="" width="627" height="247" /></a>Since our hooks are 5 bytes we need to "borrow" as many instructions as we need to insert the hook. In this case we are "borrowing" three instructions. These instructions will be executed right after our inserted function is called. This is done to preserve the packer work flow. As you can see from this diagram we are using hooks instead of breakpoints. Therefore these hooks will be placed on at least three places: when UPX calls LoadLibraryA, GetProcAddress and finally once it jumps to the entry point. The most basic sample UPX unpacker is limited to working on executables that don't import functions by ordinals and use the old jump to entry point method. It's quite limited, but it's enough for a proof-of-concept of our technique.</p>
<p style="text-align: justify;">Debugging this kind of unpacker can be rather tricky. This video shows a quick and easy way to do it:</p>
<div style="text-align: center;"><a href="http://www.youtube.com/watch?v=Sub3huN18qI"><img src="http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/inc/img.php?v=Sub3huN18qI"></a></div>
<p style="text-align: justify;">Since we are creating a hook library unpacker, we also need a loader which will execute the unpacking target and inject the unpacker library in it. This can be done in number of ways but we decided to do it via the debug - detach method. Once both the unpacker hook library and the loader are made, our unpacker is complete. We hope you got the idea on how to use this technique to build your own hooking unpackers from our short blog. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table border="0" cellspacing="0" cellpadding="0" width="600" align="center">
<tbody>
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;amp; 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;amp; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle"><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/06/upxHooks.zip">upxHooks</a><br />
(package contains the unpacker with source and the samples  used)</td>
</tr>
</tbody>
</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%2F06%2Funpacking-by-hooking%2F&amp;title=Unpacking%20by%20hooking%3F" 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/06/unpacking-by-hooking/feed/</wfw:commentRss>
		<slash:comments>5</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_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/05/back-to-the-basics/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unpacking archives with TitanEngine</title>
		<link>http://blog.reversinglabs.com/2010/04/unpacking-archives-with-titanengine/</link>
		<comments>http://blog.reversinglabs.com/2010/04/unpacking-archives-with-titanengine/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 15:01:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[BlackHat]]></category>
		<category><![CDATA[DEB]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=471</guid>
		<description><![CDATA[TitanEngine is primarily envisioned as a portable executable file format unpacker and handling framework. However due to its static unpacking functions it can be used to unpack other file format types such as installers and archives. That is why today we are showing the utilization of the new static unpacking functions that will be available [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><em>TitanEngine </em>is primarily envisioned as a portable executable file format unpacker and handling framework. However due to its static unpacking functions it can be used to unpack other file format types such as installers and archives. That is why today we are showing the utilization of the new static unpacking functions that will be available with the next update. We are discussing the upcoming features which is something we generally like to avoid but it is for a good reason. It is only because of the unveiling of the new SDK we have secretly been working on during these last few months that we are even touching the archive unpacking subject. What is it and what does it do will be talked about on <a href="http://blackhat.com/html/bh-eu-10/bh-eu-10-briefings.html#Vuksan" target="_blank">BlackHat Europe</a> next week. Until then we will tickle your imagination with an unusual blog about unpacking archives with TitanEngine.</p>
<p style="text-align: justify;">Format we have selected is a simple a <a href="http://www.debian.org/" target="_blank">Debian</a> archive file format called <a href="http://en.wikipedia.org/wiki/Deb_%28file_format%29" target="_blank">Deb</a>. Debian packages (DEB files) are standard Unix <a href="http://en.wikipedia.org/wiki/Ar_%28Unix%29" target="_blank">ar archives</a> that include two gzipped, bzipped or lzmaed <a href="http://en.wikipedia.org/wiki/Tar_%28file_format%29" target="_blank">tar</a> archives: one that holds the control information and another that contains the data. These two files present in the archive are not compressed but instead they are just stored inside the binary package. Each stored item has its own header, which is defined like this:</p>
<blockquote>
<pre class="cpp"><span style="">typedef</span> <span style="">struct</span> DEB_HEADER<span style="color: #FFFFFF;">&#123;</span>
    <span style="">char</span> FileName<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">16</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> FileTime<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">12</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> Reserved0<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> Reserved1<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> Mode<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> ItemSize<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> TerminateQuote;
    <span style="">char</span> TerminateNewLine;
<span style="color: #FFFFFF;">&#125;</span>DEB_HEADER, *PDEB_HEADER;</pre>
</blockquote>
<p style="text-align: justify;">Preceding the first header which is used to describe the archive is the magic string "!&lt;arch&gt;\n" which is used to identify the binary package type. Therefore unpacking the DEB archive format is essentially reading the archive header and copying the binary content that follows it to the selected folder. Header for each binary content contains the file name and time information which can be used during the unpacking process to restore the packed item to its pre-packing state. Because this file format doesn't employ any compression by itself unpacking the DEB format only refers to extraction of the stored binary content. That content is additionally packed but with a different file format which commonly uses compression to reduce the size of the packed file on disk.</p>
<p style="text-align: justify;">This is just one of the many uses for TitanEngine outside the area of unpacking and processing portable executable file format. As we have seen unpacking archives with TitanEngine is quite possible as long as there is no compression or content decompression is supported by the engine. Keep an eye out for our blog next week when we unveil our super secret project. Until then...</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/04/DebUnpacker.zip">DEB unpacker</a><br />
(package contains source code, binary unpacker and a sample archive)</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%2F04%2Funpacking-archives-with-titanengine%2F&amp;title=Unpacking%20archives%20with%20TitanEngine" 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/2010/04/unpacking-archives-with-titanengine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TitanEngine 2.0.3 and GUI for unpackers</title>
		<link>http://blog.reversinglabs.com/2010/01/titanengine-gui-unpackers/</link>
		<comments>http://blog.reversinglabs.com/2010/01/titanengine-gui-unpackers/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 12:58:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=241</guid>
		<description><![CDATA[In our previous blog we have shown a short video that demonstrates the usage of new LUA SDK. Since then we decided that console unpackers are very boring to we included a new function in the TitanEngine which enables creation of a simple unpacker GUI that makes your script unpackers a little bit more user [...]]]></description>
			<content:encoded><![CDATA[<div style="text-align: center;"><a href="http://www.youtube.com/watch?v=TcKiRr_ItyQ"><img src="http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/inc/img.php?v=TcKiRr_ItyQ"></a></div>
<p style="text-align: justify;">
<p style="text-align: justify;">In our previous blog we have shown a short video that demonstrates the usage of new <a href="http://www.lua.org/" target="_blank">LUA</a> SDK. Since then we decided that console unpackers are very boring to we included a new function in the <em>TitanEngine </em>which enables creation of a simple unpacker GUI that makes your script unpackers a little bit more user friendly. With this <a href="http://www.youtube.com">youtube</a> video we welcome you to 2010. ReversingLabs will be back on Monday with more reverse engineering stories just for you. Catch us next time....</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%2Ftitanengine-gui-unpackers%2F&amp;title=TitanEngine%202.0.3%20and%20GUI%20for%20unpackers" 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/2010/01/titanengine-gui-unpackers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing static decompressors, aPLib story</title>
		<link>http://blog.reversinglabs.com/2009/12/static-aplib-decompression/</link>
		<comments>http://blog.reversinglabs.com/2009/12/static-aplib-decompression/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 14:39:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[aPLib]]></category>
		<category><![CDATA[Decompression]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=202</guid>
		<description><![CDATA[With the latest TitanEngine release, we introduced new functions which enable decompression of content packed with aPLib and LZMA. Today we will use those functions to make a static decompressor for AHPack. But before we do that we must answer a simple question: "What is the difference between regular static unpackers and static decompressors?" Simply [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">With the latest <em><a href="../2009/12/titanengine-202-update/" target="_blank">TitanEngine</a> </em>release, we introduced new functions which enable decompression of content packed with <a href="http://www.ibsensoftware.com/products_aPLib.html" target="_blank">aPLib</a> and <a href="http://www.7-zip.org/" target="_blank">LZMA</a>. Today we will use those functions to make a static decompressor for AHPack. But before we do that we must answer a simple question: "What is the difference between regular static unpackers and static decompressors?"</p>
<p style="text-align: justify;">Simply put, regular static unpackers are only used to unpack "simple" crypters which don't compress data in order to decrease the encrypted file size. In contrast, in the case where some data is compressed, unpacking must decompress that data, therefore we call such unpackers static decompressors. Static decompression can be used to unpack  both PE packers and installer formats since similar unpacking logic is used for both.</p>
<p style="text-align: justify;">The Unpacker we are making today will be a static decompressor, since AHPack uses <a href="http://www.ibsensoftware.com/products_aPLib.html" target="_blank">aPLib compression</a> to decrease the file size. Furthermore we are "killing two birds with one stone" since both AHPack and <a href="http://www.team-x.ru" target="_blank">!EPPack</a> are based on the same source code base and can be unpacked the same way. If you open any of the provided samples in OllyDBG you'll see the packed file entry point:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSHAD</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">00407054</span>		<span style="color: #adadad; font-style: italic;">;String: kernel32.dll</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #FFFFFF;">&#91;</span>KERNEL32.GetModuleHandleA<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: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">00407</span><span style="color: #ff0000;">0B</span><span style="color: #ff0000;">3</span>		<span style="color: #adadad; font-style: italic;">;String: GlobalAlloc</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: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #FFFFFF;">&#91;</span>KERNEL32.GetProcAddress<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: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">3000</span>		<span style="color: #adadad; font-style: italic;">;Virtual size of first section</span>
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">40</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</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;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>4070CA<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;">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: #ff0000;">00401000</span> 	<span style="color: #adadad; font-style: italic;">;Virtual offset of first section</span>
  <span style="color: #EE4A02;">PUSHAD</span>		<span style="color: #adadad; font-style: italic;">;aPLib decompression</span>
  <span style="color: #EE4A02;">CLD</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">DL</span>,<span style="color: #ff0000;">80</span>
  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">MOVS</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: #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;">POPAD</span>			<span style="color: #adadad; font-style: italic;">;aPLib decompression end</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,2FFC		<span style="color: #adadad; font-style: italic;">;copy decompressed data to first section</span>
L002:
  <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;">EAX</span>+<span style="color: #EE1802; font-weight:bold;">ECX</span><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>ECX<span style="color: #ff0000;">+401000</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">LOOPD</span> L002
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">This first part of the packer code quite clearly shows what the packer does. First it allocates a temporary memory buffer to store decompressed data, then decompresses the content of the first section to it. After the content is decompressed it is written to its original location, which, in this case, is first section's memory. The packer only compresses the first section since all compilers create PE files with a code section as the first file section. Resources, imports, relocations and TLS data isn't compressed, it is just realigned to new physical location after the size of first section decreases. In order to decompress the file we must do the following:</p>
<p style="text-align: justify;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/12/AHPackerLayout.png" rel="lightbox[202]"><img class="aligncenter size-full wp-image-210" title="AHPackerLayout" src="http://blog.reversinglabs.com/wp-content/uploads/2009/12/AHPackerLayout.png" alt="" width="432" height="435" /></a></p>
<ul>
<li>Decompress the content of the first section</li>
<li>Move the content of all other sections (including overlay) by the size needed to write decompressed content</li>
<li>Write decompressed data to first section and correct its physical size</li>
<li>Fix section data pointers to correctly point to the new section location for the remaining sections</li>
</ul>
<p style="text-align: justify;">After this we have to fix imports, correct the entry point address, and optionally delete the last section. We have already said the imports are not compressed, but that doesn't mean that this packer doesn't process imports. This code here does exactly that:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #ff0000;">00400000</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,445C <span style="color: #adadad; font-style: italic;">;Address of first IID</span>
  <span style="color: #EE4A02;">ADD</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: #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;">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> <span style="color: #ff0000;">00407277</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;">EBX</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;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #FFFFFF;">&#91;</span>KERNEL32.GetModuleHandleA<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: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>
...
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,0FFFFFFF
  <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;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>4070CE<span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #FFFFFF;">&#91;</span>KERNEL32.GetProcAddress<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: #EE1802; font-weight:bold;">EAX</span><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;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</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>4070D2<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">4</span>
  <span style="color: #EE4A02;">JMP</span> <span style="color: #DEE002;">SHORT</span> <span style="color: #ff0000;">00407218</span>
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #ff0000;">14</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #ff0000;">00400000</span>
  <span style="color: #EE4A02;">JMP</span> 004071E5
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">You can see that it's very simple code that just goes through the normal import table and fills its content. The data we need from here is address of the first IID, which will be used to find out the size of the import table or the number of IIDs present in the import table. Keep in mind that last IID will be empty, since that is the way import table is described in <a href="http://download.microsoft.com/download/e/b/a/eba1050f-a31d-436b-9281-92cdfeae4b45/pecoff.doc" target="_blank">PECOFF</a>. Since this table is valid we can use these two values to fix it. Simply by setting ImportTableAddress and ImportTableSize values in the PE header, we fix the import table in the unpacked file. Last thing we need to do is read the address of the entry point which can be found here:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>
  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">POPAD</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,004012C0 <span style="color: #adadad; font-style: italic;">;Address of entry point</span>
...
  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
  <span style="color: #EE4A02;">RET</span>
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">Writing an unpacker for AHPack  is fairly complex, since there are a few details to worry about. It provides an interesting challenge for any reverser and it shows the potential of TitanEngine's new static unpacking function. 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/12/RLdeAHPack.zip" target="_blank">RL!deAHPack</a><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%2F12%2Fstatic-aplib-decompression%2F&amp;title=Writing%20static%20decompressors%2C%20aPLib%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/12/static-aplib-decompression/feed/</wfw:commentRss>
		<slash:comments>0</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_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/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_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/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_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/11/get-ready-for-packman/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Static decryption in reverse</title>
		<link>http://blog.reversinglabs.com/2009/10/static-decryption-in-reverse/</link>
		<comments>http://blog.reversinglabs.com/2009/10/static-decryption-in-reverse/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 12:13:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[CryptoCrackPEProtector]]></category>
		<category><![CDATA[Decryption]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=98</guid>
		<description><![CDATA[For today's blog we had to do some minor engine modifications which is always fun. Even though we met these kinds of crypters before it completely slipped our mind that some crypters decrypt data in backward direction. That is why with CryptoCrackPEProtector we introduced new function for data decryption called StaticMemoryDecryptSpecial. Only thing special about [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">For today's blog we had to do some minor engine modifications which is always fun. Even though we met these kinds of crypters before it completely slipped our mind that some crypters decrypt data in backward direction. That is why with <a href="http://www.tuts4you.com/download.php?list.52" target="_blank">CryptoCrackPEProtector</a> we introduced new function for data decryption called <em>StaticMemoryDecryptSpecial</em>. Only thing special about it is that you can choose the direction of decryption and in later versions if it proves necessary byte skipping etc.</p>
<p style="text-align: justify;">What is interesting about this unpacker is that we decided to make a static unpacker <a href="http://ap0x.jezgra.net/unpackers.html" target="_blank">this time</a>. We decided this because of the bugs in the crypter itself which prevented it from executing on x64 systems. So, in order to work around the crypters bugs today we make a static unpacker for it. This means that our analysis will be focused on different things then when we are creating dynamic unpackers.</p>
<p style="text-align: justify;">First step when building static unpackers for crypters is to check if they have encrypted layers and if so how many. To find out this we just scroll through the code and look for sections of code that look like pure gibberish. Those sections are usually located just after the the decryption loops in-charge of their decryption. There could be several encryption layers following each other but usually for internal packer code decryption only one is used. Luckily for us this crypter doesn't have any. Entry point of the protected file looks like this:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">AND</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,FFFFFF00
L002:
  <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;">EBX</span><span style="color: #FFFFFF;">&#93;</span>,5A4D
  <span style="color: #EE4A02;">JNZ</span> L018
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">ADD</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;">EBX</span>+3C<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;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">4550</span>
  <span style="color: #EE4A02;">JNZ</span> L018
  <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>ESI<span style="color: #ff0000;">+18</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;">EAX</span>
  <span style="color: #EE4A02;">IMUL</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #ff0000;">0B</span>AD
  <span style="color: #EE4A02;">MUL</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>,4B415DAB
  <span style="color: #EE4A02;">IMUL</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>,0C0DE
  <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;">JNZ</span> L018
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,<span style="color: #ff0000;">4</span>
  <span style="color: #EE4A02;">JNZ</span> 0040711E
L018:
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #ff0000;">100</span>
  <span style="color: #EE4A02;">JNZ</span> L002
  <span style="color: #EE4A02;">NOP</span>
  <span style="color: #EE4A02;">JB</span> <span style="color: #DEE002;">SHORT</span> <span style="color: #ff0000;">00407</span><span style="color: #ff0000;">0B</span>C</pre>
</blockquote>
<p style="text-align: justify;">This code is in-charge of locating the base of kernel32.dll. One of the reasons this crypter doesn't work on x64 systems is precisely this code. After it has executed and the kernel32 base has been located crypter will execute this code:</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;">FS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">30</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;">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;">FS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #ff0000;">20</span><span style="color: #FFFFFF;">&#93;</span>
  <span style="color: #EE4A02;">JS</span> L005
  <span style="color: #EE4A02;">MOVZX</span> <span style="color: #EE1802; font-weight:bold;">ECX</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>
L005:
  <span style="color: #EE4A02;">JECXZ</span> <span style="color: #DEE002;">SHORT</span> protecte<span style="color: #ff0000;">.00407138</span>
  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">JMP</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span></pre>
</blockquote>
<p style="text-align: justify;">Which is the only antidebugging code in the crypter stub. We talked about PEB.BeingDebugged detection last week so you should know that during this code execution ECX should be NULL by the point code executes label L005. But that is of no importance since we are coding a <strong><em>static </em></strong>unpacker. However following code is very important:</p>
<blockquote>
<pre class="asm">L001:
 <span style="color: #EE4A02;">PUSHAD</span>
 <span style="color: #EE4A02;">CALL</span> L003
 <span style="color: #DEE002;">DB</span> E8
L003:
 <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>,5C
 <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;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,6164652E
 <span style="color: #EE4A02;">JE</span> L031
 <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;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,7273722E
 <span style="color: #EE4A02;">JE</span> L031
 <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;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">63727372</span>
 <span style="color: #EE4A02;">JE</span> L031
 <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;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,7063632E
 <span style="color: #EE4A02;">JE</span> L031
 <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>EDI<span style="color: #ff0000;">+14</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">0</span>
 <span style="color: #EE4A02;">JE</span> L031
 <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
 <span style="color: #EE4A02;">ADD</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;">EDI</span>+<span style="color: #DEE002;">C</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: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EDI<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;">EAX</span>,D4497869    <span style="color: #adadad; font-style: italic;">;Variable</span>
 <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,2F1F0FD2    <span style="color: #adadad; font-style: italic;">;Variable</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;">JE</span> L031
L021:
 <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
 <span style="color: #EE4A02;">JECXZ</span> L031
 <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;">ECX</span>+<span style="color: #EE1802; font-weight:bold;">ESI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">AL</span>
 <span style="color: #EE4A02;">NEG</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
 <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,39E7B8FA    <span style="color: #adadad; font-style: italic;">;Variable</span>
 <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
 <span style="color: #EE4A02;">NOT</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: #EE1802; font-weight:bold;">EDX</span>
 <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
 <span style="color: #EE4A02;">JMP</span> L021
L031:
 <span style="color: #EE4A02;">RET</span>
 <span style="color: #EE4A02;">POPAD</span>
 <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>,<span style="color: #ff0000;">28</span>
 <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
 <span style="color: #EE4A02;">JNZ</span> <span style="color: #DEE002;">SHORT</span> L001</pre>
</blockquote>
<p style="text-align: justify;">This is the hearth of the crypter, code in-charge of decrypting the sections. This code also answers the question "Which sections does the crypter encrypt?". If we look at the sequence of compares at the start of this code we will see that only the sections named .rsr, rsrc and .cpp are not decrypted. It is common for crypters to use section names to determine if to decrypt the section or not. As we mentioned earlier the decryption is executed backwards, meaning that the sections are decrypted from the last to the fist byte of the section. However unlike the previous samples we can't just rip this code in order to use it in our unpacker. The reason for that is that some constants which are marked above are not the same for every packed sample. That is why those constants must be read by our unpacker and used as decryption keys for proper section decryption.</p>
<p style="text-align: justify;">After the section data has been decrypted crypter will handle import data. This part is also interesting because of the following:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,0040445C
  <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;">ESI</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;">ESI</span>+<span style="color: #DEE002;">C</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;">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;">EBX</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>ESP<span style="color: #ff0000;">+14</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;LoadLibraryA call</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;">EBX</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> <span style="color: #DEE002;">SHORT</span> 004072FE
...
 Decryption:
  <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>,3C
  <span style="color: #EE4A02;">LEA</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>+EAX<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>,F4C05BBB             <span style="color: #adadad; font-style: italic;">;Variable</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,8B079C24             <span style="color: #adadad; font-style: italic;">;Variable</span>
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
L005:
  <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;">ECX</span>+<span style="color: #EE1802; font-weight:bold;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">AL</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;">ECX</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;">JE</span> L015
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>                      <span style="color: #adadad; font-style: italic;">;Random code</span>
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,23BC3756
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,C4A2711F
  <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: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>                      <span style="color: #adadad; font-style: italic;">;Random code</span>
  <span style="color: #EE4A02;">JMP</span> L005
L015:
  <span style="color: #EE4A02;">RET</span>
004072FE:
  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,0C
  <span style="color: #EE4A02;">JMP</span> 004012C0 <span style="color: #adadad; font-style: italic;">;OEP jump</span></pre>
</blockquote>
<p style="text-align: justify;">At the start of this code ESI is set to 0x0040445C which is the location of the valid import table, more specifically first IID. Following this is a call to <em>LoadLibraryA </em>API which loads all the dependencies until there are no more to load or the dependency isn't found on the system this file is executing on. Because of this there are cases when protected files won't execute correctly. But never the less lets imagine that this will work since all dependencies are present. What is going to happen when <em>LoadLibrary </em>tries to find a NULL dll file, and fails of-course,  is that the jump to 0x004072FE will execute which will align the stack and jump to the original entry point. This is usually the last piece of puzzle but since here import fixing and entry point jumping are overlapping we will handle imports after we have found the entry point location.</p>
<p style="text-align: justify;">While still looking at the code from above we find out that another decryption is performed on every member of the import table. All the strings which are the names of functions to be found in loaded DLLs are decrypted in the decryption sub-routine. Here we also have some variable constants to harden the detection but we also have a randomly generated piece of code that uses them in order to decrypt the string. This code can't be ripped (<em>since its random</em>) and what we are going to do is copy this random piece of code, set input variables to it and call it in order to make it decrypt the data we want. Very simple procedure which is commonly used for such cases. Once all the import strings have been decrypted the file is fully decrypted and all that is left is to set the correct PE header data for entry point and import table.</p>
<p style="text-align: justify;">Writing an unpacker for CryptoCrackPEProtector 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/RLdeCryptoCrackPE.rar">RL!deCryptoCrackPE 0.9x</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%2F10%2Fstatic-decryption-in-reverse%2F&amp;title=Static%20decryption%20in%20reverse" 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/10/static-decryption-in-reverse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

