-
Notifications
You must be signed in to change notification settings - Fork 8
/
crypt12-decrypt.go
163 lines (138 loc) · 3.92 KB
/
crypt12-decrypt.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
/*
Author: Costin Canciu
Entrypoint: crypt12-decrypt.go
Description: Decrypt Whatsapp .crypt12 database files
*/
package main
import (
"bytes"
"compress/zlib"
"crypto/aes"
"crypto/cipher"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
func check_error(err error) {
if err != nil {
panic(err)
}
}
func exit(message string) {
fmt.Printf(message)
os.Exit(2)
}
func get_files_arguments() (string, string, string) {
var keyFile, crypt12File, outputFile string
flag.StringVar(&keyFile, "keyfile", "key", "decryption key file path")
flag.StringVar(&crypt12File, "crypt12file", "msgstore.db.crypt12", "crypt12 file path")
flag.StringVar(&outputFile, "outputfile", "msgstore.db", "decrypted output file path")
flag.Parse()
return keyFile, crypt12File, outputFile
}
func fileExists(fileName string) (bool, int64) {
info, err := os.Stat(fileName)
if os.IsNotExist(err) {
return false, 0
}
return !info.IsDir(), info.Size()
}
func get_files_size(keyFile, cryptFile string) (int64, int64) {
keyFileExist, keySize := fileExists(keyFile)
if !keyFileExist {
exit("Key file does not exist.")
}
if keySize != 158 {
exit("Key file is invalid. It should be 158 byte size.")
}
cryptFileExist, cryptSize := fileExists(cryptFile)
if !cryptFileExist {
exit("Crypt12 file does not exist.")
}
return keySize, cryptSize
}
func read_key(keyFile string) ([]byte, []byte) {
fileInput, fileError := os.Open(keyFile)
defer fileInput.Close()
check_error(fileError)
keyBytes := make([]byte, 158)
_, readError := fileInput.Read(keyBytes)
check_error(readError)
t1 := make([]byte, 32)
copy(t1[:], keyBytes[30:62])
key := make([]byte, 32)
copy(key[:], keyBytes[126:158])
return key, t1
}
func read_crypt12_file(crypt12File string, crypt12Size int64) ([]byte, []byte, []byte) {
fileInput, fileError := os.Open(crypt12File)
defer fileInput.Close()
check_error(fileError)
crypt12Bytes := make([]byte, crypt12Size)
_, readError := fileInput.Read(crypt12Bytes)
check_error(readError)
t2 := make([]byte, 32)
copy(t2[:], crypt12Bytes[3:35])
IV := make([]byte, 16)
copy(IV[:], crypt12Bytes[51:67])
cipherText := make([]byte, crypt12Size-67-20)
copy(cipherText[:], crypt12Bytes[67:crypt12Size-20])
return cipherText, IV, t2
}
func validate_header(t1, t2 []byte) {
if string(t1) != string(t2) {
exit("Key file mismatch or crypt12 file is corrupt.")
}
}
func crypt12_decrypt(key, IV, cipherText []byte) []byte {
block, cipherError := aes.NewCipher(key)
check_error(cipherError)
aesGCM, gcmError := cipher.NewGCMWithNonceSize(block, 16)
check_error(gcmError)
plaintext, encryptError := aesGCM.Open(nil, IV, cipherText, nil)
check_error(encryptError)
return plaintext
}
func decompress(compressedBytes []byte) []byte {
bytesReader := bytes.NewReader(compressedBytes)
zlibReader, zlibError := zlib.NewReader(bytesReader)
check_error(zlibError)
result, inflateError := ioutil.ReadAll(zlibReader)
check_error(inflateError)
return result
}
func write_output_file(outputFile string, resultBytes []byte) {
file, fileError := os.OpenFile(
outputFile,
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
0666,
)
defer file.Close()
check_error(fileError)
_, writeError := file.Write(resultBytes)
check_error(writeError)
}
func validate_sqlite_file(resultBytes []byte) {
if strings.ToLower(string(resultBytes[:6])) != "sqlite" {
exit("Decryption failed. SQLite file format is corrupt.")
}
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Decryption failed. Error: %v \n", r)
}
}()
keyFile, crypt12File, outputFile := get_files_arguments()
_, crypt12Size := get_files_size(keyFile, crypt12File)
key, t1 := read_key(keyFile)
cipherText, IV, t2 := read_crypt12_file(crypt12File, crypt12Size)
validate_header(t1, t2)
plainText := crypt12_decrypt(key, IV, cipherText)
resultBytes := decompress(plainText)
validate_sqlite_file(resultBytes)
write_output_file(outputFile, resultBytes)
fmt.Printf("Decryption successful: %s", outputFile)
}