H2ONaCl
H2ONaCl

Reputation: 11269

rename an internal class with Obfuscar

I need Obfuscar to obfuscate (rename) internal classes such as Secret1 in the following. This is a minimal WPF application with ancillary public and internal classes for exercising Obfuscar.

namespace WpfApp
{
    public enum Category { Low, High }

    public partial class MainWindow : Window
    {
        private ViewModel ViewModel;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this.ViewModel = new ViewModel();
        }

        private void MyButtonClick(object sender, RoutedEventArgs e)
        {
            this.ViewModel.Process(MyTextBox.Text);
            var s1 = new Secret1();
            s1.SecretMethod1();
            var s2 = new Secret2();
            s2.SecretMethod();
        }
    }

    internal class Secret1
    {
        internal int SecretInt;
        private float SecretFloat;

        internal int SecretMethod1()
        {
            if (SecretMethod2() == false)
                return 0;
            else
                return 1;
        }

        private bool SecretMethod2()
        {
            return false;
        }
    }

    public class Secret2
    {
        private int SecretInt;

        public int SecretMethod()
        {
            return this.SecretInt;
        }
    }

    [Serializable] internal class ViewModel : WpfNotifier
    {
        private const float DefaultKilograms = 80.0f;

        private string _kilograms;
        public string Kilograms // WPF binds here
        {
            get { return this._kilograms; }
            set { this._kilograms = value; NotifyPropertyChanged(); }
        }
        private string _resultText;
        public string ResultText // WPF binds here
        {
            get { return this._resultText; }
            set { this._resultText = value; NotifyPropertyChanged(); }
        }

        internal void Process(string input)
        {
            float kilograms;
            if (Single.TryParse(input, out kilograms))
            {
                Category c = (kilograms > 100.0f) ? Category.High : Category.Low;
                this.ResultText = c.ToString();
            }
            else
            {
                this.Kilograms = ViewModel.DefaultKilograms.ToString();
            }
        }
    }

    [Serializable] public class WpfNotifier : INotifyPropertyChanged
    {
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged; // public for interface

        internal void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

My config file is:

<?xml version="1.0"?>
<configuration>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
  </startup>

  <Obfuscator>
    <Var name="InPath"  value="\users\user\documents\visual studio 2013\projects\wpfapp\wpfapp\bin\debug" />
    <Var name="OutPath" value="\users\user\documents\visual studio 2013\projects\wpfapp\wpfapp\bin\debug" />

    <Module file="$(InPath)\wpfapp.exe">
      <Var name="KeepPublicApi" value="false" />
      <Var name="HidePrivateApi" value="true" />
      <SkipType name="WpfApp.ViewModel" skipFields="true" skipProperties="true" />
      <SkipType name="WpfApp.WpfNotifier" skipFields="true" skipProperties="true" />
      <SkipType name="WpfApp.Category" skipFields="true" skipProperties="true" />
    </Module>

  </Obfuscator>

</configuration>

All possible combinations for setting KeepPublicApi and HidePrivateApi result in the Secret1 class being skipped. "Skipped" probably is Obfuscar terminology for "not obfuscated". From my notes:

<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="false" />
Secret1 class was skipped

<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="true" />
Secret1 class was skipped

<Var name="KeepPublicApi" value="false" />
<Var name="HidePrivateApi" value="false" />
Secret1 class was skipped

<Var name="KeepPublicApi" value="false" />
<Var name="HidePrivateApi" value="true" />
Secret1 class was skipped

Two examples of Obfuscar's mapping output follow.

An excerpt from Mapping.txt (keep public = false, hide private = true):

[WpfApp]WpfApp.Secret1 skipped:  HidePrivateApi option in configuration
{

    [WpfApp]WpfApp.Secret1::SecretMethod1[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::SecretMethod2[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::.ctor[0]( ) skipped:  special name


    System.Int32 [WpfApp]System.Int32 WpfApp.Secret1::SecretInt skipped:  HidePrivateApi option in configuration
    System.Single [WpfApp]System.Single WpfApp.Secret1::SecretFloat skipped:  HidePrivateApi option in configuration
}

An excerpt from Mapping.txt (keep public = false, hide private = false)

[WpfApp]WpfApp.Secret1 skipped:  HidePrivateApi option in configuration
{

    [WpfApp]WpfApp.Secret1::SecretMethod1[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::SecretMethod2[0]( ) skipped:  HidePrivateApi option in configuration
    [WpfApp]WpfApp.Secret1::.ctor[0]( ) skipped:  special name


    System.Int32 [WpfApp]System.Int32 WpfApp.Secret1::SecretInt skipped:  HidePrivateApi option in configuration
    System.Single [WpfApp]System.Single WpfApp.Secret1::SecretFloat skipped:  HidePrivateApi option in configuration
}

An acceptable solution should not call for source code changes because the real world application is larger than this minimal WPF example.

Upvotes: 1

Views: 1400

Answers (1)

Lex Li
Lex Li

Reputation: 63183

I hope you can check out the sample project carefully,

https://github.com/lextm/obfuscar/blob/master/Examples/BasicExample/obfuscar.xml

<Var> tags must be placed under <Obfuscator> tag. Otherwise, Obfuscar won't recognize them.

I must admit the setting system was badly designed (by the original developer), but I have no time to replace it yet.

Upvotes: 1

Related Questions