prolin-xcb-client/client.py

110 lines
3.1 KiB
Python
Raw 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
port = '/dev/ttyS3,115200'
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)
2019-05-14 08:40:17 +02:00
if sys.argv[1] == 'ls':
root = device.List(sys.argv[2])
for i in root:
print(i[0].decode('utf-8') + ' Perm: ' + str(oct(i[1])) + ' Size: ' + str(i[2]))
if sys.argv[1] == 'pull':
2019-05-14 08:40:17 +02:00
root = device.Pull(sys.argv[2], sys.argv[2].replace('/', '_'))
print(root)
if sys.argv[1] == 'push':
2019-05-14 08:40:17 +02:00
root = device.Push(sys.argv[2], sys.argv[3])
print(root)
if sys.argv[1] == 'logcat':
logcat = device.Logcat()
print(logcat)
if sys.argv[1] == '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")
if sys.argv[1] == 'dump':
name = sys.argv[2]
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))