用Obfuscar重命名内部类

时间:2015-01-19 19:09:21

标签: obfuscar

我需要Obfuscar来混淆(重命名)内部类,例如下面的Secret1。这是一个最小的WPF应用程序,具有用于行使Obfuscar的辅助publicinternal类。

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类。 “Skipped”可能是Obfuscar“未混淆”的术语。从我的笔记:

<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的摘录(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
}

来自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
}

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

1 个答案:

答案 0 :(得分:1)

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

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

<Var>代码必须放在<Obfuscator>代码下。否则,Obfuscar不会认出它们。

我必须承认设置系统设计糟糕(由原始开发人员设计),但我还没有时间更换它。

相关问题