<?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; Decompression</title>
	<atom:link href="http://blog.reversinglabs.com/tag/decompression/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>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_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/2009/12/static-aplib-decompression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

