简体   繁体   English

在.NET中使用强名称程序集确保代码没有被篡改?

[英]Do strong-named assemblies in .NET ensure the code wasn't tampered with?

I'm trying to understand the point of strong-naming assemblies in .NET. 我试图理解.NET中强命名程序集的意义。 While googling about it I noticed that everywhere it is said that it ensures that the code comes from me and wasn't tampered with. 在谷歌上搜索它时,我注意到它无处不在,它确保代码来自我并且没有被篡改。 So I tested it. 所以我测试了它。 I created a simple DLL, signed it with a newly created PFX key and referenced it by my WPF application. 我创建了一个简单的DLL,用新创建的PFX密钥对其进行签名,并由我的WPF应用程序引用它。 And ok, everything works. 好吧,一切正常。 When I compile the DLL with another PFX file I get an error, so it's ok. 当我用另一个PFX文件编译DLL时,我收到一个错误,所以没关系。

BUT when I decompile the DLL by ildasm, modify it and recompile it by ilasm the WPF application still works without any error. 但是,当我通过ildasm反编译DLL时,修改它并通过ilasm重新编译它,WPF应用程序仍然可以正常工作而没有任何错误。 So I tampered the strongly-named DLL and changed it manually with the old one and the application still works with the tampered DLL. 所以我篡改了强名称的DLL并使用旧的DLL手动更改它,应用程序仍然使用篡改的DLL。 The PublicKeyToken is the same. PublicKeyToken是一样的。 So what is the point of strong-naming? 那么强命名的意义何在? It doesn't ensure the code hasn't been tampered with since I strong-named it. 它不能确保代码没有被篡改,因为我强烈命名它。

It used to check for tampering, but the overhead of checking every strong-name-signed assembly at application startup was too high, so Microsoft disabled this behaviour by default a number of years ago (way back when ".NET Framework version 3.5 Service Pack 1" was released). 它用于检查篡改,但是在应用程序启动时检查每个强名称签名的程序集的开销太高,因此Microsoft在几年前默认禁用了此行为 (回到“.NET Framework 3.5版Service Pack”时) 1“被释放了”。

This is called the Strong-Name bypass feature . 这称为强名称旁路功能

You can disable the feature (ie make Windows check for tampering) for a particular application by adding the following to its ".config" file: 您可以通过在其“.config”文件中添加以下内容来禁用特定应用程序的功能(即,使Windows检查篡改):

<configuration>  
  <runtime>  
    <bypassTrustedAppStrongNames enabled="false" />  
  </runtime>  
</configuration>  

You can enable strong-name checking for ALL applications by editing the registry (which is clearly not a feasible solution!). 您可以通过编辑注册表为所有应用程序启用强名称检查(这显然不是一个可行的解决方案!)。

For more details, see the following page: 有关更多详细信息,请参阅以下页面:

https://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-disable-the-strong-name-bypass-feature https://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-disable-the-strong-name-bypass-feature

The advice nowadays is to use a full code-signing certificate for your executable and DLLs if you want to prevent code tampering. 现在的建议是,如果要防止代码被篡改,请为可执行文件和DLL使用完整的代码签名证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM