Preface:
All the Winntxil series of OS implements EFS (Encrypt File System), which describes the workflow of EFS based on (Win2000) in detail.
WinXP SP1 and its WIN2003 support AES (256) 3DES, DESX encryption algorithm
note
[Previous] [Next]
Encrypting File System Security
EFS security relies on Windows 2000 cryptography support, which Microsoft introduced in Windows NT 4. The first time a file is encrypted, EFS assigns the account of the user performing the encryption a private / public key pair for use in file encryption. Users can encrypt files via Windows Explorer by opening a file's Properties dialog box, pressing Advanced, and selecting the Encrypt Contents To Secure Data option, as shown in Figure 12-44. Users can also encrypt files via a command-line utility named cipher. Windows 2000 automatically encrypts files that reside in directories that are designated as encrypted directories. When a file is encrypted, EFS generates a random number for the file that EFS calls the file's file encryption key (FEK). EFS uses the FEK to encrypt the file's contents with a Stronger Variant of The Data Encryption Standard (des) Algorithm-Desx. EFS Stores The File's Fek with The File But Encrypts The File with The user's EFS PUBLIC Key by Using The RSA Public Key-B . Ased encryption algorithm After EFS completes these steps, the file is secure: other users can not decrypt the data without the file's decrypted FEK, and they can not decrypt the FEK without the private key.
Figure 12-44 Encrypt Files by Using The Advanced Attributes Dialog Box
EFS uses a private / public key algorithm to encrypt FEKs. To encrypt file data, EFS uses DESX because DESX is a symmetric encryption algorithm, which means it uses the same key to encrypt and decrypt data. Symmetric encryption algorithms are typically very fast, which makes them suitable for encrypting large amounts of data, such as file data However, symmetric encryption algorithms have a weakness:. you can bypass their security if you obtain the key If multiple users want to share one encrypted file protected only by DESX, each. user would require access to the file's FEK. Leaving the FEK unencrypted would obviously be a security problem, but encrypting the FEK once would require all the users to share the same FEK decryption key-another potential security problem.
Keeping the FEK secure is a difficult problem, which EFS addresses with the public key-based half of its encryption architecture. Encrypting a file's FEK for individual users who access the file lets multiple users share an encrypted file. EFS can encrypt a file's FEK with each user's public key and can store each user's encrypted FEK with the file. Anyone can access a user's public key, but no one can use a public key to decrypt the data that the public key encrypted. The only way users can decrypt a file is with their private key, which the operating system must access and typically stores in a secure location. A user's private key decrypts the user's encrypted copy of a file's FEK. Windows 2000 stores private keys on a computer's hard disk (which is not terribly secure ), but subsequent releases of the operating system will let users store their private key on portable media such as smart cards. Public key-based algorithms are usually slow, but EFS uses these algorithms only to encryp t FEKs. Splitting key management between a publicly available key and a private key makes key management a little easier than symmetric encryption algorithms do and solves the dilemma of keeping the FEK secure.
Several components work together to make EFS work, as the diagram of EFS architecture in Figure 12-45 shows. As you can see, EFS is implemented as a device driver that runs in kernel mode and is tightly connected with the NTFS file system driver. Whenever NTFS encounters an encrypted file, NTFS executes functions in the EFS driver that the EFS driver registered with NTFS when EFS initialized. The EFS functions encrypt and decrypt file data as applications access encrypted files. Although EFS stores an FEK with a file's data, users The File Data, ENCRYPT or Decrypt File's Fek with The Aid of Cryptography Services That Reside In User Mode.figure 12-45 EFS Architecture
The Local Security Authority Subsystem. (Lsass - /Winnt/System32/Lsass.exe) manages logon sessions but also handles EFS key management chores For example, when the EFS driver needs to decrypt an FEK in order to decrypt file data a user wants to access, the EFS driver sends a request to Lsass. EFS sends the request via a local procedure call (LPC). The KSecDD (/Winnt/System32/Drivers/Ksecdd.sys) device driver exports functions for other drivers that need to send LPC messages to Lsass The Local Security Authority Server. (Lsasrv - /Winnt/System32/Lsasrv.dll) component of Lsass that listens for remote procedure call (RPC) requests passes requests to decrypt an FEK to the appropriate EFS-related decryption function, which Also Resides in Lsasrv. Lsasrv Uses Functions in Microsoft's CryptoAPI (Also Referred To As Capi) To Decrypt The Fek, Which The EFS DRIVER SENT TO LSASS IN Encrypted Form.
CryptoAPI comprises cryptographic service provider (CSP) DLLs that make various cryptography services (such as encryption / decryption and hashing) available to applications. The CSP DLLs manage retrieval of user private and public keys, for example, so that Lsasrv does not need to concern itself with the details of how keys are protected or even with the details of the encryption algorithms. After Lsasrv decrypts an FEK, Lsasrv returns the FEK to the EFS driver via an LPC reply message. After EFS receives the decrypted FEK, EFS can use Desx to Decrypt The File Data for NTFS. Let's Look At The Details of How Efs Integrates with ntfs and how lsasrv users.registering callbacks
NTFS does not require the EFS driver's (Winnt / System32 / Drivers / Efs.sys) presence to execute, but encrypted files will not be accessible if the EFS driver is not present. NTFS has a plug-in interface for the EFS driver, so when the EFS driver initializes, it can attach itself to NTFS. The NTFS driver exports several functions for the EFS driver to use, including one that EFS calls to notify NTFS both of the presence of EFS and of the EFS-related APIs EFS is MAKING AVAILABLE.
ENCRYPTING A File for The First Time
The NTFS driver calls only the EFS functions that register when NTFS encounters an encrypted file. A file's attributes record that the file is encrypted in the same way that a file records that it is compressed (discussed earlier in this chapter). NTFS and EFS have specific interfaces for converting a file from nonencrypted to encrypted form, but user-mode components primarily drive the process Windows 2000 lets you encrypt a file in two ways:. by using the cipher command-line utility or by checking the Encrypt Contents to Secure Data box in the Advanced attibutes dialog box for a file in Windows Explorer. Both Windows Explorer and the cipher command rely on the EncryptFile Win32 API that the Advapi32.dll (Advanced Win32 APIs DLL) exports. Advapi32 loads another DLL, Feclient.dll (File Encryption Client DLL) impersonation facility to impersonate the user that ran the application (either cipher or Windows Explorer) that is encrypting the file. This procedure lets Windows 2000 treat the file operations that Lsasrv performs as if the user who wants to encrypt the file is performing them. Lsasrv Usually Runs in the system account. (The System Account IS Described in Chapter ?.) in Fact, IF IT Doesn't Impersonate The User, Lsasrv Usually Won't have questions.
Lsasrv next creates a log file in the volume's System Volume Information directory into which Lsasrv records the progress of the encryption process. The log file usually has the name efs0.log, but if other files are undergoing encryption, increasing numbers replace the 0 until a unique log file name for the current encryption is created.CryptoAPI relies on information that a user's registry profile stores, so Lsasrv next uses the LoadUserProfile API function of Userenv.dll (User Environment DLL) to load the profile into the registry of the user it is impersonating. Typically, the user profile is already loaded, because Winlogon loads a user's profile when a user logs on. However, if a user uses the Windows 2000 RunAs command to log on to a different account, when you try to access encrypted files From That Account, The Account's Profile Might Not Load.
LSASRV THENERATES An Fek for the File By Using The RSA Encryption Facilities of The Microsoft Base Cryptographic Province 1.0 CSP.
Constructing key rings
At this point, Lsasrv has an FEK and can construct EFS information to store with the file, including an encrypted version of the FEK. Lsasrv reads the HKEY_CURRENT_USER / Software / Microsoft / Windows NT / CurrentVersion / EFS / CurrentKeys / CertificateHash value of the user performing the encryption to obtain the user's public key signature. (Note that this key does not appear in the registry until a file or folder is encrypted.) Lsasrv uses the signature to access the user's public key and encrypt FEKs.
Lsasrv can now construct the information that EFS stores with the file. EFS stores only one block of information in an encrypted file, and that block contains an entry for each user sharing the file. These entries are called key entries, and EFS stores them in the Data Decryption Field (DDF) portion of the file's EFS data. A collection of multiple key entries is called a key ring, because, as mentioned earlier, EFS lets multiple users share encrypted files.Figure 12-46 shows a file's EFS information format and key entry format. EFS stores enough information in the first part of a key entry to precisely describe a user's public key. This data includes the user's security ID (SID), the container name in which the key is stored, the cryptographic provider name , and the private / public keypair certificate hash. The second part of the key entry contains an encrypted version of the FEK. Lsasrv uses the CryptoAPI to encrypt the FEK with the RSA algorithm and the user's public key.
Figure 12-46 Format of EFS Information and Key Entries
Next, Lsasrv creates another key ring that contains recovery key entries. EFS stores information about recovery key entries in a file's Data Recovery Field (DRF). The format of DRF entries is identical to the format of DDF entries. The DRF's purpose is to let designated accounts, or Recovery Agents, decrypt a user's file when administrative authority must have access to the user's data. For example, suppose a company employee used a CryptoAPI that let him store his private key on a smart card, and then he lost the card . WITHOUT RECOVERY AGENTS, No One Could Recover His Encrypted Data.
Recovery Agents are defined with the Encrypted Data Recovery Agents security policy of the local computer or domain. This policy is available from the Group Policy MMC snap-in, as shown in Figure 12-47. When you use the Recovery Agent Wizard (by right -clicking on Encrypted Data Recovery Agents and selecting Encrypted Recovery Agent from the New option), you can add Recovery Agents and specify which private / public key pairs (designated by their certificates) the Recovery Agents use for EFS recovery. Lsasrv interprets the recovery policy when it initializes and when it receives notification that the recovery policy has changed. EFS creates a DRF key entry for each Recovery Agent by using the cryptographic provider registered for EFS recovery. The default Recovery Agent provider is the RSA encryption facility of Base Cryptographic Provider 1.0 -The Same Provider Lsasrv Uses for User Keys.figure 12-47 Encrypted Data Recovery Agents Group Policy
In the final step in creating EFS information for a file, Lsasrv calculates a checksum for the DDF and DRF by using the MD5 hash facility of Base Cryptographic Provider 1.0. Lsasrv stores the checksum's result in the EFS information header. EFS references this checksum during decryption TO Ensure That The Contents of a File's EFS Information Haven't Become Corrupted or Been Tampered with.
Encrypting File Data
Figure 12-48 illustrates the flow of the encryption process. After Lsasrv constructs the necessary information for a file a user wants to encrypt, it can begin encrypting the file. Lsasrv creates a backup file, Efs0.tmp, for the file undergoing encryption. (Lsasrv uses higher numbers in the backup filename if other backup files exist.) Lsasrv creates the backup file in the directory that contains the file undergoing encryption. Lsasrv applies a restrictive security descriptor to the backup file so that only the System account can access the file's contents. Lsasrv next initializes the log file that it created in the first phase of the encryption process. Finally, Lsasrv records in the log file that the backup file has been created. Lsasrv encrypts the original file only after the file is completely backed up .Lsasrv next Sends The EFS Device Driver, Through NTFS, A Command To Add to the Original File The EFS Information That It Just Created. NTFS Receives this Command, But Because NTFS does not understand EFS commands, NTFS calls the EFS driver. The EFS driver takes the EFS information that Lsasrv sent and uses exported NTFS functions to apply the information to the file. The exported NTFS functions let EFS add the $ LOGGED_UTILITY_STREAM attribute to NTFS file . Execution returns to Lsasrv, which copies the contents of the file undergoing encryption to the backup file. When the backup copy is complete, including backups of all alternate data streams, Lsasrv records in the log file that the backup file is up to date. .
Figure 12-48 Flow of EFS
When NTFS receives the EFS command to encrypt the file, NTFS deletes the contents of the original file and copies the backup data to the file. After NTFS copies each section of the file, NTFS flushes the section's data from the file system cache, which prompts the cache manager to tell NTFS to write the file's data to disk. Because the file is marked as encrypted, at this point in the file-writing process, NTFS calls EFS to encrypt the data before NTFS writes the data to disk. EFS uses the unencrypted FEK that NTFS passes it to perform DESX encryption of the file, one sector (512 bytes) at a time.On Windows 2000 versions approved for export outside the United States, the EFS driver implements a 56-bit key DESX encryption. for the US-ONLY VERSION OF Windows 2000, The Key IS 128 BITS LONG.
After EFS encrypts the file, Lsasrv records in the log file that the encryption was successful and deletes the file's backup copy. Finally, Lsasrv deletes the log file and returns control to the application that requested the file's encryption.
Encryption Process Summary
The Following List Summarizes The Steps EFS Performs To Encrypt A File:
The user profile is loaded if necessary. A log file is created in the System Volume Information directory with the name Efsx.log, where x is a unique number (for example, Efs0.log). As subsequent steps are performed, records are written to the log so that the file can be recovered in case the system fails during the encryption process. Base Cryptographic Provider 1.0 generates a random 128-bit FEK for the file. A user EFS private / public key pair is generated or obtained. HKEY_CURRENT_USER / Software / Microsoft / Windows NT / CurrentVersion / EFS / CurrentKeys / CertificateHash identifies the user's key pair. A DDF key ring is created for the file that has an entry for the user. The entry contains a copy of the FEK that has been encrypted with the user's EFS public key. A DRF key ring is created for the file. It has an entry for each Recovery Agent on the system, with each entry containing a copy of the FEK encrypted with the agent's EFS public key. A backup file with a Name in the form Efs0.tmp is created in the same directory as the file to be encrypted. The DDF and DRF key rings are added to a header and augment the file as its EFS attribute. The backup file is marked encrypted, and the original file is copied to the backup. The original file's contents are destroyed, and the backup is copied to the original. This copy operation results in the data in the original file being encrypted because the file is now marked as encrypted. The backup file is deleted. The log file IS deleded. The user profile is unloaded (if it is loaded in step 1).
If the system crashes during the encryption process, either the original file remains intact or the backup file contains a consistent copy. When Lsasrv initializes after a system crash, it looks for log files under the System Volume Information subdirectory on each NTFS volume on the system .. If Lsasrv finds one or more log files, it examines their contents and determines how recovery should take place Lsasrv deletes the log file and the corresponding backup file if the original file was not modified at the time of the crash; otherwise, Lsasrv copies the backup file over the original, partially encrypted file and then deletes the log and backup. After Lsasrv processes log files, the file system will be in a consistent state with respect to encryption, with no loss of user data.The Decryption Process
The decryption process begins when a user opens an encrypted file. NTFS examines the file's attributes when opening the file and then executes a callback function in the EFS driver. The EFS driver reads the $ LOGGED_UTILITY_STREAM attribute associated with the encrypted file. To read the attribute , the driver calls EFS support functions that NTFS exports for EFS's use. NTFS completes the necessary steps to open the file. The EFS driver ensures that the user opening the file has access privileges to the file's encrypted data (that is, that an encrypted FEK in either the DDF or DRF key rings corresponds to a private / public key pair associated with the user). As EFS performs this validation, EFS obtains the file's decrypted FEK to use in subsequent data operations the user might perform on the file.
EFS can not decrypt an FEK and relies on Lsasrv (which can use CryptoAPI) to perform FEK decryption. EFS sends an LPC message by way of the Ksecdd.sys driver to Lsasrv that asks Lsasrv to obtain the decrypted form of the encrypted FEK in the $ LOGGED_UTILITY_STREAM attribute data (the EFS data) that corresponds to the user who is opening the file.When Lsasrv receives the LPC message, Lsasrv executes the Userenv.dll (User Environment DLL) LoadUserProfile API function to bring the user's profile into the registry , if the profile is not already loaded. Lsasrv proceeds through each key field in the EFS data, using the user's private key to try to decrypt each FEK. For each key, Lsasrv attempts to decrypt a DDF or DRF key entry's FEK. If THE CERTIFICATE HASH IN A Key Field Doesn't Refer to a Key The User Owns, Lsasrv Moves on To The Next Key Field. IF Lsasrv Can't Decrypt Any Ddf or Drf Key Field's Fek, The User Can't Obtain The File's Fek , EFS Denies Access To the Application OPE Ning The File. However, IF Lsasrv Identifies A Hash AS Corresponding to a KEY The User Owns, IT Decrypts The Fek with The User's Private Key USING CRYPTOAPI.
Because Lsasrv processes both DDF and DRF key rings when decrypting an FEK, it automatically performs file recovery operations. If a Recovery Agent that is not registered to access an encrypted file (that is, it does not have a corresponding field in the DDF KEY RING) TRIES TO Access A File, EFS WILL Let The Recovery Agent Gain Access Because The Agent Has Access To a Key Pair for a KEY Field in The DRF Key Ring.
Decrypted fek caching
Traveling the path from the EFS driver to Lsasrv and back can take a relatively long time-in the process of decrypting an FEK, CryptoAPI uses results in more than 2000 registry API calls and 400 file system accesses on a typical system. The EFS driver, With the Aid of NTFS, Uses a cache to try to avoid this expense.decrypting file data
After an application opens an encrypted file, the application can read from and write to the file. NTFS calls the EFS driver to decrypt file data as NTFS reads the data from the disk, and before NTFS places the data in the file system cache. Similarly , when an application writes data to a file, the data remains in unencrypted form in the file system cache until the application or the cache manager uses NTFS to flush the data back to disk. When an encrypted file's data writes back from the cache to the Disk, NTFS Calls The EFS Driver To Encrypt The Data.
As stated earlier, the EFS driver performs encryption and decryption in 512-byte units. The 512-byte size is the most convenient for the driver because disk reads and writes occur in multiples of the 512-byte sector.
Backing Up Encrypted Files
An important aspect of any file encryption facility's design is that file data is never available in unencrypted form except to applications that access the file via the encryption facility. This restriction particularly affects backup utilities, in which archival media store files. EFS addresses this problem by providing a facility for backup utilities so that the utilities can back up and restore files in their encrypted states. Thus, backup utilities do not have to be able to decrypt file data, nor do they need to decrypt file data in their backup procedures.
Backup utilities use the new EFS API functions OpenEncryptedFileRaw, ReadEncryptedFileRaw, WriteEncryptedFileRaw, and CloseEncryptedFileRaw in Windows 2000 to access a file's encrypted contents. The Advapi32.dll library provides these API functions, which all use LPCs to invoke corresponding functions in Lsasrv. For example, after a backup utility opens a file for raw access during a backup operation, the utility calls ReadEncryptedFileRaw to obtain the file data. The Lsasrv function EfsReadFileRaw issues control commands (which the EFS session key encrypts with DESX) to the NTFS driver to read the file's EFS attribute first and then the encrypted contents.EfsReadFileRaw might have to perform multiple read operations to read a large file. As EfsReadFileRaw reads each portion of such a file, Lsasrv sends an RPC message to Advapi32.dll that executes a callback function that the backup Program Specified When It Issued The ReadencryptedFileRaw API Function. EfsreadFileRaw Hands The Encrypt ed data it just read to the callback function, which can write the data to the backup media. Backup utilities restore encrypted files in a similar manner. The utilities call the WriteEncryptedFileRaw API function, which invokes a callback function in the backup program to obtain the Unencrypted Data from The Backup Media While Lsasrv's EfswriteFileRaw Function Is Restoring The File's Contents.
Experiment
Viewing EFS Information
EFS has a handful of other API functions that applications can use to manipulate encrypted files. For example, applications use the AddUsersToEncryptedFile API function to give additional users access to an encrypted file and RemoveUsersFromEncryptedFile to revoke users' access to an encrypted file. Applications use the QueryUsersOnEncryptedFile function to obtain information about a file's associated DDF and DRF key fields. QueryUsersOnEncryptedFile returns the SID, certificate hash value, and display information that each DDF and DRF key field contains. The following output is from the EFSDump utility, included on the companion CD under /Sysint/Efsdump.exe, when an encrypted file is specified as a command-line argument: C: /> efsdump furnace est.txt EFS camp nformation Liao Yi umper 1.02 Copyright C) 999 Tai Lian ussinovich Systems ark camp nternals?? ? 爃 TTP: //www.sysinternals.com? Test.txt: DDF 燛 NTRY: Yunkue Susancomp / Joe: Yun Yao Shim N = Joe, L = EFS, OU = EFS i 焖 燛 Ncryption Hot Ertificate DRF 燛NTRY: Su Susancomp / Administrator Show 笺 燨 u = EFS 焖 燛 ncryptio N hot Ertificate, 燣 = EFS, hot n = administrator
You can see That The File Test.txt HAS One DDF Entry for User Joe and One DRF Entry for Administrator, Which is The Only Recovery Agent Currently Registered on The System.