首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我自己的类中使用被GetMethod混淆的ConfuserEx?

如何在我自己的类中使用被GetMethod混淆的ConfuserEx?
EN

Stack Overflow用户
提问于 2018-12-11 09:54:32
回答 2查看 549关注 0票数 0

我有我自己的DLL,我用ConfuserEx来保护它。在ConfuserEx中,我使用“重命名”保护:

代码语言:javascript
复制
<protection id="rename">
    <argument name="mode" value="unicode" />
    <argument name="renEnum" value="true" />        
</protection>    

当然,这可以保护DLL不查看代码,但是我的类(我作为DLL的一部分进行了保护)使用:

代码语言:javascript
复制
MethodInfo mi = typeof(MyClass).GetMethod(nameof(MyStaticMethod), BindingFlags.Static | BindingFlags.NonPublic);

问题在这里开始了,因为甚至连我自己的代码都找不到并使用我的(受ConfuserEx保护)方法。我使用GetMethod调用: Delegate.CreateDelegate。我能做些什么来解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-11 10:49:07

我仍然不知道为什么不能直接创建不需要反射的委托,但是如果确实需要获得MethodInfo,请尝试这样做:

代码语言:javascript
复制
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,而不是它的名称,这样就可以在没有问题的情况下避免混淆。

票数 2
EN

Stack Overflow用户

发布于 2018-12-11 10:54:42

我通过在GetMethod和目标方法之间添加一个额外的“桥委托”来解决这个问题。然后,我使用的不是名称(MyStaticMethod),而是BridgeDelegate.Method.Name。我检查并正确地工作了。

示例解决方案:

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53721503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档