4

Obfuscar を使用する際に、元の列挙型の値の名前が必要なため、列挙型が難読化されるのを防ぎたいと考えています。ToString()列挙値はユーザーにとって便利なので、列挙値で呼び出します。要素を持つ構成ファイルに表示されるものを除いて、すべての型が難読化されている通常の構成では苦労しました<SkipType name="namespace.EnumType"/>。私は<MarkedOnly />、アノテーションでマークされたものだけを難読化する、より悲観的な方法に頼っています。かなり最小限の構成ファイルが続きます。

    <?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="true" />
        <Var name="HidePrivateApi" value="true" />

        <Var name="MarkedOnly" value="true" />

      </Obfuscator>

    </configuration>

注釈付きのコードは次のとおりです。

namespace WpfApp
{
    public enum Category { Low, High }

    [System.Reflection.Obfuscation]
    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);
        }
    }

    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();
            }
        }
    }

    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));
            else
                ; // it is harmless to fail to notify before the window has been loaded and rendered
        }
    }
}

ご覧のとおり、注釈が付けられている型は 1 つだけですが[System.Reflection.Obfuscation]、出力マップは列挙型の名前が変更されたことを示しています。列挙型は と呼ばれCategoryます。

Renamed Types:

[WpfApp]WpfApp.Category -> [WpfApp]A.a
{
    WpfApp.Category [WpfApp]WpfApp.Category WpfApp.Category::Low -> A
    WpfApp.Category [WpfApp]WpfApp.Category WpfApp.Category::High -> a

    System.Int32 [WpfApp]System.Int32 WpfApp.Category::value__ skipped:  special name
}

私の使い方が間違っているのでしょうか、それともバグですか?

4

1 に答える 1

0

フィールドなどを難読化する際に、「MarkedOnly」オプションのチェックに失敗するというバグが確認されました。マスターブランチで修正しました。

https://github.com/lextm/obfuscar

この変更後、「MarkedOnly」オプションは他のオプションに排他的であることに注意してください。要素 (クラス/列挙型/メソッド/フィールドなど) にObfuscation属性が関連付けられていない場合、同じままになります。「KeepPublicApi」や「HidePrivateApi」などの設定は無視されます。

于 2015-01-17T05:14:11.347 に答える