<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>My infected computer</title>
	<atom:link href="http://zairon.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://zairon.wordpress.com</link>
	<description>something strange happens inside it</description>
	<lastBuildDate>Mon, 20 May 2013 07:32:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='zairon.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>My infected computer</title>
		<link>http://zairon.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://zairon.wordpress.com/osd.xml" title="My infected computer" />
	<atom:link rel='hub' href='http://zairon.wordpress.com/?pushpress=hub'/>
		<item>
		<title>WhatsApp Backup Inspector</title>
		<link>http://zairon.wordpress.com/2013/04/22/whatsapp-backup-inspector/</link>
		<comments>http://zairon.wordpress.com/2013/04/22/whatsapp-backup-inspector/#comments</comments>
		<pubDate>Sun, 21 Apr 2013 22:21:57 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[Forensics]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=677</guid>
		<description><![CDATA[In these days I had the opportunity to retrieve some information from an Iphone backup, in the specific case I was interested in WhatsApp&#8217;s messages. Nothing hard per se, it&#8217;s all inside some database files and with a tool like SQLite Database Browser you can easily access all the needed information but the annoying thing [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=677&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In these days I had the opportunity to retrieve some information from an Iphone backup, in the specific case I was interested in WhatsApp&#8217;s messages. Nothing hard per se, it&#8217;s all inside some database files and with a tool like SQLite Database Browser you can easily access all the needed information but the annoying thing is represented by the interpretation of the result of your queries.<br />
I didn&#8217;t have an immediate deadline for the analysis so I decided to code a little extractor for WhatsApp. Here are some screenshots:</p>
<p>- all the available chats on the top and the selected chat below:<br />
<a href="http://zairon.files.wordpress.com/2013/04/gui.png"><img src="http://zairon.files.wordpress.com/2013/04/gui.png?w=300&#038;h=199" alt="Gui" width="300" height="199" class="aligncenter size-medium wp-image-672" /></a></p>
<p>- it can display images, movies and audio. A click will open the associated Windows program able to show/play the media:<br />
<a href="http://zairon.files.wordpress.com/2013/04/image.png"><img src="http://zairon.files.wordpress.com/2013/04/image.png?w=300&#038;h=46" alt="Image" width="300" height="46" class="aligncenter size-medium wp-image-673" /></a><br />
<a href="http://zairon.files.wordpress.com/2013/04/movie.png"><img src="http://zairon.files.wordpress.com/2013/04/movie.png?w=300&#038;h=141" alt="Movie" width="300" height="141" class="aligncenter size-medium wp-image-674" /></a><br />
<a href="http://zairon.files.wordpress.com/2013/04/audio.png"><img src="http://zairon.files.wordpress.com/2013/04/audio.png?w=300&#038;h=42" alt="Audio" width="300" height="42" class="aligncenter size-medium wp-image-668" /></a></p>
<p>- it shows deleted messages:<br />
<a href="http://zairon.files.wordpress.com/2013/04/deleted.png"><img src="http://zairon.files.wordpress.com/2013/04/deleted.png?w=300&#038;h=42" alt="Deleted" width="300" height="42" class="aligncenter size-medium wp-image-671" /></a></p>
<p>- events:<br />
<a href="http://zairon.files.wordpress.com/2013/04/events.png"><img src="http://zairon.files.wordpress.com/2013/04/events.png?w=300&#038;h=38" alt="Events" width="300" height="38" class="aligncenter size-medium wp-image-682" /></a></p>
<p>- and messages that are not sent due to an internal error:<br />
<a href="http://zairon.files.wordpress.com/2013/04/not-sent.png"><img src="http://zairon.files.wordpress.com/2013/04/not-sent.png?w=300&#038;h=81" alt="Not sent" width="300" height="81" class="aligncenter size-medium wp-image-675" /></a></p>
<p>- I can get the onwer&#8217;s info:<br />
<a href="http://zairon.files.wordpress.com/2013/04/owner.png"><img src="http://zairon.files.wordpress.com/2013/04/owner.png?w=300&#038;h=131" alt="Owner" width="300" height="131" class="aligncenter size-medium wp-image-676" /></a></p>
<p>- and the contacts info too:<br />
<a href="http://zairon.files.wordpress.com/2013/04/contacts.png"><img src="http://zairon.files.wordpress.com/2013/04/contacts.png?w=300&#038;h=207" alt="Contacts" width="300" height="207" class="aligncenter size-medium wp-image-670" /></a></p>
<p>The program has been tested on this configuration:<br />
Os: Win7/XP<br />
iOS: 6.1.3<br />
WhatsApp: 2.8.7<br />
It&#8217;s just a personal project but if you want you can try it. Download WABI from <a href="https://www.dropbox.com/s/nn1rsdoypunz8zk/WABI.zip" title="WABI" target="_blank">here</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/677/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/677/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=677&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2013/04/22/whatsapp-backup-inspector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/gui.png?w=300" medium="image">
			<media:title type="html">Gui</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/image.png?w=300" medium="image">
			<media:title type="html">Image</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/movie.png?w=300" medium="image">
			<media:title type="html">Movie</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/audio.png?w=300" medium="image">
			<media:title type="html">Audio</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/deleted.png?w=300" medium="image">
			<media:title type="html">Deleted</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/events.png?w=300" medium="image">
			<media:title type="html">Events</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/not-sent.png?w=300" medium="image">
			<media:title type="html">Not sent</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/owner.png?w=300" medium="image">
			<media:title type="html">Owner</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2013/04/contacts.png?w=300" medium="image">
			<media:title type="html">Contacts</media:title>
		</media:content>
	</item>
		<item>
		<title>Female reverse engineering challenge</title>
		<link>http://zairon.wordpress.com/2013/04/02/female-reverse-engineering-challenge/</link>
		<comments>http://zairon.wordpress.com/2013/04/02/female-reverse-engineering-challenge/#comments</comments>
		<pubDate>Tue, 02 Apr 2013 10:35:33 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=639</guid>
		<description><![CDATA[Two months ago Halvar Flake announced a new malware challenge for female reversers only. I&#8217;m a man and I couldn&#8217;t submit a solution but I decided to give it a try, challenges are always funny. I shared this reversing session with Kayaker, so credit for this blog post goes to him too. The solution posted [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=639&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Two months ago <a href="http://addxorrol.blogspot.it/2013/01/encouraging-female-reverse-engineers.html" target="_blank">Halvar Flake</a> announced a new malware challenge for female reversers only. I&#8217;m a man and I couldn&#8217;t submit a solution but I decided to give it a try, challenges are always funny. I shared this reversing session with Kayaker, so credit for this blog post goes to him too.</p>
<p>The solution posted by <a href="https://docs.google.com/file/d/0B5hBKwgSgYFaVmxTaFk3OXl4cjg/edit?pli=1" target="_blank">Marion Marschalek</a> (congratulations!) is pretty nice and it explains almost everything in details. I&#8217;m not writing a blog post with the same information she gave, but I would like to add something about the way I used to automatically naming/resolving the imported functions.<br />
The idea is to change instructions like:<br />
<code>.text:0040100B call    dword ptr [ecx+220h]</code><br />
into something like:<br />
<code>.text:0040100B call    dword ptr [ecx+_API.malloc]</code></p>
<p>To perform this switch I have written an IDC <a href="https://www.dropbox.com/s/7ralanfll773z9l/Girl_Contest.idc" target="_blank">script</a>. There are two functions inside it, GetAPINames and	ResolveAPINames. The first function is used to retrieve the name of all the hidden API while the other one will change the call instructions into a new readable version.<br />
The script stores all the information inside a structure named _API which is filled with all the API names. The structure is necessary and I&#8217;ll use it for some minor manual fix too.</p>
<p><strong><em><u>GetAPINames</u></em></strong><br />
There&#8217;s no trace of clear API names inside the disasm, everything is constructed at runtime inside <em>call 402DB0</em>, take a look at this piece of code (without unnecessary junk code lines):</p>
<p><code>00403E42 C6 44 24 24 43   mov     [esp+179CCh+var_179A8], 'C'<br />
...<br />
00403E49 C6 44 24 25 72   mov     [esp+179CCh+var_179A7], 'r'<br />
...<br />
00403EC1 C6 44 24 26 65   mov     [esp+179CCh+var_179A6], 'e'<br />
...<br />
00403F14 C6 44 24 2F 61   mov     [esp+179D4h+var_179A5], 'a'<br />
00403F19 C6 44 24 30 74   mov     [esp+179D4h+var_179A4], 't'<br />
00403F1E C6 44 24 31 65   mov     [esp+179D4h+var_179A3], 'e'<br />
00403F23 C6 44 24 32 54   mov     [esp+179D4h+var_179A2], 'T'<br />
00403F28 C6 44 24 33 68   mov     [esp+179D4h+var_179A1], 'h'<br />
00403F2D C6 44 24 34 72   mov     [esp+179D4h+var_179A0], 'r'<br />
00403F32 C6 44 24 35 65   mov     [esp+179D4h+var_1799F], 'e'<br />
00403F37 C6 44 24 36 61   mov     [esp+179D4h+var_1799E], 'a'<br />
00403F3C C6 44 24 37 64   mov     [esp+179D4h+var_1799D], 'd'<br />
00403F41 C6 44 24 38 00   mov     [esp+179D4h+var_1799C], 0</code></p>
<p>As you can see <em>CreateThread</em> string is obtained appending every single char. To create names the malware uses another similar way:</p>
<p><code>00406EC9 C6 84 24 BC 0F 00 00 47    mov     [esp+179CCh+var_16A10], 'G'<br />
00406ED1 C6 84 24 BD 0F 00 00 65    mov     [esp+179CCh+var_16A0F], 'e'<br />
...<br />
00406F4C C6 84 24 BE 0F 00 00 74    mov     [esp+179CCh+var_16A0E], 't'<br />
...<br />
00406FA5 C6 84 24 C7 0F 00 00 4D    mov     [esp+179D4h+var_16A0D], 'M'<br />
00406FAD C6 84 24 C8 0F 00 00 6F    mov     [esp+179D4h+var_16A0C], 'o'<br />
00406FB5 C6 84 24 C9 0F 00 00 64    mov     [esp+179D4h+var_16A0B], 'd'<br />
00406FBD C6 84 24 CA 0F 00 00 75    mov     [esp+179D4h+var_16A0A], 'u'<br />
00406FC5 C6 84 24 CB 0F 00 00 6C    mov     [esp+179D4h+var_16A09], 'l'<br />
00406FCD C6 84 24 CC 0F 00 00 65    mov     [esp+179D4h+var_16A08], 'e'<br />
00406FD5 C6 84 24 CD 0F 00 00 46    mov     [esp+179D4h+var_16A07], 'F'<br />
00406FDD C6 84 24 CE 0F 00 00 69    mov     [esp+179D4h+var_16A06], 'i'<br />
00406FE5 C6 84 24 CF 0F 00 00 6C    mov     [esp+179D4h+var_16A05], 'l'<br />
00406FED C6 84 24 D0 0F 00 00 65    mov     [esp+179D4h+var_16A04], 'e'<br />
00406FF5 C6 84 24 D1 0F 00 00 4E    mov     [esp+179D4h+var_16A03], 'N'<br />
00406FFD C6 84 24 D2 0F 00 00 61    mov     [esp+179D4h+var_16A02], 'a'<br />
00407005 C6 84 24 D3 0F 00 00 6D    mov     [esp+179D4h+var_16A01], 'm'<br />
0040700D C6 84 24 D4 0F 00 00 65    mov     [esp+179D4h+var_16A00], 'e'<br />
00407015 C6 84 24 D5 0F 00 00 45    mov     [esp+179D4h+var_169FF], 'E'<br />
0040701D C6 84 24 D6 0F 00 00 78    mov     [esp+179D4h+var_169FE], 'x'<br />
00407025 C6 84 24 D7 0F 00 00 41    mov     [esp+179D4h+var_169FD], 'A'<br />
0040702D C6 84 24 D8 0F 00 00 00    mov     [esp+179D4h+var_169FC], 0<br />
</code><br />
The way used to create <em>GetModuleFileNameExA</em> is pretty similar to the previous one but there&#8217;s a little difference, look at the opcodes. The <em>mov</em> instructions are similar but the ModR/M byte defines a distinct displacement.<br />
Is it possible to recognize and isolate all the instructions used to create all those strings? Well, it&#8217;s not so hard because some bytes are fixed! The idea is to parse all the instructions inside 402DB0 trying to recognize those two special <em>mov</em> instructions:</p>
<p><code>if (Byte(currAddress) == 0xC6) {<br />
&nbsp;&nbsp;if (Byte(currAddress+1) == 0x44) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (Byte(currAddress+2) == 0x24) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Byte(currAddress+4) != 0x00) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Get current char and append it to partial name<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;szChar = sprintf("%c", Byte(currAddress+4));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;szAPI = sprintf("%s", szAPI + szChar);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((strstr(szAPI, ".DLL") == -1) &amp;&amp; (strstr(szAPI, ".dll") == -1))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//   Add member to struct (no DLL name)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddStrucMember(id, szAPI, -1, FF_DATA, -1, 4);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;szAPI = sprintf("%s", "");   // reset for next string<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
}<br />
</code></p>
<p>The instructions are checked byte by byte and the strings are created char by char. szChar is the current char to append to the partial string szAPI.<br />
There&#8217;s a little problem with this parser, it constructs DLL names too. I&#8217;m not interested in DLL names, so a check over the formatted string is necessary:</p>
<p><code>if ((strstr(szAPI, ".DLL") == -1) &amp;&amp; (strstr(szAPI, ".dll") == -1))</code></p>
<p>Now that I&#8217;m sure I don&#8217;t have a DLL name I can insert it into the structure:</p>
<p><code>AddStrucMember(id, szAPI, -1, FF_DATA, -1, 4);</code></p>
<p>The use of the structure is fondamental for the script.<br />
Now that you know how to parse the first type of <em>mov</em> instruction you can easily change some checks over the fixed bytes and you&#8217;ll retrieve names like GetModuleFileNameExA too:</p>
<p><code>if (Byte(currAddress) == 0xC6) {<br />
&nbsp;&nbsp;if (Byte(currAddress+1) == 0x84) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (Byte(currAddress+2) == 0x24) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Byte(currAddress+5) == 0x00) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Byte(currAddress+6) == 0x00) &nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Byte(currAddress+7) != 0x00) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...		</code>														</p>
<p>This part of the script works pretty fine but it has a little problem with few functions. To understand it here is an example with GetQueuedCompletionStatus:</p>
<p><code>00405C2A   mov     [esp+179CCh+var_179A8], 'G'<br />
00405C31   mov     [esp+179CCh+var_179A7], 'e'<br />
00405CA9   mov     [esp+179CCh+var_179A6], 't'<br />
00405CF3   mov     bl, 'Q'<br />
00405CFE   mov     [esp+179D4h+var_179A5], bl<br />
00405D02   mov     [esp+179D4h+var_179A4], 'u'<br />
00405D07   mov     [esp+179D4h+var_179A3], 'e'<br />
00405D0C   mov     [esp+179D4h+var_179A2], 'u'<br />
00405D11   mov     [esp+179D4h+var_179A1], 'e'<br />
00405D16   mov     [esp+179D4h+var_179A0], 'd'<br />
00405D1B   mov     [esp+179D4h+var_1799F], 'C'<br />
00405D20   mov     [esp+179D4h+var_1799E], 'o'<br />
00405D25   mov     [esp+179D4h+var_1799D], 'm'<br />
	...</code></p>
<p>As you can see the letter &#8216;Q&#8217; is obtained by a sequence of two instructions and my script is not able to catch it; it creates <em>GetueuedCompletionStatus</em> name. It has been proved that human brain is able to recognize word without few letters or with scrambled letters so I think I can pass over this minor problem!</p>
<p><strong><em><u>ResolveAPINames</u></em></strong><br />
Ok, now that I have the API structure I need to use it for the resolution part. The function ResolveAPINames scans the entire disasmed code trying to fix the necessary calls. To identify the call you can use a simple <em>strstr</em> function, and to convert it you can use OpStroff (it converts operand to an offset in a structure):</p>
<p><code>if(strstr(GetDisasm(ea), "call    dword ptr") != -1) {<br />
&nbsp;&nbsp;OpStroff(ea, 0, GetStrucIdByName("_API"));</code></p>
<p>The malware uses a nice addressing method and IDA is not able to parse the hidden API but the nature of the addressing method lets us to solve the problem with some lines of code only. Now you can understand why the structure is the core of the entire script.</p>
<p><strong><em><u>Manual fix</u></em></strong><br />
The script is able to resolve 678 calls, but it fails to fix some special cases like this:</p>
<p><code>00414E36   lea     esi, [eax+1FCh]<br />
00414E3C   call    dword ptr [eax+_API.GetTickCount]<br />
00414E42   push    eax<br />
00414E43   call    dword ptr [esi]<br />
</code><br />
GetTickCount has been resolved but the next call not. It&#8217;s obvious that <em>esi</em> points to the API at offset 0x1FC. You can solve it manually because the structure contains it. Right click over 0x1FC and select the line &#8220;[eax+_API.srand]&#8220;. Now you know how to manually fix special cases too.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/639/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/639/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=639&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2013/04/02/female-reverse-engineering-challenge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>
	</item>
		<item>
		<title>promix17&#8242;s MazeJumps crackme</title>
		<link>http://zairon.wordpress.com/2013/01/14/promix17s-mazejumps-crackme/</link>
		<comments>http://zairon.wordpress.com/2013/01/14/promix17s-mazejumps-crackme/#comments</comments>
		<pubDate>Mon, 14 Jan 2013 11:51:05 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[reverse code engineering]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=623</guid>
		<description><![CDATA[I enjoy browsing crackmes.de, and I used to try a crackme from time to time. It&#8217;s not my intention to write a tutorial for this crackme, but after reading a comment from andrewl.us I decided to spend some words for this tutorial. andrewl.us&#8217;s comment is pretty simple: &#8220;two solutions accepted, and neither uses a deobfuscator [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=623&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I enjoy browsing crackmes.de, and I used to try a crackme from time to time. It&#8217;s not my intention to write a tutorial for this crackme, but after reading a comment from andrewl.us I decided to spend some words for this tutorial.<br />
andrewl.us&#8217;s comment is pretty simple: &#8220;two solutions accepted, and neither uses a deobfuscator that emits a cleaner crackme&#8221;. That&#8217;s the point, nowadays only few people spend some time writing a detailed/original/complete solution, most of them prefer to say &#8220;I did it&#8221;&#8230; that&#8217;s sad and it&#8217;s not the spirit of crackmes.de!</p>
<p><span style="text-decoration:underline;"><em><strong>Intro</strong></em></span><br />
The protection routine created by the author is really simple, but the obfuscation method applied makes our reversing session hard. Look here and you&#8217;ll understand how the code works:<br />
<code><br />
406E2A jmp short loc_406E2D<br />
...<br />
406E2D nop<br />
406E2E jmp loc_405B10<br />
...<br />
405B10 nop<br />
405B11 nop<br />
405B12 and al, 0FFh<br />
405B14 jmp loc_4053CC<br />
...<br />
</code><br />
A lot of jumps and a lot of junk code. The real challenge is to identify the valid instructions of the crackme because most of them are only useless junk code. So, to get the valid instruction you can:<br />
1. step all the crackme code using a debugger trying to identify valid and not valid instruction<br />
2. deobfuscate the crackme in some way</p>
<p>Point #1 represents the easy way because you will surely find out the valid instructions checking the code instruction by instruction. In this case it&#8217;s a good approach because the crackme is only 35kb&#8230; Will you apply this method on a huge obfuscated file? That&#8217;s why I opt for point #2!</p>
<p>Due to the nature of the protected code I think it&#8217;s not possible to write a static program able to deobfuscate an exe protected with this method, but if you imagine the exe like a set of procedures combined all together you can produce something which is pretty near to the original untouched program.<br />
To solve this crackme I&#8217;ll write an idc script able to extract valid instructions from a single procedure. Having this in mind you can then put everything all together obtaining a working deobfuscated crackme.</p>
<p><span style="text-decoration:underline;"><strong><em>The deobfuscator</em></strong></span><br />
The idea is pretty simple: starting from a specific address the script traces through the necessary instructions trying to identify (and eventually remove) junk code. The script will show all the valid instructions inside the output area.<br />
First of all I have to create the skeleton of the script.<br />
<code><br />
static main()<br />
{<br />
&nbsp;&nbsp;&nbsp;do {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;check_current_instruction;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!junk_instruction)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print_valid_instruction;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get_address_next_instruction;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;while (valid_address_instruction);<br />
}<br />
</code></p>
<p>The script checks every single instruction trying to understand if it&#8217;s valid or junk. I&#8217;ll show you later how to check it, now take a look at the snippet used to print a valid instruction:<br />
<code><br />
static print_valid_instruction(address)<br />
{<br />
&nbsp;&nbsp;&nbsp;auto instrLen;<br />
&nbsp;&nbsp;&nbsp;auto op1, op2;</p>
<p>&nbsp;&nbsp;&nbsp;Jump(address);<br />
&nbsp;&nbsp;&nbsp;MakeUnkn(address, 0);<br />
&nbsp;&nbsp;&nbsp;instrLen = MakeCode(address);<br />
&nbsp;&nbsp;&nbsp;Message("\n%X: %s", address, GetMnem(address));<br />
&nbsp;&nbsp;&nbsp;op1 = GetOpnd(address, 0);<br />
&nbsp;&nbsp;&nbsp;if (op1 != 0x00)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Message(" %s", op1);<br />
&nbsp;&nbsp;&nbsp;op2 = GetOpnd(address, 1);<br />
&nbsp;&nbsp;&nbsp;if (op2 != 0x00)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Message(", %s", op2);<br />
&nbsp;&nbsp;&nbsp;return instrLen;<br />
}<br />
</code></p>
<p>The function receives the address of the current valid instruction printing it inside the output area.<br />
I have used Jump, MakeUnkn and MakeCode because the code produced by IDA has a lot of undefined lines: with an undefined instruction is not always possible to get the right menmonic text associated to a specific opcode.<br />
So, when you have the decoded instruction you can print it with the necessary operands. The function returns the length of the current instruction, used to get the address of the next one to check.</p>
<p>Nothing hard, let&#8217;s see how to identify the junk code. The idea is to isolate all the unnecessary instructions. First of all, to have a general idea of the crackme&#8217;s code take a look at this blocks:<br />
<code><br />
.jmp:00405B10 90 nop<br />
.jmp:00405B11 90 nop<br />
.jmp:00405B12 24 FF and al, 0FFh &lt;-- al remains the same<br />
.jmp:00405B14 E9 B3 F8 FF FF jmp loc_4053CC<br />
...<br />
.jmp:00408969 40 inc eax &lt;--<br />
.jmp:0040896A 48 dec eax &lt;-- eax remains the same<br />
.jmp:0040896B 90 nop<br />
.jmp:0040896C 90 nop<br />
.jmp:0040896D E9 14 D4 FF FF jmp near ptr unk_405D86<br />
...<br />
.jmp:00405CC3 90 nop<br />
.jmp:00405CC4 80 EB 00 sub bl, 0 &lt;-- bl remains the same<br />
.jmp:00405CC7 E9 34 0E 00 00 jmp near ptr unk_406B00<br />
...<br />
.jmp:00407163 50 push eax<br />
.jmp:00407164 04 66 add al, 66h &lt;-- add inside push/pop it's nonsense...<br />
.jmp:00407166 58 pop eax<br />
.jmp:00407167 E9 0C EB FF FF jmp near ptr unk_405C78<br />
</code></p>
<p>If you execute the snippets above you&#8217;ll find out that the state of the involved register remains the same, no changes are applied.<br />
So, what can we put inside the junk code set? All the &#8220;jmp&#8221; instructions, &#8220;nop2, &#8220;and&#8221; with 0xFF and so on&#8230; If you look at the code I think you&#8217;ll surely find out what to mark as junk in a short time.<br />
Just to understand it, here is a part of the script used to avoid some specific instructions:<br />
<code><br />
&nbsp;&nbsp;&nbsp;opcode = Byte(currAddress); // Get the byte at the current address<br />
&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;else if ((opcode == 0x24) &amp;&amp; (Byte(currAddress+1) == 0xFF)) // Avoid "and op1, 0xFF" instruction<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = currAddress + 2; // Move to the next instruction<br />
&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;else if ((opcode == 0x60) &amp;&amp; (Byte(currAddress+2) == 0x61)) // Avoid instructions from pushad to popad<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = currAddress + 3;<br />
&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;else if (opcode == 0x90) // Avoid nop instruction<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = currAddress + 1;<br />
&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;if (opcode == 0xE9) // Avoid jump instruction<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = (currAddress + Dword(currAddress+1) + 5) &amp; 0x00000000FFFFFFFF;<br />
</code></p>
<p>Instead of printing the instruction I simply avoid it moving the attention to the next one. You don&#8217;t need to use MakeCode or something else because the script works directly on the bytes code.<br />
Ok, is it really all? Hmm, there is something more to add. You have to pay particular attention to a point: how can you manage a conditional jump?<br />
The question deserves a reply because sooner or later the crackme will have a check and you&#8217;ll have to face a conditional jump. I decided to let you choose the path to follow! What does it mean?<br />
I remove all the conditional jumps except the one which is after a compare instruction. I need to remember that a compare has been executed:<br />
<code><br />
&nbsp;&nbsp;&nbsp;else if (opcode == 0x3B) { // Compare found, take it in mind!<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmp = 1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = currAddress + print_valid_instruction(currAddress);<br />
&nbsp;&nbsp;&nbsp;}<br />
</code></p>
<p>The variable &#8220;cmp&#8221; is used to take in mind that a compare instruction has been executed. cmp variable is originally initialized to -1. Here is how I handle a conditional jump instruction:<br />
<code><br />
&nbsp;&nbsp;&nbsp;else if ((opcode == 0x74) || (opcode == 0x75)){<br />
&nbsp;&nbsp;&nbsp;// Check to see if a cmp instruction has been executed<br />
&nbsp;&nbsp;&nbsp;if (cmp == 1) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Jump(currAddress);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jump = AskYN(0, "Want to jump?"); // Box with a question<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (jump == 0) // No jump<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = currAddress + 2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else // Jump<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = (currAddress + Byte(currAddress+1) + 2) &amp; 0x00000000FFFFFFFF;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmp = -1; // Restore the original value<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currAddress = (currAddress + Byte(currAddress+1) + 2) &amp; 0x00000000FFFFFFFF;<br />
}<br />
</code></p>
<p>That&#8217;s what I meant when I said &#8220;I decided to let you choose the path to follow&#8221;. Pressing Yes or No inside the appeared box you can decide the flow of the crackme code, it&#8217;s just like a live trace.<br />
I decided to not print the conditional jump but if you think it will help you put it inside the set of valid instructions.</p>
<p>That&#8217;s how my deobfuscator works (get it from <a href="https://www.dropbox.com/s/xgrqs4ht80otb39/zairon_mazejumps_deobfuscator.idc" title="ZaiRoN MazeJumps Deobfuscator" target="_blank">here</a>). I don&#8217;t need anything else because with the output produced by the script I can study the entire protection routine via dead list without having to debug the program.<br />
For those who wants to study the deobfuscated crackme using a debugger it&#8217;s possible to produce a clean crackme too. Instead of printing the result in the output area you could move the valid instructions somewhere inside the file. There are a lot of free bytes to use inside the exe; just choose a starting point and with some minor adjustments, as I suggested before combining all the deobfuscated procedures, you can have a working clean crackme version.</p>
<p><span style="text-decoration:underline;"><strong><em>The protection routine</em></strong></span><br />
The algorithm used by the crackme is really simple, but it requires a brute-force approach&#8230;<br />
<code><br />
4092BA: mov cx, 54h<br />
408B0D: shl ecx, 10h<br />
405507: mov cx, 5854h<br />
405680: push ecx &lt;-- push "TXT<br />
4085F4: mov cx, 2E59h<br />
407CC1: shl ecx, 10h<br />
405B4C: mov cx, 454Bh<br />
4087F4: push ecx &lt;-- push "KEY."<br />
406705: mov edx, esp &lt;-- edx -&gt; "KEY.TXT"<br />
40881F: mov cx, 40h<br />
407FAF: shl ecx, 10h<br />
408603: mov cx, 812Fh<br />
405435: push ecx &lt;-- push 0x40812F: first instruction to execute after CreateFileA<br />
4078B7: xor eax, eax<br />
408EDE: push eax<br />
407E92: mov al, 50h<br />
408810: push eax &lt;-- Flags &amp; attributes: 0x50: FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE<br />
4070EB: mov al, 3<br />
4052CF: push eax &lt;-- OPEN_EXISTING<br />
405B5C: mov al, 0<br />
408441: push eax<br />
406E3B: push eax<br />
40868B: mov al, 50h<br />
40610A: shl eax, 18h<br />
408AF1: push eax &lt;-- Desired access: 0x50000000: GENERIC_WRITE | GENERIC_ALL<br />
4080CC: push edx &lt;-- Filename: "KEY.TXT"<br />
4092CE: call CreateFileA<br />
4092D3: ret<br />
</code></p>
<p>From the readme file we already know that the protection routine is based on a keyfile, now we know the name of the file to produce: KEY.TXT<br />
At the beginning of the tutorial I told you that it&#8217;s not possible to write a fully working static deobfuscator, and now you can understand why: look at the push at 405435. The address on the stack (0x40812F) is the point where the precedure will return after ret instruction at 4092D3. How can you identify the address as a possible return value? Well, maybe possible, but hard&#8230;</p>
<p>Ok, back to our protection routine: after executing CreateFileA function, the code pass from instruction 40812F:<br />
<code><br />
4077E5: xor edx, edx<br />
4068B9: dec edx<br />
407FDC: cmp edx, eax &lt;-- check<br />
4092D9: call ExitProcess &lt;-- here if check fails<br />
</code></p>
<p>The snippet above contains a first check which is done on the keyfile handle. If the file doesn&#8217;t exist the crackme ends. As you&#8217;ll see there are some more checks performed by the crackme, all of them end with a call to ExitProcess without a single error message.<br />
To pass this check create the file and put some bytes inside. As you can guess the next piece of code will be used to read the content of the keyfile:<br />
<code><br />
407FDC: cmp edx, eax<br />
406372: xor edx, edx<br />
406C87: push edx &lt;-- push 0<br />
40909F: push edx &lt;-- push 0<br />
405DD3: push edx &lt;-- push 0<br />
405D95: mov ebp, esp<br />
406FFC: push edx &lt;-- push 0<br />
405B01: mov esi, esp<br />
407426: push eax &lt;-- push file handle<br />
407235: mov cx, 40h<br />
406263: shl ecx, 10h<br />
406F38: mov cx, 5ADDh<br />
40759B: push ecx &lt;-- push 0x405ADD: first instruction to execute after ReadFile<br />
40778A: push edx<br />
4089C3: push esi &lt;-- bytes read<br />
407181: push 18h<br />
4070DC: push ebp &lt;-- buffer that receives the data<br />
408E56: push eax &lt;-- file handle<br />
4092C8: call ReadFile<br />
4092CD: retn<br />
</code></p>
<p>An obvious conseguence of CreateFile: ReadFile. It&#8217;s time to see how it will use the keyfile&#8217;s bytes.<br />
<code><br />
405426: mov esp, ebp &lt;-- ESP points to the contents of the keyfile<br />
40764F: mov cx, 0F45Ah<br />
40782F: shl ecx, 10h<br />
405DA4: mov cx, 675Dh<br />
4066E6: mov esi, ecx &lt;-- ESI = 0xF45A675D<br />
407B95: mov cx, 4DDAh<br />
40892D: shl ecx, 10h<br />
407CDF: mov cx, 0FA31h<br />
406B4C: mov edi, ecx &lt;-- EDI = 0x4DDAFA31<br />
406083: pop eax &lt;-- EAX = first 4 bytes of the file (Key_1)<br />
405999: pop ecx &lt;-- ECX = second 4 bytes of the file (Key_2)<br />
405A04: push ecx<br />
405EC3: push eax<br />
407B59: xor eax, esi &lt;--<br />
4075AA: xor ecx, edi &lt;-- operations based on Key_1 and Key_2<br />
4067C8: add eax, ecx &lt;--<br />
406F47: mov cx, 0BAE3h<br />
406F83: shl ecx, 10h<br />
406BB4: mov cx, 0DC73h &lt;-- ECX = 0xBAE3DC73<br />
405544: cmp eax, ecx &lt;-- first check<br />
</code></p>
<p>This first check is based on the first eight bytes of the keyfile. The scheme is:<br />
<code><br />
(Key_1 ^ 0xF45A675D) + (Key_2 ^ 0x4DDAFA31) = 0xBAE3DC73<br />
</code><br />
Nothing hard per se but without other informations if you want to solve this equation you are forced to write a brute force algorithm over key_1 and key_2. Really expansive in terms of time&#8230;<br />
<code><br />
408DCE: mov cx, 0AADDh<br />
40846E: shl ecx, 10h<br />
406E48: mov cx, 357Dh<br />
408324: mov esi, ecx &lt;-- ESI = 0xAADD357D<br />
408DEC: mov cx, 44FAh<br />
405FED: shl ecx, 10h<br />
405B2E: mov cx, 0FC3Ch<br />
4059A9: mov edi, ecx &lt;-- EDI = 0x44FAFC3C<br />
4069F3: pop eax &lt;-- EAX = Key_1<br />
4074BB: pop ecx &lt;-- ECX = Key_2<br />
4085A9: push ecx<br />
40919F: push eax<br />
4051FC: xor eax, esi &lt;--<br />
408EB0: xor ecx, edi &lt;-- operations based on Key_1 and Key_2<br />
409143: add eax, ecx &lt;--<br />
407901: mov cx, 0F23Fh<br />
40893C: shl ecx, 10h<br />
406335: mov cx, 2C88h &lt;-- ECX = 0xF23F2C88<br />
406B87: cmp eax, ecx &lt;-- second check<br />
</code></p>
<p>Another check over the keyfile&#8217;s content. As you can see it&#8217;s pretty similar to the previous one:<br />
<code><br />
(Key_1 ^ 0xAADD357D) + (Key_2 ^ 0x44FAFC3C) = 0xF23F2C88<br />
</code><br />
2 equations and 2 variables, we can reduce the time of the brute-agorithm because you can test all possible values of key_1 only (key_2 is obtained as a conseguence).<br />
<code><br />
407E66: pop esi &lt;-- ESI = Key_1<br />
407FCD: pop edi &lt;-- EDI = Key_2<br />
407228: pop ebx &lt;-- EBX = third 4 bytes of the file (Key_3)<br />
408144: mov cx, 2164h<br />
406AF1: shl ecx, 10h<br />
406A4C: mov cx, 3172h &lt;-- ECX = 0x21643172<br />
40602A: cmp ecx, ebx &lt;-- third check<br />
</code></p>
<p>4 more bytes join the party. This time no math or logic operations but a simple compare byte to byte: Key_3 must be &#8220;r1d!&#8221;. We know part of the keyfile, but this information doesn&#8217;t reduce our brute-time&#8230;<br />
<code><br />
408C84: mov cx, 6F04h<br />
406290: shl ecx, 10h<br />
4091E8: mov cx, 530Ah &lt;-- ECX = 0x6F04530A<br />
406F65: xor ecx, edi &lt;-- Key_2 ^ 0x6F04530A<br />
406F57: push ecx<br />
407514: mov cx, 0F0Fh<br />
405408: shl ecx, 10h<br />
40597B: mov cx, 101Bh &lt;-- ECX = 0x0F0F101B<br />
40819E: xor ecx, esi &lt;-- Key_1 ^ 0x0F0F101B<br />
406BD3: push ecx<br />
408D0B: mov eax, esp<br />
40638F: xor ebp, ebp<br />
405284: push ebp<br />
407AD2: push ebp &lt;-- Caption: NULL<br />
408EBF: push eax &lt;-- Text:<br />
406E95: push ebp &lt;-- 0<br />
4092D4: call MessageBoxA &lt;-- CONGRATULATION BOX<br />
4092D9: call ExitProcess<br />
</code></p>
<p>The final part of the crackme, it shows a congratulation message box. The text of the box is obtained using the correct value for Key_1 and Key_2.</p>
<p>To sum-up: there are 3 checks to pass, the first two are based on Key_1 and key_2 while the last check is over key_3 only. Key_3 values is fixed and I have to find the first two dwords (represented by Key_1 and Key_2) using a brute. The solution is not unique, you can provide some different keyfiles but I think the Valid solution is the one wich reveal the right text inside the message box. For me the valid text is &#8220;Success&#8221; that is obtained with keyfile = &#8220;Hello wor1d!&#8221; but who knows&#8230; :)</p>
<p><span style="text-decoration:underline;"><strong><em>Final notes</em></strong></span><br />
It&#8217;s indeed an easy crackme, but it represents a good starting sample for those who have never played with an obfuscated target.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/623/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=623&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2013/01/14/promix17s-mazejumps-crackme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>
	</item>
		<item>
		<title>Unusual crackme by ksydfius</title>
		<link>http://zairon.wordpress.com/2012/12/21/unusual-crackme-by-ksydfius/</link>
		<comments>http://zairon.wordpress.com/2012/12/21/unusual-crackme-by-ksydfius/#comments</comments>
		<pubDate>Thu, 20 Dec 2012 23:15:48 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[reverse code engineering]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=574</guid>
		<description><![CDATA[I use to browse crackmes.de from time to time when I want a reversing challenge to play with. I have to admit that there are much more interesting crackmes in the past, but today (digging deeper) I&#8217;ve found an unusual one; it&#8217;s not hard but the idea is really nice! The name of the crackme [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=574&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span>I use to browse <a title="crackmes.de" href="http://crackmes.de/" target="_blank">crackmes.de</a> from time to time when I want a reversing challenge to play with. I have to admit that there are much more interesting crackmes in the past, but today (digging deeper) I&#8217;ve found an unusual one; it&#8217;s not hard but the idea is really nice! The name of the crackme is &#8220;What Is This???&#8221;</span></p>
<p style="text-align:center;"><a href="http://zairon.wordpress.com/2012/12/21/unusual-crackme-by-ksydfius/crackmesde8-3/" target="_blank" rel="attachment wp-att-576"><img class="aligncenter size-medium wp-image-576" alt="crackmesde8" src="http://zairon.files.wordpress.com/2012/12/crackmesde82.jpeg?w=300&#038;h=105" width="300" height="105" /></a></p>
<p>No exe file this time, the downloaded archive contains a readme file and the above image, nothing else. You can&#8217;t interact with an image so you can only load it into your preferred hex editor hoping to understand what&#8217;s going on.</p>
<p style="text-align:center;">It&#8217;s pretty easy to understand that something has been added at the end of the file (jpeg file ends with FF D9 bytes sequence):<a href="http://zairon.wordpress.com/2012/12/21/unusual-crackme-by-ksydfius/end_file/" target="_blank" rel="attachment wp-att-579"><img class="aligncenter size-medium wp-image-579" alt="end_file" src="http://zairon.files.wordpress.com/2012/12/end_file.jpg?w=300&#038;h=68" width="300" height="68" /></a></p>
<p>As you can see, the author appended some bytes at the end of the image. Now, I have to understand what&#8217;s behind these unknown bytes and Google will help me. I decided to start with &#8220;**TI83F*&#8221; which is the only reasonable string to search for. At the end I reach a page at <a href="http://merthsoft.com/linkguide/ti83+/fformat.html" target="_blank">http://merthsoft.com/linkguide/ti83+/fformat.html</a> which enlighten the path to the solution of the crackme: the string &#8220;**TI83F&#8221; represents a tag used to define a program for a Texas Instruments calculator.</p>
<p>With the file format under your eyes it&#8217;s pretty easy to understand each byte:</p>
<p>- <span style="color:#ff0000;">2A 2A 54 49 38 33 46 2A</span>:  8 bytes signature: it&#8217;s always &#8220;**TI83F*&#8221;</p>
<p>- <span style="color:#ff0000;">1A 0A 00</span><span style="color:#000000;">: </span>3-byte, further signature: these three bytes always contain {1Ah, 0Ah, 00h}<br />
- <span style="color:#ff0000;">00..00</span>: 2Ah bytes, comment: it&#8217;s either zero-terminated or padded on the right with space characters<br />
- <span style="color:#ff0000;">56 00</span>: 2 bytes, length, in bytes, of the data section of the file<br />
- <span style="color:#ff0000;">0D 00 45 &#8230; 2A 3F D4</span>: n bytes, data section: consists of a number of variable entries<br />
- <span style="color:#ff0000;">90 16</span>: 2 bytes, file checksum: lower 16 bits of the sum of all bytes in the data section</p>
<p>Now, the data section:</p>
<p>- <span style="color:#ff0000;">0D 00</span>: 2 bytes, always has a value of 0Bh or 0Dh<br />
- <span style="color:#ff0000;">45 00</span>: 2 bytes, length, in bytes, of the variable data<br />
- <span style="color:#ff0000;">05</span>: 1 byte, variable type ID byte (0&#215;05 = Programs)<br />
- <span style="color:#ff0000;">41 00 00 .. 00</span>: 8 bytes, variable name padded with NULL characters<br />
- <span style="color:#ff0000;">00</span>: 1 byte, version: usually set to 00<br />
- <span style="color:#ff0000;">00</span>: 1 byte, flag: set to 80h if variable is archived, 00h else<br />
- <span style="color:#ff0000;">45 00</span>: 2 bytes, length, in bytes, of the variable data<br />
- <span style="color:#ff0000;">43 00 DC .. 2A 3F D4</span>: n bytes, variable data</p>
<p>Nothing interesting right now, just some definitions. The algo is all inside the variable data, each byte of that block represents a piece of the algo. I won&#8217;t explain every single byte definition because I think you can understand it by yourself simply using the table at <a href="http://merthsoft.com/linkguide/ti82/tokens.html" target="_blank">http://merthsoft.com/linkguide/ti82/tokens.html</a></p>
<p>To sum-up, the variable data starting with &#8220;43 00 DC&#8221; bytes sequence can be decoded into this program:</p>
<p>Input A<br />
A-&gt;B<br />
0-&gt;C<br />
While C&lt;100<br />
A+10*tan(A)-&gt;A<br />
C+1-&gt;C<br />
End<br />
If A=19911.236<br />
Then<br />
Disp &#8220;OK&#8221;<br />
Else<br />
Disp &#8220;NO&#8221;<br />
End</p>
<p>The algo is a TI-83 program, it&#8217;s pretty basic and the language is intuitive but if you need help take a look at chapter 16 of TI-83 Guidebook available online. Now that you know what&#8217;s going on you only have to solve it, good luck :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/574/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/574/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=574&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2012/12/21/unusual-crackme-by-ksydfius/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/12/crackmesde82.jpeg?w=300" medium="image">
			<media:title type="html">crackmesde8</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/12/end_file.jpg?w=300" medium="image">
			<media:title type="html">end_file</media:title>
		</media:content>
	</item>
		<item>
		<title>Dex Inspector online</title>
		<link>http://zairon.wordpress.com/2012/12/04/dex-inspector-online/</link>
		<comments>http://zairon.wordpress.com/2012/12/04/dex-inspector-online/#comments</comments>
		<pubDate>Mon, 03 Dec 2012 22:56:30 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[reverse code engineering]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=534</guid>
		<description><![CDATA[I got some requests for this little tool so I decided to put it online for everyone. As I told you in a recent blog post the program is still uncomplete because I want to add some more features and I don&#8217;t know if it has real bad bug(s) inside. So, your question might be [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=534&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I got some requests for this little tool so I decided to put it online for everyone.</p>
<p>As I told you in a recent blog post the program is still uncomplete because I want to add some more features and I don&#8217;t know if it has real bad bug(s) inside. So, your question might be &#8220;why are you putting it online?&#8221;. Just because I think that bug reports, constructive feedbacks, criticisms and comments represent a good way for making decent programs. Feel free to use my e-mail address for any kind of messages you want to send me.</p>
<p><a href="http://zairon.wordpress.com/dexinspector/dexinspector-2/" rel="attachment wp-att-530"><img class="aligncenter size-medium wp-image-530" alt="DexInspector" src="http://zairon.files.wordpress.com/2012/12/dexinspector.jpg?w=300&#038;h=215" height="215" width="300" /></a></p>
<p><a title="DexInspector" href="https://www.dropbox.com/sh/ipkq2ttk4ajctyq/XCtUZUAfOi" target="_blank">DexInspector</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/534/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/534/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=534&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2012/12/04/dex-inspector-online/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/12/dexinspector.jpg?w=300" medium="image">
			<media:title type="html">DexInspector</media:title>
		</media:content>
	</item>
		<item>
		<title>AIVD Cyber-challenge</title>
		<link>http://zairon.wordpress.com/2012/11/21/aivd-cyber-challenge/</link>
		<comments>http://zairon.wordpress.com/2012/11/21/aivd-cyber-challenge/#comments</comments>
		<pubDate>Wed, 21 Nov 2012 19:27:08 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[Blogroll]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[reverse code engineering]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=522</guid>
		<description><![CDATA[This is more like a twitter post than a blog post&#8230; Anyway, for all those who haven&#8217;t tried yet I suggest to take a look at the Cyber challenge by the General Intelligence and Security Service of the Netherlands: https://www.aivd.nl/organisatie/eenheden/nationaal-bureau/nieuws/aivd-cyber-challenge/ I had fun decrypting the ENCrypted file. I would like to blog something about the [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=522&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is more like a twitter post than a blog post&#8230; Anyway, for all those who haven&#8217;t tried yet I suggest to take a look at the Cyber challenge by the General Intelligence and Security Service of the Netherlands: <a href="https://www.aivd.nl/organisatie/eenheden/nationaal-bureau/nieuws/aivd-cyber-challenge/" target="_blank">https://www.aivd.nl/organisatie/eenheden/nationaal-bureau/nieuws/aivd-cyber-challenge/</a></p>
<p>I had fun decrypting the ENCrypted file. I would like to blog something about the crackme but I think I can&#8217;t give out spoilers&#8230;</p>
<p>Good luck and enjoy this cyber-challenge! :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/522/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/522/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=522&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2012/11/21/aivd-cyber-challenge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>
	</item>
		<item>
		<title>DexInspector</title>
		<link>http://zairon.wordpress.com/2012/11/08/dexinspector/</link>
		<comments>http://zairon.wordpress.com/2012/11/08/dexinspector/#comments</comments>
		<pubDate>Thu, 08 Nov 2012 11:02:01 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=453</guid>
		<description><![CDATA[Rewording a quote from a famous film I would say &#8220;The Malwares are everywhere, even now in this very room&#8221;, and that&#8217;s could be true if you have an Android based mobile phone because the number of this kind of malwares are increasing nowadays. It&#8217;s not so hard to study an Android malware; there are [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=453&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Rewording a quote from a famous film I would say &#8220;The Malwares are everywhere, even now in this very room&#8221;, and that&#8217;s could be true if you have an Android based mobile phone because the number of this kind of malwares are increasing nowadays. It&#8217;s not so hard to study an Android malware; there are some nice tools available on the net, and in the foreseeable future I could add my simple DexInspector too.</p>
<p>Why do I need another .dex analyzer? Well, I decided to write this tool because I had some problems dealing with disassembled output produced by some existing tools. Some of them shows an output which is hard to follow because of its syntax that is strictly related to .dex opcodes; on the other hand other tools try to understand what&#8217;s behind the code, but it&#8217;s really hard with a static analysis and the result may produce ambiguous results. I always use more than one single tool in my reversing sessions, and I think DexInspector could be a valid help for .dex analysis.</p>
<p>Since I don&#8217;t like to fill this blog post with a series of anonymous screenshots without a single word, to introduce the tool I&#8217;m going to use a malware named &#8220;FakeLookout.A&#8221; guiding you through a possible reversing session.<br />
You can find a nice description of the malware at <a href="http://blog.trustgo.com/fakelookout/" target="_blank">http://blog.trustgo.com/fakelookout/</a> and I&#8217;ll explain you how to get this information:</p>
<blockquote><p>This malware can receive and execute commands from remote server.<br />
Server address: hxxp://[hidden]press.com/controls.php<br />
Commands:<br />
clearFileList<br />
getDir<br />
clearAlarm<br />
getFile<br />
getSize<br />
getTexts</p></blockquote>
<p><span style="text-decoration:underline;"><strong><em>DexInspector</em></strong></span></p>
<div id="attachment_495" class="wp-caption aligncenter" style="width: 310px"><a href="http://zairon.files.wordpress.com/2012/11/main_dialog2.jpg"><img class="size-medium wp-image-495 " title="Main dialog" alt="" src="http://zairon.files.wordpress.com/2012/11/main_dialog2.jpg?w=300&#038;h=216" height="216" width="300" /></a><p class="wp-caption-text">.dex has been fully loaded</p></div>
<p>This is the main dialog of DexInspector, exactly when a .dex file has been fully loaded.<br />
The box with a series of &#8220;[INFO]&#8221; entries is used to display information, depending on the current task. In this case it tells us everything has been loaded correctly.  The treeview on the left contains a list of all the .java files declared in the malware project.  Each disassembled class will be showed inside the empty box. The toolbar contains some buttons (used to view necessary information about structures inside a .dex file), a control used to store all the methods inside the current disassembled class and  3 search text controls.</p>
<div id="attachment_474" class="wp-caption aligncenter" style="width: 310px"><a href="http://zairon.files.wordpress.com/2012/11/map_list.jpg"><img class="size-medium wp-image-474" title="Map list dialog" alt="" src="http://zairon.files.wordpress.com/2012/11/map_list.jpg?w=300&#038;h=198" height="198" width="300" /></a><p class="wp-caption-text">Map list dialog</p></div>
<p>From the map_list dialog you can have an idea of what the file is. It has a lot of methods, classes and strings (item: method_id_item, class_def_item and string_id_item), so the question is: if I want to understand what&#8217;s going on inside the malware, where is a good starting point? The name of the classes and the original files used by the programmer are valid candidates, but like a common reversing session I prefer to start the static analysis directly from strings window, even if there are too many strings.</p>
<div id="attachment_476" class="wp-caption aligncenter" style="width: 310px"><a href="http://zairon.files.wordpress.com/2012/11/string_list.jpg"><img class="size-medium wp-image-476" title="String list dialog" alt="" src="http://zairon.files.wordpress.com/2012/11/string_list.jpg?w=300&#038;h=223" height="223" width="300" /></a><p class="wp-caption-text">Selected string with location info</p></div>
<p>Due to the .dex nature it&#8217;s easy to discard useless entries; i.e. you can avoid specific type strings (something like &#8220;IILI&#8221;), strings starting with &#8216;L&#8217; or &#8216;[', etcetc.  Among all the strings I see a suspicious address: "<em><a href="http://thelongislandpress.com/controls.php" rel="nofollow">http://thelongislandpress.com/controls.php</a></em>", the server doesn't exist anymore so I think I don't need to obscure the address.<br />
As you can see from the listview at the bottom of the dialog the string is located in a single file named "<em>com.updateszxt.HttpFileUploader.zdi</em>".<br />
What kind of file is this? It's the disassembled file and the .zdi is the extension I use, you can find the file on your hard disk too.  Anyway, the name of the original file is HttpFileUploader.java:</p>
<div id="attachment_479" class="wp-caption aligncenter" style="width: 310px"><a href="http://zairon.files.wordpress.com/2012/11/file_start.jpg"><img class="size-medium wp-image-479 " title="Disassembled file" alt="" src="http://zairon.files.wordpress.com/2012/11/file_start.jpg?w=300&#038;h=216" height="216" width="300" /></a><p class="wp-caption-text">Disassembled view: class, fields and constructor</p></div>
<p>The original file has inside the definition of the public class <em>com.updateszxt.HttpFileUploader</em> which extends <em>java.lang.Object</em> and contains some fields definitions (both static and instance). The http address is used inside the constructor of the class, here is constructor&#8217;s code:</p>
<div id="attachment_482" class="wp-caption aligncenter" style="width: 310px"><a href="http://zairon.files.wordpress.com/2012/11/constructor.jpg"><img class="size-medium wp-image-482" title="Constructor's code" alt="" src="http://zairon.files.wordpress.com/2012/11/constructor.jpg?w=300&#038;h=214" height="214" width="300" /></a><p class="wp-caption-text">Code of the constructor</p></div>
<p style="text-align:left;">The program analysis is done instruction-by-instruction involving debug information which are mostly useful when you have to understand the code. In this specific case the program uses debug information in different places, the first one is inside the declaration of the method: the parameters have specific names which are not randomly created.<br />
It&#8217;s pretty simple to understand what&#8217;s going on. First thing: the method is used to check if a specific folder exists. If the folder, named &#8220;dataCache&#8221;, doesn&#8217;t exist it will be created inside Android external storage directory. Second thing: it initializes an url using the http address above. Nothing special indeed, but before going directly to the next part of the class I wanto to point your attention to an instruction:<br />
<em>  java.io.File folder = v1;</em><br />
It&#8217;s not a line derived from one of the .dex opcodes but it comes from debug information and represents a local variable definition. Nothing special, but dealing with the original name could be handy.<br />
Another addition is represented by the info inside the small box, it shows something depending on the selected instruction. i.e.:<br />
selected instruction:   <em>v2 = v2-&gt;append(v3);</em><br />
info: <em>java.lang.StringBuilder java.lang.StringBuilder-&gt;append(java.lang.String)<br />
</em>The meaning of the info is: the method <em>append</em> from <em>java.lang.StringBuilder</em> gets a<em> java.lang.String</em> parameter and returns a <em>java.lang.StringBuilder</em> type.</p>
<p style="text-align:left;">Ok, back to the code!</p>
<div id="attachment_485" class="wp-caption aligncenter" style="width: 310px"><a href="http://zairon.files.wordpress.com/2012/11/run_start.jpg"><img class="size-medium wp-image-485" title="Run start" alt="" src="http://zairon.files.wordpress.com/2012/11/run_start.jpg?w=300&#038;h=228" height="228" width="300" /></a><p class="wp-caption-text">Interesting code starts here</p></div>
<p style="text-align:left;">Searching through the methods listed in the combo box I select the one named <em>runIt</em>, after a little glance you can understand what&#8217;s going on: the program estabilishes a connection with the remote server, it gets a command and execute it. The command is returned by getControls method and it&#8217;s one of the six I have mentioned at the beginning of this post (it&#8217;s also possible to have a path following the command).</p>
<div id="attachment_487" class="wp-caption aligncenter" style="width: 239px"><a href="http://zairon.files.wordpress.com/2012/11/commands.jpg"><img class="size-medium wp-image-487" title="Commands" alt="" src="http://zairon.files.wordpress.com/2012/11/commands.jpg?w=229&#038;h=300" height="300" width="229" /></a><p class="wp-caption-text">Check for the right command to execute</p></div>
<p style="text-align:left;">Except few lines at the beginning of the method the codeflow of the program depends on the invoked command. As you can see from the snippet the command is compared with &#8220;clearFileList&#8221;, if it&#8217;s the one they want to invoke it will be executed otherwise the next command will be checked (&#8220;getDir&#8221;). This kind of check is done until the right command is not found.</p>
<p><em><strong><span style="text-decoration:underline;">Final words</span><br />
</strong></em>Malware analysis ends here, there are a some nice methods to explore and I&#8217;ll let you discover the rest yourself because I think I have attached too many pictures for a simple program. It&#8217;s time to end up with this blog post, you should have an idea of what DexInspector does.<br />
It&#8217;s not perfect, but I feel comfortable with it. It&#8217;s still under development because I have to test it again and again (bugs are like malwares, they are everywhere!) and I would like to add some more things.<br />
If you think it might help your Android reversing sessions I&#8217;ll put it online available for everyone, on condition that you&#8217;ll report bugs, comments and criticisms to me, ok? Let me know and don&#8217;t expect too much, it&#8217;s only a disassembler!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/453/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=453&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2012/11/08/dexinspector/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/11/main_dialog2.jpg?w=300" medium="image">
			<media:title type="html">Main dialog</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/11/map_list.jpg?w=300" medium="image">
			<media:title type="html">Map list dialog</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/11/string_list.jpg?w=300" medium="image">
			<media:title type="html">String list dialog</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/11/file_start.jpg?w=300" medium="image">
			<media:title type="html">Disassembled file</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/11/constructor.jpg?w=300" medium="image">
			<media:title type="html">Constructor&#039;s code</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/11/run_start.jpg?w=300" medium="image">
			<media:title type="html">Run start</media:title>
		</media:content>

		<media:content url="http://zairon.files.wordpress.com/2012/11/commands.jpg?w=229" medium="image">
			<media:title type="html">Commands</media:title>
		</media:content>
	</item>
		<item>
		<title>Some notes on how to find out hidden callbacks</title>
		<link>http://zairon.wordpress.com/2011/06/18/some-notes-on-how-to-find-out-hidden-callbacks/</link>
		<comments>http://zairon.wordpress.com/2011/06/18/some-notes-on-how-to-find-out-hidden-callbacks/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 17:17:54 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[reverse code engineering]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=409</guid>
		<description><![CDATA[Can I blog an incomplete solution or an incomplete analysis? Why not! That&#8217;s the spirit of this blog entry! More than one year ago I started a project with Kayaker, we decided to write a tool able to show hidden callbacks. If I remember correctly the idea was born while we were putting our hands [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=409&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Can I blog an incomplete solution or an incomplete analysis? Why not! That&#8217;s the spirit of this blog entry!</p>
<p>More than one year ago I started a project with Kayaker, we decided to write a tool able to show hidden callbacks. If I remember correctly the idea was born while we were putting our hands on a rootkit. In the same days I bet there were many reversers around thinking the same thing because the same tool was developed by others. As you can imagine our tool never see the light, but not because there are similar tools available online; mostly because we are two old lazy reversers! </p>
<p>I bet you are thinking: why the hell are you writing this stupid intro? Well, the tools I mentioned before were bugged and some months ago I discovered the same thing, they are still bugged (I don&#8217;t know if they have solved their problems right now&#8230;). Strange that no one else noticed it yet.<br />
Anyway, we won&#8217;t complete the tool, but with this blog post I would like to tell you some notes about our investigations. At the beginning I wanted to write a detailed and complete article about the subject, but I don&#8217;t know when I&#8217;ll be able to end this project so I decided to spread out some of my notes.</p>
<p>It&#8217;s a sort of two minds work so credit goes to Kayaker too!</p>
<p>The idea is to try to retrieve hidden callbacks that has been installed via CmRegisterCallback, PsSetCreateProcessNotifyRoutine, PsSetCreateThreadNotifyRoutine and PsSetLoadImageNotifyRoutine. After that it would be good to deregister one or more of them.<br />
&#x2028;&#x2028;<br />
<strong>Where to start?</strong><br />
First of all you have to understand what&#8217;s behind functions like CmRegisterCallback, and others. Then, you&#8217;ll have something to work on. I&#8217;ll start with CmRegisterCallback (from XP SP2), the function is used to register a RegistryCallback routine, and I think the XP version is the most simple one to fully undestand the principles behind the function. There are some differencies between XP and 7 versions, but I think you&#8217;ll be able to fully understand 7 structure too! Here is the disassembled function (without useless parts of course):</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
487E6B  push   'bcMC'                          ; Pool Tag: "CMcb"
487E70  xor    ebx, ebx
487E72  push   38h                             ; NumberOfBytes: 0x38
487E74  inc    ebx
487E75  push   ebx                             ; PoolType: PAGEDPOOL
487E76  call   ExAllocatePoolWithTag           ; ExAllocatePoolWithTag(x,x,x): allocates pool memory
487E7B  mov    esi, eax                        ; eax is the pointer to the allocated pool memory, PCM_CALLBACK_CONTEXT_BLOCK
487E7D  xor    edi, edi
487E7F  cmp    esi, edi                        ; Is PCM_CALLBACK_CONTEXT_BLOCK a NULL pointer?
487E81  jz     cmRegisterCallback_fails        ; yes: function fails...
487E87  push   esi
487E88  push   [ebp+Function]                  ; PEX_CALLBACK_FUNCTION, pointer to callback function
487E8B  call   _ExAllocateCallBack             ; allocates and fill EX_CALLBACK_ROUTINE_BLOCK structure (more on this later...)
487E90  cmp    eax, edi                        ; ExAllocateCallback success or not?
487E92  mov    [ebp+PEX_CALLBACK_ROUTINE_BLOCK], eax ; store the pointer to the allocated pool memory
487E95  jnz    short _ExAllocateCallBack_success
   ...                                         ; fill CM_CALLBACK_CONTEXT_BLOCK fields
487EDC  mov    ebx, offset CmpCallBackVector
487EE1  mov    [ebp+i], edi                    ; i = 0
487EE4 try_next_slot:
487EE4  push   edi                             ; OldBlock: NULL
487EE5  push   [ebp+PEX_CALLBACK_ROUTINE_BLOCK] ; NewBlock with information to add
487EE8  push   ebx                             ; CmpCallbackVector[i]
487EE9  call   _ExCompareExchangeCallBack   ; try to *insert* the new callback inside CmpCallBack vector
487EEE  test   al, al                       ;check the result...
487EF0  jnz    short free_slot_has_been_found    ; jump if the vector has an empty space for the new entry
487EF2  add    [ebp+i], 4                      ; i++, increase the counter
487EF6  add    ebx, 4                          ; shift to the next item of the vector to check
487EF9  cmp    [ebp+i], 190h                   ; is the end of the vector? 
487F00  jb     short try_next_slot             ; no: try another one. yes: no free slot!
   ...    
487F11 cmRegisterCallback_fails:
487F11  mov    eax, STATUS_INSUFFICIENT_RESOURCES
487F16 end_CmRegisterCallback:
   ...
487F1A  retn   0Ch
   ... 
487F1D free_slot_has_been_found:
487F1D  mov    eax, 1
487F22  mov    ecx, offset _CmpCallBackCount   ; CmpCallBackCount: number of not NULL item inside the vector
487F27  xadd   [ecx], eax                      ; there's a new callback, it increases the number of item inside the vector
487F2A  xor    eax, eax
487F2C  jmp    short end_CmRegisterCallback</code>
</pre>
</div>
<p>&#x2028;&#x2028;<br />
As you can see the idea behind the function is really simple!<br />
Basically, it tries to add a new entry inside a vector named CmpCallBackVector, and when the entry is correctly inserted the registration process will end with a success.<br />
How do I know is it using a vector? The add instruction at 0x487EF6 represents a clear clue, and the cmp at 0x487EF9 reveals the fixed length of the vector (the vector has 100 items (0&#215;190/4&#8230;)). Now that I have this information I&#8217;m going to try to explain the entire procedure in detail. The algorithm could be divided into 5 big blocks:</p>
<p><em>1</em>: try to allocate 0&#215;38 bytes for a structure named CM_CALLBACK_CONTEXT_BLOCK<br />
<em>2</em>: try to allocate 0x0C bytes for a structure named EX_CALLBACK_ROUTINE_BLOCK<br />
<em>3</em>: fill CM_CALLBACK_CONTEXT_BLOCK fields<br />
<em>4</em>: look for an empty slot, insert a sort of PEX_CALLBACK_ROUTINE_BLOCK in it and update CmpCallBackCount<br />
<em>5</em>: notify success or error and exit</p>
<p><em>Point #1</em> is pretty simple to understand, it&#8217;s only a call to ExAllocatePoolWithTag.</p>
<p>To understand <em>point #2</em> you have to see what&#8217;s going on behind ExAllocateCallBack procedure. Let&#8217;s start taking a look at it:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
52AB35  push   'brbC'                              ; Pool Tag: Cbrb
52AB3A  push   0Ch                                 ; NumberOfBytes: 0x0C
52AB3C  push   1                                   ; PoolType: PAGED_POOL
52AB3E  call   ExAllocatePoolWithTag               ; alloc a EX_CALLBACK_ROUTINE_BLOCK structure
52AB43  test   eax, eax                            ; ExAllocatePoolWithTag success or not?
52AB45  jz     short _ExAllocateCallBack_fails
52AB47  mov    ecx, [ebp+_pex_callback_function]   ; pointer to callback function (PEX_CALLBACK_FUNCTION)
52AB4A  and    dword ptr [eax], 0                  ; 1° field: 0
52AB4D  mov    [eax+4], ecx                        ; 2° field: _pex_callback_function
52AB50  mov    ecx, [ebp+_pool_allocated_memory]   ; PCM_CALLBACK_CONTEXT_BLOCK
52AB53  mov    [eax+8], ecx                        ; 3° field: _pcm_callback_context_block
52AB56 _ExAllocateCallBack_fails:
   ...
</pre>
</div>
<p>&#x2028;&#x2028;<br />
The procedure is used to allocate and fill a special structure:</p>
<p>typedef struct _EX_CALLBACK_ROUTINE_BLOCK<br />
{<br />
   EX_RUNDOWN_REF             RundownProtect;<br />
   PEX_CALLBACK_FUNCTION      Function;<br />
   PCM_CALLBACK_CONTEXT_BLOCK Context;<br />
} EX_CALLBACK_ROUTINE_BLOCK, *PEX_CALLBACK_ROUTINE_BLOCK;</p>
<p>As you can see from the lines above the first field has been setted to 0 while the other fields are filled with two pointers: the function to register and the context containing info about the callback. </p>
<p>While <em>point #3</em> is just a series of mov instructions used to fill CM_CALLBACK_ROUTINE_BLOCK structure, <em>point #4</em> gives some usefull information to us: CmpCallBackVector has 100 elements and this part of code is used to scan the entire vector until an empty element is found. A failure leads us to a non-registration of the callback. What happens when there&#8217;s a empty slot inside the vector? The new entry will be added inside the vector. Most of the job is done by the function named ExCompareExchangeCallBack, here is the core of the function:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
52AB81  mov    eax, [ebp+CmpCallbackVector]    ; vector at the current position
52AB84  mov    ebx, [eax]                      ; ebx is a PEX_CALLBACK_ROUTINE_BLOCK, the item could be NULL or not
52AB86  mov    eax, ebx
52AB88  xor    eax, [ebp+OldBlock]             ; OldBlock is NULL for a registration process
52AB8B  mov    [ebp+current_pex_callback_routine_block], ebx
52AB8E  cmp    eax, 7                          ; check used to see if the current item is NULL or not
52AB91  ja     short loc_52ABB5                ; jump if not NULL
52AB93  test   esi, esi                        ; is NewBlock NULL?
52AB95  jz     short loc_52ABA1                ; jump if it's NULL
52AB97  mov    eax, esi                        ; esi, NewBlock pointer (changed...)
52AB99  or     eax, 7                          ; PAY ATTENTION HERE: or 7 !?!
52AB9C  mov    [ebp+NewBlock], eax             ; change NewBlock pointer: NewBlock = NewBlock OR 7
52AB9F  jmp    short loc_52ABA5
   ...
52ABA5 mov    eax, [ebp+var_4]               ; here if CmpCallbackVector's item is null
52ABA8  mov    ecx, [ebp+CmpCallbackVector]    ; current empty slot
52ABAB  mov    edx, [ebp+NewBlock]             ; new pointer to insert
52ABAE  cmpxchg [ecx], edx                     ; insert the new pointer inside the empty slot!
52ABB1 cmp    eax, ebx
52ABB3 jnz    short loc_52AB81
52ABB5 and    ebx, not 7                     ; PAY ATTENTION HERE!
52ABB8 cmp    ebx, [ebp+OldBlock]            ; here if CmpCallbackVector's item is not null
52ABBB jnz    short loc_52AC19
52ABBD test   ebx, ebx
52ABBF jz     short loc_52AC15
</pre>
</div>
<p>&#x2028;&#x2028;<br />
The routine contains some more things inside, but we can stop here with the analysis because we have everything we need. If the pointer to the NewBlock to insert is not NULL and there&#8217;s an available empty slot the pointer is inserted inside the vector; after that CmpCallBackCount value will be updated (remember the snippet at the beginning of this blog entry?). </p>
<p>The last part of the algorithm (<em>point #5</em>) is a simple return with a success or insuccess value:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
52AC15 mov    al, 1                          ; 1 means success, new item has been added to CmpCallbackVector
52AC17 jmp    short loc_52AC29
52AC19 test   esi, esi                      ; esi -&gt; NewBlock 
52AC1B jz     short loc_52AC27
52AC1D push   8                       
52AC1F pop    edx
52AC20 mov    ecx, esi
52AC22 call   ExReleaseRundownProtectionEx   ; if esi is not null something went wrong...
52AC27 xor    al, al                         ; 0 means insuccess, new item has not been added to CmpCallbackVector
</pre>
</div>
<p>&#x2028;&#x2028;<br />
Ok, I think we have a general idea about the vector; each entry contains a *sort* of pointer to a EX_CALLBACK_ROUTINE_BLOCK, and to reveal all of them you only have to scan the entire vector! </p>
<p>To sum up, I have 3 possible scenes:<br />
1. CmpCallbackVector&#8217;s item is empty:<br />
the new block will be inserted inside the vector. The added value is not the one passed to ExCompareExchangeCallBack, but it&#8217;s the value modified by a &#8220;OR 7&#8243; logic operation.<br />
2. CmpCallbackVector&#8217;s item is full:<br />
it simply returns STATUS_INSUCCESS and it will try with the next item of the vector<br />
3. Someone is working on the CmpCallbackVector&#8217;s item:<br />
the registration process reveals an interesting behaviour, just to be sure to be the only one accessing the resource the system uses a lock mechanism. The OR and AND operations are the core of that mechanism (0x52AB99 and 0x52ABB5, commented using &#8220;PAY ATTENTION HERE!&#8221;). If the current item of the vector is not NULL the compare instruction at 0x52AB8E fails and the code flow continues from 0x52ABB5. At this point the real address of the item is extracted (stored_value AND NOT 7) and compared with NULL; it&#8217;s obviously not NULL and as you can see around 0x52AC22 the resource is released because someone else is working on it. Now you should understand why the hell the system uses to OR by 7 the value to add inside the vector. </p>
<p>With all this kind of information I can finally write a routine able to read all the stored callbacks: </p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
cells = 0x64;                    // cells inside CmpCallbackVector
nMod = *(DWORD*)_sysmodBuffer;   //	_sysmodBuffer filled by "ZwQuerySystemInformation(SystemModuleInformation..."
for(i=0;i&lt;cells;i++)
{
   // take current item from CmpCallbackVector (look at the &quot;&amp; ~7&quot; operation)
   pCBRB = (PEX_CALLBACK_ROUTINE_BLOCK)((*(DWORD*)(_CmpCallbackVectorAddress + 4*i )) &amp; ~7);
   if (pCBRB != 0)
   {
      sysmodTmp = (PSYSTEM_MODULE_INFORMATION)((DWORD)_sysmodBuffer + 4);
      j = 0;
      while (jFunction) Base + (DWORD)sysmodTmp-&gt;Size) &amp;&amp;
			((DWORD)pCBRB-&gt;Function) &gt; ((DWORD)sysmodTmp-&gt;Base))
         {
            // Callback has been found
            DbgPrint("Result: %LX: %s\r\n", pCBRB-&gt;Function, sysmodTmp-&gt;ImageName);
            break;
	 }
	 // get the next module
	 sysmodTmp = (PSYSTEM_MODULE_INFORMATION)((DWORD)sysmodTmp + sizeof(SYSTEM_MODULE_INFORMATION));
	 j = j + 1;
    }
}
</pre>
</div>
<p>&#x2028;&#x2028;<br />
It&#8217;s important to scan all the cells inside the vector! One of the tool available on the web fails to retrieve callbacks stored after an empty element of the vector.</p>
<p>Well, the only thing to reveal about the code above is CmpCallbackVectorAddress, the address of CmpCallBackVector. How can I locate the exact address of CmpCallBackVector? Imho, that&#8217;s the hardest part of the entire process!<br />
&#x2028;&#x2028;<br />
<strong>How to find CmpCallbackVector address</strong><br />
To develop a tool for a specific OS is pretty easy because the vector&#8217;s address is hardcoded; it would be nice to discover an OS independent technique.<br />
I think the most used approach is a byte-search based on a specific sequence of bytes; it&#8217;s a nice idea but I don&#8217;t want to list every OS version known to man inside my source code. We (I and kayaker) spent a lot of time over this point, we both wanted to develop something that is not totally related to a specific OS version; something that doesn&#8217;t require a series of &#8220;if OS == xxx&#8221; statements inside the code. It&#8217;s quite impossible to write a non OS dependent code but I believe it&#8217;s possible to remove some OS checks from the code.</p>
<p>We finally came up with two ideas, a practical and a theoretical idea. I hate theory and mine is the practical solution of course. I think both ideas are valid and just to be sure to find the right vector&#8217;s address we decided to combine them inside a hypothetical tool, four eyes are always better than two!<br />
&#x2028;&#x2028;<br />
<strong>The practical approach</strong><br />
My idea is really simple, since of the vector&#8217;s address is hardcoded you&#8217;ll surely have it in two different parts of the code:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
PAGE:005392D0   BB 20 05 48 00   mov    ebx, offset _CmpCallBackVector
.data:00480520                   _CmpCallBackVector db    0
</pre>
</div>
<p>&#x2028;&#x2028;<br />
The address is inside two sections, PAGE and data. An *xref-search* is the core of the idea! It&#8217;s pretty stupid indeed, but from what I&#8217;ve seen so far it works!<br />
The pseudo code of my xref search is explained here, basically it scans the entire PAGE section trying to locate the right address:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
callbackAddress = CmUnregisterCallback address in memory
pagePointer = pointer_to_PAGE_section
while (pagePointer &lt; pointer_to_PAGE_section + size_of_PAGE_section)   
{
   value = get dword pointed by pagePointer
   if (value is inside DATA section)
      if ((pagePointer &gt; callbackAddress) &amp;&amp; (pagePointer &lt; callbackAddress + range))   
      {
         CmpCallbackVector = value
	 exit!
      }
   pagePointer++      
}
</pre>
</div>
<p>&#x2028;&#x2028;<br />
As you can imagine a simple xref-search is unable to find out the right value, you need one more check. That&#8217;s why I added the line:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
if ((pagePointer &gt; callbackAddress) &amp;&amp; (pagePointer &lt; callbackAddress + range))   
</pre>
</div>
<p>&#x2028;&#x2028;<br />
where callbackAddress is the address of CmUnregisterCallback. What does it mean? Well, &#8216;pagePointer&#8217; should be inside the first &#8220;range&#8221; bytes of CmUnregisterCallback function. If both &#8220;if&#8221; statements are satisfied I&#8217;m pretty sure about the vector&#8217;s address value.</p>
<p>There are still 2 points to clarify:<br />
- what&#039;s range variable?<br />
- why CmUnregisterCallback?</p>
<p>range is just a numerical value and you&#039;ll only have to decide a value to assign to it. Under XP the first bytes of the CmUnregisterCallback function are:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
PAGE:005392C3 8B FF           mov    edi, edi
PAGE:005392C5 55              push   ebp
PAGE:005392C6 8B EC           mov    ebp, esp
PAGE:005392C8 51              push   ecx
PAGE:005392C9 83 65 FC 00     and    [ebp+var_4], 0
PAGE:005392CD 53              push   ebx
PAGE:005392CE 56              push   esi
PAGE:005392CF 57              push   edi
PAGE:005392D0 BB 20 05 48 00  mov    ebx, offset _CmpCallBackVector
</pre>
</div>
<p>&#x2028;&#x2028;<br />
In this specific case 16 could be a possible value&#8230; What about the other OSs? Well, as I said before I think it&#039;s hard to write a universal piece of code, but as far as I have seen it&#039;s possible to adjust the &quot;range&quot; to cover some more OSs. I don&#039;t have Vista and 7 running on my system and I&#039;m working on the dead list only, but I think 148 could be a nice value to set and it should cover all the OSs. If you are still reading and you have Vista or 7, can you confirm that?<br />
One more thing about the search pattern: I use CmUnregisterCallback because (inspecting all the OSs) CmRegisterCallback doesn&#039;t always store the CmpCallbackVector value inside the main routine, but it hides it under some calls. i.e. look at CmRegisterCallback from 7:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
PAGE:0065712A mov  edi, edi
PAGE:0065712C push ebp
PAGE:0065712D mov  ebp, esp
PAGE:0065712F push [ebp+Cookie]
PAGE:00657132 mov  eax, offset stru_4FFDF0
PAGE:00657137 push 1
PAGE:00657139 push [ebp+Context]
PAGE:0065713C push [ebp+Function]
PAGE:0065713F call sub_657153                 ; It&#039;s everything inside this call!!!
PAGE:00657144 pop  ebp
PAGE:00657145 retn 0Ch
</pre>
</div>
<p>&#x2028;&#x2028;<br />
It&#8217;s much more complex to attack a procedure with sub-routines, don&#039;t you think? That&#039;s why I did opt for CmUnregisterCallback.<br />
&#x2028;&#x2028;<br />
<strong>What about the PsSet* functions?</strong><br />
At the beginning of this blog post I mentioned some more functions, it&#039;s time to spend some words for them too.</p>
<p>The functions are:<br />
PsSetCreateProcessNotifyRoutine<br />
PsSetCreateThreadNotifyRoutine<br />
PsSetLoadImageNotifyRoutine</p>
<p>There are some similarities between CmRegisterCallback and the new three functions: they all register something, they all use a vector to store the information, and they all use the same function! YES, to register a function they use the same scheme:</p>
<p>1. get the address of a specific vector<br />
2. try to insert the new item inside the vector calling ExCompareExchangeCallBack</p>
<p>Just to clarify everything look at this snippet, taken from PsSetCreateThreadNotifyRoutine:</p>
<div class="code" style="overflow:scroll;">
<pre style="margin-top:0;display:inline;">
4ED7C4  mov    esi, offset _threadVector   ; the vector
4ED7C9  push   0
4ED7CB  push   ebx
4ED7CC  push   esi
4ED7CD  call   _ExCompareExchangeCallBack   ; the function
4ED7D2  test   al, al
4ED7D4  jnz    short loc_4ED7F3
4ED7D6  add    edi, 4
4ED7D9  add    esi, 4
4ED7DC  cmp    edi, 20h   ; the check over the number of items inside the vector
4ED7DF  jb     short loc_4ED7C9
</pre>
</div>
<p>&#x2028;&#x2028;<br />
The only different thing is the length of the vector:<br />
_callbackVector: 0&#215;64 slots<br />
_processVector: 0&#215;8 slots<br />
_threadVector: 0&#215;8 slots<br />
_imageVector: 0&#215;8 slots</p>
<p>Well, you can use all the info I gave you about CmRegisterCallback for these three functions too! I think you&#039;ll be able to retrieve all the hidden callbacks, and -just in case- unregister a callback. There are so many ways from the dirty one (put NULL inside the vector&#039;s slot) to the right one (calling the right unregister function)&#8230; you only have to decide!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/409/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=409&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2011/06/18/some-notes-on-how-to-find-out-hidden-callbacks/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>
	</item>
		<item>
		<title>Dynamic forking in action</title>
		<link>http://zairon.wordpress.com/2011/01/10/dynamic-forking-in-action/</link>
		<comments>http://zairon.wordpress.com/2011/01/10/dynamic-forking-in-action/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 12:34:26 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[Malware]]></category>
		<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=395</guid>
		<description><![CDATA[Here&#8217;s a sample I got some days ago via Facebook (md5: 6853d7ce162adb707ddfa6529b82b4af). Only few antivirus are able to recognize it right now. To obtain the exe you have to simply click on a link which is show inside a chat request from a friend of you (the infected friend). The message I got is: &#8220;Foto [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=395&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Here&#8217;s a sample I got some days ago via Facebook (md5: 6853d7ce162adb707ddfa6529b82b4af). Only few antivirus are able to recognize it right now.</p>
<p>To obtain the exe you have to simply click on a link which is show inside a chat request from a friend of you (the infected friend). The message I got is:<br />
&#8220;Foto :D <a href="http://apps.facebook.com/peoplephotos/photo.php?=id_number&#038;#8221" rel="nofollow">http://apps.facebook.com/peoplephotos/photo.php?=id_number&#038;#8221</a>;<br />
where &#8220;foto&#8221; is the italian translation of &#8220;photo&#8221;, and id_number is your Facebook id (I removed mine).</p>
<p>The link is not working anymore on Facebook but I think they have some more available links to use&#8230; anyway, the interesting part of the malware is the use of a technique called Dynamic forking. It&#8217;s an old technique used sometimes inside packers and stuff like that, but as far as I have seen (and according to some friends) it&#8217;s a rarity to see it inside a malware. There&#8217;s an article explaining everything about the method right here: <a href="http://www.security.org.sg/code/loadexe.html">http://www.security.org.sg/code/loadexe.html</a> (it&#8217;s dated April 2004!).<br />
Is it really so interesting? Yes indeed, mainly because it&#8217;s not so easy to catch and debug the second exe on the fly. Not to mention you can&#8217;t dump it in an easy way. Is it impossible to study the exe? Not really&#8230; there&#8217;s always a simple way to do such things.</p>
<p>The &#8220;dynamic forking&#8221; method is all inside a call starting at 0x4013A0 address: </p>
<p><code>   ...<br />
4013CE   push CREATE_SUSPENDED<br />
   ...<br />
4013D3   push edx               ; CommandLine = "PATH OF THE MALWARE\facebook-pic000163927.exe"<br />
   ...<br />
4013DB   call _CreateProcessA   ; it runs itself<br />
   ...<br />
401414   push ecx<br />
401415   push edx<br />
401416   mov  _GetThreadContext, eax<br />
40141B   call eax<br />
   ...<br />
401432   push 4                 ; number of bytes to read<br />
   ...<br />
401440   mov  _ReadProcessMemory, eax<br />
401445   call eax               ; read BaseAddress from CONTEXT structure, it's 0x400000<br />
   ...<br />
401459   push edx               ; BaseAddress: 0x400000<br />
40145A   push ecx               ; process handle<br />
40145B   mov  _ZwUnmapViewOfSection, eax<br />
401460   call eax               ; unmap!</code></p>
<p>It follows the article step by step, the last call is the most important one because it&#8217;s used to unmap the entire view of the section that contains BaseAddress. After that, the next part of the procedure is used to fill part of the unmapped section:</p>
<p><code>401462   mov  esi, [ebp+decompressedBuffer]   ; take care of ESI value!!!<br />
401465   mov  edi, [esi+3Ch]<br />
401468   mov  edx, [edi+esi+54h]              ; SizeOfHeaders<br />
40146C   add  edi, esi                        ; edi -&gt; "PE"<br />
40146E   mov  [ebp+decompressedBuffer], edx<br />
   ...<br />
40148A   mov  _VirtualAllocEx, eax<br />
   ...<br />
40149A   push ecx                             ; size: 0x2FC000<br />
40149B   mov  ecx, [ebp+var_1C]<br />
40149E   push edx                             ; 0x400000<br />
40149F   push ecx                             ; hprocess<br />
4014A0   call eax                             ; VirtualAllocEx<br />
   ...<br />
4014D2   push 0<br />
4014D4   push edx                             ; 0x400: number of bytes to write<br />
4014D5   mov  edx, [ebp+var_1C]<br />
4014D8   push esi                             ; decompressedBuffer<br />
4014D9   push ecx                             ; 0x400000<br />
4014DA   push edx<br />
4014DB   mov  _WriteProcessMemory, eax<br />
4014E0   call eax                             ; replace the header of the *new* process<br />
   ...                                        ; replace the rest of the *new* process's code</code></p>
<p>It allocs the necessary space for the *new* process (0x2FC000 represent the SizeOfImage of the real malware), and it replaces the old process code with the new one which is taken from a buffer pointed by ESI (look at 0&#215;401462). This buffer is really important, I&#8217;ll tell you later why. Now, the final part of the procedure:  </p>
<p><code>40158D   mov     [ebp+var_280], eax            ; change oep of the file from 0x4017D3 to 0x40C4FF<br />
   ...<br />
4015A4   push    ecx<br />
4015A5   push    edx<br />
4015A6   mov     _SetThreadContext, eax<br />
4015AB   call    eax                           ; SetThreadContext<br />
   ...<br />
4015BE   mov     _ResumeThread, eax<br />
4015C3   call    eax                           ; ResumeThread<br />
</code><br />
Pretty simple, it changes the entry point and it runs the process from the new location. </p>
<p>At this point I&#8217;m not able to debug the new process with a debugger like Ollydbg, I&#8217;m not even able to dump the new process&#8230; and the question is: how can I solve the problem? The dynamic forking is a powerfull method but there&#8217;s a big flaw in it, you always know how to dump the real malware: in this case decompressedBuffer is the answer! The buffer contains all the new code, and you only have to understand where the hell it was stored:</p>
<p><code>401383   push    ecx             ; finaluncompressedsize<br />
401384   push    ebp             ; compressedbuffersize<br />
401385   push    edx             ; compressedbuffer<br />
401386   push    edi             ; uncompressedbuffersize<br />
401387   push    esi             ; uncompressed buffer<br />
401388   push    COMPRESSION_FORMAT_LZNT1<br />
40138A   mov     RtlDecompressBuffer, eax<br />
40138F   call    eax</code></p>
<p>The malware uses lznt1 compression algo to reveal the real code. When the decompression ends you only have to dump 0&#215;11200 bytes starting from UncompressedBuffer address and voilà! Now you can easily check the real malware!</p>
<p>Well, something unusual is always better than the usual boring thing&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/395/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/395/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=395&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2011/01/10/dynamic-forking-in-action/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>
	</item>
		<item>
		<title>How to clear chat history on facebook</title>
		<link>http://zairon.wordpress.com/2010/10/08/how-to-clear-chat-history-on-facebook/</link>
		<comments>http://zairon.wordpress.com/2010/10/08/how-to-clear-chat-history-on-facebook/#comments</comments>
		<pubDate>Fri, 08 Oct 2010 13:13:16 +0000</pubDate>
		<dc:creator>zairon</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://zairon.wordpress.com/?p=389</guid>
		<description><![CDATA[This is more just a sort of twitter post than a blog post, but since of they told me there is not an answer on the net I decide to write few lines on my blog. I have to admit I&#8217;m a facebook fan, it&#8217;s really funny! I don&#8217;t spend my spare time on it, [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=389&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is more just a sort of twitter post than a blog post, but since of they told me there is not an answer on the net I decide to write few lines on my blog.</p>
<p>I have to admit I&#8217;m a facebook fan, it&#8217;s really funny! I don&#8217;t spend my spare time on it, but from time to time I like to exchange some words with good old friends. Some days ago I did receive a lot of questions about the chat feature, many people are worried because the developers removed the &#8220;clear history chat&#8221; option. And now? How can they hide their secrets? Well, a little inspection inside the javascript code reveals the solution to this problem. </p>
<p>The function to call is named <em>clearHistory</em>. To clear the history you only have to put this line inside the address bar of your browser:</p>
<p>javascript:chatDisplay.tabs[id_person].clearHistory();</p>
<p>where id_person is the id of the person you were chatting with. </p>
<p>Good luck and good secrets to everyone! :P</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/zairon.wordpress.com/389/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/zairon.wordpress.com/389/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=zairon.wordpress.com&#038;blog=448309&#038;post=389&#038;subd=zairon&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://zairon.wordpress.com/2010/10/08/how-to-clear-chat-history-on-facebook/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/96af3de97184170603a7cc688cbafeee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">zairon</media:title>
		</media:content>
	</item>
	</channel>
</rss>
