KeePass源代码分析3

来源:互联网 发布:启动数据库归档模式 编辑:程序博客网 时间:2024/05/22 00:30

 

 

KeePass源代码分析3----KeePass文件格式

 

 

 


 

KeePass文件格式由文件头部和数据部分组成。由于文件存储涉及到密码安全方面的东西,所以硬盘存储的是KeePass文件经过加密后的文件数据。

总体上KeePass的文件格式组织方式如下:

       

KeePass文件格式开始部分是头部信息,紧跟着是分组信息,最后才是关于密码的信息。首先我们分析KeePass的头部信息结构PW_DBHEADERPW_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个字节分别是dwSignature1dwSignature2,也就是KeePass文件格式的签名信息,其值分别等于PWM_DBSIG_1PWM_DBSIG_2PWM_DBSIG_1PWM_DBSIG_2KeePass中定义如下


   #define PWM_DBSIG_1      0x9AA2D903

   #define PWM_DBSIG_2      0xB54BFB65


它们的作用仅仅是作为识别KeePass文件格式而已,没有特殊的意义。

dwFlags:标示对KeePass文件的数据部分(除文件头部信息)使用的加密算法,目前可用的加密算法有AESTWOFISH算法。

dwVersion:标示KeePass文件的版本号,可用于实现版本兼容性。

aMasterSeed16位的主密钥。是随机生成的,也就是没有KeePass的数据库文件所使用的aMasterSeed都不同。用于对用户密 钥进行加密。

aEncryptionIV16位密钥。也是随机生成的,用于对文件的数据部分进行加密。

dwGroups:标示本数据库文件中分组的条数。

dwEntries:标示本数据库文件中密码的条数。

aContentsHash:标示本数据库文件中数据部分的hash值,KeePass使用它进行完整性检查。

aMasterSeed216位次密钥。随机生成的。用途同aMasterSeed

dwKeyEncRounds:标示用户密钥的加密次数。

 

KeePass的分组数据部分是由多个PW_GROUP依次存储。类似数组的存储结构。PW_GROUPpwstructs.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)图像Id3)组名(4)创建日期(5)最后修改日期(6)最后访问日期(7)树状结构中所处的层次数(8)标志位。

 

KeePass文件的最后部分才是密码信息,其存储方式和组数据一样。也是依次存储。PW_ENTRYpwstructs.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 for 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格式存储的。读取的时候要进行相关的转换才能使用。

原创粉丝点击