forked from keeweb/kdbxweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKdbx.tcl
167 lines (161 loc) · 7.01 KB
/
Kdbx.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# KDBX file template for HexFiend
# https://github.com/keeweb/kdbxweb/blob/master/format/Kdbx.tcl
# MIT license
#
# Format reference: https://keepass.info/help/kb/kdbx_4.html
# HexFiend templates docs: https://github.com/ridiculousfish/HexFiend/tree/master/templates
little_endian
requires 0 "03D9A29A 67FB4BB5"
set version 0
section "Header" {
uint32 -hex "Magic"
uint32 -hex "Signature"
uint16 "Version minor"
set version [uint16 "Version major"]
if {$version == 3 || $version == 4} {
set field 1
while {![end] && $field != 0} {
set field [uint8]
move -1
if {$field == 0} {
set field_desc "EndOfHeader"
} elseif {$field == 1} {
set field_desc "Comment"
} elseif {$field == 2} {
set field_desc "CipherID"
} elseif {$field == 3} {
set field_desc "CompressionFlags"
} elseif {$field == 4} {
set field_desc "MasterSeed"
} elseif {$field == 5} {
set field_desc "TransformSeed"
} elseif {$field == 6} {
set field_desc "TransformRounds"
} elseif {$field == 7} {
set field_desc "EncryptionIV"
} elseif {$field == 8} {
set field_desc "ProtectedStreamKey"
} elseif {$field == 9} {
set field_desc "StreamStartBytes"
} elseif {$field == 10} {
set field_desc "InnerRandomStreamID"
} elseif {$field == 11} {
set field_desc "KdfParameters"
} elseif {$field == 12} {
set field_desc "PublicCustomData"
} else {
set field_desc "Unknown"
}
section "Header field" {
uint8 "TypeID"
move -1
entry "Type" $field_desc 1
move 1
if {$version < 4} {
set size [uint16 "Data length"]
} else {
set size [uint32 "Data length"]
}
hex $size "Data"
if {$field == 1} {
move -$size
ascii $size "Comment"
} elseif {$field == 2} {
if {$size == 16} {
move -16
uuid "Cipher UUID"
}
} elseif {$field == 3} {
if {$size == 4} {
move -4
set compression_flags [uint32 "Compression algorithm ID"]
move -4
if {$compression_flags == 0} {
entry "Compression algorithm" "None" 4
} elseif {$compression_flags == 1} {
entry "Compression algorithm" "Gzip" 4
}
move 4
}
} elseif {$field == 6} {
if {$size == 8} {
move -8
uint64 "Transform rounds"
}
} elseif {$field == 11} {
section "KDF parameters" {
move -$size
set dict_version [uint16 -hex "Version"]
if {$dict_version == 0x100} {
set param_type 1
while {![end] && $param_type != 0} {
section "Parameter" {
set param_type [uint8 "TypeID"]
if {$param_type == 0} {
set param_desc "End"
} elseif {$param_type == 0x04} {
set param_desc "UInt32"
} elseif {$param_type == 0x05} {
set param_desc "UInt64"
} elseif {$param_type == 0x08} {
set param_desc "Bool"
} elseif {$param_type == 0x0C} {
set param_desc "Int32"
} elseif {$param_type == 0x0D} {
set param_desc "Int64"
} elseif {$param_type == 0x18} {
set param_desc "String"
} elseif {$param_type == 0x42} {
set param_desc "Bytes"
} else {
set param_desc "Unknown"
}
move -1
entry "Type" $param_desc 1
move 1
if {$param_type != 0} {
set key_length [uint32 "Key length"]
ascii $key_length "Key"
set value_length [uint32 "Value length"]
hex $value_length "Value"
if {$param_type == 0x04 && $value_length == 4} {
move -4
uint32 "UInt32"
} elseif {$param_type == 0x05 && $value_length == 8} {
move -8
uint64 "UInt64"
} elseif {$param_type == 0x08 && $value_length == 1} {
move -1
int8 "Bool"
} elseif {$param_type == 0x0C && $value_length == 4} {
move -4
int32 "Int32"
} elseif {$param_type == 0x0D && $value_length == 8} {
move -8
int64 "Int64"
} elseif {$param_type == 0x18} {
move -$value_length
ascii $value_length "String"
}
}
}
}
} else {
move -2
move $size
}
}
}
}
}
} else {
entry "Error" "Unexpected version, supported: 3 or 4"
}
}
if {$version == 3} {
bytes eof "Encrypted data"
} elseif {$version == 4} {
hex 32 "Header SHA256"
hex 32 "Header HMAC"
bytes eof "Encrypted data"
}