KeePass源代碼分析 3----KeePass 文件格式
KeePass文件格式由文件頭部和數據部分組成。由於文件存儲涉及到密碼安全方面的東西,所以硬盤存儲的是 KeePass 文件經過加密後的文件數據。
總體上KeePass 的文件格式組織方式如下:
KeePass文件格式開始部分是頭部信息,緊跟着是分組信息,最後纔是關於密碼的信息。首先我們分析 KeePass 的頭部信息結構 PW_DBHEADER , PW_DBHEADER 在文件PwStructs.h 中定義。
/// Database header structure. All KeePass KDB files begin with this structure.
typedef struct _PW_DBHEADER
{
DWORD dwSignature1 ; ///< File identifier word, set to PWM_DBSIG_1.
DWORD dwSignature2 ; ///< File identifier word, set to PWM_DBSIG_2.
DWORD dwFlags ;
DWORD dwVersion ;
BYTE aMasterSeed [16]; ///< Seed that gets hashed with the user key to form the final key.
UINT8 aEncryptionIV [16]; ///< IV used for content encryption.
DWORD dwGroups ; ///< Number of groups in the database.
DWORD dwEntries ; ///< Number of entries in the database.
BYTE aContentsHash [32]; ///< SHA-256 hash of the database, used for integrity checking.
BYTE aMasterSeed2 [32]; ///< Seed used for the dwKeyEncRounds AES transformations.
DWORD dwKeyEncRounds ; ///< Number of master key transformations.
} PW_DBHEADER , * PPW_DBHEADER ;
頭部結構信息開始的8 個字節分別是 dwSignature1 和 dwSignature 2,也就是 KeePass 文件格式的簽名信息,其值分別等於 PWM_DBSIG_1 和 PWM_DBSIG_ 2 , PWM_DBSIG_1 和 PWM_DBSIG_ 2 在KeePass 中定義如下 :
#define PWM_DBSIG_1 0x9AA2D903
#define PWM_DBSIG_2 0xB54BFB65
它們的作用僅僅是作爲識別KeePass 文件格式而已,沒有特殊的意義。
dwFlags:標示對 KeePass 文件的數據部分(除文件頭部信息)使用的加密算法,目前可用的加密算法有 AES 和 TWOFISH 算法。
dwVersion :標示KeePass 文件的版本號,可用於實現版本兼容性。
aMasterSeed :16 位的主密鑰。是隨機生成的,也就是沒有 KeePass 的數據庫文件所使用的 aMasterSeed 都不同。用於對用戶密 鑰進行加密。
aEncryptionIV :16 位密鑰。也是隨機生成的,用於對文件的數據部分進行加密。
dwGroups :標示本數據庫文件中分組的條數。
dwEntries :標示本數據庫文件中密碼的條數。
aContentsHash :標示本數據庫文件中數據部分的hash 值, KeePass 使用它進行完整性檢查。
aMasterSeed2 :16 位次密鑰。隨機生成的。用途同 aMasterSeed 。
dwKeyEncRounds :標示用戶密鑰的加密次數。
KeePass的分組數據部分 是由 多個 PW_GROUP 依次存儲。類似數組的存儲結構。 PW_GROUP 在pwstructs.h 中定義,
/// Group structure, containing information about one group.
typedef struct _PW_GROUP
{
DWORD uGroupId ; ///< ID of the group. The ID is a unique identifier in one database.
DWORD uImageId ; ///< Index of the icon in the image list to use for this group.
TCHAR * pszGroupName ; ///< Name of the group.
PW_TIME tCreation ; ///< Time when the group was created.
PW_TIME tLastMod ; ///< Time when the group was last modified.
PW_TIME tLastAccess ; ///< Time when the group was last accessed.
PW_TIME tExpire ; ///< Time when the group will expire.
USHORT usLevel ; ///< Indentation/depth level in the group tree.
#ifdef VPF_ALIGN
USHORT usDummy;
#endif
DWORD dwFlags ; ///< Used by KeePass internally, don't use (set to 0 for a new structure).
} PW_GROUP , * PPW_GROUP ;
由此可見,PW_GROUP 存儲一下信息: (1) 分組 Id ,( 2 )圖像 Id ( 3 )組名( 4 )創建日期( 5 )最後修改日期( 6 )最後訪問日期( 7 )樹狀結構中所處的層次數( 8 )標誌位。
KeePass文件的最後部分纔是密碼信息,其存儲方式和組數據一樣。也是依次存儲。 PW_ENTRY 在pwstructs.h 中定義:
/// Entry structure, containing information about one entry.
typedef struct _PW_ENTRY
{
BYTE uuid [16]; ///< Unique GUID identifying this entry (not only in this database).
DWORD uGroupId ; ///< ID of the group that contains this entry.
DWORD uImageId ; ///< Index of the icon in the image list to use fo r this entry.
TCHAR * pszTitle ; ///< Title.
TCHAR * pszURL ; ///< URL.
TCHAR * ; ///< User name.
DWORD uPasswordLen ; ///< Length of the password (required for memory protection).
TCHAR * pszPassword ; ///< Password (may be encrypted, use IKpDatabase::UnlockEntryPassword to decrypt).
TCHAR * pszAdditional ; ///< Notes.
PW_TIME tCreation ; ///< Time when the entry was created.
PW_TIME tLastMod ; ///< Time when the entry was last modified.
PW_TIME tLastAccess ; ///< Time when the entry was last accessed.
PW_TIME tExpire ; ///< Time when the entry will expire.
TCHAR * pszBinaryDesc ; ///< A string describing the contents of pBinaryData.
BYTE * pBinaryData ; ///< Attachment data (of length uBinaryDataLen), may be NULL.
DWORD uBinaryDataLen ; ///< Length of the attachment data in bytes.
} PW_ENTRY , * PPW_ENTRY ;
值得一提的是,KeePass 字符串都是以 UTF-8 格式存儲的。讀取的時候要進行相關的轉換才能使用。