Dynamic Keyword and Dotfuscator = Very Unhappy

October 3 2011

I’ve been using Dotfuscator to obfuscate an assembly.  I thought all was good, but when I tried to use the obfuscate assembly, the assembly didn’t work like the unobfuscated one did and was throwing exceptions all over the place. After much debugging, I finally discovered that the use of the dynamic keyword was causing bigtime problems with Dotfucator.

Understanding what is going on with a simple sample is useful.  Say I have the following method to be obfuscated:

static void MyMethod() { int num = 1; int i = DoSomething(num); }

When obfuscated and then inspected in Reflector, here’s what it looks like:

private static void a() { int num = 1; int num2 = a(num); }

That’s what you’d expect.  But, now let’s say you change that method to use dynamic types instead of int. So it looks like this:

static void MyMethod() { dynamic num = 1; int i = DoSomething(num); }

When obfuscated and then disassembled in Reflector, here’s what you get:

private static void a() { object obj2 = 1; if (a.a == null) { a.a = CallSite<Func<CallSite, object, int>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(int), typeof(a))); } if (a.b == null) { a.b = CallSite<Func<CallSite, Type, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "DoSomething", null, typeof(a), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) })); } int num = a.a.Target(a.a, a.b.Target(a.b, typeof(a), obj2)); }

Eek! Not only is it ugly as can be, look at how the method name DoSomething is passed as a  string to the InvokeMember method. But, DoSomething has been ofbuscated as a, so the assembly no longer has any notion of the DoSomething name for that method.

I’m not the only one who’s hit this as you can see from this post in the Dotfuscator forums.  Unfortunately, I couldn’t find a workaround other than removing the use of the dynamic keyword.

Comments (3) -

10/4/2011 9:52:40 AM #

Rick

I wouldn't use DotFuscator due to there is several de-dotfuscator tools that (more or less)completely remove the obfuscation. I would look into other products.

Rick

10/4/2011 12:13:39 PM #

Karsten Januszewski

Tell me more. I poked around on the web and found one shady project that seemed to do this, although I was scared to run it on my machine!  

Karsten Januszewski

10/4/2011 1:57:48 PM #

Steve Hall

You should give Eazfuscator a try.  You can find it at:  www.foss.kharkov.ua/.../Default.aspx

I was a prior user of Dotfuscator, which has UI much more complex compared to Eazfuscator.  Plus I think Eaz has more features.  (I haven't done an exhaustive comparison...)  Certainly it's MUCH easier to use.  Although, I don't know if it supports dynamic vars.

And on the Reflector war-front:  I've switched to using ILSpy after the free edition of Reflector turned into $$$-ware.  It's a pretty good alternative.  See community.sharpdevelop.net/.../...-has-landed.aspx for that...

Steve Hall

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading