<?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; aPLib</title>
	<atom:link href="http://blog.reversinglabs.com/tag/aplib/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>Unpacking layered protections</title>
		<link>http://blog.reversinglabs.com/2010/02/unpacking-layered-protections/</link>
		<comments>http://blog.reversinglabs.com/2010/02/unpacking-layered-protections/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 12:03:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[AlexProtector]]></category>
		<category><![CDATA[aPLib]]></category>
		<category><![CDATA[Protection]]></category>
		<category><![CDATA[Unpacking]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=351</guid>
		<description><![CDATA[Today we finish our AlexProtector unpacker. We started creating it last week with file format analysis. We initially intended to create a dynamic unpacker for this protection, but since it is just as "easy" to create a static one, we went for that option. We are a day late with our blog as a result, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Today we finish our AlexProtector unpacker. We started creating it last week with <a href="http://blog.reversinglabs.com/2010/02/analyzing-layered-protections/" target="_blank">file format analysis</a>. We initially intended to create a dynamic unpacker for this protection, but since it is just as "easy" to create a static one, we went for that option. We are a day late with our blog as a result, and we are glad we are, since we noticed some bugs in the <em>Importer </em>module that we have since resolved. But we did more then just bug fixing - we made some tweaks to the existing functions, improving import elimination protection support.</p>
<p style="text-align: justify;">Since we already did the analysis, let's go straight to coding an unpacker and describe everything that needs to be done to complete it. We'll start by reserving enough space for the section data that needs to be decompressed. This can be a complex task if we try to manually re-size the file and move all data to the appropriate locations. However <em>TitanEngine</em> comes with two functions which can do the hard work for us, even though they are not meant to do this exact task.</p>
<p style="text-align: justify;">But before we go into that let's explain what is really needed. The problem: some sections are compressed by <a href="http://www.ibsensoftware.com/products_aPLib.html" target="_blank">aPLib</a> which means that the protected file size is most likely smaller than the original file size. To ensure there is enough extra space to hold the decompressed data, we have to re-size the sections so they can hold the physical data. But how much space is needed? As it turns out, that part is simple, because AlexProtector stores compressed section data inside its original sections. This means that if the first PE section has been compressed, its compressed data will still be located in the first section. After decompression that data will still need to be written in the first section, but will require more physical space, since the compressed data is up to ~70% smaller than the original data. However, since the original data is still in the section, we know that the maximum physical size of that data is equal to the virtual size of that section. As a result, we can simply re-size all sections so that their physical size is equal to their rounded up virtual size.</p>
<p style="text-align: justify;">Doing this is just like dumping the memory of a running process with the <em>TitanEngine's DumpProcess</em> function, but since we're making a static unpacker, we don't want to start the process, so we will do it a little differently. We will use <em>StaticFileLoad </em>to simulate the file load, which has the same effect.  Then we will dump that memory to disk with <em>DumpMemory</em>. But that doesn't complete the task - we need to correct the PE section values of  the dumped image to make the file a valid PE image. To do this, we set the raw size of each section to the rounded up virtual size, and set the raw offset of each section to its virtual offset. Once that is done, all virtual locations in the file will be equal to their physical counterparts. For example, the virtual address 0x1000 will be on the 0x1000 byte from the start of the file.</p>
<p style="text-align: justify;">Once we have the file re-sized to its approximate original size, we can start decompressing data. However we don't want to guess which section is compressed and which isn't, so we will directly read the protector data to reverse the compression. To find out where this data is stored, we monitor calls to the aPLib decompression code and find a call to it from the second protection layer, which is exactly what we need:</p>
<blockquote>
<pre class="asm"><span style="color: #adadad; font-style: italic;">;Layer base: 0x00220000</span>
<span style="color: #adadad; font-style: italic;">;--------------------------------------------------------------------</span>
/*2202D1*/  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023C1<span style="color: #FFFFFF;">&#93;</span>  <span style="color: #adadad; font-style: italic;">;Internal section data</span>
/*2202D7*/  <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>ESI<span style="color: #ff0000;">+4</span><span style="color: #FFFFFF;">&#93;</span>
/*2202DA*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">4</span>
/*2202DC*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">1000</span>
/*2202E1*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
... Junk removed with DeJunk ...
/*<span style="color: #ff0000;">220387</span>*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">0</span>
/*<span style="color: #ff0000;">220389</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;">+402411</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;VirtualAlloc</span>
/*22038F*/  <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;">EBP</span>+40239D<span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
/*<span style="color: #ff0000;">220395</span>*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
/*<span style="color: #ff0000;">220396</span>*/  <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;">ESI</span><span style="color: #FFFFFF;">&#93;</span>
/*<span style="color: #ff0000;">220398</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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+402399</span><span style="color: #FFFFFF;">&#93;</span>
/*22039E*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
... Junk removed with DeJunk ...
/*<span style="color: #ff0000;">220444</span>*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
/*<span style="color: #ff0000;">220445</span>*/  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+401108</span><span style="color: #FFFFFF;">&#93;</span>
/*22044B*/  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>                       <span style="color: #adadad; font-style: italic;">;aPLib decompress</span>
/*22044D*/  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,<span style="color: #ff0000;">8</span>
/*<span style="color: #ff0000;">220450</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: #ff0000;">220452</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>
... Junk removed with DeJunk ...
/*2204F9*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+402399</span><span style="color: #FFFFFF;">&#93;</span>
/*2204FF*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+40239D<span style="color: #FFFFFF;">&#93;</span>
/*<span style="color: #ff0000;">220505</span>*/  <span style="color: #EE4A02;">REP</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: #ff0000;">220507</span>*/  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>
/*<span style="color: #ff0000;">220508</span>*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+40239D<span style="color: #FFFFFF;">&#93;</span>
/*22050E*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">8000</span>
/*<span style="color: #ff0000;">220513</span>*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #ff0000;">0</span>
/*<span style="color: #ff0000;">220515</span>*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
/*<span style="color: #ff0000;">220516</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;">+402415</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;VirtualFree</span>
... Junk removed with DeJunk ...
/*2205C1*/  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESI</span>,<span style="color: #ff0000;">8</span>
/*2205C4*/  <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;">0</span>
/*2205C7*/  <span style="color: #EE4A02;">JNZ</span> 002202D7
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">As we can see from this code snippet, the protector is reading data located at EBP+0x004023c1. Here both numbers are constants with EBP being calculated at the start of the protector code and referred to as "protector delta." It is determined 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;">EBP</span>
  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">EBP</span>,<span style="color: #ff0000;">00401006</span>
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">Most protections use this kind of coding which is popularly called <a href="http://en.wikipedia.org/wiki/Position-independent_code" target="_blank"><em>offset independent coding</em></a>. Protection authors who use this method don't need to know the exact offset at which their protection will be written. Since we are only coding an unpacker for this one protection version, we will use the same internal constants that the protector uses. So, at the location Delta+0x004023c1 we will find and read the internal AlexProtector data about the compressed sections. That data is a two dimensional array with the following structure:</p>
<blockquote>
<pre class="cpp"><span style="">typedef</span> <span style="">struct</span> AlexProt_SectionData<span style="color: #FFFFFF;">&#123;</span>
	DWORD SectionVirtualOffset;
	DWORD SectionVirtualSize;
<span style="color: #FFFFFF;">&#125;</span>AlexProt_SectionData, *PAlexProt_SectionData;
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">After we decompress that data, all the sections are restored to their original state - with a couple of small exceptions: we need to identify the original entry point for the code section, and we need to remove import elimination protection. Of those two things, we will fix the import protection first, because removing that protection will correct the code section data.</p>
<p style="text-align: justify;">When we were analyzing the file format, we determined the internal format that AlexProtector uses for storing import data. Now that we are creating an unpacker, we need that data, so we need to determine where it is stored and if it's encrypted at any point. With some tracing we determine that this code does the decryption of the compressed import data:</p>
<blockquote>
<pre class="asm"><span style="color: #adadad; font-style: italic;">;Layer base: 0x00220000</span>
<span style="color: #adadad; font-style: italic;">;--------------------------------------------------------------------</span>
/*22080F*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+402531</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;Pointer to import data</span>
/*<span style="color: #ff0000;">220815</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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023B5<span style="color: #FFFFFF;">&#93;</span>
/*<span style="color: #ff0000;">2208</span><span style="color: #ff0000;">1B</span>*/  <span style="color: #EE4A02;">XOR</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
/*22081D*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #ff0000;">100</span>
/*<span style="color: #ff0000;">220822</span>*/  <span style="color: #EE4A02;">DIV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
/*<span style="color: #ff0000;">220824</span>*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
/*<span style="color: #ff0000;">220826</span>*/  <span style="color: #EE4A02;">DIV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
/*<span style="color: #ff0000;">220828</span>*/  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
/*22082A*/  <span style="color: #EE4A02;">DIV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
/*22082C*/  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
/*22082E*/  <span style="color: #EE4A02;">DIV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
/*<span style="color: #ff0000;">220830</span>*/  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #EE1802; font-weight:bold;">EDX</span>
/*<span style="color: #ff0000;">220832</span>*/  <span style="color: #EE4A02;">POP</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
/*<span style="color: #ff0000;">220833</span>*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
/*<span style="color: #ff0000;">220835</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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023B9<span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;Size of the import data</span>
/*22083B*/  <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;">EDI</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">AL</span>          <span style="color: #adadad; font-style: italic;">;Decryption loop</span>
/*22083D*/  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EDI</span>
/*22083E*/  <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>
/*22083F*/  <span style="color: #EE4A02;">JNZ</span> 0022083B
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">Internal import data is compressed and then encrypted. In order to use it, we must reverse this process by decrypting the memory content, then decompressing it. To decrypt it, we need the decryption key and the decryption algorithm. Since the algorithm is known - it is a simple XOR - we  simply need the decryption key in order to continue. Here is where the protection author made a mistake: he used a decryption key which is a CRC hash of the selected memory part. Now this is a only partly a "mistake," since it does prevent us from using software breakpoints in some areas, but it makes the decryption key calculation unnecessary because that key can't change for this particular protector version. Since the EAX value at the start of this code snippet is 0xD0340178 we can calculate that the decryption key is 0x7D.</p>
<p style="text-align: justify;">Once this memory is decrypted, it can be decompressed and then processed in order to correct both the import table and the code section. If we remember our analysis from the last time, here is how the internal import data is packed:</p>
<blockquote>
<pre class="cpp"><span style="">typedef</span> <span style="">struct</span> ALEX_IAT_DLL<span style="color: #FFFFFF;">&#123;</span>
       BYTE DLLSignature; <span style="color: #ff0000;">//0xC3</span>
       BYTE DLLNameLength;
       <span style="color: #ff0000;">// DLLName[DLLNameLength] followed by 0x00</span>
<span style="color: #FFFFFF;">&#125;</span>ALEX_IAT_DLL, *PALEX_IAT_DLL;
&nbsp;
<span style="">typedef</span> <span style="">struct</span> ALEX_IAT_APIENTRY<span style="color: #FFFFFF;">&#123;</span>
       <span style="color: #ff0000;">// 0xC4 indicates Ordinal import</span>
       BYTE APINameLength;
       <span style="color: #ff0000;">// APIName[APINameLength] followed by 0x00</span>
       BYTE RedirectionNumber; <span style="color: #ff0000;">//Number of redirections</span>
       DWORD RedirectionAddress<span style="color: #FFFFFF;">&#91;</span>RedirectionNumber<span style="color: #FFFFFF;">&#93;</span>;
<span style="color: #FFFFFF;">&#125;</span>ALEX_IAT_APIENTRY, *PALEX_IAT_APIENTRY;</pre>
</blockquote>
<p style="text-align: justify;">Above is the pseudo C code that describes the internal import data structure. Here, "redirection data" refers to the addresses in the code section that need to be corrected to point to the correct API pointers. We can consider this table as a sort of relocation table, because the data that needs to be written at that location corresponds to a random location in memory at which the import redirection is allocated. Since that memory allocation is outside the PE image file memory, we call that kind of import protection "import eliminations." To repair it, we must estimate the location at which to reconstruct the import table, and create a table aligned to that location on the fly. There are two ways to do this and we decided to go with the more complex one. Its logic goes like this:</p>
<ul>
<li>Initialize the importer and say that the import table will be moved</li>
<li>Create a relative virtual import table with no point of reference</li>
<li>Relocate the virtual import table to the reserved spot</li>
<li>Write the new import table</li>
</ul>
<p style="text-align: justify;">While the term "relative virtual import table" sounds confusing, it can be described simply as an import table whose references start with zero and increment by four. That would look something like this:</p>
<ol>
<li>New DLL: Kernel32.dll</li>
<li>TrunkValue: 0x00000000; GetProcAddress (e.g.)</li>
<li>TrunkValue: 0x00000004; LoadLibraryA (e.g.)</li>
<li>TrunkValue: 0x00000008; FreeLibrary (e.g.); Remember its +8 for the last item</li>
<li>New DLL: User32.dll</li>
<li>TrunkValue: 0x00000010; MessageBoxA (e.g.)</li>
<li>TrunkValue: 0x00000014; MessageBoxW (e.g.)</li>
</ol>
<p style="text-align: justify;">Once we compile the table, we simply relocate it by adding the correct value to its trunk relative address. That value is the virtual address of the section we will add to the file to hold the import table. During unpacking, that address will be a <em>SectionAlignment </em>aligned value of <em>NtSizeOfImage</em>. We could have built our table like this from the start, without relocating it, but we wanted to show a more general approach. Now we just need to write the correct pointers to the redirection addresses found in  the AlexProtector internal data to connect the code section with the new import table.</p>
<p style="text-align: justify;">With imports sorted, we move on the last item on our list, the entry point protection. It is processed by this code here:</p>
<blockquote>
<pre class="asm"><span style="color: #adadad; font-style: italic;">;Layer base: 0x00220000</span>
<span style="color: #adadad; font-style: italic;">;--------------------------------------------------------------------</span>
/*220D<span style="color: #ff0000;">0B</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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+402531</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;Pointer to EP data</span>
... Junk removed with DeJunk ...
/*220DB6*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023B5<span style="color: #FFFFFF;">&#93;</span>
/*220DBC*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023B9<span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;Pointer correction</span>
... Junk removed with DeJunk ...
/*220E67*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+402395</span><span style="color: #FFFFFF;">&#93;</span>
... Junk removed with DeJunk ...
/*220F12*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023AD<span style="color: #FFFFFF;">&#93;</span>
/*220F18*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
/*220F19*/  <span style="color: #EE4A02;">PUSH</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
/*220F1A*/  <span style="color: #EE4A02;">LEA</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+401108</span><span style="color: #FFFFFF;">&#93;</span>
/*220F20*/  <span style="color: #EE4A02;">CALL</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">EDX</span>                     <span style="color: #adadad; font-style: italic;">;aPLib</span>
... Junk removed with DeJunk ...
/*220FC7*/  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">ESP</span>,<span style="color: #ff0000;">8</span>
/*220FCA*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023AD<span style="color: #FFFFFF;">&#93;</span>
/*220FD0*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+402391</span><span style="color: #FFFFFF;">&#93;</span>
... Junk removed with DeJunk ...
/*22107B*/  <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;">SS</span>:<span style="color: #FFFFFF;">&#91;</span>EBP<span style="color: #ff0000;">+402385</span><span style="color: #FFFFFF;">&#93;</span> <span style="color: #adadad; font-style: italic;">;EP resumes here</span>
/*<span style="color: #ff0000;">221081</span>*/  <span style="color: #EE4A02;">ADD</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #EE1802; font-weight:bold;">EBX</span>
/*<span style="color: #ff0000;">221083</span>*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EAX</span><span style="color: #FFFFFF;">&#93;</span>,0E9         <span style="color: #adadad; font-style: italic;">;Write OEP jump</span>
/*<span style="color: #ff0000;">221086</span>*/  <span style="color: #EE4A02;">INC</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>
/*<span style="color: #ff0000;">221087</span>*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">ESI</span>
... Junk removed with DeJunk ...
/*22112E*/  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #EE1802; font-weight:bold;">EAX</span>
/*<span style="color: #ff0000;">221130</span>*/  <span style="color: #EE4A02;">SUB</span> <span style="color: #EE1802; font-weight:bold;">ECX</span>,<span style="color: #ff0000;">4</span>
/*<span style="color: #ff0000;">221133</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><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #EE1802; font-weight:bold;">ECX</span>
... Junk removed with DeJunk ...
/*2211DA*/  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,<span style="color: #DEE002;">DWORD</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">SS</span>:<span style="color: #FFFFFF;">&#91;</span><span style="color: #EE1802; font-weight:bold;">EBP</span>+4023AD<span style="color: #FFFFFF;">&#93;</span>
/*2211E0*/  <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>
/*2211E4*/  <span style="color: #EE4A02;">POPAD</span>
/*2211E5*/  <span style="color: #EE4A02;">JMP</span> <span style="color: #DEE002;">NEAR</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>                      <span style="color: #adadad; font-style: italic;">;Jump to stolen EP</span>
&nbsp;</pre>
</blockquote>
<p style="text-align: justify;">As we can see, the stolen entry point data is decompressed and a new jump to the first non-stolen instruction is written. To reverse this we must decompress the buffer with EP jump correction, after which we need to write it to a new section. It is possible to extract the correct the entry point to its original state, but that would mean that we would have to disassemble and analyze the decompressed buffer which contains those original functions riddled with junk code. Such disasesmbly and analysis is an error-prone process, however, so keeping the junk is the simplest and safest option.</p>
<p style="text-align: justify;">Writing  an unpacker for AlexProtector is a nice exercise for  any reverser. We have shown in detail how it can be done statically. If you have any questions about any of the steps in writing this unpacker feel free to contact us. 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/02/alexprot.zip">Samples  and Protector</a> / <a href="http://blog.reversinglabs.com/wp-content/uploads/2010/02/RLdeAlexProtector.zip">RL!deAlexProtector</a><br />
(package contains protector binary, 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%2F02%2Funpacking-layered-protections%2F&amp;title=Unpacking%20layered%20protections" 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/02/unpacking-layered-protections/feed/</wfw:commentRss>
		<slash:comments>5</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_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/2009/12/static-aplib-decompression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

