/
PADDump.py
418 lines (346 loc) · 17.5 KB
/
PADDump.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
'''
SETUP:
install mitm cert.
add this computer's ip to dns list (must be first).
See README.md for more detail.
If you want a nice summary of your mailbox put this in another worksheet (change the "Mails" reference as needed)
in cell A1:
=SORT(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))),LEN(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A="")))),True)
in cell B1:
=ARRAYFORMULA(COUNTIF(Mails!A:A,OFFSET(A1,0,0,COUNTA(A:A),1)))
for a total:
=SUM(B:B)&" Unopened Mails"
for pal points sum:
=SUM(ARRAYFORMULA(B:B*VALUE(IFERROR(REGEXEXTRACT(A:A,"^Pal Points \((\d*)")))))&" Pal Points"
for tamadra count:
=COUNTIF(Mails!A:A,"TAMADRA*")&" TAMADRA"
jewels count:
=COUNTIF(Mails!A:A,"Jewel of*")&" Jewels"
py count:
=COUNTIF(Mails!A:A,"*py")&" Pys"
If you want them to be more sorted, put this in A1 instead:
=SORT(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))),LEN(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))),"y","")),False,LEN(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))),"J","")),False,LEN(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))),"T","")),False,LEN(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(Mails!A:A,NOT(Mails!A:A="")))," ","")),True)
heres a revised one that inclued tans
=SORT(UNIQUE(FILTER(A:A,NOT(A:A=""))),LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"py","")),False,LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"Jewel","")),False,LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"tan","")),False,LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"TAMA","")),False)
'''
'''
dependencies: mitmproxy, gspread, requests, dnslib, python-dateutil, pytz, paramiko, biplist
'''
import time
import os
##import cffi#added just for pyinstaller's sake
import re
from requests import session
import json
##from oauth2client.client import SignedJwtAssertionCredentials
from oauth2client.service_account import ServiceAccountCredentials
import ConfigParser
from contextlib import closing
import socket
# extra modules
import gspread
import parsemails
import network
import atexit
"""http://code.activestate.com/recipes/491264-mini-fake-dns-server/"""
import sys
##import binascii,copy,struct, time
from dnslib import DNSRecord,RR,QTYPE,RCODE,parse_time,A
from dnslib.server import DNSServer,DNSHandler,BaseResolver,DNSLogger
from dnslib.label import DNSLabel
##import re
from mitmproxy import controller, proxy, flow, dump, cmdline, contentviews
from mitmproxy.proxy.server import ProxyServer
import thread
####################################################
################### CONFIG SETUP ###################
####################################################
Gateway = socket.gethostbyname(socket.gethostname()) # ip of this computer
# actually that method might fail if you have multi network adapters (including virtual ones)
# this should be more reliable:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('1.1.1.1', 0))
Gateway = s.getsockname()[0]
config_essentials = 'PADHerder Credentials'
config_gsheets='Google Sheets Integration'
config_jailbreak = 'Automatic iPhone DNS Setup'
config_padherder = 'PADHerder Upload Options'
def set_defaults(config):
config.add_section(config_essentials)
config.set(config_essentials,'padherder_username','yourusernamehere')
config.set(config_essentials,'padherder_password','yourpasswordhere')
config.set(config_essentials,'run_continuously','0') # if 0, will exit after first successful update. otherwise will just run forever
config.add_section(config_gsheets)
config.set(config_gsheets,'json_key_file','')#oauth2 credentials for google drive http://gspread.readthedocs.org/en/latest/oauth2.html
config.set(config_gsheets,'spreadsheet_name','') # spreadsheet that will be automatically updated (the first worksheet will be overwritten)
config.add_section(config_jailbreak)
config.set(config_jailbreak,'ssh_username','')#needs su privileges (i.e. root)
config.set(config_jailbreak,'ssh_password','')
config.set(config_jailbreak,'iphone_ip','')
config.set(config_jailbreak,'router_ip','')# ip of your default gateway, probably your router, probably already correct.
config.add_section(config_padherder)
config.set(config_padherder,'ignore_below','4')#leave blank for none (or just set to 99 or something)
config.set(config_padherder,'delete_old','on')
config.set(config_padherder,'add_new','on')
def get_dict(config):
return dict(config.items(config_essentials)+config.items(config_gsheets)+config.items(config_jailbreak)+config.items(config_padherder))
####################################################
############## UPDATE FUNCTIONS ####################
####################################################
def update_mails(mails):
data = parsemails.parse_mail(mails)
if CREDENTIALS['json_key_file']:
## with closing(open(os.path.join(os.path.dirname(os.path.realpath(__file__)),CREDENTIALS['json_key_file']),'r')) as f:
## json_key = json.load(f)
scope = ['https://spreadsheets.google.com/feeds']
## credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
## credentials = ServiceAccountCredentials(json_key['client_email'], json_key['private_key'], scope)
credentials = ServiceAccountCredentials.from_json_keyfile_name(os.path.join(os.path.dirname(os.path.realpath(__file__)),CREDENTIALS['json_key_file']), scope)
gc = gspread.authorize(credentials)
wks = gc.open(CREDENTIALS['spreadsheet_name']).sheet1
# "blank" existing cells as much as google will let us
cell_list = wks.range('A1:C512')#A512 if you want to use an arrayformula for B
## cell_list+=wks.range('C1:C512')
for cell in cell_list:
cell.value = ''
wks.update_cells(cell_list)
# use this arrayformula to generate the "x days old" for each mail
## wks.update_cell(1,2,'=ARRAYFORMULA(FLOOR(NOW()-INDIRECT("D1:D"&COUNTA(D:D)),1))')
#insert the mails
flat = [item for sublist in data for item in sublist]
cell_list = wks.range('A1:'+wks.get_addr_int(len(data),3))
for i in range(len(cell_list)):
cell_list[i].value = flat[i]
wks.update_cells(cell_list)
wks.update_acell("E1",'''=SORT(UNIQUE(FILTER(A:A,NOT(A:A=""))),LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"py","")),False,LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"Jewel","")),False,LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"tan","")),False,LEN(UNIQUE(FILTER(A:A,NOT(A:A=""))))-LEN(SUBSTITUTE(UNIQUE(FILTER(A:A,NOT(A:A=""))),"TAMA","")),False)''')
wks.update_acell("F1",'''=ARRAYFORMULA(COUNTIF(A:A,OFFSET(E1,0,0,COUNTA(E:E),1)))''')
## else:
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),'pad_mails.csv'),'w') as f:
f.write('\n'.join([','.join(line) for line in data]))
def update_padherder(json_data):
if CREDENTIALS['padherder_username']:
login_url = ur'https://www.padherder.com/auth/login'
json_upload_url = ur'https://www.padherder.com/account/import_json/'
csrf_token = re.compile("name='csrfmiddlewaretoken' value='([^']*)")
## headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36',
## 'Referer':url}
s = session()
s.headers.update({'Referer':login_url})
response = s.get(login_url)
csrf = csrf_token.findall(response.text)[0]
forms = {'username':CREDENTIALS['padherder_username'], 'password':CREDENTIALS['padherder_password'],'csrfmiddlewaretoken':csrf}
response = s.post(login_url, data=forms)
csrf = csrf_token.findall(response.text)[0]
forms = {'add_new':CREDENTIALS['add_new'],
'delete_old':CREDENTIALS['delete_old'],
'friends':'off',
'csrfmiddlewaretoken':csrf,
'ignore_below':'off',
'ignore_value':'4'}
try:
forms['ignore_value']=str(int(CREDENTIALS['ignore_below']))
forms['ignore_below']= 'on'
except:
pass
for key in forms.keys():
if forms[key] == 'off':
forms.pop(key)
#quick and dirty fix for padherder not handling latents correctly ( this just remove all latents. and some other data im not sure what )
## json_data['card']=[m[:-2]+[0,m[-1]] for m in json_data['card']]
json_data = re.sub('\d*,\d*],','0,0],',json_data)
response = s.post(json_upload_url, data=forms, files={'json_file':('json.json',json_data)})
s.close()
else:
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),'padherder.json'),'w') as f:
f.write(json_data)
####################################################
############### PROXY FUNCTIONS ####################
####################################################
parse_host_header = re.compile(r"^(?P<host>[^:]+|\[.+\])(?::(?P<port>\d+))?$")
class PadMaster(flow.FlowMaster):
def __init__(self, server, main_window, region):
flow.FlowMaster.__init__(self, server, flow.State())
self.region = region
self.monster_data = self.mailbox_data = None
def run(self):
try:
return flow.FlowMaster.run(self)
except KeyboardInterrupt:
self.shutdown()
def handle_request(self, f):
if f.client_conn.ssl_established:
f.request.scheme = "https"
sni = f.client_conn.connection.get_servername()
port = 443
else:
f.request.scheme = "http"
sni = None
port = 80
host_header = f.request.pretty_host
m = parse_host_header.match(host_header)
if m:
host_header = m.group("host").strip("[]")
if m.group("port"):
port = int(m.group("port"))
f.request.host = sni or host_header
f.request.port = port
## print("Got HTTPS request, forwarding")
flow.FlowMaster.handle_request(self, f)
if f:
f.reply()
return f
def reset_data(self):
self.mailbox_data=self.monster_data=None
if not CREDENTIALS['run_continuously'] or CREDENTIALS['run_continuously'] == '0' or str(CREDENTIALS['run_continuously']).lower() == 'false' or str(CREDENTIALS['run_continuously']).lower() == 'no':
print("Update complete, shutting down...")
DNS_cleanup()
os._exit(0)
else:
print("Update complete, standing by...")
def handle_response(self, f):
flow.FlowMaster.handle_response(self, f)
if f:
f.reply()
if f.request.path.startswith('/api.php?action=get_player_data'):
print("Got monster data, processing...")
resp = f.response.content
type, lines = contentviews.get_content_view(
contentviews.get("Raw"),
f.response.content,
headers=f.response.headers)
def colorful(line):
for (style, text) in line:
yield text
content = u"\r\n".join(
u"".join(colorful(line)) for line in lines
)
cap = open('captured_data.txt', 'w')
cap.write(content)
cap.close()
self.monster_data = content
if f.request.path.startswith('/api.php?action=get_user_mail'):
resp = f.response.content
type, lines = contentviews.get_content_view(
contentviews.get("Raw"),
f.response.content,
headers=f.response.headers)
def colorful(line):
for (style, text) in line:
yield text
content = u"\r\n".join(
u"".join(colorful(line)) for line in lines
)
cap = open('captured_mail.txt', 'w')
cap.write(content)
cap.close()
self.mailbox_data = content
print("Got mail data, processing...")
if self.mailbox_data and self.monster_data:
thread.start_new_thread(lambda x,y,z: (update_padherder(x) and None) or (update_mails(y) and None) or z(),(self.monster_data,self.mailbox_data,self.reset_data))
return f
####################################################
################ DNS FUNCTIONS #####################
####################################################
class InterceptResolver(BaseResolver):
"""
Intercepting resolver
Proxy requests to upstream server optionally intercepting requests
matching local records
"""
def __init__(self,address, port, ttl, hostaddr):
"""
address/port - upstream server
ttl - default ttl for intercept records
"""
self.address = address
self.port = port
self.ttl = parse_time(ttl)
self.hostaddr = hostaddr
self.proxy_master = None
def onDNSEvent(self, message):
if self.proxy_master is not None:
self.proxy_master.shutdown()
if message.startswith('api-na'):
region = 'NA'
else:
region = 'JP'
host = self.hostaddr
httpsport = 443
try:
proxy_config = proxy.ProxyConfig(port=httpsport, host=host, mode='reverse', upstream_server=cmdline.parse_server_spec('https://%s:443/' % message))
proxy_server = ProxyServer(proxy_config)
except Exception as e:
raise
self.proxy_master = PadMaster(proxy_server, self, region)
thread.start_new_thread(self.proxy_master.run, ())
def resolve(self,request,handler):
reply = request.reply()
qname = request.q.qname
qtype = QTYPE[request.q.qtype]
if qname.matchGlob("api-*padsv.gungho.jp."):
host = self.hostaddr
reply.add_answer(RR(qname,QTYPE.A,rdata=A(host)))
self.onDNSEvent(str(qname)[:-1])
time.sleep(0.5) # we need to sleep until the proxy is up, half a second should do it...
# Otherwise proxy
if not reply.rr:
if handler.protocol == 'udp':
proxy_r = request.send(self.address,self.port)
else:
proxy_r = request.send(self.address,self.port,tcp=True)
reply = DNSRecord.parse(proxy_r)
return reply
def serveDNS(hostaddr):
dnsport = 53
resolver = InterceptResolver('8.8.8.8',
dnsport,
'60s',
hostaddr)
try:
udp_server = DNSServer(resolver,
port=dnsport,
address=hostaddr,
logger = DNSLogger("-send,-recv,-request,-reply,-truncated,-error", False))
except Exception as e:
raise
udp_server.start_thread()
try:
while udp_server.isAlive():
time.sleep(1)
except KeyboardInterrupt:
sys.exit()
def DNS_cleanup():
if CREDENTIALS['ssh_username']:
print("Fixing your iPhone's DNS (requires network restart).")
network.change_router_ip(CREDENTIALS['iphone_ip'],CREDENTIALS['router_ip'],CREDENTIALS['ssh_username'],CREDENTIALS['ssh_password'])
else:
print("Change your phone's gateway back to its default.")
time.sleep(7)
if __name__=='__main__':
print('Your IP is %s'%Gateway)
Config = ConfigParser.ConfigParser()
configfile='PADDumpConfig.ini'
if os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)),configfile)):
with closing(open(os.path.join(os.path.dirname(os.path.realpath(__file__)),configfile),'r')) as f:
Config.readfp(f)
else:
set_defaults(Config)
with closing(open(os.path.join(os.path.dirname(os.path.realpath(__file__)),configfile),'w')) as f:
Config.write(f)
print("Please enter your padherder username and password in the %s file and restart this script."%configfile)
print("Actually, this is not required. This script will still work without any of the config options set.")
time.sleep(10)
exit(0)
CREDENTIALS = get_dict(Config)
if CREDENTIALS['ssh_username']:
print("Restarting your phone's network adapter, please wait...")
network.change_router_ip(CREDENTIALS['iphone_ip'],Gateway,CREDENTIALS['ssh_username'],CREDENTIALS['ssh_password'])
print("Setup complete! (re)Open PAD and press start now.\n")
else:
print("Change your phone's gateway to the ip of this computer (%s), then (re)open PAD and press start."%Gateway)
app_config = proxy.ProxyConfig(port=8080, host=Gateway)
app_server = ProxyServer(app_config)
app_master = dump.DumpMaster(app_server, dump.Options(app_host='mitm.it', app_port=80, app=True))
thread.start_new_thread(app_master.run, ())
atexit.register(DNS_cleanup)
serveDNS(Gateway)