SecureString 作为全局应用程序变量是安全的吗?

A SecureString is safe as global application variable?

我有一个带有第一个登录屏幕的应用程序。密码在密码文本框中输入,这是一个安全字符串。

我的想法是将此 SecureString 存储在全局应用程序变量中,因为我会在某些操作中需要它,所以我想避免用户每次都必须输入密码。一个动作可以是发送一封电子邮件,我需要用户和密码,所以我不希望用户每次发送和发送电子邮件时都写下电子邮件帐户的密码。

我读过一些帖子,我知道最后我需要来自 secureString 的纯文本,这是弱点,所以解决方案是减少纯文本在内存中的时间. Some solutions can be found here.

但是我的问题是,全局变量怎么是secureString,在不解码的情况下安全吗?如果这不是一个好的选择,还有其他更好的选择,但不需要用户在每次操作需要密码时都输入密码?

谢谢。

将要在整个应用程序中重复使用的数据集中起来是一种标准设计模式。假设您有一组网页,您希望在其上显示用户的姓名,或者您需要缓存特定用户的帖子或个人资料。不同的框架为此提供了不同的工具,例如在Vue.js中,Vuex提供了实现这种机制。

对于你的问题,你没有指定你正在使用哪个框架,无论是ASP.NetWPF,还是依赖[=的纯C#应用程序15=]。但没问题,有几种方法可以让您实现您所追求的目标,您可以选择最适合您需要的套件:

1-您可以在您的应用程序中开发一个缓存层,并通过特定会话的生命周期缓存密码或其他数据。为此,您可以依赖 "Concurrent Collection" 或使用 EF Core In-Memory 数据库。 (IMemeoryCache 也可用于 ASP.Net 核心)

2- 如果密码需要进行一些互操作,那么我建议您使用全局缓存服务器。为此,您可以使用 Redis 或 Alachisoft NCache。

3- 如果是 Web 应用程序,则可以将数据保存在 cookie 或浏览器的本地存储中。

请记住,SecureString 可能不会像您期望的那样为您提供任何安全优势,因为正如 Microsoft 所述:

A SecureString object is similar to a String object in that it has a text value. However, the value of a SecureString object is pinned in memory, may use a protection mechanism, such as encryption, provided by the underlying operating system, can be modified until your application marks it as read-only, and can be deleted from computer memory either by your application calling the Dispose method or by the .NET Framework garbage collector.

也就是说;你必须从内存和内存管理的角度来看这个类型的安全方面。最后,您必须实施自己的安全提供程序,例如哈希生成或加密等实现真正的安全性。

存储包含密码的 SecureString 安全。尽管该字符串已加密,这应该会使窥探者更难发现它是什么,但它仍然存在,并且有可能被解密。

实际上,使用 SecureString 的主要优点之一是能够在不再需要时释放它。

看这里:

Overall, SecureString is more secure than String because it limits the exposure of sensitive string data. However, those strings may still be exposed to any process or operation that has access to raw memory, such as a malicious process running on the host computer, a process dump, or a user-viewable swap file. Instead of using SecureString to protect passwords, the recommended alternative is to use an opaque handle to credentials that are stored outside of the process.

在不深入了解您的应用程序的情况下,我可以告诉您的是:如果您必须存储包含密码的字符串,请使用 SecureString。但是,如果您可以,例如,使用 Windows 集成安全性或任何其他传播凭据的机制,那就更好了。

最后,没有任何应用程序是完全安全的。您必须了解您的攻击向量...谁将使用该应用程序,它的部署位置等等。否则您可能会花太多时间担心一件小事(例如内存转储)而忘记一个更大的问题(你的数据库安全吗?管理员密码安全吗?)。