<?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; Debugging</title>
	<atom:link href="http://www.casaba.com/blog/category/debugging/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 &#8220;Roslyn&#8221; based REPL injection.</title>
		<link>http://www.casaba.com/blog/2011/12/microsoft-roslyn-based-repl-injection/</link>
		<comments>http://www.casaba.com/blog/2011/12/microsoft-roslyn-based-repl-injection/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 18:05:12 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.casaba.com/blog/?p=277</guid>
		<description><![CDATA[Microsoft recently released their new Compiler API codename &#8220;Roslyn&#8221;. If you haven&#8217;t checked it out yet you should. Here&#8217;s the link: http://msdn.microsoft.com/en-us/roslyn/. I wanted to get my hands a little dirty and play with the new API. I&#8217;ve been meaning to look into Managed DLL injection for a while to get code execution for process [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft recently released their new Compiler API codename &#8220;Roslyn&#8221;. If you haven&#8217;t checked it out yet you should. Here&#8217;s the link: http://msdn.microsoft.com/en-us/roslyn/.</p>
<p>I wanted to get my hands a little dirty and play with the new API. I&#8217;ve been meaning to look into Managed DLL injection for a while to get code execution for process interrogation. There are times when we&#8217;re testing that we want to interrogate a process for framework level information. For whatever reasons we sometimes can&#8217;t compile the target with hooks. So it would be nice to have a way to execute code. Roslyn’s CSX files look like a great way to accomplish this so that&#8217;s what I&#8217;m trying to expose. </p>
<p>Currently this only works on 32 bit processes.  </p>
<p>Let&#8217;s start by describing the architecture as there are 3 things going on. The major components are the Injector, Unmanaged Injectee and Managed Injectee. The injector is the controller in this scenario; he&#8217;s responsible for the injection into the managed process and communication between the components. Communication is handled via named pipes. </p>
<p>The injector uses a well-documented dll injection technique via CreateRemoteThread and LoadLibrary. This loads the unmanaged dll into the Managed process. The unmanaged DLL actually handles the Managed DLL injection. I wont go into unmanaged dll injection as it&#8217;s pretty well document technique. I assume the reader understands these concepts. </p>
<p>From this point I assume the unmanaged DLL has been injected into the managed process. </p>
<p>After the unmanaged DLL is injected I need to make sure the correct version of the CLR is loaded. To accomplish this use the CLR hosting API’s to determine the version of the CLR that is loaded by the process (Provided there is one loaded). The host process must be running .Net 4.0 to support the Roslyn API. Because the early versions of the hosting API&#8217;s are deprecated I need to check to see if the .net 4.0 mscoree is loaded &#8220;msvcr100_clr0400.dll&#8221;. I check via a GetModuleHandle. If it exists we know we are running .Net 4.0 and know the CLR is already running. Two birds down with a single stone. </p>
<pre>
hMod = GetModuleHandle(L"msvcr100_clr0400.dll");
</pre>
<p>Once we know the CLR is loaded and it’s 4.0 we can get a handle to the CLSID_CLRMetaHost via:</p>
<pre>
hr = CLRCreateInstance( CLSID_CLRMetaHost,
IID_ICLRMetaHost,
(LPVOID*)&amp;pMetaHost );
</pre>
<p>From the meta host we can get a handle to the running RunTimeHost via:</p>
<pre>
ICLRRuntimeHost *pClrHost = NULL;
runTimeInfo-&gt;GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID *)&amp;pClrHost);
</pre>
<p>This will return a handle to the current RuntimeHost (Or load the runtime if it isn’t running). The next call is to load my Managed DLL plus call the entry method.</p>
<pre>
pClrHost-&gt;ExecuteInDefaultAppDomain(L"InjectedManagedDll_Net_4.dll", L"InjectedManagedDll_Net_4.InjectedClass", L"Test", L"TestArg" , &amp;ret);
</pre>
<p>This loads the Managed DLL into the process. Once the Managed DLL’s Test method is called I create a managed thread.</p>
<pre>
public static int Test(string param)
{
new Thread(new ThreadStart(ThreadFunc)).Start();
return 666;
}
</pre>
<p>This thread then generates a few more threads and sets up the NamedPipe communication pipe and reports to the server things are setup.</p>
<pre>
static void ThreadFunc()
{
try
{
PipeClient.Instance.Start("CNIPipe");
}
catch (Exception e)
{
PipeClient.Instance.LogMessageToServer(e.Message);
}
}
</pre>
<p>I then expose some simple messages back and forth between the injector and injectee and expose a simple REPL loosely based on this guy’s implementation: http://visualstudiomagazine.com/articles/2011/11/16/the-roslyn-scripting-api.aspx.</p>
<pre>
private ScriptHost()
{

HashSetassemblys = new HashSet();
assemblys.Add(Assembly.GetCallingAssembly());
assemblys.Add(Assembly.GetEntryAssembly());
assemblys.Add(Assembly.GetExecutingAssembly());

Listnamespaces = new List() { "System", "System.Collections", "System.Collections.Generic" };

ScriptEngine = new ScriptEngine(assemblys.ToList(), namespaces);

Session = Session.Create(this);
}

public object Execute(string code)
{
return ScriptEngine.Execute(code, Session);
}
</pre>
<p>This gets you a basic REPL inside another process. Next steps include making sure the communication API between the host and injectee are more well formed and able to handle both 32 and 64 bit processes. Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2011/12/microsoft-roslyn-based-repl-injection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
		<item>
		<title>Some good .Net debugging info</title>
		<link>http://www.casaba.com/blog/2007/02/some-good-net-debugging-info/</link>
		<comments>http://www.casaba.com/blog/2007/02/some-good-net-debugging-info/#comments</comments>
		<pubDate>Mon, 12 Feb 2007 08:00:00 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Visual Studio 2005/2008 debugging with sos.dll The blog seems to have gone cold, so copying here for good luck. http://blogs.msdn.com/vancem/archive/2006/09/05/742062.aspx Vance Morrison&#039;s Weblog Vance Morrison is currently an Architect on the .NET Runtime Team, specializing in performance issues with the runtime or managed code in general. Digging deeper into managed code with Visual Studio: Using [...]]]></description>
			<content:encoded><![CDATA[<p>Visual Studio 2005/2008 debugging with sos.dll<br />
The blog seems to have gone cold, so copying here for good luck.<br />
<a href="http://blogs.msdn.com/vancem/archive/2006/09/05/742062.aspx">http://blogs.msdn.com/vancem/archive/2006/09/05/742062.aspx</a></p>
<p>        <a id="ctl00___ctl00___ctl00_ctl00_bcr_bth___BlogTitle" class="headermaintitle" href="http://blogs.msdn.com/vancem/archive/2006/09/05/742062.aspx" bound="true">Vance Morrison&#039;s Weblog</a></p>
<p>        Vance Morrison is currently an Architect on the .NET Runtime Team, specializing in performance issues with the runtime or managed code in general.</p>
<p>                Digging deeper into managed code with Visual Studio: Using SOS </p>
<p>I have let my blog laps for too long. &nbsp;&nbsp; I am back to blogging. &nbsp; I realized reciently that we have simply not written down many interesting facts about how the runtime actually works.&nbsp; I want to fix this. &nbsp; Coming up in future blogs I am going to be doing a bit of a &#039;architectural overview&#039; which describe the differences between managed and unmanaged code, but before I do that I realized that I have not even finished a blog entry I started in March.</p>
<p>In my blog <a href="http://blogs.msdn.com/vancem/archive/2006/02/20/535807.aspx" bound="true">How to use Visual Studio to investigate code generation questions in managed code</a>, I talk about the how to configure Visual Studio so that you can actually look at optimized code in the debugger (which sadly is not as trivial as you would like), and showed how to look at the disassembly of managed code. &nbsp; &nbsp;Unfortunately manage code is hard to read without a guide, and so in this blog I will show you some very useful tips for reading managed assembly code.</p>
<p>In this blog entry I will show you the instructions ACTUALLY need to get executed to do something as simple as assigning a string to field of a class.  Note that I am assuming a familiarity with X86 assembly code. If you are the type who never wants to read assembly code, you should stop reading now, because most of this blog is a step-by-step explanation of it.</p>
<p>I have attached the file InspectingManageCode.zip, which contains a (trivial), project that I used for this example.&nbsp; You are STRONLY encouraged to open it (you can browse it the main file is Program.cs).&nbsp; Copy the files (simply drag the &#039;InspectingManagedCode directory inside the ZIP to a directory of your choosing), launch the InspecingManagedCode.sln file and run the example.&nbsp;  &nbsp;While the project is already set to build and run optimized code, you will still need to turn off ‘just my code’ and turn on JIT optimization as described in my previous blog to follow along.</p>
<p>The code in the attached example is pretty trivial.<br />
class Program<br />
{<br />
&nbsp; &nbsp; string myString;<br />
&nbsp; &nbsp; private Program()<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; myString = &#8220;foo&#8221;;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; static void Main(string[] args)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; Program p = new Program();<br />
&nbsp; &nbsp;}<br />
}</p>
<p>If you were to follow the instructions in the <a href="http://blogs.msdn.com/vancem/archive/2006/02/20/535807.aspx" bound="true">previous blog </a>to see what code was generated for the body of ‘Main’ you would find the following code.</p>
<p>00000000&nbsp; push &nbsp; &nbsp; &nbsp;&nbsp;esi<br />
00000001&nbsp; mov &nbsp; &nbsp; &nbsp;&nbsp; ecx,9181F4h<br />
00000006&nbsp; call &nbsp; &nbsp; &nbsp;&nbsp;FFCB1264<br />
0000000b&nbsp; mov &nbsp; &nbsp; &nbsp;&nbsp; esi,eax<br />
0000000d&nbsp; mov &nbsp; &nbsp; &nbsp; &nbsp; eax,dword ptr ds:[0227307Ch]<br />
00000013&nbsp; lea &nbsp; &nbsp; &nbsp; &nbsp; edx,[esi+4]<br />
00000016&nbsp; call &nbsp; &nbsp; &nbsp;&nbsp; 79222B78<br />
0000001b&nbsp; pop &nbsp; &nbsp; &nbsp; &nbsp; esi<br />
0000001c&nbsp; ret</p>
<p>At first glance this code has little similarity to the source code: the original source has a call the constructor ‘Program’ and the assembly code has two calls to strange hex addresses.&nbsp; There are also references to magical numbers like 9181F4H and 0227307CH. &nbsp; In this case the disassembly has not proven to be very valuable. &nbsp;&nbsp; What can we do? &nbsp;&nbsp; </p>
<p>Sadly if we try to peer into these CALL instructions we cannot, the debugger comes back with the very unhelpful message ‘There is no code at the specified  location’. &nbsp; Actually Visual Studio is LIEING to you. There really is code there, but it simply will not show you.  I will show you techniques to get around this.</p>
<p>The key to unlocking mysteries of managed code, is a debug helper called SOS.DLL (it is a dll that is shipped with the runtime).  The DLL is what is called a ‘debugger extension’.  Basically it implements functionality that is useful in a debugger implementing<br />
                        functions that are useful for debugging code associated with it (in this case the<br />
                        runtime).&nbsp; &nbsp;Other bloggers have<br />
                        also commented on the use of this DLL (do a web search of SOS.DLL for more).</p>
<p>                    In Visual Studio, you load SOS.DLL by<br />
                        opening the immediate window (Ctrl-D I) and typing</p>
<p>                        .load SOS.dll</p>
<p>                    If you do this you may get the message</p>
<p>                        SOS not available while Managed only debugging.&nbsp;<br />
                        To load SOS, enable unmanaged debugging in your project properties.</p>
<p>                    This message is actually reasonably<br />
                        helpful.&nbsp; By stopping<br />
                        the debugger (Shift F5) going to Solution Explorer (Right hand pane), right clicking<br />
                        on the InspectingManagedCode project file, and selecting Properties, you will get<br />
                        the properties pane for the project.&nbsp;<br />
                        If you select the ‘Debug’ tab on the left side you will find 3 check boxes<br />
                        at the bottom, one of which is labeled ‘Enable unmanaged code debugging’&nbsp; If you check this, you put the debugger<br />
                        into a mode where it can debug both mananged and unmanaged code, (which means you<br />
                        can then use SOS.DLL). &nbsp;&nbsp;<br />
                        I have already done this on the InspectingManagedCode project, but you will<br />
                        have to repeat this any time you need to use SOS.<br />
                            &nbsp; (Sadly the instructions for setting the debugger mode are different<br />
                        for C++).&nbsp; &nbsp; Note that running<br />
                        the debugger to debug both managed and unmanaged code will slow the debugger down<br />
                        a bit (it loads the symbols for all the unmanaged DLLS), so you probably only want<br />
                        do this on projects like this one where you want to use SOS.DLL.&nbsp; </p>
<p>                    Now you should be able to set a breakpoint<br />
                        in Main(), run the program (F5), and go to the immediate window (CTRL-D I) and type</p>
<p>                        .load SOS.dll</p>
<p>                    And get the message</p>
<p>                        extension C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\sos.dll loaded.</p>
<p>                    If you are curious the SOS.DLL has reasonably<br />
                        good help, if you type the command</p>
<p>                        !Help</p>
<p>                    It will give you a list of commands,<br />
                        and you can get help on individual commands by specifying the name eg.</p>
<p>                        !Help u</p>
<p>                    It will give you help on the ‘u’ (unassembled)<br />
                        command. &nbsp;&nbsp;<br />
                        All SOS commands need to be prefixed by a ! character so that the Visual Studio<br />
                        Debugger knows that it is an SOS command and not an immediate value to be interpreted<br />
                        (the normal meaning of text typed in the immediate window).</p>
<p>                    The unassemble SOS command is the command<br />
                        we are interested in. &nbsp;<br />
                        It will disassemble a managed routine, but do a much better job than Visual Studio<br />
                        presently does. &nbsp;<br />
                        Unfortunately, we need the address of the routine we want disassemble, and Visual<br />
                        Studio goes to some length to hide this information.&nbsp;<br />
                        If you look at the disassembly for the code (CTRL-ALT-D), you will see that<br />
                        the address of the routine is never given, only the offset from the beginning of<br />
                        the method.&nbsp; </p>
<p>                    The way around this is to use the ‘Registers<br />
                        window’ (Ctrl-D R).&nbsp;<br />
                        I happen to like to put this window just above the immediate window and shrink it<br />
                        so that only the two lines that actually show values are showing.&nbsp;<br />
                                &nbsp;One of the registers is ‘EIP’ which stands for Extended Instruction<br />
                        Pointer’.&nbsp; It is<br />
                        the address of the current instruction pointer.<br />
                            &nbsp; In my particular invokaction EIP has the value of 00DE0071, so<br />
                        I can do the command</p>
<p>                        !u 00DE0071</p>
<p>                    Which will disassemble the ENTIRE routine<br />
                        that the address 00DE0071 lives in.&nbsp;<br />
                        I like to right click in the immediate window and select ‘Clear All’ before<br />
                        I do this so the only thing in that window is the disassembly. &nbsp; On my machine I get the result</p>
<p>                        Normal JIT generated code</p>
<p>                        Program.Main(System.String[])</p>
<p>                        Begin 00de0070, size 1d</p>
<p>                        00DE0070 56 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; push<br />
                                &nbsp; &nbsp;&nbsp; &nbsp;<br />
                                esi</p>
<p>                        &gt;&gt;&gt; 00DE0071 B904309100<br />
                            &nbsp; &nbsp; &nbsp; mov<br />
                                &nbsp; &nbsp; &nbsp; &nbsp; ecx,913004h</p>
<p>                        00DE0076 E8A11FB2FF &nbsp; &nbsp;<br />
                            &nbsp; call &nbsp; &nbsp;<br />
                                &nbsp;&nbsp; 0090201C (JitHelp: CORINFO_HELP_NEWSFAST)</p>
<p>                        00DE007B 8BF0 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; mov<br />
                                &nbsp; &nbsp; &nbsp; &nbsp; esi,eax</p>
<p>                        00DE007D 8B053C302B02 &nbsp; &nbsp;<br />
                        mov &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; eax,dword ptr ds:[022B303Ch]</p>
<p>                        00DE0083 8D5604 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; lea &nbsp;<br />
                                &nbsp; &nbsp; &nbsp; edx,[esi+4]</p>
<p>                        00DE0086 E8A5380979 &nbsp; &nbsp;<br />
                            &nbsp; call &nbsp; &nbsp;<br />
                                &nbsp;&nbsp; 79E73930</p>
<p>                        00DE008B 5E &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; pop<br />
                                &nbsp; &nbsp; &nbsp; &nbsp; esi</p>
<p>                        00DE008C C3 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; ret</p>
<p>                    It is not unlike the version the Visual<br />
                        Studio produced, but there are differences</p>
<p>                            1. &nbsp; &nbsp;&nbsp;<br />
                            You will note that the ‘call instruction<br />
                                is annoted with ‘JitHelp: CORINFO_HELP_NEWFAST’, which makes it at least a bit clearer<br />
                                that this helper is used to create a New object (and is the fast version, we have<br />
                                many variations).</p>
<p>                            2. &nbsp; &nbsp;&nbsp;<br />
                            It printed the whole routine that 00DE0071<br />
                                lives in and prints a &gt;&gt;&gt;&nbsp;<br />
                                on the instruction corresponding to the 00DE0071 address.&nbsp; </p>
<p>                            3. &nbsp; &nbsp;&nbsp;<br />
                            While it did not print the name for<br />
                                the ‘call 79E73930’, notice that the HEX value is different than the value in the<br />
                                Visual Studio Disassembly (79222B78).&nbsp;<br />
                                The value in the&nbsp;<br />
                                VS disassembly is simply WRONG (it is bug no one bothered to fix).&nbsp; </p>
<p>                    So let’s take a look at the first two<br />
                        instructions.</p>
<p>                        00DE0071 B904309100 &nbsp; &nbsp;<br />
                            &nbsp; mov &nbsp; &nbsp;<br />
                                &nbsp; &nbsp; ecx,913004h</p>
<p>                        00DE0076 E8A11FB2FF &nbsp; &nbsp;<br />
                            &nbsp; call &nbsp; &nbsp;<br />
                                &nbsp;&nbsp; 0090201C (JitHelp: CORINFO_HELP_NEWSFAST)</p>
<p>                    I mentioned that this helper call creates<br />
                        a new object from the GC heap. To do so it needs to know that type of the object<br />
                        to be created. This is what the magic number 913004 does.&nbsp; Internally in the runtime types are<br />
                        described by a structure called a MethodTable, and 913004 is the address of the<br />
                        MethodTable to create.&nbsp;<br />
                        We can find out what type 913004 corresponds to by using the !DumpMT (dump Method<br />
                        Table) SOS command.&nbsp; </p>
<p>                        !DumpMT 913004h</p>
<p>                    Produces the output</p>
<p>                        EClass: 00911254</p>
<p>                        Module: 00912c14</p>
<p>                    Name: Program</p>
<p>                        mdToken: 02000002&nbsp;<br />
                        (C:\Documents and Settings\vancem\My Documents\Visual Studio 2005\Projects\InspectingManagedCode\bin\Release\InspectingManagedCode.exe)</p>
<p>                        BaseSize: 0xc</p>
<p>                        ComponentSize: 0&#215;0</p>
<p>                        Number of IFaces in IFaceMap: 0</p>
<p>                        Slots in VTable: 6</p>
<p>                    The only output of this that is interesting<br />
                        at this point is the ‘Name’ field, which as you can see, indicates that 913004 cooresponds<br />
                        to the ‘Program’ type. &nbsp;<br />
                        Thus these first two instructions create a program object. &nbsp; This program object comes back from<br />
                        the helper with all its fields zeroed, so the next instructions in the program are<br />
                        the body of the constructor (the Program() constructor has been inlined into the<br />
                        body of Main().&nbsp; </p>
<p>                    The next instructions</p>
<p>                        00DE007B 8BF0 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; mov<br />
                                &nbsp; &nbsp; &nbsp; &nbsp; esi,eax</p>
<p>                        00DE007D 8B053C302B02 &nbsp; &nbsp;<br />
                        mov &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; eax,dword ptr ds:[022B303Ch]</p>
<p>                        00DE0083 8D5604 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; lea &nbsp;<br />
                                &nbsp; &nbsp; &nbsp; edx,[esi+4]</p>
<p>                        00DE0086 E8A5380979 &nbsp; &nbsp;<br />
                            &nbsp; call &nbsp; &nbsp;<br />
                                &nbsp;&nbsp; 79E73930</p>
<p>                    Basically implement the statement ‘myString<br />
                            = &#8220;foo&#8221;’ The helper returns a pointer into the<br />
                                uninitialized object in the EAX register.&nbsp;<br />
                                The mov saves this into the ESI register.&nbsp;<br />
                                EAX is then loaded with what is at the address 022B303Ch.&nbsp; This happens to be the string “foo”<br />
                                (more on how it go there in a later blog).<br />
                                    &nbsp; You can confirm this by going to the disassembly code, setting<br />
                                a breakpoing right after the eax,dword ptr ds:[022B303Ch] instruction and looking<br />
                                at the value of the EAX register in the ‘registers’ window.&nbsp;<br />
                                        &nbsp;In my example it happens to be the value 012B1D44. &nbsp; You can then use the command</p>
<p>                        !DumpObj 012B1D44</p>
<p>                    Which will dump the managed object at<br />
                        this address.&nbsp; This<br />
                        will print .</p>
<p>                        DumpObj 012B1D44</p>
<p>                    Name: System.String</p>
<p>                        MethodTable: 790fa3e0</p>
<p>                        EEClass: 790fa340</p>
<p>                        Size: 24(0&#215;18) bytes</p>
<p>                        &nbsp;(C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)</p>
<p>                    String: foo</p>
<p>                        Fields:</p>
<p>                        &nbsp; &nbsp; &nbsp;<br />
                        MT &nbsp;&nbsp; Field &nbsp; Offset &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            Type VT &nbsp; &nbsp;<br />
                        Attr &nbsp;&nbsp; Value<br />
                        Name</p>
<p>                        790fed1c&nbsp; 4000096 &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        4 &nbsp; &nbsp; &nbsp; &nbsp;<br />
                        System.Int32&nbsp; 0<br />
                        instance &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        4 m_arrayLength</p>
<p>                        790fed1c&nbsp; 4000097 &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        8 &nbsp; &nbsp; &nbsp; &nbsp;<br />
                        System.Int32&nbsp; 0<br />
                        instance &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        3 m_stringLength</p>
<p>                        790fbefc&nbsp; 4000098 &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        c &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        System.Char&nbsp;<br />
                        0 instance &nbsp; &nbsp; &nbsp;<br />
                        66 m_firstChar</p>
<p>                        790fa3e0&nbsp; 4000099 &nbsp; &nbsp; &nbsp; 10 &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        System.String&nbsp; 0 &nbsp; shared &nbsp; static Empty</p>
<p>                        &nbsp; &nbsp; &gt;&gt;<br />
                        Domain:Value&nbsp; 0014c550:790d6584<br />
                        &lt;&lt;</p>
<p>                        79124670&nbsp; 400009a &nbsp; &nbsp; &nbsp; 14 &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        System.Char[]&nbsp; 0 &nbsp; shared &nbsp; static WhitespaceChars</p>
<p>                        &nbsp; &nbsp; &gt;&gt;<br />
                        Domain:Value&nbsp; 0014c550:012b186c<br />
                        &lt;&lt; Basically</p>
<p>                            &nbsp;Again, most of the output is uninteresting at this point, except<br />
                        the Name field (which says its a string), and the ‘String’ field (which shows the<br />
                        string value is ‘foo’).&nbsp;<br />
                        So we have confirmed that this instruction loads up the address of the ‘foo’ string<br />
                        into the EAX register.&nbsp;<br />
                        &nbsp;What is left is</p>
<p>                        00DE0083 8D5604 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; lea &nbsp;<br />
                                &nbsp; &nbsp; &nbsp; edx,[esi+4]</p>
<p>                        00DE0086 E8A5380979 &nbsp; &nbsp;<br />
                            &nbsp; call &nbsp; &nbsp;<br />
                                &nbsp;&nbsp; 79E73930</p>
<p>                    The first instruction ‘LEA’ may not<br />
                        be familiar to you.&nbsp;<br />
                        It is Load Effective Address (LEA).&nbsp;<br />
                        Basically it works just like a MOV instruction, but instead of moving what<br />
                        was AT the memory specified, it loads the ADDRESS of the memory. &nbsp; Another way of looking at this is to<br />
                        imagine a MOV instruction with the [] dropped (which represent memory fetching).&nbsp; Thus</p>
<p>                        00DE0083 8D5604 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; lea &nbsp;<br />
                                &nbsp; &nbsp; &nbsp; edx,[esi+4]</p>
<p>                    Can be thought of as</p>
<p>                        00DE0083 8D5604 &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; mov &nbsp;<br />
                                &nbsp; &nbsp; &nbsp; edx, esi+4</p>
<p>                    That is it adds 4 to ESI and places<br />
                        it in EDX. &nbsp; Now<br />
                        remember ESI points at our newly created ‘Program’ object. &nbsp; We could find out all the fields of<br />
                        this object by dumping it,&nbsp;<br />
                        In my debugger ESI has the value of 012B1D5C so I can do</p>
<p>                        !DumpObj 012B1D5C</p>
<p>                    And get</p>
<p>                    Name: Program</p>
<p>                        MethodTable: 00913004</p>
<p>                        EEClass: 00911254</p>
<p>                    Size: 12(0xc) bytes</p>
<p>                        &nbsp;(C:\Documents and<br />
                        Settings\vancem\My Documents\Visual Studio 2005\Projects\InspectingManagedCode\bin\Release\InspectingManagedCode.exe)</p>
<p>                        Fields:</p>
<p>                        &nbsp; &nbsp; &nbsp;<br />
                        MT &nbsp;&nbsp; Field &nbsp; Offset &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            Type VT &nbsp; &nbsp;<br />
                        Attr &nbsp;&nbsp; Value<br />
                        Name</p>
<p>                        790fa3e0&nbsp; 4000001 &nbsp; &nbsp; &nbsp;<br />
                                    &nbsp;4 &nbsp; &nbsp;<br />
                                        &nbsp;&nbsp; System.String&nbsp;<br />
                        0 instance 00000000 myString</p>
<p>                    Which tells us that ESI points at a<br />
                        ‘Program’ object and that the total size of the object is 12 (more on that in a<br />
                        later blog), and that at offset 4 there is a field calls ‘myString’ of type System.String<br />
                        that currently has the value of 0 (null).<br />
                            &nbsp; </p>
<p>                    So now we can make a pretty good guess<br />
                        that the LEA instruction is setting EDX to the address of the ‘myString’ field of<br />
                        the program object.&nbsp;<br />
                        EAX has been set to the ‘Foo’ String, and next comes the mysterious</p>
<p>                        00DE0086 E8A5380979 &nbsp; &nbsp;<br />
                            &nbsp; call &nbsp; &nbsp;<br />
                                &nbsp;&nbsp; 79E73930</p>
<p>                    Ideally SOS would have annotated this<br />
                        helper. &nbsp; It is<br />
                        what we call a ‘WriteBarrier’. &nbsp;<br />
                        More on exactly what a write barrier is later,<br />
                            &nbsp;but for now the important thing to know is that ALL updates to<br />
                        OBJECT REFERENCES that live in the GC heap need to be done by calling a write barrier<br />
                        helper. &nbsp;&nbsp;<br />
                        Since the Program object lives in the heap, and we are updating a object reference<br />
                        pointer inside it we need to use the write barrier.<br />
                            &nbsp; </p>
<p>                    The runtime actually has many write<br />
                        barriers.&nbsp; All the<br />
                        write barriers have an unusual calling convention.&nbsp;<br />
                        They all take the address to be updated in the EDX register. &nbsp; Then depending on the write barrier,<br />
                        they take the value to update in some other register (this particular write barrier<br />
                        is the most commonly used, and takes its argument in the EAX register). &nbsp;&nbsp; Logically all the write barrier<br />
                        does is do (*EDX = EAX)&nbsp;<br />
                        (that is update what EDX points at to be the value in EAX).</p>
<p>                    That is about it for this example&nbsp; The only instructions<br />
                        &nbsp;we did not cover<br />
                        are the PUSH ESI, and POP ESI at the beginning and end of the routine.&nbsp; As anyone who deals with assembly code<br />
                        this is simply saving and restoring ESI since we used it in the routine itself.&nbsp; </p>
<p>                    To recap here are the instructions that<br />
                        actually got executed in the ‘Main’ program and what they do.&nbsp; </p>
<p>                    push &nbsp; &nbsp;<br />
                        &nbsp;&nbsp; esi &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; // save ESI<br />
                        mov &nbsp; &nbsp; &nbsp; &nbsp; ecx,913004h<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        // ECX = MethodTable(Program)<br />
                        call &nbsp; &nbsp; &nbsp;&nbsp; 0090201C<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp;&nbsp; // EAX = New Object (Program)<br />
                        mov &nbsp; &nbsp; &nbsp; &nbsp; esi,eax<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; // ESI = this (new object)<br />
                        mov &nbsp; &nbsp; &nbsp; &nbsp; eax,dword ptr ds:[022B303Ch] &nbsp; &nbsp;&nbsp; // EAX = “foo”<br />
                        lea &nbsp; &nbsp; &nbsp; &nbsp; edx,[esi+4]<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; // EDX = &amp;this.myString<br />
                        call &nbsp; &nbsp; &nbsp;&nbsp; 79E73930<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp;&nbsp; // this.myString = EAX (“foo”)<br />
                        pop &nbsp; &nbsp; &nbsp; &nbsp; esi<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // restore ESI<br />
                        ret &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
                        // return.</p>
<p>                        &nbsp;</p>
<p>                    We just understood very deaply EXACTLY<br />
                        what happens when a particular piece of managed code executes.&nbsp;<br />
                                &nbsp;Hopefully that wasn’t so bad.<br />
                                    &nbsp; &nbsp; Next time we will dig a bit into this WriteBarrier<br />
                        is and exactly what it does (how expensive is it?).<br />
                            &nbsp; We will also dig into exactly what went on inside the ‘New’ helper.&nbsp; &nbsp; In later blogs I will go into<br />
                        how exactly other run time features get converted to native code.&nbsp; </p>
<p>                        &nbsp;</p>
<p>                    I hope you are enjoying this peek under<br />
                        the hood of the .NET Runtime.&nbsp;</p>
<p>                    &nbsp;</p>
<p>                Published Tuesday, September 05, 2006 7:55 PM by <a id="ctl00___ctl00___ctl00_ctl00_bcr_ctl00___Entry___AuthorLink" href="/user/Profile.aspx?UserID=3800" bound="true">vancem</a></p>
<p>                        Filed under: <a href="/vancem/archive/tags/Tools/default.aspx" bound="true" rel="tag">Tools</a></p>
<p>                Attachment(s): <a id="ctl00___ctl00___ctl00_ctl00_bcr_ctl00___Entry___Attachment___DownLoadLink" href="/vancem/attachment/742062.ashx" bound="true">InspectingManagedCode.zip</a></p>
<p>                Comments</p>
<p>                            &nbsp;</p>
<p>                                <a id="ctl00___ctl00___ctl00_ctl00_bcr_ctl00___Comments___Comments_ctl01_NameLink" href="/utility/Redirect.aspx?U=http%3a%2f%2fbarrkel.blogspot.com%2f" bound="true" title="barrkel">barrkel</a> said:</p>
<p>                                Great info! Thanks.</p>
<p>                                BTW, when using windbg + sos to debug, what breakpoint (native: bp / bu) is best<br />
                                to set in order to use managed breakpoints (thus both !name2ee and !bpmd probably<br />
                                needed)? With a breakpoint on loading of mscorwks or calling of various CLR functions,<br />
                                when is the CLR booted up enough so that !name2ee etc. can work?</p>
<p>                                September 6, 2006 4:14 AM</p>
<p>                            &nbsp;</p>
<p>                                <a id="ctl00___ctl00___ctl00_ctl00_bcr_ctl00___Comments___Comments_ctl02_NameLink" href="/utility/Redirect.aspx?U=http%3a%2f%2fblogs.msdn.com%2fvancem" bound="true" title="vancem">vancem</a> said:</p>
<p>                                The subject of using SOS in windbg will be the subject of a future blog, however,<br />
                                I can quickly answer your question. &nbsp; &nbsp;</p>
<p>                                The !bpmd (Breakpoint MethodDescriptor), is a command that will set a breakpoint<br />
                                on a managed method by name. &nbsp; For example in the example the command</p>
<p>                                &nbsp; &nbsp;!bpmd &nbsp;InspectingManagedCode.exe Program.Main</p>
<p>                                Will set a breakpoint in the ‘Main’ routine of the example program in the ZIP file.<br />
                                &nbsp; Note that UNLIKE the !name2ee SOS command (which looks up a method, or class<br />
                                by name), the method being referenced in the !BPMD command does NOT need to be loaded<br />
                                to work (it sets a ‘deferred’ breakpoint).</p>
<p>                                &nbsp;</p>
<p>                                However to use ANY SOS command, you need to load SOS, and it turns out that SOS<br />
                                needs the .NET runtime dlls ‘mscorwks.dll’ to be loaded before it can load. &nbsp;<br />
                                There are a variety of techniques you can use. &nbsp; &nbsp;The one I use is</p>
<p>                                &nbsp; &nbsp;bu mscorwks!EEStartup</p>
<p>                                This sets a breakpoint at the ‘EEStartup’ method in the .NET runtime DLL ‘mscorwks.<br />
                                &nbsp; When this breakpoint hits you can do the command</p>
<p>                                &nbsp; &nbsp;.loadby sos mscorwks</p>
<p>                                Which tells windbg to load the sos.dll by searching the in the directory where mscorwks<br />
                                lives. &nbsp;Once loaded you can execute a ! bpmd &nbsp;command.</p>
<p>                                Finally if you need !name2ee to work and the module is not yet loaded, you should<br />
                                set a breakpoint (using !bpmd &nbsp;command), in the module of interest, run to<br />
                                that breakpoint (now it is loaded), and then do the !name2ee command. &nbsp;</p>
<p>                                September 6, 2006 12:46 PM</p>
<p>            &nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2007/02/some-good-net-debugging-info/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

