-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.go
171 lines (144 loc) · 3.45 KB
/
main.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
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/Rhymen/go-whatsapp/crypto/cbc"
"github.com/Rhymen/go-whatsapp/crypto/hkdf"
"github.com/golang/protobuf/proto"
)
type mediaType int
const (
_ = iota
mediaTypeImage = mediaType(iota)
mediaTypeVideo
mediaTypeAudio
mediaTypeDocument
)
var (
appInfo = map[mediaType]string{
mediaTypeImage: "WhatsApp Image Keys",
mediaTypeVideo: "WhatsApp Video Keys",
mediaTypeAudio: "WhatsApp Audio Keys",
mediaTypeDocument: "WhatsApp Document Keys",
}
)
func main() {
var Usage = func() {
fmt.Fprintf(os.Stderr,
"Usage: %s -o FILE -t TYPE ENCFILE HEXMEDIAKEY\n\nOptions:\n",
os.Args[0])
flag.PrintDefaults()
}
mt := flag.Int("t", 0, "media `TYPE` (1 = image, 2 = video, 3 = audio, 4 = doc)")
outputFileName := flag.String("o", "", "write decrypted output to `FILE`")
flag.Parse()
if *mt == 0 || len(*outputFileName) == 0 || flag.NArg() < 2 {
Usage()
os.Exit(1)
}
data, err := decryptMediaFile(flag.Args()[0], flag.Args()[1], mediaType(*mt))
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(*outputFileName, data, 0400)
if err != nil {
log.Fatal(err)
}
}
func decryptMediaFile(encFilePath string, hexMediaKey string, mt mediaType) (
[]byte,
error,
) {
encFileData, err := ioutil.ReadFile(encFilePath)
if err != nil {
return nil, err
}
mediaKeyBlob, err := hex.DecodeString(hexMediaKey)
if err != nil {
return nil, err
}
var mediaKey []byte
var fileHash []byte
if len(mediaKeyBlob) == 32 {
mediaKey = mediaKeyBlob
} else {
// Decode as protobuf
mk := &MediaKey{}
err = proto.Unmarshal(mediaKeyBlob, mk)
if err != nil {
return nil, err
}
mediaKey = []byte(*mk.MediaKey)
fileHash = []byte(*mk.FileEncSha256)
}
if len(fileHash) > 0 {
// Verify the fileHash of the .enc file data
h := sha256.New()
h.Write(encFileData)
encFileHash := h.Sum(nil)
if !bytes.Equal(encFileHash, fileHash) {
return nil, fmt.Errorf(".enc file hash does not match mediaKey")
}
}
data, err := decryptMedia(encFileData, mediaKey, mt)
if err != nil {
return nil, err
}
return data, err
}
func decryptMedia(encFileData []byte, mediaKey []byte, mt mediaType) (
[]byte,
error,
) {
//
// Implement reverse engineered media decryption algorithm from:
// https://github.com/sigalor/whatsapp-web-reveng#decryption
//
// mediaKey should be 32 bytes
if len(mediaKey) != 32 {
return nil, fmt.Errorf("mediaKey length %d != 32",
len(mediaKey))
}
mediaKeyExpanded, err := hkdf.Expand(mediaKey, 112, appInfo[mt])
if err != nil {
return nil, err
}
iv := mediaKeyExpanded[0:16]
cipherKey := mediaKeyExpanded[16:48]
macKey := mediaKeyExpanded[48:80]
//refKey := mediaKeyExpanded[80:]
fileLen := len(encFileData) - 10
file := encFileData[:fileLen]
mac := encFileData[fileLen:]
err = validateMedia(iv, file, macKey, mac)
if err != nil {
return nil, err
}
data, err := cbc.Decrypt(cipherKey, iv, file)
if err != nil {
return nil, err
}
return data, nil
}
// copied from https://github.com/Rhymen/go-whatsapp/blob/master/media.go
func validateMedia(iv []byte, file []byte, macKey []byte, mac []byte) error {
h := hmac.New(sha256.New, macKey)
n, err := h.Write(append(iv, file...))
if err != nil {
return err
}
if n < 10 {
return fmt.Errorf("hash to short")
}
if !hmac.Equal(h.Sum(nil)[:10], mac) {
return fmt.Errorf("invalid media hmac")
}
return nil
}