DPAPI Secrets. Security analysis and data recovery in DPAPI (Part 1)16:43:58 12.03.2012 posted by Passcape_Admin at 16:43:58 12.03.2012
Introduction DPAPI architecture and securityData encryption in DPAPI
DPAPI Master Keys
Next to the Part 2 of the article >> AbstractIn this paper, we are making an attempt to analyze the operation of DPAPI, review the undocumented structures and encryption algorithms of DPAPI, understand and describe the internal functioning of the system. What is DPAPIBeginning with Windows 2000, Microsoft ships their operating systems with a special data protection interface, known as Data Protection Application Programming Interface (DPAPI). DPAPI is currently widespread and used in many Windows applications and subsystems. For example, in the file encryption system, for storing wireless connection passwords, in Windows Credential Manager, Internet Explorer, Outlook, Skype, Windows CardSpace, Windows Vault, Google Chrome, etc. DPAPI has become popular among programmers first of all due to its simplicity of use, as it consists of just a couple of functions for encrypting and decrypting data, CryptProtectData and CryptUnprotectData. What does DPAPI protect?DPAPI is utilized to protect the following personal data:
An example of a successful and clever way to protect data using DPAPI is the implementation of the auto-completion password encryption algorithm in Internet Explorer. To encrypt the login and password for a certain web page, it calls the CryptProtectData function, where in the optional entropy parameter it specifies the address of the web page. Thus, unless one knows the original URL where the password was entered, nobody, not even Internet Explorer itself, can decrypt that data back.
DPAPI architectureBeginning with Windows 2000, as it was mentioned earlier, any application can protect its personal data (e.g., passwords) by simply calling the CryptProtectData function, which returns a "non-transparent" binary structure, also known as DPAPI blob. "Non-transparent", by Microsoft's definition, means that besides the original encrypted data of the application it also contains other supplemential stuff, necessary for decrypting that data. Despite that the structure of DPAPI blobs is not documented, with the release of the new version of Windows Password Recovery, for the first time ever it has become possible to decrypt DPAPI blobs offline and thoroughly analyze them. DPAPI securityDPAPI was created with the view of many aspects in terms of security. Here is a list of key features that distinguish it from similar systems:
Registry key: HKLM/Software/Microsoft/Cryptography/Protect/Providers/%GUID% Value: MasterKeyIterationCount
Figure 2. Decryptinng DPAPI blob without knowing the original user logon password in Windows 2000. If taken as a whole, DPAPI provides perhaps the strongest to date protection and safety of user data.
DPAPI configurationDPAPI runs on top of Crypto API and can use any encryption service provider. The crypto provider is selected as defined in the following registry key: Registry key: HKLM/Software/Microsoft/Cryptography/Protect/Providers Value: PreferredBy default, the system uses Microsoft's crypto provider named df9d8cd0-1501-11d1-8c7a-00c04fc297eb, implemented in the psbase.dll library. The encryption is configured by modifying the respective value in the following registry key: HKLM/Software/Microsoft/Cryptography/Protect/Providers/%GUID% Where %GUID% is the service provider's unique identifier. By default, the DPAPI settings key corresponds with HKLM/Software/Microsoft/Cryptography/Protect/Providers/df9d8cd0-1501-11d1-8c7a-00c04fc297eb This hive of the registry can be modified by users with Administrator rights only. Here are the descriptions of some keys of this hive. Value: MasterKeyIterationCount Data type: REG_DWORD Description: Iteration counter for function PBKDF2, cannot be smaller than the default value. The key generation algorithm is used in the encryption of user's Master Key and credential history. Value: MasterKeyLegacyCompliance Data type: REG_DWORD Description: Set the registry key to 1 to make DPAPI behavior similar to that of Windows 2000 when backing up/restoring Master Keys. Value: MasterKeyLegacyNt4Domain Data type: REG_DWORD Description: Since DPAPI backup/restore support doesn't exist on NT4, DPAPI provides a workaround to avoid losing its data and force the client machine to use W2K style master key backup/restore. For that, you should set the MasterKeyLegacyNt4Domain parameter to 1. Value: DistributeBackupKey Data type: REG_DWORD Description: Determines compatibility settings when backing up or restoring domain key. For example, if the domain functional level is set to Windows 2000 native. Value: Recovery Version Data type: REG_DWORD Description: Determines version compatibility when backing up and restoring local copies of Master Keys. The default version is 2; Windows 7 and Windows Server 2008 R2 can be configured to use version 3 by setting the DWORD registry value to 3. Value: ProtectionPolicy Data type: REG_DWORD Description: Default data protection policy flag. Corresponds with the dwPolicy flag in Master Key header. Value: Encr Alg, Encr Alg Key Size Data type: REG_DWORD Description: Pair of values that control the type of the encryption algorithm used in DPAPI and the size of the key used in that algorithm. Value: MAC Alg, MAC Alg Key Size Data type: REG_DWORD Description: Keys for setting the DPAPI data integrity control algorithm. Description of CryptProtectData and CryptUnprotectData functionsThese functions' parameters are pretty much identical, so we are going to take a look only at CryptProtectData, which in C++ is declared as follows: BOOL WINAPI CryptProtectData( __in DATA_BLOB *pDataIn, __in LPCWSTR szDataDescr, __in DATA_BLOB *pOptionalEntropy, __in PVOID pvReserved, __in_opt CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct, __in DWORD dwFlags, __out DATA_BLOB *pDataOut );Description of function fields: DATA_BLOB *pDataIn
pointer to a DATA_BLOB structure that refers to the original data to be encrypted.
LPCWSTR szDataDescr
data descriptor; stored as plain text in DPAPI blob. This optional parameter is for information purposes only; it may be omitted in the function call.
DATA_BLOB *pOptionalEntropy
as the foregoing, is an optional parameter. However, unlike the descriptor, the entropy directly affects data security, so it is not stored in the DPAPI blob. User must ensure the proper management of this secret. Some third-party applications (such as GTalk) store this secret in the registry, other Windows components (such as .Net Passport or WININET) use a hard-coded constant for the secret, third use a variable value. For example, the internal FTP client of Windows Vista creates entropy based on the server name. One way or the other, this may at least amuse the potential attacker. In our opinion, the most successful example of utilizing entropy is the one implemented in the password storage mechanism of Internet Explorer, which doesn't store the secret at all.
PVOID pvReserved
currently not used.
CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct
when this structure is defined, user is required to enter an additional password for encrypting the DPAPI blob. Put it this way: It is used very infrequently due to the fact that the password prompt occurs in interactive mode.
DWORD dwFlags
flags that control the encryption process. Here is the description of the most interesting of them:
CRYPTPROTECT_UI_FORBIDDEN (0x1) Used when user interface is not available. For example, when using remote access. CRYPTPROTECT_LOCAL_MACHINE (0x4) Data is protected using local computer account. Any user of the system may be able to decrypt it. CRYPTPROTECT_CRED_SYNC (0x8) Forces synchronizing user's credentals. Normally runs automatically upon user password change. CRYPTPROTECT_AUDIT (0x10) Enables audit during encryption/dectyption CRYPTPROTECT_VERIFY_PROTECTION (0x40) The flag checks security level of DPAPI blob. If the default security level is higher than current security level of the blob, the function returns error CRYPT_I_NEW_PROTECTION_REQUIRED as advice to reset securiry for the source data. CRYPTPROTECT_CRED_REGENERATE (0x80) Regenerate local computer passwords. CRYPTPROTECT_SYSTEM (x20000000) Indicates that only system processes can encrypt/decrypt data. DATA_BLOB *pDataOut
target DPAPI blob that occurs in the result of calling the function
Examples of use for CryptProtectData/ CryptUnprotectData functionsFor the convenience of running tests, as well as for the purposes of providing an example, we have created two simple utilities of the respective names, which implement mere wraps around DPAPI functions. Thus, DPAPI functions can be called from the command line. Here is the С++ source for CryptProtectData. The compiled ЕХЕ files of these programs are also available for download at: CryptProtectData, CryptUnprotectData. Source codes: CryptProtectData , CryptUnprotectData
Executable files: CryptProtectData , CryptUnprotectData CryptProtectData source code// CryptProtectData.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <windows.h> #include <stdio.h> #pragma comment (lib, "Crypt32") int _tmain(int argc, _TCHAR* argv[]) { if ( argc<3 || argc>5 ) { _tprintf(TEXT("Syntax: %s secret output_filename"\ " [entropy_string] [flags]n"),argv[0]); return 1; } //Declare variables DATA_BLOB DataIn; DATA_BLOB DataOut; DATA_BLOB DataEntropy; DWORD dwFlags; LPTSTR pFoo; //Initialize the structure DataOut.pbData=NULL; DataOut.cbData=0; // DataIn.pbData=(LPBYTE)(argv[1]); DataIn.cbData=(lstrlen(argv[1])+1) * sizeof(TCHAR) ; // if ( argc>=4 ) { DataEntropy.pbData=(LPBYTE)(argv[3]); DataEntropy.cbData=(lstrlen(argv[3])+1) * sizeof(TCHAR) ; } // if ( argc==5 ) dwFlags=_tcstoul(argv[4],&pFoo,10); else dwFlags=0; //Protect the secret if ( !CryptProtectData( &DataIn, TEXT("CryptProtectData by Passcape Software"), argc>=4?&DataEntropy:NULL, NULL, NULL, dwFlags, &DataOut) ) { dwFlags=GetLastError(); _tprintf(TEXT("CryptProtectData failed with the following"\ "error code: %lu\n"),dwFlags); exit(1); } //save the output blob FILE *f=NULL; _tfopen_s(&f,argv[2],TEXT("wb")); if ( !f ) { if ( DataOut.pbData ) { LocalFree(DataOut.pbData); DataOut.pbData=NULL; } _tprintf(TEXT("Can't open output file for writing\n")); exit(2); } //write if ( DataOut.pbData ) { size_t written=fwrite(DataOut.pbData,DataOut.cbData,1,f); LocalFree(DataOut.pbData); DataOut.pbData=NULL; if ( written!=1 ) { _tprintf(TEXT("Can't write %lu bytes to output file\n")\ ,DataOut.cbData); exit(3); } } fclose(f); return 0; } Data encryption in DPAPIThe encryption of user's personal data in DPAPI goes in three phases:
CryptProtectData and CryptUnprotectData are basically just wrapper functions, since the actual data encryption runs in the context of the system. The magic part of DPAPI takes place in the LSA, and is hidden from the stranger's eye. What is DPAPI Master KeyA user's Master Key is a binary file that contains encrypted data used for creating the primary encryption key in all DPAPI blobs. Since Master Key encrypts user's confidential data, the key itself requires serious protection. User's password was meaningfully chosen as source data for protecting the Master Key. Master Key structureAs shown on Figure 4, a Master Key file consists of 5 units:
In Windows 2000, instead of the CREDHIST identifier, the system could store the local Master Key backup. That is what made the DPAPI system extremely vulnerable. Windows Password Recovery, for example, can decrypt a Master Key from its local backup and wouldn't even need the user's password to do that. typedef struct _tagMasterKey { DWORD dwVersion; DWORD dwReserved1; DWORD dwReserved2; WCHAR szGuid[0x24]; DWORD dwUnused1; DWORD dwUnused2; DWORD dwPolicy; DWORD dwUserKeySize; DWORD dwLocalEncKeySize; DWORD dwLocalKeySize; DWORD dwDomainKeySize; } MASTERKEY, *PMASTERKEY; DWORD dwVersion
version of the Master Key file used for the purposes of compatibility check. For Windows 2000 this field is set to 1; for other operating systems - to 2.
WCHAR szGuid[0x24]
string value with a unique Master Key identifier, normally matching the file name. Any DPAPI blob stores this identifier, which links it to a specific (and only one) Master Key.
DWORD dwPolicy
field with various flags. For example, if bit 3 of this field is set, the decryption key for the Master Key (from user's password) will be created using the SHA1 algorithm. In Windows 2000, this flag is always cleared; i.e. it uses NTLM hash.
DWORD dwUserKeySize
contains the size of user's Master Key.
DWORD dwLocalEncKeySize
size of backup copy encryption key.
DWORD dwLocalKeySize
size of local backup key or CREDHIST GUID field
DWORD dwDomainKeySize
size of domain's Master Key backup.
The header is followed by a sequence of 4 Master Key slots. Each slot contains its own header and the actual key. The header structure is common for all slots. Here is what it looks like for version 1 (Windows 2000): typedef struct _tagMasterKey1Base { DWORD dwVersion; BYTE pSalt[0x10]; BYTE pKey[]; } MASTERKEY1BASE, *PMASTERKEY1BASE; In other versions of OS, the structure is complemented with new fields: typedef struct _tagMasterKey2Base { DWORD dwVersion; BYTE pSalt[0x10]; DWORD dwPBKDF2IterationCount; ALG_ID HMACAlgId; ALG_ID CryptAlgId; BYTE pKey[]; } MASTERKEY2BASE, *PMASTERKEY2BASE; typedef struct _tagMasterKey3Base { DWORD dwVersion; GUID guidCredhist; } MASTERKEY3BASE, *PMASTERKEY3BASE; Description of header slot fields: DWORD dwVersion
slot version.
BYTE pSalt[0x10]
salt, i.e. set of random data for unifying Master Key and avoiding possible rainbow table attack.
DWORD dwPBKDF2IterationCount
iteration counter in the PBKDF2 key generation algorithm.
ALG_ID HMACAlgId
data integrity check algorithm.
ALG_ID CryptAlgId
slot encryption algorithm identifier.
BYTE pKey[]
encrypted Master Key or slot data.
GUID guidCredhist
unique credentials history file identifier.
The table 2 shows the default Master Key encryption algorithms for various operating systems, as well as the analysis of their protection from a potential brute force attack.
Generating new Master KeyLet's take a closer look at the process of creating and encrypting user's new Master Key:
Regenerating Master KeysIf you open Master Key Storage Folder on your disk, having previously allowed the system to show hidden folders and files (Master Key files have the Hidden attribute), you will likely see several Master Key files. Why several? - Because DPAPI, for safety reasons, forces the creation of a new Master Key approximately every 90 days, so any user account normally has several Master Keys; the number depends on the user profile creation date. typedef struct _tagPreferredMasterKey { GUID guidMasterKey; FILETIME ftCreated; } PREFERREDMASTERKEY, *PPREFERREDMASTERKEY; Every time some DPAPI function is called, the system checks the last Master Key creation date in Preferred and, if finds it older than 90 days, creates a new Master Key, updating Preferred along the way. The Master Key regeneration process can be managed manually. To do that, call the CryptProtectData function with the CRYPTPROTECT_CRED_SYNC flag set. The system will re-encrypt all the Master Keys and update them on the disk. That operation is normally performed after changing user's password. To prevent the optional Master Key updates, Windows 7 introduced a Master Key synchronization mechanism, which we are going to cover below. Backing up Master Keys and restoring them from backupsBacking up Master Keys of a domain PCWhen the computer is a member of a domain, the Master Key file stores the backup of user's Master Key. When creating a Master Key, DPAPI sends a request to the domain controller that stores the RSA encryption private/public key pair. The Master Key is encrypted using the domain's public key and stored as backup in the same file with the user's Master Key. If an error occurs while decrypting the primary copy of the Master Key, DPAPI sends the backup to the domain controller. The domain controller decrypts it using its private RSA key and sends the decrypted Master Key back. Backing up Master Key of a stand-alone PC under Windows XP - Windows 7When it comes to stand-alone computers of home users (Windows XP and higher), DPAPI employs a different recovery mechanism based on password reset disk. Password reset disk, which user can create at any time in the control panel, allows user to recover a forgotten password. The key phrase in the previous sentence is user can. The burden of data recovery rests entirely on user's shoulders. Backing up Master Keys on a stand-alone PC in Windows 2000But the password reset system came only in Windows XP. What was before that? Indeed, despite the fact that Windows 2000 did not have password reset disk, user's Master Key backup was still performed on stand-alone computers. Both the primary Master Key and the Master Key backup were stored in the same! That means that any data encrypted with DPAPI could be decrypted by a potential malefactor WITHOUT knowing user's logon password (by just having physical access to the system). Master Key file structure in Windows 2000Figure 5. Master Key file structure in Windows 2000. Microsoft strongly recommends upgrading Windows 2000 to a more current version. But if you have a good reason for not doing it, we are going to show you how to best protect your good old Win2K from a potential compromising. For that purpose, let's take a look at the local Master Key backup creation algorithm. A Win2K Master Key file conventionally consists of 5 units:
Restoring user's Master Key from a local backup of begins with decoding the local backup encryption key. In the decryption process, we use the local computer account password (oh yes, it does have such an account), which is stored in the LSA secret named DPAPI_SYSTEM. Any user that can access a PC under OS Windows 2000, which is not a domain member, can easily decrypt other user's encrypted DPAPI data, with no need for the logon passwords or even password hash!
To partially prevent that outrage, we must change the operating mode for SYSKEY. Run the SYSKEY utility (press the WIN + R shortcut on the keyboard, then type in syskey.exe and then click OK), replacing the SYSKEY storage location. Now, to decrypt the DPAPI_SYSTEM LSA secret, a potential malefactor would have to either know the SYSKEY bootup password or have the SYSKEY startup disk. Next to the Part 2 of the article >>
CommentsCan't decrypt the dpapi blob from win 7~
posted by Mr Chen at 10:53:42 24.07.2012
Hi , I use the code represented in the paper, that is cryptprotect function, to encrypt some plain text to a dpapi blob in win 7. Then I copy the encypted dpapi blob and the master key to Xp system, using the software to decrypt. And I received error 9, saying that the entropy may be wrongly setted. But I didn't set entropy at all. I don't why the software can't decrypt the dpapi blob in win 7. I had test that it can make it in vista? Passcape_Admin
Re: Can't decrypt the dpapi blob from win 7~
posted by Passcape_Admin at 11:13:34 24.07.2012
No, it's not a mistake. The most interesting thing about DPAPI is that any program can set a salt (entropy) when calling DPAPI encryption function. So without the entropy, you will not be able to decrypt DPAPI blob even if other data is known to you.
For example, Internet Explorer 7 - 9 uses URL address as the entropy when encrypting the site password and saving it to DPAPI blob. So to decode the blob, you'll have to provide the name of the source URL address (ended up with zero character) as entropy parameter. Have a look at the end of the article. It has an example that describes how to decrypt facebook password saved by Internet Explorer. Most probably the blob you're trying to decrypt was encoded with entropy. So the program complains. |