如何防止未经授权的代码访问 .NET 2.0 中的程序集?

发布于 2024-07-11 13:20:12 字数 436 浏览 6 评论 0原文

在 .NET 1.x 中,您可以使用 StrongNameIdentityPermissionAttribute 在您的程序集上,以确保只有您签名的代码才能访问您的程序集。 根据 MSDN 文档,

在 .NET Framework 2.0 及更高版本中,对身份的要求 如果调用程序集完全信任,则权限无效。

这意味着任何完全信任的应用程序都可以绕过我的安全要求。

如何防止未经授权的代码访问 .NET 2.0 中的程序集?

In .NET 1.x, you could use the StrongNameIdentityPermissionAttribute on your assembly to ensure that only code signed by you could access your assembly. According to the MSDN documentation,

In the .NET Framework version 2.0 and later, demands for identity
permissions are ineffective if the calling assembly has full trust.

This means that any application with full trust can just bypass my security demands.

How can I prevent unauthorized code from accessing my assembly in .NET 2.0?

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

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

发布评论

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

评论(3

在梵高的星空下 2024-07-18 13:20:12

按照Eric的建议,我自己检查了密钥解决了这个问题。 在我想要保护的代码中,我添加了以下调用,

EnsureAssemblyIsSignedByMyCompany( Assembly.GetCallingAssembly() );

然后该方法的实现是

  /// <summary>
  /// Ensures that the given assembly is signed by My Company or Microsoft.
  /// </summary>
  /// <param name="assembly"></param>
  private static void EnsureAssemblyIsSignedByMyCompany( Assembly assembly )
  {
     if ( assembly == null )
        throw new ArgumentNullException( "assembly" );

     byte[] pubkey = assembly.GetName().GetPublicKeyToken();
     if ( pubkey.Length == 0 )
        throw new ArgumentException( "No public key token in assembly." );

     StringBuilder builder = new StringBuilder();
     foreach ( byte b in pubkey )
     {
        builder.AppendFormat( "{0:x2}", b );
     }
     string pkString = builder.ToString();
     if ( pkString != "b77a5c561934e089" /* Microsoft */ &&
          pkString != "abababababababab" /* Ivara */ )
     {
        throw new ArgumentException( "Assembly is not signed by My Company or Microsoft. You do not have permission to call this code." );
     }
  }

** 更改了名称和密钥以保护无辜者。 任何与真实姓名或公司的相似之处均纯属巧合。*

As per Eric's suggestion, I solved it by checking the key myself. In the code I want to protect, I add the following call,

EnsureAssemblyIsSignedByMyCompany( Assembly.GetCallingAssembly() );

Then the implementation of that method is

  /// <summary>
  /// Ensures that the given assembly is signed by My Company or Microsoft.
  /// </summary>
  /// <param name="assembly"></param>
  private static void EnsureAssemblyIsSignedByMyCompany( Assembly assembly )
  {
     if ( assembly == null )
        throw new ArgumentNullException( "assembly" );

     byte[] pubkey = assembly.GetName().GetPublicKeyToken();
     if ( pubkey.Length == 0 )
        throw new ArgumentException( "No public key token in assembly." );

     StringBuilder builder = new StringBuilder();
     foreach ( byte b in pubkey )
     {
        builder.AppendFormat( "{0:x2}", b );
     }
     string pkString = builder.ToString();
     if ( pkString != "b77a5c561934e089" /* Microsoft */ &&
          pkString != "abababababababab" /* Ivara */ )
     {
        throw new ArgumentException( "Assembly is not signed by My Company or Microsoft. You do not have permission to call this code." );
     }
  }

** Names and keys changed to protect the innocent. Any likeness to real names or companies is merely a coincidence.*

挖个坑埋了你 2024-07-18 13:20:12

请参阅这篇文章:
http:// blogs.msdn.com/ericlippert/archive/2008/10/06/preventing-third-party-derivation-part-two.aspx

特别是这一部分:

在 .NET 的最新版本中,“完全信任意味着完全信任”。 也就是说,完全可信的代码满足所有要求,包括“用此密钥签名”之类的要求,无论它是否实际上已签名。

这不是安全系统的致命缺陷吗? 不会。完全可信的代码始终有能力做到这一点,因为完全可信的代码有能力控制与给定程序集关联的证据。 如果你可以控制证据,那么你可以伪造一个看起来像是来自微软的程序集,没问题。 (如果您的进程中已经存在恶意的完全信任代码,那么您就已经失败了——它不需要模拟 Microsoft 签名的程序集;它已经有能力执行用户可以执行的任何操作。)

显然, .Net 设计者认为此属性对于 .Net 1.x 中的完全信任代码也不是很有效。

See this article:
http://blogs.msdn.com/ericlippert/archive/2008/10/06/preventing-third-party-derivation-part-two.aspx

Particularly this part:

In recent versions of .NET, "full trust means full trust". That is, fully-trusted code satisfies all demands, including demands for things like "was signed with this key", whether it actually was signed or not.

Isn't that a deadly flaw in the security system? No. Fully trusted code always had the ability to do that, because fully trusted code has the ability to control the evidence associated with a given assembly. If you can control the evidence,then you can forge an assembly that looks like it came from Microsoft, no problem. (And if you already have malicious full-trust code in your process then you have already lost -- it doesn't need to impersonate Microsoft-signed assemblies; it already has the power to do whatever the user can do.)

Apparently, the .Net designers felt that this attribute wasn't very effective for full trust code in .Net 1.x either.

秋日私语 2024-07-18 13:20:12

正如 Joel 所说,您在 CAS 方面运气不好。 但是,您可以在需要保护的任何方法中自行进行检查,方法是使用 Assembly.GetCallingAssembly() 获取对包含调用代码的程序集的引用,然后手动检查该程序集的强名称。

As Joel indicated, you are out of luck with regard to CAS. However, you may be able to do the check yourself in any method you need to protect by using Assembly.GetCallingAssembly() to get a reference to the assembly containing the calling code, then check the strong name on that assembly manually.

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