commit d3706c129c364dae46e4282230d83a7285119fab Author: kass Date: Sun Aug 11 13:28:08 2024 -0400 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6985b0c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +twrp.img +*.ab +*.tar.gz \ No newline at end of file diff --git a/helpers/adb/adb.py b/helpers/adb/adb.py new file mode 100644 index 0000000..b336cbd --- /dev/null +++ b/helpers/adb/adb.py @@ -0,0 +1,31 @@ +import os, time +from ppadb.client import Client as AdbClient + +def connect_device(): + os.system('adb start-server') + client = AdbClient(host="127.0.0.1", port=5037) + devices = client.devices() + if len(devices) == 1: + device = client.device(devices[0].serial) + return device + elif len(devices) == 0: + raise ConnectionError + else: + n = 1 + for device in devices: + print(f'[{n}] {device.serial}') + n += 1 + choice = input('Please enter the number of the device you would like\ + to connect to: ') + device = client.device(devices[choice-1].serial) + return device + +def shell(device, command): + return device.shell(command) + +def reboot_bootloader(): + print("Rebooting to Bootloader...") + os.system('adb reboot-bootloader') + +def backup(): + os.system('adb backup -all') \ No newline at end of file diff --git a/helpers/fastboot/fastboot.py b/helpers/fastboot/fastboot.py new file mode 100644 index 0000000..9d031c0 --- /dev/null +++ b/helpers/fastboot/fastboot.py @@ -0,0 +1,20 @@ +import os, sys +def devices(): + os.system('fastboot devices') + +def unlock(): + unlock = os.popen('fastboot getvar unlocked 2>&1').read() + if "yes" not in unlock: + check_unlock = os.popen('fastboot flashing get_unlock_ability 2>&1').read() + if check_unlock == "(bootloader) get_unlock_ability: 1": + input('Please note: Unlocking the bootloader will wipe any data on the device. If you need to pull data from a locked device please exit the program and use a different method!!') + os.system('fastboot flashing unlock') + input('Ensure that unlocking is complete on the device before pressing enter.') + else: + print("OEM Unlocking not supported on this device.") + sys.exit() + else: + print("Device already unlocked.") + +def boot(img): + os.system('fastboot boot ' + img) \ No newline at end of file diff --git a/helpers/platform-tools/Linux/adb b/helpers/platform-tools/Linux/adb new file mode 100644 index 0000000..48a0d4a Binary files /dev/null and b/helpers/platform-tools/Linux/adb differ diff --git a/helpers/platform-tools/Linux/fastboot b/helpers/platform-tools/Linux/fastboot new file mode 100644 index 0000000..738393b Binary files /dev/null and b/helpers/platform-tools/Linux/fastboot differ diff --git a/helpers/platform-tools/Linux/lib64/libc++.so b/helpers/platform-tools/Linux/lib64/libc++.so new file mode 100644 index 0000000..e543bc0 Binary files /dev/null and b/helpers/platform-tools/Linux/lib64/libc++.so differ diff --git a/helpers/platform-tools/Windows/AdbWinApi.dll b/helpers/platform-tools/Windows/AdbWinApi.dll new file mode 100644 index 0000000..7abe26c Binary files /dev/null and b/helpers/platform-tools/Windows/AdbWinApi.dll differ diff --git a/helpers/platform-tools/Windows/AdbWinUsbApi.dll b/helpers/platform-tools/Windows/AdbWinUsbApi.dll new file mode 100644 index 0000000..e7a6de1 Binary files /dev/null and b/helpers/platform-tools/Windows/AdbWinUsbApi.dll differ diff --git a/helpers/platform-tools/Windows/adb.exe b/helpers/platform-tools/Windows/adb.exe new file mode 100644 index 0000000..9f5b737 Binary files /dev/null and b/helpers/platform-tools/Windows/adb.exe differ diff --git a/helpers/platform-tools/Windows/fastboot.exe b/helpers/platform-tools/Windows/fastboot.exe new file mode 100644 index 0000000..712b1bd Binary files /dev/null and b/helpers/platform-tools/Windows/fastboot.exe differ diff --git a/helpers/platform-tools/Windows/libwinpthread-1.dll b/helpers/platform-tools/Windows/libwinpthread-1.dll new file mode 100644 index 0000000..ed00b46 Binary files /dev/null and b/helpers/platform-tools/Windows/libwinpthread-1.dll differ diff --git a/helpers/unpackBackup.py b/helpers/unpackBackup.py new file mode 100644 index 0000000..c6f6913 --- /dev/null +++ b/helpers/unpackBackup.py @@ -0,0 +1,116 @@ +# ABtoTar.py +# Change the Android Backup file (*.ab) into a tar for fun and profit. +# Basically; remove the first 24 bytes and replace them with a tar header. +# Ed Greybeard 2021 + +from tarfile import is_tarfile +import os.path +import logging +import argparse +from sys import exit + +AB_HEADER = b"ANDROID BACKUP" +TAR_HEADER = b"\x1f\x8b\x08\x00\x00\x00\x00\x00" +IGNORE_OFFSET = 24 + + +logging.basicConfig(format='%(asctime)s | %(levelname)s | %(message)s', level='DEBUG') + +def extract_tar_from_ab(path_to_ab, output_dir=None): + """ + An AB file is a tar with 8 bytes added to the start. + This will extract the tar to the same directory as the AB file unless output_dir is specified. + :param path_to_ab: + :param output_dir: + :return: + """ + if output_dir is None: + output_dir = os.path.dirname(path_to_ab) + + # Check we can open the file + try: + ab_data = open(path_to_ab, 'rb') + except: + logging.critical(f"Unable to open AB file at {path_to_ab}") + return False + + # Check the AB file header is intact + ab_bytes_to_remove = ab_data.read(24) + + if ab_bytes_to_remove[:14] == AB_HEADER: + logging.info("AB Header checked and intact") + else: + logging.error("AB Header not found; is it definitely the right file?") + return False + + # Open the target tar file + output_path = build_tar_filepath(input_path, output_dir) + + try: + output_file = open(output_path, 'wb') + except: + logging.error("Unable to open file at {output_path}") + return False + + logging.info("Writing tar header..") + output_file.write(TAR_HEADER) + + logging.info("Writing rest of AB file..") + output_file.write(ab_data.read()) + + logging.info("..done.") + logging.info("Closing files..") + + output_file.close() + ab_data.close() + + # quick verify + try: + test_val = is_tarfile(output_path) + logging.info("Output verified OK") + return True + except: + logging.error("Verification failed; maybe it's encrypted?") + return False + + +def build_tar_filepath(input_path, output_dir): + input_filename = os.path.splitext(os.path.basename(input_path))[0] + output_filename = f"{input_filename}.tar.gz" + logging.info(f"Output filename: {output_filename}") + output_filepath = os.path.join(output_dir, output_filename) + return output_filepath + + +if __name__ == '__main__': + logging.info("-------------------") + logging.info("AB to Tar") + logging.info("By Greybeard") + logging.info("-------------------") + logging.info("Global variables:") + logging.info(f"AB_HEADER: {AB_HEADER}") + logging.info(f"TAR_HEADER: {TAR_HEADER}") + logging.info(f"BYTES TO IGNORE: {IGNORE_OFFSET}") + + parser = argparse.ArgumentParser(description="Convert an Android Backup (AB) file to a tar") + parser.add_argument('-i', '--input', dest='input_path', type=str, required=True, action='store', + help='AB file') + parser.add_argument('-o', '--output', dest='output_dir', type=str, default=None, + help='Output directory (default: same as AB file)') + + args = parser.parse_args() + + input_path = args.input_path + output_dir = args.output_dir + + if os.path.exists(input_path) is not True: + logging.critical(f"{input_path} not found. Exiting..") + exit(1) + + logging.info(f"Input file: {input_path}") + + if extract_tar_from_ab(input_path, output_dir) is True: + logging.info("Processing complete.") + else: + logging.info("Error with processing. Exiting..") + diff --git a/helpers/usb_driver/amd64/WUDFUpdate_01009.dll b/helpers/usb_driver/amd64/WUDFUpdate_01009.dll new file mode 100644 index 0000000..1424634 Binary files /dev/null and b/helpers/usb_driver/amd64/WUDFUpdate_01009.dll differ diff --git a/helpers/usb_driver/amd64/WdfCoInstaller01009.dll b/helpers/usb_driver/amd64/WdfCoInstaller01009.dll new file mode 100644 index 0000000..1731b96 Binary files /dev/null and b/helpers/usb_driver/amd64/WdfCoInstaller01009.dll differ diff --git a/helpers/usb_driver/amd64/winusbcoinstaller2.dll b/helpers/usb_driver/amd64/winusbcoinstaller2.dll new file mode 100644 index 0000000..30e5502 Binary files /dev/null and b/helpers/usb_driver/amd64/winusbcoinstaller2.dll differ diff --git a/helpers/usb_driver/android_winusb.inf b/helpers/usb_driver/android_winusb.inf new file mode 100644 index 0000000..39c9821 --- /dev/null +++ b/helpers/usb_driver/android_winusb.inf @@ -0,0 +1,195 @@ +; +; Android WinUsb driver installation. +; +[Version] +Signature = "$Windows NT$" +Class = AndroidUsbDeviceClass +ClassGuid = {3F966BD9-FA04-4ec5-991C-D326973B5128} +Provider = %ProviderName% +DriverVer = 08/28/2014,11.0.0000.00000 +CatalogFile.NTx86 = androidwinusb86.cat +CatalogFile.NTamd64 = androidwinusba64.cat + +[ClassInstall32] +Addreg = AndroidWinUsbClassReg + +[AndroidWinUsbClassReg] +HKR,,,0,%ClassName% +HKR,,Icon,,-1 + + +[Manufacturer] +%ProviderName% = Google, NTx86, NTamd64 + + +[Google.NTx86] + +;Google Nexus One +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_0D02 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_0D02&MI_01 +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_4E11 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E12&MI_01 + +;Google Nexus S +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_4E21 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E22&MI_01 +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_4E23 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E24&MI_01 + +;Google Nexus 7 +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_4E40 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E42&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E44&MI_01 + +;Google Nexus Q +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_2C10 +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_2C11 + +;Google Nexus (generic) +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_4EE0 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE2&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE4&MI_02 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE6&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE7 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_D001 + +;Google Glass +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_9001 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_9001&MI_01 + +;Google Glass EE1 + +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_9003 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_9003&MI_01 +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_9004 + +;Google Glass EE2 + +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_9005 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_9005&MI_00 +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_9006 + +;Project Tango (generic) +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_4D00 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D02&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D04&MI_02 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D06&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D07 + + +[Google.NTamd64] + +;Google Nexus One +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_0D02 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_0D02&MI_01 +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_4E11 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E12&MI_01 + +;Google Nexus S +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_4E21 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E22&MI_01 +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_4E23 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E24&MI_01 + +;Google Nexus 7 +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_4E40 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E42&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E44&MI_01 + +;Google Nexus Q +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_2C10 +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_2C11 + +;Google Nexus (generic) +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_4EE0 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE2&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE4&MI_02 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE6&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4EE7 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_D001 + +;Google Glass +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_9001 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_9001&MI_01 + +;Google Glass EE1 + +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_9003 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_9003&MI_01 +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_9004 + +;Google Glass EE2 + +%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_9005 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_9005&MI_00 +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_9006 + +;Project Tango (generic) +%SingleBootLoaderInterface% = USB_Install, USB\VID_18D1&PID_4D00 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D02&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D04&MI_02 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D06&MI_01 +%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4D07 + + +[USB_Install] +Include = winusb.inf +Needs = WINUSB.NT + +[USB_Install.Services] +Include = winusb.inf +AddService = WinUSB,0x00000002,WinUSB_ServiceInstall + +[WinUSB_ServiceInstall] +DisplayName = %WinUSB_SvcDesc% +ServiceType = 1 +StartType = 3 +ErrorControl = 1 +ServiceBinary = %12%\WinUSB.sys + +[USB_Install.Wdf] +KmdfService = WINUSB, WinUSB_Install + +[WinUSB_Install] +KmdfLibraryVersion = 1.9 + +[USB_Install.HW] +AddReg = Dev_AddReg + +[Dev_AddReg] +HKR,,DeviceInterfaceGUIDs,0x10000,"{F72FE0D4-CBCB-407d-8814-9ED673D0DD6B}" + +[USB_Install.CoInstallers] +AddReg = CoInstallers_AddReg +CopyFiles = CoInstallers_CopyFiles + +[CoInstallers_AddReg] +HKR,,CoInstallers32,0x00010000,"WdfCoInstaller01009.dll,WdfCoInstaller","WinUSBCoInstaller2.dll" + +[CoInstallers_CopyFiles] +WinUSBCoInstaller2.dll +WdfCoInstaller01009.dll + +[DestinationDirs] +CoInstallers_CopyFiles=11 + +[SourceDisksNames] +1 = %DISK_NAME%,,,\i386 +2 = %DISK_NAME%,,,\amd64 + +[SourceDisksFiles.x86] +WinUSBCoInstaller2.dll = 1 +WdfCoInstaller01009.dll = 1 + +[SourceDisksFiles.amd64] +WinUSBCoInstaller2.dll = 2 +WdfCoInstaller01009.dll = 2 + +[Strings] +ProviderName = "Google, Inc." +SingleAdbInterface = "Android ADB Interface" +CompositeAdbInterface = "Android Composite ADB Interface" +SingleBootLoaderInterface = "Android Bootloader Interface" +WinUSB_SvcDesc = "Android USB Driver" +DISK_NAME = "Android WinUsb installation disk" +ClassName = "Android Device" diff --git a/helpers/usb_driver/androidwinusb86.cat b/helpers/usb_driver/androidwinusb86.cat new file mode 100644 index 0000000..3aa4b92 Binary files /dev/null and b/helpers/usb_driver/androidwinusb86.cat differ diff --git a/helpers/usb_driver/androidwinusba64.cat b/helpers/usb_driver/androidwinusba64.cat new file mode 100644 index 0000000..40636d9 Binary files /dev/null and b/helpers/usb_driver/androidwinusba64.cat differ diff --git a/helpers/usb_driver/i386/WUDFUpdate_01009.dll b/helpers/usb_driver/i386/WUDFUpdate_01009.dll new file mode 100644 index 0000000..f19c370 Binary files /dev/null and b/helpers/usb_driver/i386/WUDFUpdate_01009.dll differ diff --git a/helpers/usb_driver/i386/WdfCoInstaller01009.dll b/helpers/usb_driver/i386/WdfCoInstaller01009.dll new file mode 100644 index 0000000..30e81af Binary files /dev/null and b/helpers/usb_driver/i386/WdfCoInstaller01009.dll differ diff --git a/helpers/usb_driver/i386/winusbcoinstaller2.dll b/helpers/usb_driver/i386/winusbcoinstaller2.dll new file mode 100644 index 0000000..fc450d2 Binary files /dev/null and b/helpers/usb_driver/i386/winusbcoinstaller2.dll differ diff --git a/helpers/usb_driver/source.properties b/helpers/usb_driver/source.properties new file mode 100644 index 0000000..ad91904 --- /dev/null +++ b/helpers/usb_driver/source.properties @@ -0,0 +1,3 @@ +Pkg.Revision=13 +Archive.HostOs=WINDOWS +Extra.Path=usb_driver diff --git a/install.py b/install.py new file mode 100644 index 0000000..7d8887d --- /dev/null +++ b/install.py @@ -0,0 +1,32 @@ +from shutil import which +import os, sys, platform + + +def install_adb_fastboot_windows(): + if which('adb') is None and which('fastboot') is None: + print('ADB/Fastboot not found. Temporarily adding to PATH...') + os.environ['PATH'] += ';'+os.getcwd()+'\\helpers\\platform-tools\\Windows' + os.system('C:\\Windows\\System32\\PNPUTIL.exe -i -a ' + os.getcwd(), + + '\\helpers\\usb_driver\\android_winusb.inf') + else: + print('ADB/Fastboot are installed') + + +def install_adb_fastboot_linux(): + if which('adb') is None and which('fastboot') is None: + print('ADB/Fastboot not found. Temporarily adding to PATH...') + os.environ['PATH'] += ';'+os.getcwd()+'\\helpers\\platform-tools\\Linux' + else: + print('ADB/Fastboot are installed') + +def main(): + if platform.system() == 'Linux': + install_adb_fastboot_linux() + elif platform.system() == 'Windows': + install_adb_fastboot_windows() + else: + print('Unsupported OS') + sys.exit() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..0b3ccb9 --- /dev/null +++ b/main.py @@ -0,0 +1,69 @@ +import helpers.adb.adb as adb, helpers.fastboot.fastboot as fastboot, install, time, os, requests, urllib.request, sys, helpers.unpackBackup as unpackBackup +from lxml import html + +def download_twrp(): + cpu = os.popen('adb shell getprop ro.product.board').read() + cpu = cpu.rstrip('\n') + r = urllib.request.urlopen('https://dl.twrp.me/'+str(cpu)) + text = r.read().decode('utf-8') + tree = html.fromstring(text) + urls = tree.xpath('//a/@href') + downloads = [] + for i in urls: + if "img" in i: + downloads.append(i) + url_to_download = "https://dl.twrp.me"+downloads[0] + url_to_download = url_to_download.replace('.html', '') + s = requests.Session() + s.headers.update({'referer': url_to_download}) + img = s.get(url_to_download) + with open("twrp.img", 'wb') as f: + f.write(img.content) + +def device_pull(): + install.main() + print("connecting device...") + try: + device = adb.connect_device() + except: + print("Unable to connect to device. Ensure that Debug Mode is enabled and USB Debugging is allowed") + sys.exit() + print("getting twrp...") + download_twrp() + adb.reboot_bootloader() + fastboot.devices() + fastboot.unlock() + fastboot.boot('./twrp.img') + time.sleep(20) + try: + device = adb.connect_device() + except: + time.sleep(10) + device = adb.connect_device() + command = adb.shell(device, "whoami") + print(command) + +def menu(): + print('Acquisition Script for Mobile Resources') + print("---------------------------------------") + print("[1] Device Pull") + print("[2] Application Pull") + print("[3] ADB Backup") + print("[99] Exit") + +def main(): + menu() + choice = int(input("Please select an option: ")) + match choice: + case 1: + device_pull() + case 2: + pass + case 3: + adb.backup() + os.system("python helpers/unpackBackup.py -i ./backup.ab") + case 99: + sys.exit() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..6516e2c --- /dev/null +++ b/test.py @@ -0,0 +1,5 @@ +import os, subprocess +unlock = os.popen('fastboot getvar unlocked 2>&1').read() +print(unlock) +#if "yes" not in unlock: +# print(True) \ No newline at end of file