forked from tomer8007/whatsapp-web-incognito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy_single_device.js
310 lines (273 loc) · 9.81 KB
/
legacy_single_device.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// This file contains code that was used in earlier versions of the extension,
// when WhatsApp Web mirrored the traffic through the phone.
// It it not used anymore, and is there for historical reasons.
WACrypto.isTagBasedPayload = function(payload)
{
var looksTagBased = false;
if (payload instanceof ArrayBuffer || payload instanceof Uint8Array)
{
var array = new Uint8Array(payload);
if (array.includes(44))
{
for (var o, i=0, a = [];(o=array[i]) != 44;i++) // 44 == ','
a.push(o);
var tag = String.fromCharCode.apply(String, a);
looksTagBased = tag.length < 40 && !/[\x00-\x1F]/.test(tag);
}
}
else
{
looksTagBased = true;
}
return looksTagBased;
}
WACrypto.parseWebSocketPayload = function(payload)
{
if (!WACrypto.isTagBasedPayload(payload))
return null;
var t, r, n = payload;
if (payload instanceof ArrayBuffer)
{
var array = new Uint8Array(payload);
for (var o, i=0, a = [];(o=array[i]) != 44;i++) // 44 == ','
a.push(o);
t = String.fromCharCode.apply(String, a);
r = payload.slice(i+1);
if (r.byteLength % 16 != 0)
{
// this is a client-to-phone binary message.
var dataArray = new Uint8Array(r);
if (dataArray[0] == ",")
{
// no binaryOpts
r = r.slice(1);
}
else
{
var metric = dataArray[1]; // message type
var binaryFlags = dataArray[2];
r = r.slice(2);
return { tag: t, data: r, metric: metric, binaryFlags: binaryFlags }
}
}
}
else
{
var d = payload.indexOf(",");
t = payload.slice(0, d);
r = payload.slice(d + 1);
if (r[0] == ",") r = r.slice(1);
try
{
r = JSON.parse(r);
}
catch (e)
{
// just leave it unparsed
}
}
return { tag: t, data: r }
}
WACrypto.decryptWithWebCrypto = async function(buffer, isMultiDevice, isIncoming = true)
{
if (buffer instanceof Uint8Array) buffer = toArayBufer(buffer);
if (!isMultiDevice)
{
try
{
var hmac = buffer.slice(0, 32);
var dataIncludingIV = buffer.slice(32);
var iv = buffer.slice(32, 48);
var data = buffer.slice(48);
var keys = getKeys();
var algorithmInfo = {name: "AES-CBC",iv: new Uint8Array(iv)};
const key = await window.crypto.subtle.importKey("raw", new Uint8Array(keys.enc), algorithmInfo, false, ["decrypt"]);
try
{
const decrypted = await window.crypto.subtle.decrypt(algorithmInfo, key, data);
return [{ frame: decrypted, counter: 0 }];
} catch (e)
{
console.log(e.code + ", " + e.toString());
}
}
catch (exception)
{
// getKeys might fail due to WASecretBundle not being set yet, or this is a multi-device
// (if this is the case, we can ignore it)
console.error("WhatsAppInvisible: can't decrypt packet due to exception:");
console.error(exception);
return new Promise(function(resolve, reject) {resolve(null);});
}
}
}
WACrypto.encryptWithWebCrypto = function(nodeBuffer, isMultiDevice = false, isIncoming = false, counter = 0)
{
if (nodeBuffer instanceof Uint8Array) nodeBuffer = toArayBufer(nodeBuffer);
if (!isMultiDevice)
{
// tag based
var hmac = new Uint8Array(16), iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
var data = new Uint8Array(nodeBuffer);
var keys = keys = getKeys();
var algorithmInfo = {name: "AES-CBC", iv: new Uint8Array(iv)};
return window.crypto.subtle.importKey("raw", new Uint8Array(keys.enc), algorithmInfo, false, ["encrypt"]).then(function(key)
{
return window.crypto.subtle.encrypt(algorithmInfo, key, data.buffer).then(function(encryptedData)
{
var encryptedData2 = new Uint8Array(encryptedData);
var totalEncryptedData = new Uint8Array(iv.length + encryptedData2.length);
totalEncryptedData.set(iv, 0);
totalEncryptedData.set(encryptedData2, iv.length);
var algorithmInfo = {name: "HMAC", hash: { name: "SHA-256" } };
return window.crypto.subtle.importKey("raw", new Uint8Array(keys.mac), algorithmInfo, false, ["sign"]).then(function(key)
{
return window.crypto.subtle.sign(algorithmInfo, key, totalEncryptedData).then(function(hmac)
{
return BinaryReader.build(hmac, totalEncryptedData).readBuffer();
});
});
}).catch(function(e)
{
console.error(e.code + ", " + e.toString());
});
});
}
}
NodeHandler.interceptOutgoingNode = async function (node, isMultiDevice)
{
try
{
//
// Check for message nodes
//
if (node.tag == "message" || node.tag == "action")
{
// manipulating a message node
if (!isMultiDevice && node.tag == "action")
{
// non-multi device
var participants = node.content;
for (var j = 0; j < participants.length; j++)
{
var child = participants[j];
if (child.tag == "message")
{
var messageNode = await this.onSentEncNode(child, isMultiDevice);
participants[j] = messageNode;
}
}
}
}
}
catch (exception)
{
console.error("WhatsIncognito: Allowing WA packet due to exception:");
console.error(exception);
console.error(exception.stack);
return node;
}
return node;
}
NodeHandler.onSentEncNode = async function (messageNode, remoteJid, isMultiDevice)
{
if (!isMultiDevice)
{
var message = (await decryptE2EMessagesFromNode(messageNode, isMultiDevice))[0];
if (WAdebugMode)
{
console.log("WAIncognito: Sending message:");
console.log(message);
}
if (message == null || message.key == null) return;
remoteJid = message.key.remoteJid;
}
// ...
// do message manipulation if needed
// ...
var putBreakpointHere = 1;
if (!isMultiDevice)
{
// TODO: following lines are commented out due to non-complete message types
// re-assmble everything
//messageBuffer = messageTypes.WebMessageInfo.encode(message).readBuffer();
//messageNode.content = messageBuffer;
}
return messageNode;
}
async function decryptE2EMessagesFromNode(node, isMultiDevice)
{
if (!isMultiDevice)
{
// the message is not singal-encrypted, so just parse it
switch (node.tag)
{
case "message":
var message = WebMessageInfo.read(new Pbf(node.content));
return [message];
default:
return [];
}
}
}
NodeHandler.onE2EMessageNodeReceived = function(currentNode, message, isMultiDevice, encNodes, messageNodes)
{
var isAllowed = true;
var remoteJid = null;
var participant = null;
if (!isMultiDevice)
{
// non multi-device
remoteJid = message.key.remoteJid;
messageId = message.key.id;
message = message.message;
}
// ...
//
}
WAPacket.prototype =
{
getTag: function() {
return this.tag || (this.onSend ? this.onSend.tag : void 0)
},
toString: function() {
var e = this.data;
return this.binaryOpts ? this.binaryOpts.debugString : Array.isArray(e) ? 0 === e.length ? "[]" : 1 === e.length ? "[" + e[0] + "]" : "query" === e[0] || "action" === e[0] ? "[" + e[0] + ", " + e[1] + (e.length > 2 ? ", ..." : "") + "]" : void 0 : Object.isObject(e) ? "{...}" : "" + e
},
serialize: function() {
if (!this.isMultiDevice)
{
var e = this.tag;
if (this.binaryOpts) {
var t = this.binaryOpts
, n = t.metric ? t.metric : 0
, r = (this.ignore ? 0 : 1) << 7 | (!this.ignore && t.ackRequest ? 1 : 0) << 6 | (t.available === !0 ? 1 : 0) << 5 | (t.available === !1 ? 1 : 0) << 4 | (t.expires ? 1 : 0) << 3 | (t.skipOffline ? 1 : 0) << 2;
return BinaryReader.build(e, ",", n, r, this.data).readBuffer();
}
var a = this.data;
return e + ",," + a;
}
},
serializeWithoutBinaryOpts: function() {
var e = this.tag;
if (this.binaryOpts) {
var t = this.binaryOpts
, n = t.metric ? t.metric : 0
, r = (this.ignore ? 0 : 1) << 7 | (!this.ignore && t.ackRequest ? 1 : 0) << 6 | (t.available === !0 ? 1 : 0) << 5 | (t.available === !1 ? 1 : 0) << 4 | (t.expires ? 1 : 0) << 3 | (t.skipOffline ? 1 : 0) << 2;
return BinaryReader.build(e, ",", this.data).readBuffer();
}
var a = this.data;
return e + ",," + a;
},
}
function getKeys()
{
var useLocalStorage = window.localStorage.getItem("WASecretBundle") != undefined;
var storage = useLocalStorage ? window.localStorage : window.sessionStorage;
var result = {};
var secretBundle = JSON.parse(storage.getItem("WASecretBundle"));
result.enc = base64ToArrayBuffer(secretBundle["encKey"]);
result.mac = base64ToArrayBuffer(secretBundle["macKey"]);
return result;
}