<?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; CCI</title>
	<atom:link href="http://www.casaba.com/blog/tag/cci/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 3)</title>
		<link>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-3/</link>
		<comments>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-3/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 18:04:51 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Nebulous]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Babel]]></category>
		<category><![CDATA[CCI]]></category>
		<category><![CDATA[Deobfuscation]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://www.casabasecurity.com/blog/?p=188</guid>
		<description><![CDATA[Renaming parts of the assembly. So I promised this last week, but I&#8217;ve been busy on a new project. Below is some code that shows renaming of methods. This is a solution to renaming classes within namespaces. It iterates over each namespace renaming classes from class1 -&#62; classN. This is more useful for human readability [...]]]></description>
			<content:encoded><![CDATA[<p>Renaming parts of the assembly.</p>
<p>So I promised this last week, but I&#8217;ve been busy on a new project. Below is some code that shows renaming of methods. This is a solution to renaming classes within namespaces. It iterates over each namespace renaming classes from class1 -&gt; classN. This is more useful for human readability and tracing logic. I leave it as an exercise to the reader to figure out how to rename other parts of the assembly. But hey if you really need it an get stuck, let me know!</p>
<p>I&#8217;ll be posting a tool at some point that does all these different actions for you. Hopefully I&#8217;ll have a early release out by mid next month. I&#8217;m currently learning WPF well enough to build in visulalizations of the control flow graph. That way after a mutator is applied you can visually see the results.</p>
<p>There is a dictionary in the mutator class that uses the namespace string as a key in order to know which class # i left off at. I test on the string length &lt; 2 because the obfuscators I&#8217;ve seen that do this trick tend to just rename everything to some obscure unicode code point of length 1. Just a easy stop condition. You can use any stop condition that suits your needs.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p188code2'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1882"><td class="code" id="p188code2"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> NamespaceTypeDefinition Visit<span style="color: #008000;">&#40;</span>NamespaceTypeDefinition namespaceTypeDefinition<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span> key <span style="color: #008000;">=</span> namespaceTypeDefinition<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainingUnitNamespace</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>classDict<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
     classDict<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>key, <span style="color: #FF0000;">0</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;">if</span> <span style="color: #008000;">&#40;</span>namespaceTypeDefinition<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
     <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> classDict<span style="color: #008000;">&#91;</span>key<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
     namespaceTypeDefinition<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">host</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NameTable</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetNameFor</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Class{0}&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     i<span style="color: #008000;">++;</span>
     classDict<span style="color: #008000;">&#91;</span>key<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: #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>namespaceTypeDefinition<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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('p171code5'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1715"><td class="code" id="p171code5"><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('p171code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1716"><td class="code" id="p171code6"><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>
		<item>
		<title>Microsoft CCI Framework for Deobfuscating .Net binaries.</title>
		<link>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries/</link>
		<comments>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 20:01:51 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Babel]]></category>
		<category><![CDATA[CCI]]></category>
		<category><![CDATA[Deobfuscation]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://www.casabasecurity.com/blog/?p=152</guid>
		<description><![CDATA[We had an issue recently crop up with an obfuscated .Net binary. I’ve been meaning to spend more time reversing .Net protected binaries so I start looking in it. Unfortunately everything I was reading on the forums and internet seemed difficult. Having recently read a little about Microsoft’s CCI framework, I thought this might be [...]]]></description>
			<content:encoded><![CDATA[<p>We had an issue recently crop up with an obfuscated .Net binary. I’ve been meaning to spend more time reversing .Net protected binaries so I start looking in it. Unfortunately everything I was reading on the forums and internet seemed difficult. Having recently read a little about Microsoft’s CCI framework, I thought this might be the best solution to the problem. Using a hex editor and looking for patterns seems hokey and a bit impractical.</p>
<p>So the first thing I decided to try was removing the SuppressIldasmAttribute attribute.  Below is some example code doing just that using CCI and rewriting the file. This produces an executable that works and doesn’t require just hex editing out the attribute leaving an executable that doesn’t run.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p152code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1528"><td class="code" id="p152code8"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     var host <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> PeReader<span style="color: #008000;">.</span><span style="color: #0000FF;">DefaultHost</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     var module <span style="color: #008000;">=</span> host<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadUnitFrom</span><span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IModule<span style="color: #008000;">;</span>
     var attributeRemover <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AttributeRemover<span style="color: #008000;">&#40;</span>host<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     module <span style="color: #008000;">=</span> attributeRemover<span style="color: #008000;">.</span><span style="color: #0000FF;">Visit</span><span style="color: #008000;">&#40;</span>module<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     Stream peStream <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>module<span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span> <span style="color: #666666;">&quot;.fixed&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     PeWriter<span style="color: #008000;">.</span><span style="color: #0000FF;">WritePeToStream</span><span style="color: #008000;">&#40;</span>module, host, peStream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     Console<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Out</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Finished&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/*
* Removes the static attribute atm SuppressIldasmAttribute.. can be modified to remove any attribute.
*/</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AttributeRemover <span style="color: #008000;">:</span> MetadataMutator
<span style="color: #008000;">&#123;</span>
&nbsp;
     PlatformType pt<span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #0600FF; font-weight: bold;">public</span> AttributeRemover<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>
         pt <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> PlatformType<span style="color: #008000;">&#40;</span>host<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <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>ICustomAttribute<span style="color: #008000;">&gt;</span> Visit<span style="color: #008000;">&#40;</span>List<span style="color: #008000;">&lt;</span>ICustomAttribute<span style="color: #008000;">&gt;</span> customAttributes<span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
          <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> customAttributes<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span>  <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>customAttributes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;System.Runtime.CompilerServices.SuppressIldasmAttribute&quot;</span><span style="color: #008000;">&#41;</span>
               <span style="color: #008000;">&#123;</span>
                    customAttributes<span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveAt</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</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> <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>customAttributes<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see it requires very little code. Anyways that’s enough for this post. I also have some more code I’ll be posting that uses CCI to rename methods/class/methods from their “mangled names” and code that removes invalid OpCodes so reflector works at the IL level. I’m still working on code that goes through creates a optimized methods to remove the invalid jumps such that C# code can hopefully be reconstructed. We’ll see how that goes.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

