prolin-xcb-client/client.py

116 lines
3.2 KiB
Python
Raw Permalink Normal View History

2019-05-14 08:40:17 +02:00
from adb import adb_commands
from adb import sign_m2crypto
import stat
2019-05-14 08:40:17 +02:00
import sys
import os
import time
import json
2019-05-14 08:40:17 +02:00
2021-05-16 11:05:57 +02:00
tty = sys.argv[1]
port = '/dev/tty{},115200'.format(tty)
command = sys.argv[2]
2019-05-14 08:40:17 +02:00
def init_device(addr):
device = adb_commands.AdbCommands()
device.ConnectDevice(port_path=None, serial=addr)
#device.ConnectDevice(port_path=None, serial="192.168.43.168:5555")
#device.ConnectDevice(port_path=None, serial="/dev/ttyS4,115200")
return device
2020-05-27 01:12:28 +02:00
def scandir(path, device):
files = []
directories = []
unknowns = []
result = device.List(path)
for i in result[2:]:
if stat.S_ISDIR(i[1]):
directories.append(i[0])
if stat.S_ISREG(i[1]) and ((i[1] & stat.S_IRUSR) or (i[1] & stat.S_IRGRP) or (i[1] & stat.S_IROTH)):
files.append(i[0])
else:
unknowns.append(i[0])
return files, directories, unknowns
2019-05-14 08:40:17 +02:00
def tree(path, device):
exclude = ["proc", "sys", "dev"]
all_files = []
all_directories = []
all_unknowns = []
queue = []
current = path
while True:
files, directories, unknowns = scandir(current, device)
for file in files:
all_files.append(current + file.decode('utf-8'))
for directory in directories:
all_directories.append(current + directory.decode('utf-8') + '/')
if directory.decode('utf-8') not in exclude:
queue.append(current + directory.decode('utf-8') + '/')
if not queue:
break
current = queue.pop()
#print(current)
return all_files, all_directories, all_unknowns
device = init_device(port)
2021-05-16 11:05:57 +02:00
if sys.argv[2] == 'ls':
root = device.List(sys.argv[3])
2019-05-14 08:40:17 +02:00
for i in root:
print(i[0].decode('utf-8') + ' Perm: ' + str(oct(i[1])) + ' Size: ' + str(i[2]))
2021-05-16 11:05:57 +02:00
if command == 'pull':
2021-05-16 11:36:00 +02:00
if sys.argv[4]:
target = sys.argv[4]
else:
target = sys.argv[3].replace('/', '_')
root = device.Pull(sys.argv[3], target)
2019-05-14 08:40:17 +02:00
print(root)
2021-05-16 11:05:57 +02:00
if command == 'push':
root = device.Push(sys.argv[3], sys.argv[4])
2019-05-14 08:40:17 +02:00
print(root)
2021-05-16 11:05:57 +02:00
if command == 'logcat':
logcat = device.Logcat()
print(logcat)
2021-05-16 11:05:57 +02:00
if command == 'forward':
print("For port forwarding (ie: for gdbserver) use the original XCB client. xcb.exe connect com:COM12; xcb.exe forward tcp:2020 tcp:2020")
print("The protocol for port forwarding should be ADB compatible. However python-adb doesn't support it as of now")
2021-05-16 11:05:57 +02:00
if command == 'dump':
name = sys.argv[3]
print("[+] Listing everything")
all_files, all_directories, all_unknowns = tree('/', device)
print("[+] Creating local structure")
target = "dumps/" + name + '/'
if not os.path.isdir(target):
os.mkdir(target)
for dir in all_directories:
if not os.path.isdir(target + dir):
os.mkdir(target + dir)
print("[+] Pulling all files")
for file in all_files:
if not os.path.isfile(target + file):
time.sleep(1)
try:
device.Pull(file, target + file)
print("[+] Downloading " + file)
except:
print("[-] Failed downloading " + file)
os.remove(target + file)
# This sucks but...
device = None
time.sleep(5)
device = init_device(port)
print("[+] Saving lists")
with open(target + 'files.txt', 'w') as f:
f.write(json.dumps(all_files))
with open(target + 'directories.txt', 'w') as f:
f.write(json.dumps(all_directories))
with open(target + 'unknowns.txt', 'w') as f:
f.write(json.dumps(all_unknowns))