<?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; Research</title>
	<atom:link href="http://blog.reversinglabs.com/tag/research/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>Attacking the cipher</title>
		<link>http://blog.reversinglabs.com/2010/01/attacking-the-cipher/</link>
		<comments>http://blog.reversinglabs.com/2010/01/attacking-the-cipher/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 05:00:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[BruteForce]]></category>
		<category><![CDATA[forensics]]></category>
		<category><![CDATA[Password]]></category>
		<category><![CDATA[Recovery]]></category>
		<category><![CDATA[Research]]></category>

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

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

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

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=118</guid>
		<description><![CDATA[Halloween is a special time of year and it deserves a special blog we might call "writing unpackers in reverse". But wait, writing unpackers in reverse... isn't that packer writing? We are going to do some TitanEngine coding in order to make it do a complete opposite of what it is meant for. We are [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Halloween is a special time of year and it deserves a special blog we might call "<em>writing unpackers in reverse</em>". But wait, writing unpackers in reverse... isn't that packer writing?</p>
<p style="text-align: justify;">We are going to do some <em>TitanEngine </em>coding in order to make it do a complete opposite of what it is meant for. We are going to force our good friend <em>Titan </em>to recreate a crypter we already made an unpacker for. Crypter we are recreating is called LameCrypt. So, what does LameCrypt do?</p>
<p style="text-align: justify;">Its a very very simple crypter designed only to crypt the first executable section by XORing with 0x90. Extremely simple protection mechanism which looks just like this:</p>
<blockquote>
<pre class="asm">  <span style="color: #EE4A02;">PUSHAD</span>
  PUSHFW
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,4C000
L003:
  <span style="color: #EE4A02;">XOR</span> <span style="color: #DEE002;">BYTE</span> <span style="color: #DEE002;">PTR</span> <span style="color: #EE1802; font-weight:bold;">DS</span>:<span style="color: #FFFFFF;">&#91;</span>EBX<span style="color: #ff0000;">+401000</span><span style="color: #FFFFFF;">&#93;</span>,<span style="color: #ff0000;">90</span>
  <span style="color: #EE4A02;">DEC</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>
  <span style="color: #EE4A02;">CMP</span> <span style="color: #EE1802; font-weight:bold;">EBX</span>,<span style="color: #ff0000;">-1</span>
  <span style="color: #EE4A02;">JNZ</span> L003
  POPFW
  <span style="color: #EE4A02;">POPAD</span>
  <span style="color: #EE4A02;">MOV</span> <span style="color: #EE1802; font-weight:bold;">EAX</span>,0044CF38
  <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;">That is the entire crypter code. To recreate it we will copy the binary for the code above and leave the constants blank. After processing that binary data looks like this:</p>
<blockquote>
<pre class="asm"><span style="color: #ff0000;">60</span> <span style="color: #ff0000;">66</span> 9C BB <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">80</span> B3 <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">90</span> 4B
<span style="color: #ff0000;">83</span> FB FF <span style="color: #ff0000;">75</span> F3 <span style="color: #ff0000;">66</span> 9D <span style="color: #ff0000;">61</span> B8 <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> <span style="color: #ff0000;">00</span> FF E0</pre>
</blockquote>
<p style="text-align: justify;">All the zeroes above will be filled with the following data: virtual size of the first section, virtual offset of the first section and the address of the original entry point. This data, once filled to code above, will be written to the new section. List of actions to perform for this would be:</p>
<blockquote>
<ul>
<li>Read necessary PE32 data</li>
<li>Fill the  binary data from above</li>
<li>Add new section to PE32 file</li>
<li>Fill written data to it</li>
<li>Encrypt the first section by XORing it with 0x90</li>
<li>Update PE header data</li>
</ul>
</blockquote>
<p style="text-align: justify;">Since this is a very simple crypter its code is very short. What makes this a Halloween post is the spooky fact that TE can be forced to create his own worst enemies, crypters! Until next time...</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/LameCrypter.zip">LameCrypter</a><br />
(Package contains binary and source files)</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%2Fhalloween-reversing%2F&amp;title=Halloween%20reversing" id="wpa2a_8"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/10/halloween-reversing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Packer security advisory: MEW design flaw</title>
		<link>http://blog.reversinglabs.com/2009/09/packer-security-advisor-mew-design-flaw/</link>
		<comments>http://blog.reversinglabs.com/2009/09/packer-security-advisor-mew-design-flaw/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 13:15:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Advisory]]></category>
		<category><![CDATA[MEW]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Vulnerabilities]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=39</guid>
		<description><![CDATA[This is a followup on MEW file format analysis. As mentioned in our video blog yesterday we noticed that MEW 10 has a design flaw that wrongfully passes function names to LoadLibraryA which firstly tries to load it as a DLL file and once that has failed it passes the same string to GetProcAddress and [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">This is a followup on MEW file format analysis. As mentioned in our <a href="http://blog.reversinglabs.com/2009/09/analyzing-mew-10-11/">video blog yesterday</a> we noticed that MEW 10 has a design flaw that wrongfully passes function names to LoadLibraryA which firstly tries to load it as a DLL file and once that has failed it passes the same string to GetProcAddress and successfully finds selected function in previously loaded DLL. We can exploit this by creating a dummy DLL file named as a function which every file on Windows imports, for example ExitProcess or GetModuleHandleA. Placing such file in %Windows%\System32 would ensure that that file is loaded each time a MEW 10 packed file is executed on the system. But we have a problem we must resolve before we proceed. As said earlier function only gets found if the DLL isn't loaded, or more specifically call to LoadLibraryA returns <em>NULL</em>. This must be resolved because we don't want to crash the packed fie. So to work around this we simply do this inside our GetModuleHandleA.dll file:</p>
<blockquote>
<pre class="cpp"><span style="">BOOL</span> APIENTRY DllMain<span style="color: #FFFFFF;">&#40;</span>HMODULE hModule,
   DWORD  ul_reason_for_call, LPVOID lpReserved<span style="color: #FFFFFF;">&#41;</span><span style="color: #FFFFFF;">&#123;</span>
	<span style="color: #EE4A02;">switch</span> <span style="color: #FFFFFF;">&#40;</span>ul_reason_for_call<span style="color: #FFFFFF;">&#41;</span>
	<span style="color: #FFFFFF;">&#123;</span>
	<span style="color: #EE4A02;">case</span> DLL_PROCESS_ATTACH:
		<span style="color: #EE4A02;">if</span><span style="color: #FFFFFF;">&#40;</span>GetModuleHandleA<span style="color: #FFFFFF;">&#40;</span><span style="color: #666666;">&quot;BadGuy.dll&quot;</span><span style="color: #FFFFFF;">&#41;</span> == <span style="color: #EE1802;">NULL</span><span style="color: #FFFFFF;">&#41;</span><span style="color: #FFFFFF;">&#123;</span>
			LoadLibraryA<span style="color: #FFFFFF;">&#40;</span><span style="color: #666666;">&quot;BadGuy.dll&quot;</span><span style="color: #FFFFFF;">&#41;</span>;
			<span style="color: #EE4A02;">return</span> <span style="color: #EE1802;">false</span>;
		<span style="color: #FFFFFF;">&#125;</span>
	<span style="color: #EE4A02;">case</span> DLL_THREAD_ATTACH:
	<span style="color: #EE4A02;">case</span> DLL_THREAD_DETACH:
	<span style="color: #EE4A02;">case</span> DLL_PROCESS_DETACH:
		<span style="color: #EE1802;">break</span>;
	<span style="color: #FFFFFF;">&#125;</span>
	<span style="color: #EE4A02;">return</span> <span style="color: #EE1802;">TRUE</span>;
<span style="color: #FFFFFF;">&#125;</span></pre>
</blockquote>
<p style="text-align: justify;">Returning <em>false </em>at DLL_PROCESS_ATTACH makes Windows unload our GetModuleHandleA.dll but not before BadGuy.dll gets loaded. Simple code above ensures that our BadGuy.dll gets loaded only once (<em>Windows also prevents this so this isn't really needed</em>) since MEW 10 packed file can import GetModuleHandleA multiple times. Our BadGuy.dll only creates a new thread which displays a message box about it being successfully loaded. This could have been done with a single DLL file but we wanted to keep it short and simple.</p>
<p style="text-align: justify;">There are many examples of design flaws in PE shell modifiers which could seriously threaten system security. Such example are not only limited to arbitrary code execution but could also lead to privilege elevation. We will continue to write about such shell modifier flaws in the future.</p>
<p style="text-align: center;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/09/MEW-LoadLibrary-exploit.rar">Download MEW 10 LoadLibrary exploit POC</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.reversinglabs.com%2F2009%2F09%2Fpacker-security-advisor-mew-design-flaw%2F&amp;title=Packer%20security%20advisory%3A%20MEW%20design%20flaw" id="wpa2a_10"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2009/09/packer-security-advisor-mew-design-flaw/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

