Search code examples
c#accessorrenamingdotfuscator

Dotfuscator accessors renaming get and set


My issue is with Dotfuscator configuration for renaming. Imagine a class that would look like this:

Class MyClass
{
    private int _propertyA;
    private int _propertyB;

    public int PropertyA
    {
        get{return _propertyA;}
        set{_propertyA = value;}
    }

    [Obfuscation(Feature = "renaming", Exclude = true)]
    public int DestinationReference
    {
        get{return _propertyB;}
    }
}

The obfuscated class will be written into someting like this

Class a
{

    int s()
    void z(int a)

    public int DestinationReference
    {
        get{return _propertyB;}
    }
}

This is my assumption from what I can see using .Net Reflector

My issue is the following: - In our code we implemented a method that look for all attributes of a class using reflection in order to find specific parameters - This method does not work in the obfuscated code as my accessor PropertyA, has been replaced with two distinct methods for the get accessor and set accessor. - I know that if I exclude an accessor from renaming it stays an accessor in the msil code and will be found by my method that looks for accessors

My question is: - Is not renaming the only option? - Is there a parameter in Dotfuscator that would allow renaming of the accessor without splitting it into two distinct methods and loosing the accessor?

I'm pretty new to obfuscation so pardon my imperfections, this is what I can see for a class similar to the one described above in reflector.

Screenshot

As you can see the property that is excluded from renaming stays a property with a get accessor. But for the other one that got obfuscated I can see two distinct methods s and z

I'm trying to see if there would be a way of obtaining a single accessor, renamed "s" for example with the underlying getter and setter


Solution

  • I found some answers to my question, first after looking at this article : http://vicky4147.wordpress.com/2007/10/23/exploring-msil-properties/

    I see that MSIL generates get_XXX() method and set_XXX(int) methods as well as adding a property. Dotfuscator is responsible for renaming the get and set methods (which is what we want) but also for removing the property itself (which I do not want)

    A solution is to enable "Library mode" for the obfuscated DLL, if library mode is enabled, the documentation states that:

    • Names of public classes and nested public classes are not renamed. Members (fields and methods) of these classes are also not renamed if they have public, family, or famorassem access.
    • In addition, no virtual methods are renamed, regardless of access specifier. This allows clients of your library to override private virtual methods if need be (this is allowed behavior in the .NET architecture).
    • Any user-specified custom renaming exclusions are applied in addition to the exclusions implied by the above rules.
    • Property and Event metadata are always preserved.

    And this can be seen after obfuscation in reflector, at the top library mode is disabled, at the bottom library mode is enabled

    Screenshot

    As it can be seen, none of the public classes/methods/fields have been renamed, and more important to me the Property metadata has been preserved.

    Now my next question would be, how to preserve the property metadata but allow the renaming of the property itself. I would like to find a solution that is satisfying without having to define manually decorate each properties with custom obfuscation attributes.

    I'll keep searching for another day and if I can't find anything will mark this answer as solution to the issue.