client.py 3.1 KB

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