简体   繁体   English

安全窗口假冒?

[英]Secure windows Impersonation?

In my WPF application I want to allow administrators to test a database connection using integrated security for various other users. 在我的WPF应用程序中,我希望允许管理员使用集成安全性为各种其他用户测试数据库连接。 So I have a form that allows the admin to enter the domain, username and password and then test it. 所以我有一个表单,允许管理员输入域名,用户名和密码,然后测试它。 I am able to securely handle the password right up until I call LogonUser in the advapi32.dll which takes a string password 我能够安全地处理密码,直到我在带有string passwordadvapi32.dll调用LogonUser

LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref UserHandle)

I have written a utility function which converts the SecureString to a string as safe as possible, and then im calling it on the password in the LogonUser call: 我编写了一个实用程序函数,它将SecureString转换为尽可能安全的字符串,然后在LogonUser调用中将其调用密码:

LogonUser(UserName, Domain, Helper.ConvertSafely(Password), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref UserHandle)

Since the signature for LogonUser takes a string, unless LogonUser is taking proper care of the password in its execution, it could still be on my call stack in plain text after the call returns. 由于LogonUser的签名采用字符串,除非LogonUser在执行时正确地处理密码,否则在调用返回后它仍然可以在我的调用堆栈中以纯文本形式存在。 Is there a more secure way to impersonate a user in which i can be confidant the PW is secure the whole time? 是否有一种更安全的方式来冒充用户,在这种情况下,我可以确信PW一直是安全的?

Basically all I need is a WindowsImpersonationContext but i would like to aquire it without the password ever being in plain text. 基本上我只需要一个WindowsImpersonationContext但我想在没有密码的情况下查询它。

Inside your managed application you can make use of the SecureString class to handle sensitive information. 在托管应用程序中,您可以使用SecureString类来处理敏感信息。 There is usually no need to roll your own secure string mechanism. 通常不需要滚动自己的安全字符串机制。

Just before you pinvoke into LogonUser you are then passing the unmanaged copy of the SecureString password. 在您进入LogonUser之前,您将传递SecureString密码的非托管副本。 You use the Marshal.SecureStringToGlobalAllocUnicode Method for this. 您可以使用Marshal.SecureStringToGlobalAllocUnicode方法。 Thus there is never a managed object in your app domain representing the password. 因此,您的应用程序域中永远不会有代表密码的托管对象。

You can find example code in this article. 您可以在示例代码文章。

If you require even more security I would suggest to disallow direct database connection from you wpf clients. 如果您需要更高的安全性,我建议您禁止从您的wpf客户端直接连接数据库。 You can introduce a middle tier. 您可以引入中间层。 Security is thus shifted away from the clients to this one server. 因此,安全性从客户端转移到这个服务器。 Communication between Client and App-Server is encrypted and only the app server talks to the database. 客户端和App-Server之间的通信已加密,只有应用服务器与数据库通信。

If the user you want to impersonate is already logged on the machine (for example in another session), here is an answer: Is it possible for a Windows service impersonate a user without a password? 如果您要模拟的用户已登录到计算机上(例如在另一个会话中),则可以回答: Windows服务是否可以在没有密码的情况下模拟用户?

If he's not, you'll have to provide a password to LogonUser. 如果他不是,你将不得不为LogonUser提供密码。 And this password will have to reside at least a small amount of time, as is, in memory because that's the way LogonUser is defined. 此密码必须至少驻留在内存中,因为这是LogonUser的定义方式。 Note not all auth packages require passwords (like biometrics or smart card: Windows Authentication Overview ). 请注意,并非所有身份验证包都需要密码(如生物识别或智能卡: Windows身份验证概述 )。

So if you really want impersonation, somewhere you'll have to pass a password around. 因此,如果您真的想要模仿,那么您必须在某处传递密码。 In this case make sure you use postlagerkarte's code from Microsoft for this: Marshal.SecureStringToGlobalAllocUnicode Method sample code 在这种情况下,请确保使用Microsoft的postlagerkarte代码: Marshal.SecureStringToGlobalAllocUnicode方法示例代码

You can't really do much more. 你真的不能做得更多。

Update: 更新:

You could implement an encrypted app.config . 您可以实现加密的app.config You can do this through several different approaches: 您可以通过几种不同的方法来实现:

So how this would work, is when an Administrator inserts the credentials you would decrypt the app.config modify the key/value with the new credentials. 那么这是如何工作的,当管理员插入您将解密app.config的凭据时,使用新凭据修改密钥/值 Then once it is inserted it encrypts the config file again. 然后一旦插入它再次加密配置文件。

So it is exposed minimally, but also the data is inserted through encapsulation which helps hide it as well. 所以它暴露的最低限度,但数据也通过封装插入,这也有助于隐藏它。

This is one approach, though a SecureString will work it expects a char[] . 这是一种方法,虽然SecureString可以使用它需要char[] Which in some instances may not be ideal when interfacing through the advapi32.dll . 在某些情况下,在通过advapi32.dll接口时可能并不理想。

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

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