在Windows中存储密码的安全方式 [英] Secure way to store password in Windows

查看:372
本文介绍了在Windows中存储密码的安全方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保护包含敏感信息的本地数据库(类似于这个问题,只有对于delphi 2010



我正在使用 DISQLite组件,它支持AES加密,但是我仍然需要保护我用来解密的密码。阅读数据库。



我最初的想法是生成随机密码,使用DPAPI( CryptProtectData 和C code CryptUnprotectData 在Crypt32.dll中找到的函数),但是我找不到任何关于Delphi的示例



我的问题是:如何安全地存储随机生成的密码?或者,假设DPAPI路是安全的,我该如何在Delphi中实现这个DPAPI?

解决方案

最好使用Windows'一个href =http://msdn.microsoft.com/en-us/library/ms995355.aspx> DPAPI 。它比使用其他方法更安全:




  • CryptProtectData / CryptProtectMemory

  • CryptUnprotectData / CryptUnprotectMemory



CryptProtectMemory / CryptUnprotectMemory提供更多的灵活性:




  • CRYPTPROTECTMEMORY_SAME_PROCESS:只有您的进程可以解密您的数据

  • CRYPTPROTECTMEMORY_CROSS_PROCESS:任何进程都可以查看您的数据

  • CRYPTPROTECTMEMORY_SAME_LOGON:只有使用同一用户进行的进程相同的会话可以解密数据



优点:


  1. 不需要钥匙 - Windows为您执行

  2. 粒度控制:每个进程/每个会话/每个登录/每台机器

  3. CryptProtectData存在于Windows 2000和更高版本

  4. DPAPI Windows比使用更安全从你那里写的安全相关代码,我和那些相信随机()返回绝对随机数的人:)事实上,微软在安全领域拥有数十年的经验,拥有最多受到攻击的操作系统:o)

缺点:


  1. 在CRYPTPROTECTMEMORY_SAME_PROCESS的情况下,One *可以在您的进程中插入一个新线程,这个线程可以解密您的数据

  2. 如果有人重置用户密码(不更改),您将无法解密数据

  3. 在CRYPTPROTECTMEMORY_SAME_LOGON的情况下:如果用户*运行黑客进程,它可以解密您的数据

  4. 如果您使用CRYPTPROTECT_LOCAL_MACHINE - 该机器上的每个用户可以解密数据。 这就是为什么不建议将密码保存在.RDP文件中

  5. 已知问题

注意:每个用户是具有使用DPAPI的工具或技能的用户



无论如何 - 您有一个选择。



请注意,@ David-Heffernan是对的 - 存储在计算机上的任何东西都可以被解密 - 从内存读取内容,在进程中注入线程。 p>

另一方面,为什么我们不要让饼干的生活更难? :)



经验法则:清除使用它们后包含敏感数据的所有缓冲区。这不会使事情变得非常安全,但会降低您的记忆包含敏感数据的可能性。
当然这并不能解决其他主要问题:Delphi其他组件如何处理您传递给他们的敏感数据:)



JEDI安全库对DPAPI具有面向对象的方法。另外JEDI项目包含用于DPAPI(JWA IIRC)的翻译的Windows标头



更新:以下是使用DPAPI的示例代码(使用 JEDI API ):

 使用SysUtils,jwaWinCrypt,jwaWinBase,jwaWinType; 

函数dpApiProtectData(var fpDataIn:tBytes):tBytes;
var
dataIn,//输入缓冲区(clear-text / data)
dataOut:DATA_BLOB; //输出缓冲区(加密)
begin
//初始化变量
dataOut.cbData:= 0;
dataOut.pbData:= nil;

dataIn.cbData:= length(fpDataIn); //要加密多少数据(以字节为单位)
dataIn.pbData:= @fpDataIn [0]; //指向数据本身的地址 - 输入字节数组的第一个元素的地址

如果不是CryptProtectData(@dataIn,nil,nil,nil,nil,0,@dataOut)则
RaiseLastOSError; //有些事情发生在

//将加密的字节复制到RESULT变量
setLength(result,dataOut.cbData);
move(dataOut.pbData ^,result [0],dataOut.cbData);
LocalFree(HLOCAL(dataOut.pbData)); // http://msdn.microsoft.com/en-us/library/windows/desktop/aa380261(v=vs.85).aspx
// fillChar(fpDataIn [0],length(fpDataIn), #0); //最终擦除输入缓冲区,即不将敏感数据留在内存
end;

函数dpApiUnprotectData(fpDataIn:tBytes):tBytes;
var
dataIn,//输入缓冲区(clear-text / data)
dataOut:DATA_BLOB; //输出缓冲区(加密)
begin
dataOut.cbData:= 0;
dataOut.pbData:= nil;

dataIn.cbData:= length(fpDataIn);
dataIn.pbData:= @fpDataIn [0];

如果不是CryptUnprotectData(
@dataIn,
nil,
nil,
nil,
nil,
0,/ /可能的标志:http://msdn.microsoft.com/en-us/library/windows/desktop/aa380261%28v=vs.85%29.aspx
// 0(零)仅表示用户加密的数据将能够解密
@dataOut
)然后
RaiseLastOSError;

setLength(result,dataOut.cbData); //复制RESULT变量中的解密字节
move(dataOut.pbData ^,result [0],dataOut.cbData);
LocalFree(HLOCAL(dataOut.pbData)); // http://msdn.microsoft.com/en-us/library/windows/desktop/aa380882%28v=vs.85%29.aspx
end;

procedure testDpApi;
var
bytesClearTextIn,//保存输入字节
bytesClearTextOut,//保存输出字节
bytesEncrypted:tBytes; //保存生成的加密字节
strIn,strOut:string; //输入/输出字符串
begin

// *** ENCRYPT STRING BY BY ARRAY
strIn:='Some Secret Data Here';

//将字符串内容复制到bytesClearTextIn
//注意:这适用于STRING类型! (AnsiString / UnicodeString)
setLength(bytesClearTextIn,length(strIn)* sizeOf(char));
move(strIn [1],bytesClearTextIn [0],length(strIn)* sizeOf(char));

bytesEncrypted:= dpApiProtectData(bytesClearTextIn); //加密数据

// *** DECRYPT BYTE ARRAY TO STRING
bytesClearTextOut:= dpApiUnprotectData(bytesEncrypted); //解密数据

//将解密的字节(bytesClearTextOut)复制到输出字符串变量
// NB:这适用于STRING类型! (AnsiString / UnicodeString)
setLength(strOut,length(bytesClearTextOut)div sizeOf(char));
move(bytesClearTextOut [0],strOut [1],length(bytesClearTextOut));

assert(strOut = strIn,'Boom!'); // Boom永远不要bo oom :)

end;

注意:




  • 这个例子是使用CryptProtectData / CryptUnprotectData的轻量级版本;

  • 加密是面向字节的,所以使用tBytes(tBytes =字节数组)更容易;

  • 如果输入和输出字符串为UTF8String,则删除* sizeOf(char),因为UTF8String的char仅为1个字节

  • 使用 CryptProtectMemory / CryptUnProtectMemory 是相似的


I'm trying to protect a local database that contains sensitive info (similar to this question, only for delphi 2010)

I'm using DISQLite component, which does support AES encryption, but I still need to protect this password I use to decrypt & read the database.

My initial idea was to generate a random password, store it using something like DPAPI (CryptProtectData and CryptUnprotectData functions found in Crypt32.dll), but I couldn't find any example on that for Delphi

My question is: how can I safely store a randomly generated password? Or, assuming the DPAPI road is secure, how can I implement this DPAPI in Delphi?

解决方案

It's better to use Windows' DPAPI. It's much more secure than using other methods:

  • CryptProtectData / CryptProtectMemory
  • CryptUnprotectData / CryptUnprotectMemory

CryptProtectMemory / CryptUnprotectMemory offer more flexibility:

  • CRYPTPROTECTMEMORY_SAME_PROCESS: only your process can decrypt your data
  • CRYPTPROTECTMEMORY_CROSS_PROCESS: any process can dectypt your data
  • CRYPTPROTECTMEMORY_SAME_LOGON: only processes running with the same user and in the same session can decrypt data

Pros:

  1. No need to have a key - Windows do it for you
  2. Granular control: per process / per session / per login / per machine
  3. CryptProtectData exists in Windows 2000 and newer
  4. DPAPI Windows is more secure than using "security" related code written from you, me and the people that believe Random() returns absolutely random number :) In fact Microsoft has decades of experience in the security field, having the most attacked OS ever :o)

Cons:

  1. In the case of CRYPTPROTECTMEMORY_SAME_PROCESS One* can just inject a new thread in your process and this thread can decrypt your data
  2. If someone reset user's password (not change) you will be unable to decrypt your data
  3. In the case of CRYPTPROTECTMEMORY_SAME_LOGON: if the user* run hacked process it can decrypt your data
  4. If you use CRYPTPROTECT_LOCAL_MACHINE - every user* on that machine can decrypt the data. This is why it's not recommended to save passwords in .RDP files
  5. Known issues

Note: "every user" is a user who has tools or skills to use DPAPI

Anyway - you have a choice.

Note that @David-Heffernan is right - anything stored on the computer can be decrypted - reading it from memory, injecting threads in your process etc.

On the other hand ... why don't we make cracker's life harder? :)

Rule of thumb: clear all buffers that contain sensitive data after using them. This doesn't make things super safe, but decreases the possibility your memory to contain sensitive data. Of course this doesn't solve the other major problem: how other Delphi components handle the sensitive data you pass to them :)

Security Library by JEDI has object oriented approach to DPAPI. Also JEDI project contains translated windows headers for DPAPI (JWA IIRC)

UPDATE: Here's sample code that uses DPAPI (using JEDI API):

Uses SysUtils, jwaWinCrypt, jwaWinBase, jwaWinType;

function dpApiProtectData(var fpDataIn: tBytes): tBytes;
var
  dataIn,               // Input buffer (clear-text/data)
  dataOut: DATA_BLOB;   // Output buffer (encrypted)
begin
  // Initializing variables
  dataOut.cbData := 0;
  dataOut.pbData := nil;

  dataIn.cbData := length(fpDataIn); // How much data (in bytes) we want to encrypt
  dataIn.pbData := @fpDataIn[0];     // Pointer to the data itself - the address of the first element of the input byte array

  if not CryptProtectData(@dataIn, nil, nil, nil, nil, 0, @dataOut) then
    RaiseLastOSError; // Bad things happen sometimes

  // Copy the encrypted bytes to RESULT variable
  setLength(result, dataOut.cbData);
  move(dataOut.pbData^, result[0], dataOut.cbData);
  LocalFree(HLOCAL(dataOut.pbData));                  // http://msdn.microsoft.com/en-us/library/windows/desktop/aa380261(v=vs.85).aspx
//  fillChar(fpDataIn[0], length(fpDataIn), #0);  // Eventually erase input buffer i.e. not to leave sensitive data in memory
end;

function dpApiUnprotectData(fpDataIn: tBytes): tBytes;
var
  dataIn,               // Input buffer (clear-text/data)
  dataOut: DATA_BLOB;   // Output buffer (encrypted)
begin
  dataOut.cbData := 0;
  dataOut.pbData := nil;

  dataIn.cbData := length(fpDataIn);
  dataIn.pbData := @fpDataIn[0];

  if not CryptUnprotectData(
    @dataIn,  
    nil, 
    nil, 
    nil, 
    nil, 
    0,         // Possible flags: http://msdn.microsoft.com/en-us/library/windows/desktop/aa380261%28v=vs.85%29.aspx 
               // 0 (zero) means only the user that encrypted the data will be able to decrypt it
    @dataOut
  ) then
    RaiseLastOSError;

  setLength(result, dataOut.cbData);                  // Copy decrypted bytes in the RESULT variable
  move(dataOut.pbData^, result[0], dataOut.cbData);   
  LocalFree(HLOCAL(dataOut.pbData));                  // http://msdn.microsoft.com/en-us/library/windows/desktop/aa380882%28v=vs.85%29.aspx
end;

procedure testDpApi;
var
  bytesClearTextIn,       // Holds input bytes
  bytesClearTextOut,      // Holds output bytes
  bytesEncrypted: tBytes; // Holds the resulting encrypted bytes
  strIn, strOut: string;  // Input / Output strings
begin

  // *** ENCRYPT STRING TO BYTE ARRAY
  strIn := 'Some Secret Data Here';

  // Copy string contents to bytesClearTextIn
  // NB: this works for STRING type only!!! (AnsiString / UnicodeString)
  setLength(bytesClearTextIn, length(strIn) * sizeOf(char));
  move(strIn[1], bytesClearTextIn[0], length(strIn) * sizeOf(char));

  bytesEncrypted := dpApiProtectData(bytesClearTextIn);     // Encrypt data

  // *** DECRYPT BYTE ARRAY TO STRING
  bytesClearTextOut := dpApiUnprotectData(bytesEncrypted);  // Decrypt data

  // Copy decrypted bytes (bytesClearTextOut) to the output string variable
  // NB: this works for STRING type only!!! (AnsiString / UnicodeString)    
  setLength(strOut, length(bytesClearTextOut) div sizeOf(char));
  move(bytesClearTextOut[0], strOut[1], length(bytesClearTextOut));

  assert(strOut = strIn, 'Boom!');  // Boom should never booom :)

end;

Notes:

  • The example is lightweight version of using CryptProtectData / CryptUnprotectData;
  • Encryption is byte oriented so it's easier to use tBytes (tBytes = array of byte);
  • If input and output string are UTF8String, then remove "* sizeOf(char)", because UTF8String's char is 1 byte only
  • The use of CryptProtectMemory / CryptUnProtectMemory is similar

这篇关于在Windows中存储密码的安全方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆