Obfuscating enum names when enum is .ToString()'d

CodeGuruCodeGuru Posts: 13
edited May 20, 2011 12:36PM in SmartAssembly
The bug I described here:
http://www.red-gate.com/MessageBoard/vi ... hp?t=12231

STILL exists in the latest version of 6.

Its pretty frustrating have to put my exe's through IDA just to make sure SmartAssembly is obfuscating all of my names.

Just to make it even more clear whats going on here.
public class Test
{
	enum BitStateFlags
	{
		state1 = 1,
		state2 = 2,
	}

	void SomeFunction()
	{
		BitStateFlags flags = BitStateFlags.state1;

		MessageBox.Show(flags.ToString("X"));
	}
}

It's pretty obvious what you're trying to do. You detect if an enum is being printed out as a string. If it is, you're omitting it from obfuscation.

In this example though only the numerical value is being output though, not the actual enum name.

Also, you should have a policy of ALWAYS obfuscate unless the user EXPLICITLY chooses otherwise.

We should have confidence that SmartAssembly is obfuscating everything but your method to omit obfuscation on variables that you think we might want unobfuscated leaves us with zero confidence and full of doubt.

Comments

  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    I'm sorry it looks like we had been totally unresponsive when dealing with this issue. I think the problem is that, behind the scenes, .NET uses Reflection voodoo to knock up a string representation of an enum, and SmartAssembly will usually blow the assembly metadata to bits as part of the obfuscation and I can see how this would be one side-effect.

    If you want the string representation of this enum to be ("X"), maybe telling the enum to derive from the type long and using ((long)MyEnum).ToString("X")
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    I'd also like to add I could not reproduce the problem with the given code using obfuscation and pruning.
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Test t = new Test();
                t.GetCandy();
                Console.ReadLine();
            }
        }
        public class Test
        {
            enum Candy
            {
                JollyRancher = 0x01,
                ReesesPieces = 0x02
            }
            public void GetCandy()
            {
                Candy candy=Candy.JollyRancher;
                Console.WriteLine(candy.ToString());
            }
        }
    }
    
    However, from my miniscule understanding of the SmartAssembly code, I think you should be safe if you use the FlagsAttribute on the enum because there seems to be an exclusion in the SA obfuscation rules for that.
            [Flags]
            enum Candy
            {
                JollyRancher = 0x01,
                ReesesPieces = 0x02
            }
    
    Hopefully this works for you.
  • If you want the string representation of this enum to be ("X"), maybe telling the enum to derive from the type long and using ((long)MyEnum).ToString("X")

    That's exactly what I do to get SmartAssembly obfuscating the names properly.

    I have not tried the [Flags] attribute as an alternative fix.

    Thanks for the reply.
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    Did applying the "Flags" attribute fix the problem?
Sign In or Register to comment.