-
Notifications
You must be signed in to change notification settings - Fork 39
/
atCmdInterface.py
executable file
·521 lines (439 loc) · 12.9 KB
/
atCmdInterface.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env python2
'''
file name: atCmdInterface.py
authors: Imtiaz Karim ([email protected]), Fabrizio Cicala ([email protected])
python version: python 2.7.15
'''
import sys
import os
import signal
import time
import random
import utilityFunctions
import logging
import serial
import socket
import bluetooth
import subprocess
from subprocess import Popen, PIPE
from datetime import datetime, timedelta
from stat import S_ISCHR
size = 99999
try:
import readline
except ImportError:
import pyreadline as readline
# Global vars
environment = ''
# --- Bluetooth MAC address of the target device --- #
serverMACAddress = None
#serverMACAddress = '18:E2:C2:5E:29:1C' #S3
#serverMACAddress = '50:55:27:5f:16:7d' #Nexus5
#serverMACAddress = '94:8B:C1:43:0E:C4' #S8plus
#serverMACAddress = '40:4E:36:AF:01:94' #htc
#serverMACAddress = 'e4:90:7e:ee:2b:84' #nexus6
#serverMACAddress = '04:B1:67:57:58:B9' #xiaomi
#serverMACAddress = '14:A5:1A:48:0A:2D' #huwaei
DEFAULT_BAUD = 115200
DEFAULT_TIMEOUT = 1
log_file = "log/atsend.log"
log_level = logging.DEBUG
baud_rate = 115200
time_out = 1
mfuzz_port = None
max_poll = 4
poll_delay = 2
format_string = ["<value>", "%d", "%u", "%s", "%c", "%x", "<n>", "<index>", "<args>", "<gain>"]
debug_cmd_gen = True
def create_serial(port, baud):
# print("Creating serial port %s @ %d baud" % (port, baud))
return serial.Serial(port, baud, timeout=DEFAULT_TIMEOUT)
# Function to manage the reply from the target device
def recv():
my_poll = 0
lines = []
# Make sure we dont go _too_ fast
start_time = time.time()
# To deal with the response delay
while my_poll < max_poll:
line = mfuzz_port.readline()
# print line
# timeout
if line == "":
my_poll += 1
# time.sleep(poll_delay)
continue
# clean up the output for comparison
line_clean = line.strip('\r\n')
lines += [line]
# a terminal response. end NOW
if 'ERROR' == line_clean:
break
elif 'CME ERROR' in line_clean:
break
elif 'OK' == line_clean:
break
elif 'NO CARRIER' == line_clean:
break
elif 'ABORTED' == line_clean:
break
elif 'NOT SUPPORTED' == line_clean:
break
else:
continue
# post-processing
lines2 = []
for l in lines:
if l == '\r\n':
continue
if l.endswith('\r\n'):
lines2.append(l[:-1])
return lines2
# Method to send an AT command through USB channel
def send(cmd):
'''
True - sending failed
False - sending successful
'''
cmd2 = str(cmd)
mfuzz_port.write(cmd2 + '\r\r')
# Method to send an AT command through Bluetooth channel
def bluetooth_send(cmd, port):
if port is None:
port = 3
data = "NULL"
backlog = 1
cmd2 = str(cmd)
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((serverMACAddress, port))
#s.bind((serverMACAddress, port))
#s.listen(backlog)
except Exception as e:
print ("error occured")
print (e)
s.close()
subprocess.call("sudo /etc/init.d/bluetooth restart",shell=True)
time.sleep(15)
#send_blue(cmd)
try:
s.send(cmd +"\r\r")
data=s.recv(size)
print (data)
time.sleep(5)
s.close()
return data
#return
except:
subprocess.call("sudo /etc/init.d/bluetooth restart",shell=True)
time.sleep(15)
#send_blue(cmd)
def extend(cmds):
'''
Extend the cmd list
'''
cmds2 = []
for c in cmds:
cmds2.append(c)
if c.endswith("="):
cmds2.append(c[:-1])
return cmds2
def check_internet_connectivity(output):
flag = 0
if "mDataConnectionState=0" in output:
return 1
#if "UMTS" in output:
#return 1
#if "GSM EDGE" in output:
#return 1
return flag
def at_probe():
found = []
if environment == 'linux':
print('Probing for ttyACM devices...')
for i in range(10):
devname = '/dev/ttyACM%d' % i
if not os.path.exists(devname):
continue
mode = os.stat(devname).st_mode
if S_ISCHR(mode):
found += [devname]
elif environment == 'windows':
print('Probing for COM ports...')
for i in range(256):
port = 'COM%d' % i
try:
s = serial.Serial(port)
s.close()
found.append(port)
except (OSError, serial.SerialException):
pass
else:
raise Exception('EnvironmentError: unknow OS')
return found
def send_at_command(ser, cmd):
start = time.time()
ser.write(cmd + "\r")
lines = []
while True:
line = ser.readline()
# timeout
if line == "":
break
# clean up the output (we dont want line endings)
line_clean = line.strip('\r\n')
lines += [line_clean]
if 'ERROR' == line_clean:
break
elif 'CME ERROR' in line_clean:
break
elif 'OK' == line_clean:
break
elif 'NO CARRIER' == line_clean:
break
elif 'ABORTED' == line_clean:
break
end = time.time()
print (end - start)
return lines
def at_connect(dev, baud=DEFAULT_BAUD):
try:
ser = create_serial(dev, baud)
except:
ser = None
if ser != None:
resp = send_at_command(ser, 'AT')
if len(resp) > 0 and resp[-1] == 'OK':
return ser
ser.close()
return None
def check_sim_connectivity(output):
output = output[:150]
# print output
return 1 if "mServiceState=1 1" in output else 0
def run_command(command):
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b'')
def set_environment():
global environment
if os.name == 'posix':
environment = 'linux'
elif os.name == 'nt':
environment = 'windows'
else:
raise Exception('EnvironmentError: unknow OS')
def get_serial_connection(dev):
set_environment()
devices = at_probe() if (dev == None) else [dev]
if len(devices) == 0:
print ('No device found')
return
for d in devices:
print ('Trying device ', d)
ser = at_connect(d)
if ser is not None:
return ser
return None
from subprocess import check_output
def get_pid(name):
return int(check_output(["pidof", name]))
def test_adb_process():
attempt = 5
test = False
delay = 1
while not test:
if attempt == 0:
raise Exception('Error: cannot execute adb command - adb process is not responding')
timeout = 31
task = subprocess.Popen(['adb', 'devices'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while task.poll() is None and timeout > 0:
time.sleep(delay)
timeout -= delay
if timeout > 0:
test = True
else:
os.kill(get_pid('adb'), signal.SIGTERM)
attempt -= 1
# reboot adb and the connected device
def reboot_env(device=None):
# test_adb_process()
subprocess.call("adb kill-server", shell=True)
subprocess.call("adb reboot", shell=True)
subprocess.call("adb kill-server", shell=True)
if environment == 'linux':
subprocess.call(['./test.sh'], shell=True)
elif environment == 'windows':
subprocess.call(['test.bat'], shell=True)
else:
raise Exception('EnvironmentError: unknow OS')
subprocess.call("adb kill-server", shell=True)
cmd = ['adb', 'shell', 'su']
procId = subprocess.Popen(cmd, stdin=subprocess.PIPE)
if device == 'htcDesire10':
procId.communicate('setprop sys.usb.config mtp,adb,diag,modem,modem_mdm,diag_mdm\nexit\n')
#subprocess.call('adb shell setprop sys.usb.config mtp,adb,diag,modem,modem_mdm,diag_mdm\nexit\n')
elif device == 'huaweiNexus6P' or device is None:
pass
else:
procId.communicate('setprop sys.usb.config diag,adb\nexit\n')
#subprocess.call('adb shell setprop sys.usb.config diag,adb')
time.sleep(8)
def init_mfuzz_port(device, port):
global mfuzz_port
mfuzz_port = get_serial_connection(port)
if mfuzz_port is None:
if device == 'samsungNote2' or device == 'samsungGalaxyS3':
try:
init_mfuzz_port(device, port)
return
except:
pass
reboot_env(device)
init_mfuzz_port(device, port)
def write_on_logcat(stime, ftime):
command = 'adb logcat -v time -d *:E'.split()
logcat = ''
for line in run_command(command):
splitted = line.split()
timevalue = splitted[0] + ' ' + splitted[1]
timevalue = '2019-' + timevalue
try:
datetime_object = datetime.strptime(timevalue, '%Y-%m-%d %H:%M:%S.%f')
# datetime_object = timedelta(year=2019)
# print datetime_object
if stime <= datetime_object <= ftime:
if (line not in logcat) and ("/QC-time-services" not in line) and ("WakeLock( 2253)" not in line) and (
"EarsSyncAdapter(10719)" not in line) and \
("ACDB AudProc vol returned = -19" not in line):
logcat += line
except:
pass
f = open("logcat.txt", "a")
f.write(logcat)
f.close()
def bluetooth_fuzz(cmd, blu_addr, port=None):
global serverMACAddress
serverMACAddress = blu_addr
retList = []
flag = 0
timer_for_check = 30
cmd = str("AT"+cmd)
print (cmd)
start = time.time()
stime = datetime.now()
print (stime)
try:
r = bluetooth_send(cmd, port)
except serial.serialutil.SerialException as e:
print (e)
end = time.time()
total_time = (end-start)/len(cmd) # normalize the total time based on the command length
ftime = datetime.now() + timedelta(minutes=1)
retList.append(total_time)
print ('Input: ' + cmd + ' Output: ' + str(r))
if r is None:
retList.append(flag)
return retList
r = str(r).rstrip("\r\n")
if "ERROR" not in r:
flag = 1
retList.append(flag)
#time.sleep(15)
return retList
for _ in range(timer_for_check):
command = 'adb shell dumpsys telephony.registry'.split()
output = ""
for line in run_command(command):
output+=line
flag1 = check_sim_connectivity(output)
flag2 = check_internet_connectivity(output)
time.sleep(0.5)
if flag1==1 or flag2==1:
flag =1
print ('flag is now ' + str(flag))
time.sleep(5)
break
else:
print ('flag is now ' + str(flag))
retList.append(flag)
print (retList)
return retList
def usb_fuzz(cmd, device, port=None):
retList = []
flag = 0
# time.sleep(5)
# Open the serial port
init_mfuzz_port(device, port)
print ('Serial port: ', mfuzz_port.port)
logging.info("port is opened for %s" % mfuzz_port.port)
# Fuzz
timer_for_check = 20
cmd = str("AT"+cmd)
print (cmd)
cmd = str(cmd)
start = time.time()
stime = datetime.now()
print (stime)
try:
send(cmd)
except serial.serialutil.SerialException as e:
if device == 'samsungNote2' or device == 'samsungGalaxyS3' or device == "LGg3":
try:
return usb_fuzz(cmd, device, port)
except:
pass
print (e)
mfuzz_port.close()
reboot_env(device)
return usb_fuzz(cmd, device, port)
r = recv()
end = time.time()
total_time = (end - start) / len(cmd) # normalize the total time based on the command length
ftime = datetime.now() + timedelta(minutes=1)
retList.append(total_time)
print (r)
time.sleep(1)
send("AT+CHUP")
r10 = recv()
time.sleep(2)
if "OK\r" in r: # cut phone Go to sleep got phone call
#cmd2 = cmd + '\r\r'
#flag = 0 if (len(r) == 2 and cmd2 in r) or (len(r) == 1) else 1
flag = 1
retList.append(flag)
time.sleep(20)
return retList
for _ in range(timer_for_check):
# test_adb_process()
# if device == 'nexus5':
# write_on_logcat(stime, ftime)
command = 'adb shell dumpsys telephony.registry'.split()
output = ""
for line in run_command(command):
output += line
flag1 = check_sim_connectivity(output)
flag2 = check_internet_connectivity(output)
time.sleep(0.5)
if flag1 == 1 or flag2 == 1:
flag = 1
print ("flag is now " + str(flag))
time.sleep(5)
break
else:
print ("flag is now " + str(flag))
time.sleep(20)
retList.append(flag)
print (retList)
mfuzz_port.close()
logging.info("port is closed")
return retList
def test_fuzz(cmd):
return [random.random()*5, utilityFunctions.flip_coin(10)]
def main():
usb_fuzz("ATD123", 'test_dev')
# logging.info("atsend/mfuzz ends...")
print ('\n--- Test Completed ---\n')
if __name__ == "__main__":
main()