-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.go
241 lines (216 loc) · 5.05 KB
/
parser.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package lastpass
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"github.com/mattn/lastpass-go/ecb"
"io"
)
func chunkIdFromBytes(b [4]byte) uint32 {
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
}
func chunkIdFromString(s string) uint32 {
b := []byte(s)
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
}
func readId(r io.Reader) (uint32, error) {
var b [4]byte
_, err := r.Read(b[:])
if err != nil {
return 0, err
}
return chunkIdFromBytes(b), nil
}
func readSize(r io.Reader) (uint32, error) {
var b [4]byte
_, err := r.Read(b[:])
if err != nil {
return 0, err
}
return binary.BigEndian.Uint32(b[:]), nil
}
func readItem(r io.Reader) ([]byte, error) {
size, err := readSize(r)
if err != nil {
return nil, err
}
b := make([]byte, size)
n, err := r.Read(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
func skipItem(r io.Reader) error {
readSize, err := readSize(r)
if err != nil {
return err
}
b := make([]byte, readSize)
_, err = r.Read(b)
if err != nil {
return err
}
return nil
}
func extractChunks(r io.Reader, filter []uint32) (map[uint32][][]byte, error) {
chunks := map[uint32][][]byte{}
for {
chunkId, err := readId(r)
if err != nil {
if err == io.EOF {
break
}
}
payload, err := readItem(r)
if err != nil {
return nil, err
}
found := false
for _, filterId := range filter {
if filterId == chunkId {
found = true
break
}
}
if !found {
continue
}
if _, ok := chunks[chunkId]; !ok {
chunks[chunkId] = [][]byte{payload}
} else {
chunks[chunkId] = append(chunks[chunkId], payload)
}
}
return chunks, nil
}
func parseAccount(r io.Reader, encryptionKey []byte) (*Account, error) {
id, err := readItem(r)
if err != nil {
return nil, err
}
name, err := readItem(r)
if err != nil {
return nil, err
}
group, err := readItem(r)
if err != nil {
return nil, err
}
url, err := readItem(r)
if err != nil {
return nil, err
}
notes, err := readItem(r)
if err != nil {
return nil, err
}
for i := 0; i < 2; i++ {
skipItem(r)
}
username, err := readItem(r)
if err != nil {
return nil, err
}
password, err := readItem(r)
if err != nil {
return nil, err
}
return &Account{
string(id),
string(decryptAES256(name, encryptionKey)),
string(decryptAES256(username, encryptionKey)),
string(decryptAES256(password, encryptionKey)),
string(decodeHex(url)),
string(decryptAES256(group, encryptionKey)),
string(decryptAES256(notes, encryptionKey))}, nil
}
func encodeHex(b []byte) []byte {
d := make([]byte, len(b)*2)
n := hex.Encode(d, b)
return d[:n]
}
func decodeHex(b []byte) []byte {
d := make([]byte, len(b))
n, _ := hex.Decode(d, b)
return d[:n]
}
func decodeBase64(b []byte) []byte {
d := make([]byte, len(b))
n, _ := base64.StdEncoding.Decode(d, b)
return d[:n]
}
func pkcs7Pad(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padtext...)
}
func pkcs7Unpad(data []byte) []byte {
size := len(data)
unpadding := int(data[size-1])
return data[:(size - unpadding)]
}
func decrypt_aes256_cbc_plain(data []byte, encryptionKey []byte) []byte {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
panic(err.Error())
}
iv, in := data[:aes.BlockSize], data[aes.BlockSize:]
dec := cipher.NewCBCDecrypter(block, iv)
out := make([]byte, len(in))
dec.CryptBlocks(out, in)
return pkcs7Unpad(out)
}
func decrypt_aes256_cbc_base64(data []byte, encryptionKey []byte) []byte {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
panic(err.Error())
}
iv, in := decodeBase64(data[:24]), decodeBase64(data[24:])
dec := cipher.NewCBCDecrypter(block, iv)
out := make([]byte, len(in))
dec.CryptBlocks(out, in)
return pkcs7Unpad(out)
}
func decrypt_aes256_ecb_plain(data []byte, encryptionKey []byte) []byte {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
panic(err.Error())
}
dec := ecb.NewECBDecrypter(block)
out := make([]byte, len(data))
dec.CryptBlocks(out, data)
return pkcs7Unpad(out)
}
func decrypt_aes256_ecb_base64(data []byte, encryptionKey []byte) []byte {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
panic(err.Error())
}
data = decodeBase64(data)
dec := ecb.NewECBDecrypter(block)
out := make([]byte, len(data))
dec.CryptBlocks(out, data)
return pkcs7Unpad(out)
}
func decryptAES256(data []byte, encryptionKey []byte) string {
size := len(data)
size16 := size % 16
size64 := size % 64
switch {
case size == 0:
return ""
case size16 == 0:
return string(decrypt_aes256_ecb_plain(data, encryptionKey))
case size64 == 0 || size64 == 24 || size64 == 44:
return string(decrypt_aes256_ecb_base64(data, encryptionKey))
case size16 == 1:
return string(decrypt_aes256_cbc_plain(data[1:], encryptionKey))
case size64 == 6 || size64 == 26 || size64 == 50:
return string(decrypt_aes256_cbc_base64(data, encryptionKey))
}
panic("Input doesn't seem to be AES-256 encrypted")
}