我使用eazfuscator,它使方法虚拟化似乎可以很好地隐藏方法。
然后在方法中,我喜欢保护我将在发布模式下更改代码以提供空指针异常来启动一个任务,该任务将生成一个空指针并在没有调用堆栈的代码的情况下执行它。
没有多少开发人员可以在不调试的情况下使用您的生产代码...
#if !DEBUG
#pragma warning disable CS8618
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerNonUserCode]
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
#endif
namespace MyApp.Infrastructure.LicenseManager
{
[Obfuscation(Feature = "apply to member * when method or constructor: virtualization", Exclude = false)]
internal sealed class LicenseManager(ISomeClass parameter, ILoggerFactory? factory = null)
{
//hide the name space for reverse engineering the naming scheme
_logger = factory?.CreateLogger("MyApp.LicenseManager")
#if !DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
//send computer information for license violation and prosecution then crash
System.Threading.ThreadPool.UnsafeQueueUserWorkItem(SystemCrasher.SendAbuseAndCrash);
//send a log entry, of don't if you wish to give no warning
_logger?.LogWarning(Walter.BOM.LogEvents.FireWall_Warning, "Debugging will cause the developer to generate null pointer exceptions in administration context");
return;
//all class initiolizers will stay null
}
#endif
_myClass= parameter;
}
#if !DEBUG
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
#endif
如果您的代码被共享 DLL 中的其他代码使用(如 NuGet 包中),那么上述方法将阻止您受到同事的欢迎,您需要一个“调试”友好的方法,使用方法代理可能会与混淆相结合的诀窍。
[Obfuscation(Feature = "virtualization", Exclude = false)]
private void MyMethod(int parameter)
{
MyMethodProxy();
//do some code
}
private void MyMethodProxy([CallerMemberName] string? calledBy = null)
{
if (!string.Equals(nameof(MyMethod), calledBy, StringComparison.Ordinal))
{
throw new LordOfTheRingsException("You shall not pass!!");
}
}
通过使用 NameOf 方法,您可以避免混淆器重命名方案中的不可打印字符。
我使用了一些其他选项,但这会限制您自动化和使用面向方面的框架,例如 postsharp
希望对你有帮助enter code here