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

<channel>
	<title>ReversingLabs &#124; Blog &#187; Reversing</title>
	<atom:link href="http://blog.reversinglabs.com/category/reversing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.reversinglabs.com</link>
	<description>Everything in reverse...</description>
	<lastBuildDate>Mon, 06 Sep 2010 17:55:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Combat reverse engineering</title>
		<link>http://blog.reversinglabs.com/2010/09/combat-reverse-engineering/</link>
		<comments>http://blog.reversinglabs.com/2010/09/combat-reverse-engineering/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 17:55:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[Malware analysis]]></category>
		<category><![CDATA[Rogue AV]]></category>
		<category><![CDATA[Unpacking]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=883</guid>
		<description><![CDATA[Reverse engineering is the only weapon of choice when it comes to malware unpacking and analysis. It gives us an inside look into the malware creations and enables us to understand their ins and outs. One such malicious sample was sent to us today for analysis. The file in question is an update for a [...]]]></description>
			<content:encoded><![CDATA[<div style="text-align: center;">
	<script type="text/javascript">
		var flashvars = {};
		flashvars.playOnStart = "false";
		flashvars.startVolume = "70";
		flashvars.autoHideOther = "false";
		flashvars.autoHideVideoControls = "false";
		flashvars.onStartShowControls = "true";
		flashvars.fullVideoScale = "true";
		flashvars.showPlayButton = "true";
		flashvars.share = "false";
		flashvars.MediaLink2 = "http://www.youtube.com/watch?v=wC51TQvsNWU";
		flashvars.image = "http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/lib/img.php?v=wC51TQvsNWU";
		var params = {};
		params.bgcolor = "#000000";
		params.allowfullscreen = "true";
		params.wmode = "opaque";
		var attributes = {};
		attributes.id = "myplayer";
		swfobject.embedSWF("http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/lib/player.swf", "myAlternativeContent", "460", "310", "9.0.0", false, flashvars, params, attributes);
	</script> <div id="myAlternativeContent">blah</div></div>
<p style="text-align: justify;">
<p style="text-align: justify;"><a href="http://en.wikipedia.org/wiki/Reverse_engineering" target="_blank">Reverse engineering</a> is the only weapon of choice when it comes to <a href="http://en.wikipedia.org/wiki/Malware" target="_blank">malware</a> unpacking and analysis. It gives us an inside look into the malware creations and enables us to understand their ins and outs. One such malicious sample was sent to us today for analysis. The file in question is an update for a <a href="http://en.wikipedia.org/wiki/Rogue_security_software" target="_blank">rogue anti-virus solution</a> and it uses an interesting encryption and packing options to hide its presence from legitimate security software solutions. For our today's blog we demonstrate the actions needed to remove the protections utilized by malicious software in order to get to the core malware functionality. Until next week...</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/09/combat-reverse-engineering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ReversingLabs Summer Challenge</title>
		<link>http://blog.reversinglabs.com/2010/07/reversinglabs-summer-challenge/</link>
		<comments>http://blog.reversinglabs.com/2010/07/reversinglabs-summer-challenge/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 17:28:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Reversing]]></category>
		<category><![CDATA[ReversingLabs]]></category>
		<category><![CDATA[Challenge]]></category>
		<category><![CDATA[NyxEngine]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=770</guid>
		<description><![CDATA[Looks cool? Want one? All you have to do is solve this challenge and tell us what is the password we seek. Sounds easy? Its not... Mail us with your solution at: blog(at)reversinglabs(dot)com; Challenge is now closed! Thanks to everyone who participated. Click read more for the solution... We didn't even dream about getting so [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/07/124829145.jpg" rel="lightbox[770]"><img class="size-full wp-image-597 alignnone" title="TShirt" src="http://blog.reversinglabs.com/wp-content/uploads/2010/07/124829145.jpg" alt="" width="630" height="261" /></a></p>
<p style="text-align: justify;">Looks cool? Want one? All you have to do is solve this <a href="http://blog.reversinglabs.com/wp-content/uploads/2010/07/r.zip">challenge</a> and tell us what is the password we seek. Sounds easy? Its not... Mail us with your solution at: blog(at)reversinglabs(dot)com; <strong>Challenge is now closed! Thanks to everyone who participated. Click read more for the solution...<br />
</strong></p>
<p><span id="more-770"></span></p>
<p style="text-align: justify;">We didn't even dream about getting so many people to participate in our little challenge. The sheer number of emails simply flooded our mailbox with possible solutions and compliments about  our challenge! One of those compliments expresses just what we want to do in the not-so-distant future, we quote: "<em>Fun challenge, do more of  these!</em>" We definitely will!</p>
<p style="text-align: justify;">Now to the solution, and discussion of the parts that proved troublesome for many...</p>
<p style="text-align: justify;">We start by downloading the file and doing our initial analysis. Since the file is a <a href="http://en.wikipedia.org/wiki/ZIP_%28file_format%29" target="_blank">ZIP</a> archive we open it with any program that works with this archive format to find a folder named "r" with the file named "r.zip" in it. This part of the challenge is just a decoy to keep you busy and distracted from the real content which is appended to the archive as an archive comment. That real content is another archive in <a href="http://en.wikipedia.org/wiki/7-Zip" target="_blank">7Zip</a> file format, which once extracted produces a single <a href="http://en.wikipedia.org/wiki/Cabinet_%28file_format%29" target="_blank">CAB</a> file, and that is where the things get interesting. The CAB file has a single <a href="http://en.wikipedia.org/wiki/PNG" target="_blank">PNG</a> file in it, but to solve this challenge we must observe the image and the archive as two separate objects.</p>
<p style="text-align: justify;">First the image part of the puzzle. The image, once opened, shows a normal picture with the logo of our company. However the picture itself has embedded <a href="http://en.wikipedia.org/wiki/Steganography" target="_blank">steganography</a> data. Since we didn't want to do any hard stego which can be solved by inspecting image pixels we embedded our hidden information between valid records inside the PNG file header. Something very similar to what we demonstrated on <a href="http://www.blackhat.com/html/bh-eu-10/bh-eu-10-briefings.html#Vuksan" target="_blank">BlackHat Barcelona</a> earlier this year. With the obvious difference that the file format is an image not an archive. Nonetheless the principle is the same. So, what's hidden? If you open the image file with any hex editor you will see a string "pSWD" near the start of the file. That string is followed by a 16 number sequence: 538B327278BBAB654747288999FBCDA1 which isn't the password we need. Nope, its not - even though many of you thought that that was the end solution. Why isn't it?</p>
<p style="text-align: justify;">Because of the fact that the CAB file that compressed that PNG image holds the last piece of the puzzle. If we scan that CAB file with our <a href="http://www.reversinglabs.com/products/NyxEngine.php" target="_blank">NyxEngine</a> we get the following output:</p>
<blockquote><p>Steganography ID: 0x00000b<br />
Possible steganography due to suspicious CAB extra data present between entries!<br />
Data start: 0x5a; Data size: 0x0000f6</p></blockquote>
<p style="text-align: justify;">And in that data there is the following text block:</p>
<blockquote>
<p style="text-align: justify;">UmFyIRoHAM6Zc4AADQAAAAAAAAA8MSAOyRZcWCVhcEFcUfp<br />
P4JdbtU2derwgjSYp+BpxVYkWJPDtQ/TITifo4qO7qyYz+yLpd9+6<br />
nkwwxmomWHbHK0Bt6UPHOwL/pEKm6IGXo/5dioeP66Fq5brTldgi<br />
Z7do5bbFjykQIsx6PMCBre4iUJ7jcwrwD2MDs69XwuuHL+fMKy9hD<br />
UJQPDEgDskWXFjp6jPWFXoWVSNb4H1zjQpW</p>
</blockquote>
<p style="text-align: justify;">Which is, in fact, a <a href="http://en.wikipedia.org/wiki/Base64" target="_blank">base64</a> encoded password protected <a href="http://en.wikipedia.org/wiki/RAR" target="_blank">RAR</a> file. But, what's the password? The password is the PNG image number sequence converted to lower case text. So, its: <a href="http://www.google.com/search?q=538b327278bbab654747288999fbcda1" target="_blank">538b327278bbab654747288999fbcda1</a> which isn't an MD5 and needs not to be bruteforced. Once its entered and the RAR file is decrypted we can see the file named "file" containing the following text: "Password is: <a href="http://www.google.com/search?q=9ec4c12949a4f31474f299058ce2b22a" target="_blank">9ec4c12949a4f31474f299058ce2b22a</a>". And that's it, the challenge is successfully completed at that point. No more stego or hidden files.</p>
<p style="text-align: justify;">There are six accepted solutions to this challenge, but the one that simply astonished us is the following python script which solves our challenge:</p>
<blockquote><p>#! /usr/bin/env python<br />
URL="<a href="http://blog.reversinglabs.com/wp-content/uploads/2010/07/r.zip" target="_blank">http://blog.reversinglabs.com/wp-content/uploads/2010/07/r.zip</a>"</p>
<p>import os<br />
import urllib2<br />
import struct</p>
<p>os.chdir("/tmp")<br />
rzip=urllib2.urlopen("<a href="http://blog.reversinglabs.com/wp-content/uploads/2010/07/r.zip" target="_blank">http://blog.reversinglabs.com/wp-content/uploads/2010/07/r.zip</a>").read()<br />
r7z = rzip[rzip.find("7z"):]<br />
open("r.7z","w").write(r7z)<br />
os.system("7z e r.7z")<br />
cab = open("puzzle.cab").read()<br />
os.system("cabextract puzzle.cab")<br />
open("r.rar","w").write(cab[0x5a:0x5a+250].decode("base64"))<br />
png = open("ReversingLabs.png").read()<br />
ppos = png.find("pSWD")<br />
sz, = struct.unpack("&gt;I", png[ppos-4:ppos])<br />
pwd = png[ppos+4:ppos+4+sz]<br />
os.system("unrar e -P%s r.rar" % pwd.encode("hex"))<br />
print open("file").read()</p></blockquote>
<p style="text-align: justify;">Thanks to everyone who participated in our little competition. Winners, your T-shirts are in the mail. Until our next challenge...</p>
<p><!-- Facebook Badge START --></p>
<table border="0" cellspacing="0" cellpadding="0" width="600" align="center">
<tbody>
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;amp; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="NyxEngine" href="http://www.facebook.com/pages/NyxEngine/101460583240402" target="_TOP">NyxEngine</a><br />
<a title="NyxEngine" href="http://www.facebook.com/pages/NyxEngine/101460583240402" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/101460583240402.92.1401198119.png" alt="" width="120" height="146" /></a><br />
<a style="font-family: &amp;amp;amp; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">Our challenge got beaten by our own NyxEngine! Oh, Nyx...</td>
</tr>
</tbody>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/07/reversinglabs-summer-challenge/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Everything in one go</title>
		<link>http://blog.reversinglabs.com/2010/07/everything-in-one-go/</link>
		<comments>http://blog.reversinglabs.com/2010/07/everything-in-one-go/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 10:37:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[Unpacker]]></category>
		<category><![CDATA[UPX]]></category>
		<category><![CDATA[Video]]></category>

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

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=688</guid>
		<description><![CDATA[If you remember not so long ago we wrote about minor inconveniences we encountered while working with OllyDBG. Today we return to that subject with challenges we face when reversing modern software protectors. One such protection is SafeEngine or NoobyProtect, which uses a simple portable executable malformation in order to crash OllyDBG. Even though this [...]]]></description>
			<content:encoded><![CDATA[<div style="text-align: center;">
	<script type="text/javascript">
		var flashvars = {};
		flashvars.playOnStart = "false";
		flashvars.startVolume = "70";
		flashvars.autoHideOther = "false";
		flashvars.autoHideVideoControls = "false";
		flashvars.onStartShowControls = "true";
		flashvars.fullVideoScale = "true";
		flashvars.showPlayButton = "true";
		flashvars.share = "false";
		flashvars.MediaLink2 = "http://www.youtube.com/watch?v=-3nwk9DPsN4";
		flashvars.image = "http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/lib/img.php?v=-3nwk9DPsN4";
		var params = {};
		params.bgcolor = "#000000";
		params.allowfullscreen = "true";
		params.wmode = "opaque";
		var attributes = {};
		attributes.id = "myplayer";
		swfobject.embedSWF("http://blog.reversinglabs.com/wp-content/plugins/youtube-with-style/lib/player.swf", "myAlternativeContent", "460", "310", "9.0.0", false, flashvars, params, attributes);
	</script> <div id="myAlternativeContent">blah</div></div>
<p style="text-align: justify;">If you remember not so long ago we wrote about <a href="http://blog.reversinglabs.com/2010/02/minor-inconvenience/">minor inconveniences</a> we encountered while working with OllyDBG. Today we return to that subject with challenges we face when reversing modern software protectors. One such protection is <a href="http://www.safengine.com/" target="_blank">SafeEngine</a> or NoobyProtect, which uses a simple portable executable malformation in order to crash <a href="http://www.ollydbg.de/" target="_blank">OllyDBG</a>. Even though this crash is limited to old OllyDBG versions, it is interesting for us nevertheless. The crash itself wouldn't be interesting if, in fact, the application didn't then operate normally. But it does, which makes it even more sinister. This leads us to conclude that this portable executable malformation is either ignored by the system or irrelevant to the system loader ... but not to our debugger. So, where's the trick? What does it apply to?</p>
<p style="text-align: justify;">If you look at the import table you will see that the first entry is rather unusual. Its name in LordPE is "kernel32...". Which could be fine since the trailing ".dll" is optional. If only it wasn't for those three little dots. Those dots represent multiple new lines after the name. In fact if we open the file with a hex editor we see that there are exactly 0x100 bytes following the first import library name. And in addition, there are 0x3000 bytes that follow the name of the first imported function in the first library trunk. These extra bytes seem to be too large for Olly to handle, overwriting some memory and ultimately making it crash. Since EIP is rewritten to 0x0d0a0d0a this could be an exploitable attack vector. Now we knew about this kind of attack for a while, having first seen it as an attack via too long names in the export table. But this is a bigger issue, since the <em>import</em> table is affected.</p>
<p style="text-align: justify;">If we investigate further we find an even more interesting issue that arises because of the way Windows processes the import table. If the import address table pointer inside the image import descriptor isn't nil and if it points to a sequence of eight zero bytes, the import entry will be ignored completely. Also, if the import lookup table pointer is valid and it points to at least one valid import address pointer, tools such as LordPE will read that data and display a list of functions that never get imported. This means that Windows will ignore the image import descriptor entry completely, which makes the dynamic link library name irrelevant as it will never reach the loading stage. This enables anyone to craft a file that only looks like its importing a specified library which may or may not be present on the system. And that can create confusion about which library is actually needed and which isn't.</p>
<p style="text-align: justify;">This isn't something that NoobyProtect does - but it could if it wanted to. Working around our crash problem is as easy as updating to OllyDBG 2.0 or patching the broken import table. Until next week...</p>
<p style="text-align: center;"><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/06/sampleFile.zip">sampleFile</a><br />
(package contains a harmless sample file that imports a non existent DLL)</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/06/reversing-self-defense/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unpacking by hooking?</title>
		<link>http://blog.reversinglabs.com/2010/06/unpacking-by-hooking/</link>
		<comments>http://blog.reversinglabs.com/2010/06/unpacking-by-hooking/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 13:31:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[Hooks]]></category>
		<category><![CDATA[Unpacker]]></category>
		<category><![CDATA[UPX]]></category>

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

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

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=620</guid>
		<description><![CDATA[We are going to start today's blog with a short apology about the TitanEngine 2.0.3 availability during last week. Issue was that during certain amount of time during last week the old TitanEngine 2.0.2 was distributed instead of the fresh new version. This happened mainly because we were moving our hosting to a new server [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">We are going to start today's blog with a short apology about the <a href="http://blog.reversinglabs.com/2010/05/titanengine-update/">TitanEngine 2.0.3</a> availability during last week. Issue was that during certain amount of time during last week the old TitanEngine 2.0.2 was distributed instead of the fresh new version. This happened mainly because we were moving our hosting to a new server and mixed-up the TitanEngine packages.  We apologize for any inconvenience this might have caused and urge the users to update to current engine version. With that out of the way we can focus on the task at hand.</p>
<p style="text-align: justify;">We have already talked about fixing the <a href="http://blog.reversinglabs.com/2010/03/fixing-broken-files-with-nexus/">damaged, broken or missing files</a> in several occasions. Based on what we know we created the <a href="http://blog.reversinglabs.com/tag/nexus/">Nexus</a> TitanEngine plugin to deal with cases of missing dependencies and damaged files. Implementing the basic TitanEngine features to correct file abnormalities does however change the file <a href="http://en.wikipedia.org/wiki/Checksum" target="_blank">checksum</a> since modifications  needed to correct detected problems modify file and memory content. And that doesn't go well with software protections that check the file integrity during execution. One of those software protectors is <a href="http://www.softpedia.com/get/Programming/Packers-Crypters-Protectors/Telock.shtml" target="_blank">tELock</a>, and that is the starting point for today's blog. That and a question "How can we work around checksums when file repairing is necessary?".</p>
<p style="text-align: justify;">Luckily for us most software protections only check the file integrity on disk while the memory integrity checks are only limited to protected data and the protection itself. Therefore we only need to worry about the integrity of the file on disk. To be able to fool any software protection integrity check in a generic way we need to know how these checks are performed. Usually is as simple as opening a file, reading its content in a buffer, hashing it with a custom hashing algorithm and checking if the hash is different then the one stored during file protection. So the logical place to catch the integrity checks is by hooking functions used open the file. Most commonly that involves hooking CreateFile API since all protections use it to gain access to protected file.</p>
<p style="text-align: justify;">Hooking an API in a remote process is easy but not very practical since it involves injecting a DLL into the unpacking process and that isn't something we want to do. Other option is to set a breakpoint at the selected API and filter the information returned to the protection. In order to fool the checksum checks we do the following:</p>
<ul>
<li>Detect if the file is broken (Nexus already did this)</li>
<li>Correct the damaged file and produce a backup file (Nexus already did this)</li>
<li>Catch all calls to CreateFileW API to determine when the integrity check is performed</li>
<li>Open a handle to backup file (which is valid for execution since its checksum is unaltered)</li>
<li>Pass the open handle back to protector so that backup file is hashed and its checksum is confirmed</li>
</ul>
<p style="text-align: justify;">Since we only place a breakpoint on CreateFileW API we need to filter the information somehow to make the program open the backup file which is unaltered and therefore has the correct checksum. We can alter the parameter string and possibly corrupt the memory or we can pass the correct handle back to the protection. To do that we open a handle to backup file inside the context of the debugger and duplicate it inside the context of the unpacking process. That new handle is then used by the software protection to read the data from the backup file which successfully fools any integrity check regardless of the checksum algorithm used. We do this handle switch only if the file which the protected file is trying to open is the file we are currently unpacking. Since this method is generic we can use it for any software protection, not just tELock.</p>
<p style="text-align: justify;">To test out theory we intentionally damage the sample file by modifying a single non relevant byte. This damaged file is now named <strong>damaged.exe</strong> and the backup file which is the original one is named <strong>damaged.exe.bak.</strong> If we try to unpack <strong>damaged.exe</strong> file the unpacker will unpack the file correctly regardless of the damage done to the file. This process effectively simulates the scenario in which the Nexus plugin automatically corrects the damaged file. Until next week...</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/05/NexusChecksum.zip">NexusCheckSum</a><br />
(package contains the plugin with source and the samples  used)</p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/05/working-around-checksums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Back to the basics</title>
		<link>http://blog.reversinglabs.com/2010/05/back-to-the-basics/</link>
		<comments>http://blog.reversinglabs.com/2010/05/back-to-the-basics/#comments</comments>
		<pubDate>Tue, 11 May 2010 21:33:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[LUA]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Unpacker]]></category>

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

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=590</guid>
		<description><![CDATA[After few months of intense work and code polishing we are proud to present the next major update for the TitanEngine project. Latest update we labeled as TitanEngine 2.0.3. Even though the version incrementation is small the number of changes and the pure size of the code is vast. That is why we dedicate today's [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/05/TE203.png" rel="lightbox[590]"><img class="aligncenter size-full wp-image-597" title="TE203" src="http://blog.reversinglabs.com/wp-content/uploads/2010/05/TE203.png" alt="" width="588" height="226" /></a></p>
<p style="text-align: justify;">After few months of intense work and code polishing we are proud to present the next major update for the <a href="http://www.reversinglabs.com/products/TitanEngine.php" target="_blank">TitanEngine</a> project. Latest update we labeled as TitanEngine 2.0.3. Even though the version incrementation is small the number of changes  and the pure size of the code is vast. That is why we dedicate today's  blog for listing all additions and changes done to the engine. So, what is new?</p>
<p style="text-align: justify;">This update can be declared as a script update as we were driven by the idea that the TitanEngine should be as easy to use as and as widespread as possible. That is why we have extended the programming language support to <a href="http://blog.reversinglabs.com/2009/12/titanengine-on-lua/" target="_blank">LUA</a> and <a href="http://blog.reversinglabs.com/2010/02/titanengine-on-python/" target="_blank">Python</a>, two very popular script languages. Traditionally these script languages execute in a console window with little to no graphical user interface which is why for this update we have integrated easy to use unpacker interface. This interface can be customized to fit your unpacker project and still enables you to use the TitanEngine with any supported script language without the need to make your unpacker a console project. But we didn't stop there, we realized that with more than 400 functions TitanEngine can be overwhelming at first glance. That is why we tried to <a href="http://blog.reversinglabs.com/2010/02/te-simplification-project/" target="_blank">simplify</a> writing the most basic dynamic unpackers, lowering the knowledge barrier to learning only 5 engine's functions.</p>
<p style="text-align: justify;">In addition to this we introduced a way to set any breakpoint type, be it INT3 single or double byte or UD2 breakpoints. This option was present as a global setting in the engine making it possible to set only one breakpoint type at the time. Now you can set different breakpoints for any byte pattern you choose within the same existing breakpoint manipulation functions.</p>
<p>Release without bug fixes is unimaginable. In this release we fixed  all bugs that we are aware of. Thank you for all your reports, you keep <em>TitanEngine </em>with as bugs free as possible.</p>
<p style="text-align: justify;"><strong>*Note:</strong> If you downloaded the old <em>TitanEngine 2.0.2 </em>instead of the new one please re-download the package as there was a small mix-up with the files while moving hosting servers. We apologize for the inconvenience.</p>
<p><strong>TitanEngine 2.0.3 in numbers<br />
</strong></p>
<ul>
<li>405 functions</li>
<li>28,000+ lines of code</li>
<li>44 usage samples</li>
<li>6 supported programming languages</li>
<li>390 pages of documentation</li>
<li>1 <a href="http://www.reversinglabs.com/products/TitanEngine.php" target="_blank">download</a> waiting to happen...</li>
</ul>
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2009/12/RL.png" rel="lightbox[590]"><img class="aligncenter size-full wp-image-191" title="RL" src="http://blog.reversinglabs.com/wp-content/uploads/2009/12/RL.png" alt="" width="150" height="50" /></a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/05/titanengine-update/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unpacking archives with TitanEngine</title>
		<link>http://blog.reversinglabs.com/2010/04/unpacking-archives-with-titanengine/</link>
		<comments>http://blog.reversinglabs.com/2010/04/unpacking-archives-with-titanengine/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 15:01:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Reversing]]></category>
		<category><![CDATA[TitanEngine]]></category>
		<category><![CDATA[BlackHat]]></category>
		<category><![CDATA[DEB]]></category>
		<category><![CDATA[Unpacker]]></category>

		<guid isPermaLink="false">http://blog.reversinglabs.com/?p=471</guid>
		<description><![CDATA[TitanEngine is primarily envisioned as a portable executable file format unpacker and handling framework. However due to its static unpacking functions it can be used to unpack other file format types such as installers and archives. That is why today we are showing the utilization of the new static unpacking functions that will be available [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><em>TitanEngine </em>is primarily envisioned as a portable executable file format unpacker and handling framework. However due to its static unpacking functions it can be used to unpack other file format types such as installers and archives. That is why today we are showing the utilization of the new static unpacking functions that will be available with the next update. We are discussing the upcoming features which is something we generally like to avoid but it is for a good reason. It is only because of the unveiling of the new SDK we have secretly been working on during these last few months that we are even touching the archive unpacking subject. What is it and what does it do will be talked about on <a href="http://blackhat.com/html/bh-eu-10/bh-eu-10-briefings.html#Vuksan" target="_blank">BlackHat Europe</a> next week. Until then we will tickle your imagination with an unusual blog about unpacking archives with TitanEngine.</p>
<p style="text-align: justify;">Format we have selected is a simple a <a href="http://www.debian.org/" target="_blank">Debian</a> archive file format called <a href="http://en.wikipedia.org/wiki/Deb_%28file_format%29" target="_blank">Deb</a>. Debian packages (DEB files) are standard Unix <a href="http://en.wikipedia.org/wiki/Ar_%28Unix%29" target="_blank">ar archives</a> that include two gzipped, bzipped or lzmaed <a href="http://en.wikipedia.org/wiki/Tar_%28file_format%29" target="_blank">tar</a> archives: one that holds the control information and another that contains the data. These two files present in the archive are not compressed but instead they are just stored inside the binary package. Each stored item has its own header, which is defined like this:</p>
<blockquote>
<pre class="cpp"><span style="">typedef</span> <span style="">struct</span> DEB_HEADER<span style="color: #FFFFFF;">&#123;</span>
    <span style="">char</span> FileName<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">16</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> FileTime<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">12</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> Reserved0<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> Reserved1<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> Mode<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> ItemSize<span style="color: #FFFFFF;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #FFFFFF;">&#93;</span>;
    <span style="">char</span> TerminateQuote;
    <span style="">char</span> TerminateNewLine;
<span style="color: #FFFFFF;">&#125;</span>DEB_HEADER, *PDEB_HEADER;</pre>
</blockquote>
<p style="text-align: justify;">Preceding the first header which is used to describe the archive is the magic string "!&lt;arch&gt;\n" which is used to identify the binary package type. Therefore unpacking the DEB archive format is essentially reading the archive header and copying the binary content that follows it to the selected folder. Header for each binary content contains the file name and time information which can be used during the unpacking process to restore the packed item to its pre-packing state. Because this file format doesn't employ any compression by itself unpacking the DEB format only refers to extraction of the stored binary content. That content is additionally packed but with a different file format which commonly uses compression to reduce the size of the packed file on disk.</p>
<p style="text-align: justify;">This is just one of the many uses for TitanEngine outside the area of unpacking and processing portable executable file format. As we have seen unpacking archives with TitanEngine is quite possible as long as there is no compression or content decompression is supported by the engine. Keep an eye out for our blog next week when we unveil our super secret project. Until then...</p>
<p><!-- Facebook Badge START --></p>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="150" align="center" valign="middle"><a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP">TitanEngine</a><br />
<a title="TitanEngine" href="http://www.facebook.com/pages/TitanEngine/136818796342291" target="_TOP"><img style="border: 0px;" src="http://badge.facebook.com/badge/136818796342291.1698.1945128657.png" alt="" width="120" height="144" /></a><br />
<a style="font-family: &amp;amp;quot; font-size: 11px; font-variant: normal; font-style: normal; font-weight: normal; color: #3b5998; text-decoration: none;" title="" href="http://www.reversinglabs.com" target="_TOP">ReversingLabs Corporation</a></td>
<td width="450" align="center" valign="middle">
<p><a href="http://blog.reversinglabs.com/wp-content/uploads/2010/04/DebUnpacker.zip">DEB unpacker</a><br />
(package contains source code, binary unpacker and a sample archive)</p>
</td>
</tr>
</table>
<p><!-- Facebook Badge END --></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.reversinglabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.reversinglabs.com/2010/04/unpacking-archives-with-titanengine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
