簡體   English   中英

如何在ConfuserEx混淆的我自己的類中使用GetMethod?

[英]How to use GetMethod in my own class obfuscated by ConfuserEx?

我有自己的DLL,可以使用ConfuserEx進行保護。 在ConfuserEx中,我使用“重命名”保護:

<protection id="rename">
    <argument name="mode" value="unicode" />
    <argument name="renEnum" value="true" />        
</protection>    

當然,這可以確保DLL不會查看代碼,但是我的類(已作為DLL的一部分進行了保護)使用:

MethodInfo mi = typeof(MyClass).GetMethod(nameof(MyStaticMethod), BindingFlags.Static | BindingFlags.NonPublic);

問題在這里開始,因為即使我自己的代碼也找不到和使用我的 (受ConfuserEx保護) 方法 我使用GetMethod調用:Delegate.CreateDelegate。 我該怎么做才能解決這個問題?

我仍然不確定為什么不能直接創建所需的委托而不進行反射,但是如果您確實需要獲取MethodInfo ,請嘗試執行以下操作:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        Thingy t = DoStuff;
        var mi = t.Method;
    }
    private delegate void Thingy(object sender, EventArgs e);
    private static void DoStuff(object sender, EventArgs e)
    {

    }
}

也就是說,使用您自己的與其他委托定義匹配的本地定義委托,直接在您的代碼中創建它的一個實例,然后從該實例中提取MethodInfo

這段代碼將使用方法令牌來標識DoStuff而不是其名稱,因此應能避免混淆而不會出現問題。

我通過在GetMethod和目標方法之間應用附加的“橋委托”來解決此問題。 然后,我使用BridgeDelegate.Method.Name代替nameof(MyStaticMethod)。 我檢查並正常工作。

解決方案示例:

internal static class MyClass
{
    private delegate void ExecuteObfuscatedMethod(string value);
    private static ExecuteObfuscatedMethod Bridge; //This is my "bridge"

    internal static void CaptureExternalDelegate(object source)
    {
        //Using a "bridge" instead of the direct method name
        MethodInfo mi = typeof(MyClass).GetMethod(Bridge.Method.Name, BindingFlags.Static | BindingFlags.NonPublic);

        //Less interesting code
        PropertyInfo p = source.GetType().GetProperty("SomePrivateDelegate", BindingFlags.NonPublic | BindingFlags.Instance);
        Delegate del = Delegate.CreateDelegate(p.PropertyType, mi) as Delegate;
        Delegate original = p.GetValue(source) as Delegate;
        Delegate combined = Delegate.Combine(original, del);
        p.SetValue(property, combined);
    }

    static MyClass()
    {
        Bridge += MyStaticMethod;
    }

    //This is the method whose name can not be retrieved by nameof () after applying ConfuserEx
    private static void MyStaticMethod(string value)
    {
        //I am testing the method's name after calling it.
        var st = new StackTrace();
        var sf = st.GetFrame(0);
        var currentMethodName = sf.GetMethod();
        throw new Exception("The method name is: " + currentMethodName); //You can see that the method has evoked and you can even see its new name
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM