有没有办法对您的代码在 C# 中是否被混淆进行条件检查?

发布于 2024-10-26 00:22:14 字数 286 浏览 4 评论 0原文

有没有办法检查您的代码库是否以编程方式混淆?

在我的项目中,我使用反射从外部 DLL(我控制的)加载类。用于加载信息的数据(程序集和类名)存储在资源文件中。

一旦我混淆了项目,我就无法将外部 DLL 链接到输出 EXE 中(即,将所有程序集汇总到单个输出可执行文件中),因为资源文件中的程序集名称不再正确(DLL 已成为EXE)。但出于调试目的,我还必须保留未混淆的功能。

我想要做的是检查代码以查看它是否在运行时被混淆。如果没有,则使用资源文件中的程序集名称;如果有,请使用应用程序的名称。这样的事情可能吗?

Is there a way to check whether your code base is obfuscated programmatically?

In my project, I'm using reflection to load classes from an outside DLL (which I control). The data for loading the information (assembly and class name) are stored in a resource file.

Once I obfuscate the project, I am unable to link the outside DLL into the output EXE (i.e. roll up all assemblies into a single output executable), because the assembly name in the resource file is no longer correct (the DLL has become part of the EXE). But I have to preserve the un-obfuscated functionality as well for debugging purposes.

What I'd like to do, is have the code check to see if it has been obfuscated at runtime. If it hasn't, use the assembly name in the resource file; if it has, use the name of the application. Is something like this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

大多数混淆器(例如 Dotfuscate)都会在程序集上放置程序集级自定义属性。如果您的混淆器属于这种情况,请检查该属性是否存在,例如:

bool foundObfuscatationAttribute = false;
foreach (object attribute in attributes)
{
    if (attribute.GetType().FullName == "DotfuscatorAttribute")
    {
        foundObfuscatationAttribute = true;
        break;
    }
}
//If found...

由于我们不想引用包含该属性本身的程序集,因此我们按名称进行检查。

Most Obfuscators, such as Dotfuscate, will put an assembly level Custom Attribute on the assembly. If that is the case of your obfuscator, check to see if the attribute is present, such as:

bool foundObfuscatationAttribute = false;
foreach (object attribute in attributes)
{
    if (attribute.GetType().FullName == "DotfuscatorAttribute")
    {
        foundObfuscatationAttribute = true;
        break;
    }
}
//If found...

Since we don't want to reference the assembly that contains the attribute itself, we check by name.

屋顶上的小猫咪 2024-11-02 00:22:14

询问您知道将被混淆的类的类型信息,并检查名称是否仍然是未混淆的。在代码中 typeof(MyClass).Name == "MyClass" (可能需要包含命名空间)。

Ask for the type info of a class you know will be obfuscated and check if the name is still the unobfuscated one. In code typeof(MyClass).Name == "MyClass" (may need include the namespace).

风筝有风,海豚有海 2024-11-02 00:22:14

设置一个编译标志或某种可以使用反射来检查它是否是的标志

Set a compile flag or some kind of flag you can reach using Reflection to check if it is

给我一枪 2024-11-02 00:22:14

检查程序集(dll)是否被 Dotfuscator 混淆的另一种方法,无需文件夹中的依赖项:

public static bool IsDllOfuscated(string dirFile)
{
    return Assembly.Load(File.ReadAllBytes(dirFile)).GetType("DotfuscatorAttribute") != null;        
}

Another way to check if the assembly(dll) is obfuscated from Dotfuscator, without need to the dependencies in the folder is:

public static bool IsDllOfuscated(string dirFile)
{
    return Assembly.Load(File.ReadAllBytes(dirFile)).GetType("DotfuscatorAttribute") != null;        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文