-
Notifications
You must be signed in to change notification settings - Fork 830
Expand file tree
/
Copy pathwhatsapp.py
More file actions
executable file
·367 lines (314 loc) · 18 KB
/
whatsapp.py
File metadata and controls
executable file
·367 lines (314 loc) · 18 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys;
sys.dont_write_bytecode = True;
import os;
import signal;
import base64;
from threading import Thread, Timer
import math;
import time;
import datetime;
import json;
import io;
from time import sleep;
from threading import Thread;
from Crypto.Cipher import AES;
from Crypto.Hash import SHA256;
import hashlib;
import hmac;
import traceback;
import binascii
from Crypto import Random
from whatsapp_defines import WATags, WASingleByteTokens, WADoubleByteTokens, WAWebMessageInfo;
from whatsapp_binary_writer import whatsappWriteBinary, WASingleByteTokens, WADoubleByteTokens, WAWebMessageInfo;
from whatsapp_defines import WAMetrics;
import websocket;
import curve25519;
import pyqrcode;
from utilities import *;
from whatsapp_binary_reader import whatsappReadBinary;
WHATSAPP_WEB_VERSION="2,2136,10"
reload(sys);
sys.setdefaultencoding("utf-8");
def HmacSha256(key, sign):
return hmac.new(key, sign, hashlib.sha256).digest();
def HKDF(key, length, appInfo=""): # implements RFC 5869, some parts from https://github.com/MirkoDziadzka/pyhkdf
key = HmacSha256("\0"*32, key);
keyStream = "";
keyBlock = "";
blockIndex = 1;
while len(keyStream) < length:
keyBlock = hmac.new(key, msg=keyBlock+appInfo+chr(blockIndex), digestmod=hashlib.sha256).digest();
blockIndex += 1;
keyStream += keyBlock;
return keyStream[:length];
def AESPad(s):
bs = AES.block_size;
return s + (bs - len(s) % bs) * chr(bs - len(s) % bs);
def to_bytes(n, length, endianess='big'):
h = '%x' % n
s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
return s if endianess == 'big' else s[::-1]
def AESUnpad(s):
return s[:-ord(s[len(s)-1:])];
def AESEncrypt(key, plaintext): # like "AESPad"/"AESUnpad" from https://stackoverflow.com/a/21928790
plaintext = AESPad(plaintext);
iv = os.urandom(AES.block_size);
cipher = AES.new(key, AES.MODE_CBC, iv);
return iv + cipher.encrypt(plaintext);
def WhatsAppEncrypt(encKey, macKey, plaintext):
enc = AESEncrypt(encKey, plaintext)
return HmacSha256(macKey, enc) + enc; # this may need padding to 64 byte boundary
def AESDecrypt(key, ciphertext): # from https://stackoverflow.com/a/20868265
iv = ciphertext[:AES.block_size];
cipher = AES.new(key, AES.MODE_CBC, iv);
plaintext = cipher.decrypt(ciphertext[AES.block_size:]);
return AESUnpad(plaintext);
class WhatsAppWebClient:
websocketIsOpened = False;
onOpenCallback = None;
onMessageCallback = None;
onCloseCallback = None;
activeWs = None;
messageSentCount = 0;
websocketThread = None;
messageQueue = {}; # maps message tags (provided by WhatsApp) to more information (description and callback)
loginInfo = {
"clientId": None,
"serverRef": None,
"privateKey": None,
"publicKey": None,
"key": {
"encKey": None,
"macKey": None
}
};
connInfo = {
"clientToken": None,
"serverToken": None,
"browserToken": None,
"secret": None,
"sharedSecret": None,
"me": None
};
def __init__(self, onOpenCallback, onMessageCallback, onCloseCallback):
self.onOpenCallback = onOpenCallback;
self.onMessageCallback = onMessageCallback;
self.onCloseCallback = onCloseCallback;
websocket.enableTrace(True);
self.connect();
def onOpen(self, ws):
try:
self.websocketIsOpened = True;
if self.onOpenCallback is not None and "func" in self.onOpenCallback:
self.onOpenCallback["func"](self.onOpenCallback);
eprint("WhatsApp backend Websocket opened.");
except:
eprint(traceback.format_exc());
def onError(self, ws, error):
eprint(error);
def onClose(self, ws):
self.websocketIsOpened = False;
if self.onCloseCallback is not None and "func" in self.onCloseCallback:
self.onCloseCallback["func"](self.onCloseCallback);
eprint("WhatsApp backend Websocket closed.");
def keepAlive(self):
if self.activeWs is not None:
self.activeWs.send("?,,")
Timer(20.0, self.keepAlive).start()
def onMessage(self, ws, message):
try:
messageSplit = message.split(",", 1);
messageTag = messageSplit[0];
messageContent = messageSplit[1];
if messageTag in self.messageQueue: # when the server responds to a client's message
pend = self.messageQueue[messageTag];
if pend["desc"] == "_status":
if messageContent[0] == 'Pong' and messageContent[1] == True:
pend["callback"]({"Connected": True,"user":self.connInfo["me"],"pushname":self.connInfo["pushname"]})
elif pend["desc"] == "_restoresession":
pend["callback"]["func"]({ "type": "restore_session" }, pend["callback"]);
elif pend["desc"] == "_login":
eprint("Message after login: ", message);
self.loginInfo["serverRef"] = json.loads(messageContent)["ref"];
eprint("set server id: " + self.loginInfo["serverRef"]);
self.loginInfo["privateKey"] = curve25519.Private();
self.loginInfo["publicKey"] = self.loginInfo["privateKey"].get_public();
qrCodeContents = self.loginInfo["serverRef"] + "," + base64.b64encode(self.loginInfo["publicKey"].serialize()) + "," + self.loginInfo["clientId"];
eprint("qr code contents: " + qrCodeContents);
svgBuffer = io.BytesIO(); # from https://github.com/mnooner256/pyqrcode/issues/39#issuecomment-207621532
pyqrcode.create(qrCodeContents, error='L').svg(svgBuffer, scale=6, background="rgba(0,0,0,0.0)", module_color="#122E31", quiet_zone=0);
if "callback" in pend and pend["callback"] is not None and "func" in pend["callback"] and pend["callback"]["func"] is not None and "tag" in pend["callback"] and pend["callback"]["tag"] is not None:
pend["callback"]["func"]({ "type": "generated_qr_code", "image": "data:image/svg+xml;base64," + base64.b64encode(svgBuffer.getvalue()), "content": qrCodeContents }, pend["callback"]);
else:
try:
jsonObj = json.loads(messageContent); # try reading as json
except ValueError, e:
if messageContent != "":
hmacValidation = HmacSha256(self.loginInfo["key"]["macKey"], messageContent[32:]);
if hmacValidation != messageContent[:32]:
raise ValueError("Hmac mismatch");
decryptedMessage = AESDecrypt(self.loginInfo["key"]["encKey"], messageContent[32:]);
try:
processedData = whatsappReadBinary(decryptedMessage, True);
messageType = "binary";
# sort contacts obj{jid : name}
try:
if processedData[1]['type'] is "contacts":
messageType = "jsonContacts";
processedData.append(self.sortedContacts(processedData))
except:
pass
# sort statuses
try:
if processedData[2][0][0] is "status":
processedData[2] = self.sortedStatuses(processedData)
messageType = "jsonStatuses";
except:
pass
except:
processedData = { "traceback": traceback.format_exc().splitlines() };
messageType = "error";
finally:
self.onMessageCallback["func"](processedData, self.onMessageCallback, { "message_type": messageType });
else:
self.onMessageCallback["func"](jsonObj, self.onMessageCallback, { "message_type": "json" });
if isinstance(jsonObj, list) and len(jsonObj) > 0: # check if the result is an array
eprint(json.dumps(jsonObj));
if jsonObj[0] == "Conn":
Timer(20.0, self.keepAlive).start() # Keepalive Request
self.connInfo["clientToken"] = jsonObj[1]["clientToken"];
self.connInfo["serverToken"] = jsonObj[1]["serverToken"];
self.connInfo["browserToken"] = jsonObj[1]["browserToken"];
self.connInfo["me"] = jsonObj[1]["wid"];
self.connInfo["secret"] = base64.b64decode(jsonObj[1]["secret"]);
self.connInfo["sharedSecret"] = self.loginInfo["privateKey"].get_shared_key(curve25519.Public(self.connInfo["secret"][:32]), lambda a: a);
sse = self.connInfo["sharedSecretExpanded"] = HKDF(self.connInfo["sharedSecret"], 80);
hmacValidation = HmacSha256(sse[32:64], self.connInfo["secret"][:32] + self.connInfo["secret"][64:]);
if hmacValidation != self.connInfo["secret"][32:64]:
raise ValueError("Hmac mismatch");
keysEncrypted = sse[64:] + self.connInfo["secret"][64:];
keysDecrypted = AESDecrypt(sse[:32], keysEncrypted);
self.loginInfo["key"]["encKey"] = keysDecrypted[:32];
self.loginInfo["key"]["macKey"] = keysDecrypted[32:64];
self.saveSession();
# eprint("private key : ", base64.b64encode(self.loginInfo["privateKey"].serialize()));
# eprint("secret : ", base64.b64encode(self.connInfo["secret"]));
# eprint("shared secret : ", base64.b64encode(self.connInfo["sharedSecret"]));
# eprint("shared secret expanded : ", base64.b64encode(self.connInfo["sharedSecretExpanded"]));
# eprint("hmac validation : ", base64.b64encode(hmacValidation));
# eprint("keys encrypted : ", base64.b64encode(keysEncrypted));
# eprint("keys decrypted : ", base64.b64encode(keysDecrypted));
eprint("set connection info: client, server and browser token; secret, shared secret, enc key, mac key");
eprint("logged in as " + jsonObj[1]["pushname"] + " (" + jsonObj[1]["wid"] + ")");
elif jsonObj[0] == "Cmd":
if jsonObj[1]["type"] == "challenge": # Do challenge
challenge = WhatsAppEncrypt(self.loginInfo["key"]["encKey"], self.loginInfo["key"]["macKey"], base64.b64decode(jsonObj[1]["challenge"]))
challenge = base64.b64encode(challenge)
messageTag = str(getTimestamp());
eprint(json.dumps( [messageTag,["admin","challenge",challenge,self.connInfo["serverToken"],self.loginInfo["clientId"]]]))
self.activeWs.send(json.dumps( [messageTag,["admin","challenge",challenge,self.connInfo["serverToken"],self.loginInfo["clientId"]]]));
elif jsonObj[0] == "Stream":
self.getStatuses(); # request for contacts statuses
pass;
elif jsonObj[0] == "Props":
pass;
except:
eprint(traceback.format_exc());
def connect(self):
self.activeWs = websocket.WebSocketApp("wss://web.whatsapp.com/ws",
on_message = lambda ws, message: self.onMessage(ws, message),
on_error = lambda ws, error: self.onError(ws, error),
on_open = lambda ws: self.onOpen(ws),
on_close = lambda ws: self.onClose(ws),
header = { "Origin: https://web.whatsapp.com" });
self.websocketThread = Thread(target = self.activeWs.run_forever);
self.websocketThread.daemon = True;
self.websocketThread.start();
def generateQRCode(self, callback=None):
self.loginInfo["clientId"] = base64.b64encode(os.urandom(16));
messageTag = str(getTimestamp());
self.messageQueue[messageTag] = { "desc": "_login", "callback": callback };
message = messageTag + ',["admin","init",['+ WHATSAPP_WEB_VERSION + '],["Chromium at ' + datetime.datetime.now().isoformat() + '","Chromium"],"' + self.loginInfo["clientId"] + '",true]';
self.activeWs.send(message);
def restoreSession(self, callback=None):
with open("session.json","r") as f:
session_file = f.read()
session = json.loads(session_file)
self.connInfo["clientToken"] = session['clientToken']
self.connInfo["serverToken"] = session['serverToken']
self.loginInfo["clientId"] = session['clientId']
self.loginInfo["key"]["macKey"] = session['macKey'].encode("latin_1")
self.loginInfo["key"]["encKey"] = session['encKey'].encode("latin_1")
messageTag = str(getTimestamp())
message = messageTag + ',["admin","init",['+ WHATSAPP_WEB_VERSION + '],["StatusDownloader","Chromium"],"' + self.loginInfo["clientId"] + '",true]'
self.activeWs.send(message)
messageTag = str(getTimestamp())
self.messageQueue[messageTag] = {"desc": "_restoresession","callback": callback}
message = messageTag + ',["admin","login","' + self.connInfo["clientToken"] + '", "' + self.connInfo[
"serverToken"] + '", "' + self.loginInfo["clientId"] + '", "takeover"]'
self.activeWs.send(message)
def saveSession(self):
session = {"clientToken":self.connInfo["clientToken"],"serverToken":self.connInfo["serverToken"],
"clientId":self.loginInfo["clientId"],"macKey": self.loginInfo["key"]["macKey"].decode("latin_1")
,"encKey": self.loginInfo["key"]["encKey"].decode("latin_1")};
f = open("./session.json","w")
f.write(json.dumps(session))
f.close()
def sortedContacts(self,processedData):
contacts = {}
for contact in range(len(processedData[2])):
if 'name' in processedData[2][contact][1].keys() :
contacts[processedData[2][contact][1]['jid']] = processedData[2][contact][1]['name']
return contacts
def getStatuses(self):
messageId = "3EB0"+binascii.hexlify(Random.get_random_bytes(8)).upper()
encryptedMessage = WhatsAppEncrypt(
self.loginInfo["key"]["encKey"],
self.loginInfo["key"]["macKey"],
whatsappWriteBinary(["query", {"type": "status","jid":""}, None])
)
payload = bytearray(messageId) + bytearray(",") + bytearray(
to_bytes(WAMetrics.QUERY_MEDIA, 1)
) + bytearray([0x80]) + encryptedMessage
self.activeWs.send(payload, websocket.ABNF.OPCODE_BINARY)
def sortedStatuses(self,processedData):
entries = {}
bad = []
for user in range(len(processedData[2])):
jid = processedData[2][user][1]['jid']
for story in range(len(processedData[2][user][2])):
if processedData[2][user][2][story][0] == "picture" :
bad.append(story)
continue
decoded_msgs = WAWebMessageInfo.decode(processedData[2][user][2][story][2])
processedData[2][user][2][story] = decoded_msgs['message']
entries[jid] = processedData[2][user][2]
for b in bad:
del entries[jid][b]
return entries
def getLoginInfo(self, callback):
callback["func"]({ "type": "login_info", "data": self.loginInfo }, callback);
def getConnectionInfo(self, callback):
callback["func"]({ "type": "connection_info", "data": self.connInfo }, callback);
def sendTextMessage(self, number, text):
messageId = "3EB0"+binascii.hexlify(Random.get_random_bytes(8)).upper()
messageTag = str(getTimestamp())
messageParams = {"key": {"fromMe": True, "remoteJid": number + "@s.whatsapp.net", "id": messageId},"messageTimestamp": getTimestamp(), "status": 1, "message": {"conversation": text}}
msgData = ["action", {"type": "relay", "epoch": str(self.messageSentCount)},[["message", None, WAWebMessageInfo.encode(messageParams)]]]
encryptedMessage = WhatsAppEncrypt(self.loginInfo["key"]["encKey"], self.loginInfo["key"]["macKey"],whatsappWriteBinary(msgData))
payload = bytearray(messageId) + bytearray(",") + bytearray(to_bytes(WAMetrics.MESSAGE, 1)) + bytearray([0x80]) + encryptedMessage
self.messageSentCount = self.messageSentCount + 1
self.messageQueue[messageId] = {"desc": "__sending"}
self.activeWs.send(payload, websocket.ABNF.OPCODE_BINARY)
def status(self, callback=None):
if self.activeWs is not None:
messageTag = str(getTimestamp())
self.messageQueue[messageTag] = {"desc": "_status", "callback": callback}
message = messageTag + ',["admin", "test"]'
self.activeWs.send(message)
def disconnect(self):
self.activeWs.send('goodbye,,["admin","Conn","disconnect"]'); # WhatsApp server closes connection automatically when client wants to disconnect
#time.sleep(0.5);
#self.activeWs.close();