client.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from adb import adb_commands
  2. from adb import sign_m2crypto
  3. import stat
  4. import sys
  5. import os
  6. import time
  7. import json
  8. tty = sys.argv[1]
  9. port = '/dev/tty{},115200'.format(tty)
  10. command = sys.argv[2]
  11. def init_device(addr):
  12. device = adb_commands.AdbCommands()
  13. device.ConnectDevice(port_path=None, serial=addr)
  14. #device.ConnectDevice(port_path=None, serial="192.168.43.168:5555")
  15. #device.ConnectDevice(port_path=None, serial="/dev/ttyS4,115200")
  16. return device
  17. def scandir(path, device):
  18. files = []
  19. directories = []
  20. unknowns = []
  21. result = device.List(path)
  22. for i in result[2:]:
  23. if stat.S_ISDIR(i[1]):
  24. directories.append(i[0])
  25. if stat.S_ISREG(i[1]) and ((i[1] & stat.S_IRUSR) or (i[1] & stat.S_IRGRP) or (i[1] & stat.S_IROTH)):
  26. files.append(i[0])
  27. else:
  28. unknowns.append(i[0])
  29. return files, directories, unknowns
  30. def tree(path, device):
  31. exclude = ["proc", "sys", "dev"]
  32. all_files = []
  33. all_directories = []
  34. all_unknowns = []
  35. queue = []
  36. current = path
  37. while True:
  38. files, directories, unknowns = scandir(current, device)
  39. for file in files:
  40. all_files.append(current + file.decode('utf-8'))
  41. for directory in directories:
  42. all_directories.append(current + directory.decode('utf-8') + '/')
  43. if directory.decode('utf-8') not in exclude:
  44. queue.append(current + directory.decode('utf-8') + '/')
  45. if not queue:
  46. break
  47. current = queue.pop()
  48. #print(current)
  49. return all_files, all_directories, all_unknowns
  50. device = init_device(port)
  51. if sys.argv[2] == 'ls':
  52. root = device.List(sys.argv[3])
  53. for i in root:
  54. print(i[0].decode('utf-8') + ' Perm: ' + str(oct(i[1])) + ' Size: ' + str(i[2]))
  55. if command == 'pull':
  56. if sys.argv[4]:
  57. target = sys.argv[4]
  58. else:
  59. target = sys.argv[3].replace('/', '_')
  60. root = device.Pull(sys.argv[3], target)
  61. print(root)
  62. if command == 'push':
  63. root = device.Push(sys.argv[3], sys.argv[4])
  64. print(root)
  65. if command == 'logcat':
  66. logcat = device.Logcat()
  67. print(logcat)
  68. if command == 'forward':
  69. print("For port forwarding (ie: for gdbserver) use the original XCB client. xcb.exe connect com:COM12; xcb.exe forward tcp:2020 tcp:2020")
  70. print("The protocol for port forwarding should be ADB compatible. However python-adb doesn't support it as of now")
  71. if command == 'dump':
  72. name = sys.argv[3]
  73. print("[+] Listing everything")
  74. all_files, all_directories, all_unknowns = tree('/', device)
  75. print("[+] Creating local structure")
  76. target = "dumps/" + name + '/'
  77. if not os.path.isdir(target):
  78. os.mkdir(target)
  79. for dir in all_directories:
  80. if not os.path.isdir(target + dir):
  81. os.mkdir(target + dir)
  82. print("[+] Pulling all files")
  83. for file in all_files:
  84. if not os.path.isfile(target + file):
  85. time.sleep(1)
  86. try:
  87. device.Pull(file, target + file)
  88. print("[+] Downloading " + file)
  89. except:
  90. print("[-] Failed downloading " + file)
  91. os.remove(target + file)
  92. # This sucks but...
  93. device = None
  94. time.sleep(5)
  95. device = init_device(port)
  96. print("[+] Saving lists")
  97. with open(target + 'files.txt', 'w') as f:
  98. f.write(json.dumps(all_files))
  99. with open(target + 'directories.txt', 'w') as f:
  100. f.write(json.dumps(all_directories))
  101. with open(target + 'unknowns.txt', 'w') as f:
  102. f.write(json.dumps(all_unknowns))