如何验证参考组件以检查其是否被篡改?

发布于 2024-09-13 11:55:42 字数 342 浏览 5 评论 0原文

这个问题是关于验证程序集以检查它是否被恶意活动篡改。 创建程序集时,会生成元数据。元数据包括类型定义表、类型引用表和清单表等表。引用表包含每个程序集引用的条目,并且该条目包括引用的程序集、其公钥和散列值。清单包含每个程序集引用的程序集的详细信息,其中包括程序集名称、其公钥和哈希算法。我还了解到,在运行时加载程序集时,它会使用嵌入在清单中的公钥生成程序集的数字签名,并将其与已嵌入程序集中的数字签名进行比较。如果数字签名匹配,则加载。 我的问题如下。

  1. 程序集引用元数据表包括哈希。还提到它没有被使用。那么它的目的是什么呢?
  2. 每次加载程序集时都会进行程序集验证吗?
  3. 如果不是强类型会发生什么?

This question is about verifying the assembly to check whether it is tampered for malicious activity.
When an assembly is created, metadata is generated. Metadata includes tables like type definition tables, type reference tables and manifest tables. Reference tables contain an entry for each assembly reference and the entry includes referenced assembly, its public key and a hash value. The manifest includes details of assembly referenced for each assembly and it includes the assembly name, its public key and Hashing algorithm. I also understand that during runtime when the assembly is loaded, it generates digital signature of the assembly with the public key embedded in the manifest and compares it with the digital signature already embedded in the assembly. If the digital signature matches then it loads.
My questions are below.

  1. The Assembly Reference metadata table include a HASH. It is also mentioned that it is not used. Then what is its purpose?
  2. Does this assembly verification happen every time the assembly loads?
  3. What happens if it is not strongly typed?

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

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

发布评论

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

评论(2

弄潮 2024-09-20 11:55:42
  1. 好问题。

实际上,

“公共语言运行时还执行哈希验证;
程序集清单包含组成该程序的所有文件的列表
程序集,包括每个文件的哈希,因为它存在时
清单已建立。加载每个文件时,其内容都会被散列
并与清单中存储的哈希值进行比较。如果两个
哈希值不匹配,程序集无法加载。”
http://msdn.microsoft.com/en-us/library/ab4eace3.aspx


但是...

“(AssemblyRef 元数据表的)每个条目还包含一些标志
和一个哈希值。该哈希值旨在作为以下内容的校验和
引用的程序集的位。 CLR 完全忽略此哈希
有价值,并且将来可能会继续这样做。”
Jeffrey Richter“通过 C# 进行 CLR”第 3 版。

根据我对 .Net 4.0 程序集的私人调查 - 哈希和在程序集绑定阶段确实被忽略,即使程序集是由加密密钥签名的,因此具有强名称。

一段时间后,我意识到 强名称绕过功能会导致此行为。
因此,您需要“在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework 项下创建一个值为 0、名为 AllowStrongNameBypass”的 DWORD 条目”,以启用强名称 (+hashsum) 验证。

  1. Good question.

Actually,

"The common language runtime also performs a hash verification; the
assembly manifest contains
a list of all files that make up the
assembly, including a hash of each file as it existed when the
manifest was built. As each file is loaded, its contents are hashed
and compared with the hash value stored in the manifest. If the two
hashes do not match, the assembly fails to load."
http://msdn.microsoft.com/en-us/library/ab4eace3.aspx

but...

"Each entry (of AssemblyRef Metadata table) also contains some flags
and a hash value . This hash value was intended to be a checksum of
the referenced assembly’s bits. The CLR completely ignores this hash
value and will probably continue to do so in the future."
Jeffrey Richter "CLR via C#" 3rd Edition.

And per my private investigations on .Net 4.0 assemblies - hash sums are really ignored on Assembly Binding stage, even if assembly was signed by crypto-key and so has a strong name.

After a while I've realized that strong-name bypass feature causes this behavior.
So you need to "create a DWORD entry with a value of 0 named AllowStrongNameBypass under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework key" in order to enable strong name (+hashsum) validation.

小红帽 2024-09-20 11:55:42

1:没有,已经用过了。 Ecma-335,分区 II,第 6.2.3 章有关 .file 指令:

.hash 后面的字节指定为文件计算的哈希值。 VES 应在访问此文件之前重新计算此哈希值,如果不匹配,则应生成异常。用于计算此哈希值的算法由 .hash 算法指定(请参阅第 6.2.1.1 节)。

2:仅当启用强名称验证时。请注意,自 .NET 3.5 SP1 起,此功能在完全信任场景中默认处于关闭状态。您必须使用 caspol.exe

3 显式启用它:假设“强命名”,则无法进行验证。

1: No, it's used. Ecma-335, partition II, chapter 6.2.3 on the .file directive:

The bytes after the .hash specify a hash value computed for the file. The VES shall recompute this hash value prior to accessing this file and shall generate an exception if it does not match. The algorithm used to calculate this hash value is specified with .hash algorithm (see clause 6.2.1.1).

2: Only if strong name validation is enabled. Note that this is off by default since .NET 3.5 SP1 in full trust scenarios. You'd have to explicitly enable it with caspol.exe

3: assuming "strongly named", then no validation is possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文