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

<channel>
	<title>Casaba Security &#187; Deobfuscated</title>
	<atom:link href="http://www.casaba.com/blog/tag/deobfuscated/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.casaba.com/blog</link>
	<description>Building and breaking software and robots</description>
	<lastBuildDate>Wed, 11 Jan 2012 18:08:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Microsoft CCI Framework for Deobfuscating .Net binaries. (Part 2)</title>
		<link>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-2/</link>
		<comments>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-2/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 23:53:14 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Babel]]></category>
		<category><![CDATA[CCI]]></category>
		<category><![CDATA[Deobfuscated]]></category>
		<category><![CDATA[Microsoft CCI]]></category>
		<category><![CDATA[Reversing]]></category>

		<guid isPermaLink="false">http://www.casabasecurity.com/blog/?p=171</guid>
		<description><![CDATA[So yesterday I talked a about using CCI to remove attributes from .Net binaries. Specifically the SupressIldasm attribute. I promised I&#8217;d put up some more code highlighting the framework&#8217;s benefits. So some more detail on the binary I&#8217;m working with. It has been ran through Babel -&#62; Netz -&#62; Babel again. My goals have been [...]]]></description>
			<content:encoded><![CDATA[<p>So yesterday I talked a about using CCI to remove attributes from .Net binaries. Specifically the SupressIldasm attribute. I promised I&#8217;d put up some more code highlighting the framework&#8217;s benefits. So some more detail on the binary I&#8217;m working with. It has been ran through Babel -&gt; Netz -&gt; Babel again. My goals have been to reverse Debabel-&gt; Unpack Netz -&gt; Rebuild the .exe -&gt; debabel again, although the first stage of babel could be skipped, but why not analyze it.</p>
<p>Babel uses a couple of simple techniques to prevent programs like reflector from analyzing protected binaries. These techniques are also found in other protections, so it&#8217;s useful to understand why the work and how they work, they are really very simple.</p>
<p>Today I&#8217;ll cover a simple but annoying technique being employed; inserting junk bytes. Babel inserts junk bytes into the IL stream of each method. When reflected it causes the disassembler to fail as it does not recognize the byte sequences it can&#8217;t continue.</p>
<p>Below is an example of a method ildasm’ed after removing the “suppressIldasm” attribute from the previous post.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p171code3'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1713"><td class="code" id="p171code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">auto</span> ansi beforefieldinit netz<span style="color: #008000;">.</span><span style="color: #0000FF;">NetzStarter</span>
       extends <span style="color: #008000;">&#91;</span>mscorlib<span style="color: #008000;">&#93;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">.</span><span style="color: #0000FF;">field</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> initonly <span style="color: #6666cc; font-weight: bold;">string</span> Property0
  <span style="color: #008000;">.</span><span style="color: #0000FF;">field</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> initonly <span style="color: #6666cc; font-weight: bold;">string</span> Property1
  <span style="color: #008000;">.</span><span style="color: #0000FF;">field</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> initonly <span style="color: #6666cc; font-weight: bold;">string</span> Property2
  <span style="color: #008000;">.</span><span style="color: #0000FF;">field</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #008000;">&#91;</span><span style="color: #000000;">System</span><span style="color: #008000;">&#93;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Specialized</span><span style="color: #008000;">.</span><span style="color: #0000FF;">HybridDictionary</span> Property3
  <span style="color: #008000;">.</span><span style="color: #0000FF;">field</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #008000;">&#91;</span>mscorlib<span style="color: #008000;">&#93;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Resources</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ResourceManager</span> Property4
  <span style="color: #008000;">.</span><span style="color: #0000FF;">field</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #008000;">&#91;</span>mscorlib<span style="color: #008000;">&#93;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ArrayList</span> Property5
  <span style="color: #008000;">.</span><span style="color: #0000FF;">field</span> <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Property6
  <span style="color: #008000;">.</span><span style="color: #0000FF;">method</span> <span style="color: #0600FF; font-weight: bold;">public</span> hidebysig specialname rtspecialname
          instance <span style="color: #6666cc; font-weight: bold;">void</span>  <span style="color: #008000;">.</span><span style="color: #0000FF;">ctor</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> cil managed
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Code size       14 (0xe)</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">maxstack</span>  <span style="color: #FF0000;">8</span>
    IL_0000<span style="color: #008000;">:</span>  br         IL_0007
&nbsp;
    IL_0005<span style="color: #008000;">:</span>  unused
    IL_0006<span style="color: #008000;">:</span>  unused
    IL_0007<span style="color: #008000;">:</span>  ldarg<span style="color: #008000;">.</span>0
    IL_0008<span style="color: #008000;">:</span>  call       instance <span style="color: #6666cc; font-weight: bold;">void</span> <span style="color: #008000;">&#91;</span>mscorlib<span style="color: #008000;">&#93;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">::.</span><span style="color: #0000FF;">ctor</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    IL_000d<span style="color: #008000;">:</span>  ret
  <span style="color: #008000;">&#125;</span> <span style="color: #008080; font-style: italic;">// end of method NetzStarter::.ctor</span></pre></td></tr></table></div>

<p>As you can see it does an absolute jump over some &#8220;unused&#8221; bytes which are really invalid bytes. This way the logic of the program is maintained while confusing the disassembler. One technique I&#8217;ve read to handle this is to use a hex editor to look for the absolute jump op code and nop out those bytes. However this is unreliable as babel inserts bytes not just at the start of the method.</p>
<p><strong>Microsoft CCI to the rescue again!. </strong></p>
<p>So lets use CCI to handle rebuilding the binary by replacing invalid bytes with nops. This way we can now view this application in reflector and be able to navigate it.  Below is the mutator class i wrote to handle NOP&#8217;ing invalid bytes. Again a very simple solution. Now the code is visible in reflector using the IL view. At least you get the &#8220;browsing&#8221; functionality and easily go to functions and view their dependencies and cross-references.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p171code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1714"><td class="code" id="p171code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> InvalidCodeNOPReplace <span style="color: #008000;">:</span> MetadataMutator
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> InvalidCodeNOPReplace<span style="color: #008000;">&#40;</span>IMetadataHost host<span style="color: #008000;">&#41;</span>
	    <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span>host<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> List<span style="color: #008000;">&lt;</span>IOperation<span style="color: #008000;">&gt;</span> Visit<span style="color: #008000;">&#40;</span>List<span style="color: #008000;">&lt;</span>IOperation<span style="color: #008000;">&gt;</span> operations<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	    operations <span style="color: #008000;">=</span> Utilities<span style="color: #008000;">.</span><span style="color: #0000FF;">ReplaceInvalidOpCodeAsNOP</span><span style="color: #008000;">&#40;</span>operations<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Visit</span><span style="color: #008000;">&#40;</span>operations<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> List<span style="color: #008000;">&lt;</span>IOperation<span style="color: #008000;">&gt;</span> ReplaceInvalidOpCodeAsNOP<span style="color: #008000;">&#40;</span>List<span style="color: #008000;">&lt;</span>IOperation<span style="color: #008000;">&gt;</span> ops<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    List<span style="color: #008000;">&lt;</span>IOperation<span style="color: #008000;">&gt;</span> newOps <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>IOperation<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>IOperation op <span style="color: #0600FF; font-weight: bold;">in</span> ops<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>IsValidOpCode<span style="color: #008000;">&#40;</span>op<span style="color: #008000;">.</span><span style="color: #0000FF;">OperationCode</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	    Operation o <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Operation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	    o<span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span> <span style="color: #008000;">=</span> op<span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span><span style="color: #008000;">;</span>
	    o<span style="color: #008000;">.</span><span style="color: #0000FF;">Offset</span> <span style="color: #008000;">=</span> op<span style="color: #008000;">.</span><span style="color: #0000FF;">Offset</span><span style="color: #008000;">;</span>
	    o<span style="color: #008000;">.</span><span style="color: #0000FF;">OperationCode</span> <span style="color: #008000;">=</span> OperationCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Nop</span><span style="color: #008000;">;</span>
	    o<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> 0x0<span style="color: #008000;">;</span>
	    newOps<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>o<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">else</span>
	<span style="color: #008000;">&#123;</span>
	    newOps<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>op<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> newOps<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> populateOpCodeDic<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
   OpCodes <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Dictionary<span style="color: #008000;">&lt;</span>OperationCode,<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValues</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>OperationCode<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
     OpCodes<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span>OperationCode<span style="color: #008000;">&#41;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsValidOpCode<span style="color: #008000;">&#40;</span>OperationCode opCode<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>OpCodes <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
            populateOpCodeDic<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
       <span style="color: #0600FF; font-weight: bold;">return</span> OpCodes<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>opCode<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Unfortunately reconstructing the C# source doesn&#8217;t work at this stage due to the nops and invalid branching structure. However, I&#8217;m trying to work out a middle layer which can take a methodbody&#8217;s operations list, abstract it out, turn it in to a control flow graph, optimize it and rewrite. However i&#8217;m still stuck at the rewriting part. I hit a small snag in the logic I haven&#8217;t had time to work out just yet. Hopefully then the C# can be reconstructed.</p>
<p>Tomorrow I&#8217;ll post some simple methods to get readable names out of the method/properties/class names to make following logic easier.</p>
<p>*Edit forgot to add the IsValidOpCode method.</p>
<p>**Edit had to readd disappearing generic types.. Ugh!  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

