简体   繁体   English

保护VB.NET中的内存(存储密钥)

[英]Protect memory (storing keys) in VB.NET

I have an application that handles encrypted data and I have tried to be as careful as possible to make the keys inaccessible, but of course the application needs to know them somehow. 我有一个处理加密数据的应用程序,并且我尝试过尽可能小心以使密钥不可访问,但是当然,该应用程序需要以某种方式知道它们。

I have hardcoded the keys as function return values so they should never hang around in memory too long, however I guess it is still possible for them to be read. 我已经将这些键硬编码为函数返回值,因此它们不应在内存中停留太久,但是我猜仍然可以读取它们。 Is there a way to make a region of memory inaccessible? 有没有办法使内存区域不可访问? I am not a computer scientist so apologies if this is a well-known problem (I suspect it is). 我不是计算机科学家,所以如果这是一个众所周知的问题,我深表歉意(我怀疑是这样)。

Protecting them from a decompiler is a question for another day... 保护他们免受反编译器困扰是另一天的问题。

Thanks in advance! 提前致谢!

The way to do this is to use a SecureString . 做到这一点的方法是使用SecureString It is a bit more awkward to use than a normal string but it is the exact thing you need for this scenario. 与普通字符串相比,使用它有点尴尬,但这是您在此情况下确实需要的东西。

Here is the docs to it: https://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspx 这是它的文档: https : //msdn.microsoft.com/zh-cn/library/system.security.securestring(v=vs.110).aspx

and some more information here about it: Is SecureString ever practical in a C# application? 以及与此有关的更多信息: SecureString在C#应用程序中是否可行?

Here is an example of setting this to use a 'String value' 这是将其设置为使用“字符串值”的示例

        var s = new System.Security.SecureString();           
        s.AppendChar('s');
        s.AppendChar('e');
        s.AppendChar('c');
        s.AppendChar('r');
        s.AppendChar('e');
        s.AppendChar('t');
        s.AppendChar('s');
        s.AppendChar('q');
        s.AppendChar('u');
        s.AppendChar('i');
        s.AppendChar('r');
        s.AppendChar('r');
        s.AppendChar('e');
        s.AppendChar('l');
        s.MakeReadOnly();

Edit: Added a case where an HSM isn't necessarily the solution 编辑:添加了HSM不一定是解决方案的情况

As you mentioned, a decompiler can get at your keys; 如您所述,反编译器可以帮助您解决问题。 that's part of the reason that you should NEVER have a decryption key hard-coded (a hard-coded public key to encrypt for is less bad). 这是您永远不要对解密密钥进行硬编码的部分原因(要加密的硬编码公共密钥不太坏)。

If I mis-understood the question and your key isn't hard-coded but you're trying to hide the key when it isn't needed then an API like ProtectedData.Protect may what you are looking for. 如果我误解了这个问题,并且您的密钥不是硬编码的,但是您试图在不需要密钥时将其隐藏,那么诸如ProtectedData.Protect之类的API可能就是您想要的。

But depending on what you're doing, that may not be the right answer (who is your attacker? what are you defending from?). 但是,根据您在做什么,这可能不是正确的答案(您的攻击者是谁?您在防御什么?)。

Defending from: 捍卫:

  • A Watson dump that gets sent to Microsoft containing the key Watson转储,该转储发送给Microsoft,其中包含密钥
    • ProtectedData.Protect (or HSM) ProtectedData.Protect(或HSM)
  • An arbitrary-memory-read security vulnerability in your code being exploited by a remote attacker 远程攻击者正在利用代码中的任意内存读取安全漏洞
    • ProtectedData.Protect (or the sarcastic "don't write that kind of vulnerability", or HSM) ProtectedData.Protect(或讽刺性的“不要写那种漏洞”或HSM)
  • An administrator (legit or otherwise) being able to attach a debugger and read the key 管理员(合法或其他方式)能够附加调试器并读取密钥
    • Move the key to a Hardware Security Module (HSM) 将密钥移至硬件安全模块(HSM)
  • Your process being paged out and an administrator being able to read it from the pagefile 您的进程被调出页面,管理员可以从页面文件中读取它
    • HSM HSM
  • An arbitrary-execute security vulnerability in your code being exploited by a remote attacker 您的代码中的任意执行安全漏洞被远程攻击者利用
    • HSM HSM
  • Your system hibernating and the hibernation payload being read by someone who steals your hard drive 系统处于休眠状态,并且窃取硬盘驱动器的人正在读取休眠负载
    • BitLocker or HSM BitLocker或HSM
  • A process crash dump that is still on the hard drive and the drive gets stolen 仍在硬盘驱动器上的进程崩溃转储,驱动器被盗
    • BitLocker or HSM BitLocker或HSM
  • The entire computer gets physically stolen 整个计算机被盗
    • BitLocker (but not necessarily an HSM, since that would have been stolen, too). BitLocker(但不一定是HSM,因为那样也会被盗)。

You'll note that the list of things that you can actually defend against with memory protection is small. 您会注意到,实际上可以通过内存保护来防御的事物列表很小。 Much better is making it so that nothing can ever read the key, by moving it to an HSM. 更好的办法是将其移至HSM,以使任何人都无法读取密钥。 Windows CNG supports symmetric encryption via an HSM, and .NET 4.6.2 (currently in preview) supports utilizing this behavior with the AesCng(string, CngProvider) constructor . Windows CNG通过HSM支持对称加密,.NET 4.6.2(当前处于预览状态)支持通过AesCng(string,CngProvider)构造函数利用此行为。

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

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