Red Gate forums :: View topic - Obfuscating enum names when enum is .ToString()'d
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SmartAssembly 6
SmartAssembly 6 forum

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

Search in SmartAssembly 6 forum
Post new topic   Reply to topic
Jump to:  
Author Message
CodeGuru



Joined: 22 Nov 2010
Posts: 7

PostPosted: Tue May 17, 2011 5:23 am    Post subject: Obfuscating enum names when enum is .ToString()'d Reply with quote

The bug I described here:
http://www.red-gate.com/MessageBoard/viewtopic.php?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.

Code:
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.
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6645

PostPosted: Wed May 18, 2011 9:36 am    Post subject: Reply with quote

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")
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6645

PostPosted: Wed May 18, 2011 1:28 pm    Post subject: Reply with quote

I'd also like to add I could not reproduce the problem with the given code using obfuscation and pruning.
Code:
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.
Code:

        [Flags]
        enum Candy
        {
            JollyRancher = 0x01,
            ReesesPieces = 0x02
        }
Hopefully this works for you.
Back to top
View user's profile Send private message
CodeGuru



Joined: 22 Nov 2010
Posts: 7

PostPosted: Wed May 18, 2011 4:30 pm    Post subject: Re: Reply with quote

Brian Donahue wrote:
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.
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6645

PostPosted: Fri May 20, 2011 5:36 pm    Post subject: Reply with quote

Did applying the "Flags" attribute fix the problem?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group