使用 Obfuscar 重命名内部 class

rename an internal class with Obfuscar

我需要 Obfuscar 来混淆(重命名)内部 classes,例如下面的 Secret1。这是一个带有辅助 publicinternal classes 的最小 WPF 应用程序,用于执行 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));
        }
    }
}

我的配置文件是:

<?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>

设置 KeepPublicApiHidePrivateApi 的所有可能组合都会导致跳过 Secret1 class。 "Skipped" 可能是 "not obfuscated" 的混淆术语。来自我的笔记:

<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

Obfuscar 映射输出的两个示例如下。

摘自 Mapping.txt(保持 public = false,隐藏私有 = 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
}

摘自 Mapping.txt(保持 public = false,隐藏私有 = 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
}

可接受的解决方案不应要求更改源代码,因为现实世界的应用程序比这个最小的 WPF 示例要大。

希望您仔细查看示例项目,

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

<Var> 标签必须放在 <Obfuscator> 标签下。否则,Obfuscar 将无法识别它们。

不得不承认设置系统(原开发者)设计的很糟糕,但我还没来得及更换。