forked from EliteAndroidApps/WhatsApp-GD-Extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhatsAppGDExtract.py
executable file
·193 lines (177 loc) · 7.69 KB
/
WhatsAppGDExtract.py
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
#!/usr/bin/env python
from configparser import ConfigParser
import json
import os
import re
import requests
import sys
def getGoogleAccountTokenFromAuth():
payload = {'Email':gmail, 'Passwd':passw, 'app':client_pkg, 'client_sig':client_sig, 'parentAndroidId':devid}
request = requests.post('https://android.clients.google.com/auth', data=payload)
token = re.search('Token=(.*?)\n', request.text)
if token:
return token.group(1)
else:
quit(request.text)
def getGoogleDriveToken(token):
payload = {'Token':token, 'app':pkg, 'client_sig':sig, 'device':devid, 'google_play_services_version':client_ver, 'service':'oauth2:https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.file', 'has_permission':'1'}
request = requests.post('https://android.clients.google.com/auth', data=payload)
token = re.search('Auth=(.*?)\n', request.text)
if token:
return token.group(1)
else:
quit(request.text)
def rawGoogleDriveRequest(bearer, url):
headers = {'Authorization': 'Bearer '+bearer}
request = requests.get(url, headers=headers)
return request.text
def downloadFileGoogleDrive(bearer, url, local):
if not os.path.exists(os.path.dirname(local)):
os.makedirs(os.path.dirname(local))
if os.path.isfile(local):
os.remove(local)
headers = {'Authorization': 'Bearer '+bearer}
request = requests.get(url, headers=headers, stream=True)
request.raw.decode_content = True
if request.status_code == 200:
with open(local, 'wb') as asset:
for chunk in request.iter_content(1024):
asset.write(chunk)
print('Downloaded: "'+local+'".')
def gDriveFileMap():
global bearer
data = rawGoogleDriveRequest(bearer, 'https://www.googleapis.com/drive/v2/files')
jres = json.loads(data)
backups = []
for result in jres['items']:
try:
if result['title'] == 'gdrive_file_map':
backups.append((result['description'], rawGoogleDriveRequest(bearer, result['downloadUrl'])))
except:
pass
if len(backups) == 0:
quit('Unable to locate google drive file map for: '+pkg)
return backups
def getConfigs():
global gmail, passw, devid, pkg, sig, client_pkg, client_sig, client_ver
config = ConfigParser()
try:
config.read('settings.cfg')
gmail = config.get('auth', 'gmail')
passw = config.get('auth', 'passw')
devid = config.get('auth', 'devid')
pkg = config.get('app', 'pkg')
sig = config.get('app', 'sig')
client_pkg = config.get('client', 'pkg')
client_sig = config.get('client', 'sig')
client_ver = config.get('client', 'ver')
except(ConfigParser.NoSectionError, ConfigParser.NoOptionError):
quit('The "settings.cfg" file is missing or corrupt!')
def jsonPrint(data):
print(json.dumps(json.loads(data), indent=4, sort_keys=True))
def localFileLog(md5):
logfile = 'logs'+os.path.sep+'files.log'
if not os.path.exists(os.path.dirname(logfile)):
os.makedirs(os.path.dirname(logfile))
with open(logfile, 'a') as log:
log.write(md5+'\n')
def localFileList():
logfile = 'logs'+os.path.sep+'files.log'
if os.path.isfile(logfile):
flist = open(logfile, 'r')
return [line.split('\n') for line in flist.readlines()]
else:
open(logfile, 'w')
return localFileList()
def createSettingsFile():
with open('settings.cfg', 'w') as cfg:
cfg.write('[auth]\ngmail = alias@gmail.com\npassw = yourpassword\ndevid = 0000000000000000\n\n[app]\npkg = com.whatsapp\nsig = 38a0f7d505fe18fec64fbf343ecaaaf310dbd799\n\n[client]\npkg = com.google.android.gms\nsig = 38918a453d07199354f8b19af05ec6562ced5788\nver = 9877000')
def getSingleFile(data, asset):
data = json.loads(data)
for entries in data:
if entries['f'] == asset:
return entries['f'], entries['m'], entries['r'], entries['s']
def getMultipleFiles(data, folder):
files = localFileList()
data = json.loads(data)
for entries in data:
if any(entries['m'] in lists for lists in files) == False or 'database' in entries['f'].lower():
local = folder+os.path.sep+entries['f'].replace("/", os.path.sep)
if os.path.isfile(local) and 'database' not in local.lower():
quit('Skipped: "'+local+'".')
else:
downloadFileGoogleDrive(bearer, 'https://www.googleapis.com/drive/v2/files/'+entries['r']+'?alt=media', local)
localFileLog(entries['m'])
def runMain(mode, asset, bID):
global bearer
if os.path.isfile('settings.cfg') == False:
createSettingsFile()
getConfigs()
bearer = getGoogleDriveToken(getGoogleAccountTokenFromAuth())
drives = gDriveFileMap()
if mode == 'info':
for i, drive in enumerate(drives):
if len(drives) > 1:
print("Backup: "+str(i))
jsonPrint(drive[0])
elif mode == 'list':
for i, drive in enumerate(drives):
if len(drives) > 1:
print("Backup: "+str(i))
jsonPrint(drive[1])
elif mode == 'pull':
try:
drive = drives[bID]
except IndexError:
quit("Invalid backup ID: " + str(bID))
target = getSingleFile(drive[1], asset)
try:
f = target[0]
m = target[1]
r = target[2]
s = target[3]
except TypeError:
quit('Unable to locate: "'+asset+'".')
local = 'WhatsApp'+os.path.sep+f.replace("/", os.path.sep)
if os.path.isfile(local) and 'database' not in local.lower():
quit('Skipped: "'+local+'".')
else:
downloadFileGoogleDrive(bearer, 'https://www.googleapis.com/drive/v2/files/'+r+'?alt=media', local)
localFileLog(m)
elif mode == 'sync':
for i, drive in enumerate(drives):
folder = 'WhatsApp'
if len(drives) > 1:
print('Backup: '+str(i))
folder = 'WhatsApp-' + str(i)
getMultipleFiles(drive[1], folder)
def main():
args = len(sys.argv)
if args < 2 or str(sys.argv[1]) == '-help' or str(sys.argv[1]) == 'help':
print('\nUsage: '+str(sys.argv[0])+' -help|-vers|-info|-list|-sync|-pull file [backupID]\n\nExamples:\n')
print('python '+str(sys.argv[0])+' -help (this help screen)')
print('python '+str(sys.argv[0])+' -vers (version information)')
print('python '+str(sys.argv[0])+' -info (google drive app settings)')
print('python '+str(sys.argv[0])+' -list (list all availabe files)')
print('python '+str(sys.argv[0])+' -sync (sync all files locally)')
print('python '+str(sys.argv[0])+' -pull "Databases/msgstore.db.crypt12" [backupID] (download)\n')
elif str(sys.argv[1]) == '-info' or str(sys.argv[1]) == 'info':
runMain('info', 'settings', 0)
elif str(sys.argv[1]) == '-list' or str(sys.argv[1]) == 'list':
runMain('list', 'all', 0)
elif str(sys.argv[1]) == '-sync' or str(sys.argv[1]) == 'sync':
runMain('sync', 'all', 0)
elif str(sys.argv[1]) == '-vers' or str(sys.argv[1]) == 'vers':
print('\nWhatsAppGDExtract Version 1.1 Copyright (C) 2016 by TripCode\n')
elif args < 3:
quit('\nUsage: python '+str(sys.argv[0])+' -help|-vers|-info|-list|-sync|-pull file [backupID]\n')
elif str(sys.argv[1]) == '-pull' or str(sys.argv[1]) == 'pull':
try:
bID = int(sys.argv[3])
except (IndexError, ValueError):
bID = 0
runMain('pull', str(sys.argv[2]), bID)
else:
quit('\nUsage: python '+str(sys.argv[0])+' -help|-vers|-info|-list|-sync|-pull file [backupID]\n')
if __name__ == "__main__":
main()