Esempio n. 1
0
    def run(self) -> None:
        LOGGER.debug('{} start'.format(self.__class__.__name__))

        threads = []

        while True:
            devices = frida.enumerate_devices()

            for device in devices:
                if device.type != 'usb':
                    continue

                duplicated = False

                for t in threads:
                    if t.device.id == device.id:
                        if not t.is_alive():
                            threads.remove(t)
                            break

                        duplicated = True
                        break

                if duplicated:
                    continue
                try:
                    new_thread = FridaThread(device, self.install, self.port,
                                             self.regexps, True)
                    new_thread.start()
                    threads.append(new_thread)
                except Exception as e:
                    LOGGER.error(e)

            time.sleep(0.1)
Esempio n. 2
0
def shutdown(signum, frame):
    if signum == signal.SIGINT:
        LOGGER.debug('keyboard interrupt event detected')
    elif signum == signal.SIGTERM:
        LOGGER.debug('termination event detected')
    else:
        LOGGER.warn('unknown event detected')

    raise MainExit
Esempio n. 3
0
    def shutdown(self):
        LOGGER.debug('shutdown device ' + self.device.id)

        self.kill_frida_servers()

        if self.device.type == 'remote':
            port_manager.release_port(self.forward_port)
            self.adb.clear_forward(self.forward_port)

        if self.port:
            self.iptables.uninstall()
            self.adb.clear_reverse(self.port)
Esempio n. 4
0
    def download(self, url, file_path):
        # get total size of file
        r1 = requests.get(url, stream=True, verify=False)
        total_size = int(r1.headers['Content-Length'])

        # check downloaded size
        if os.path.exists(file_path):
            temp_size = os.path.getsize(file_path)
        else:
            temp_size = 0

        if temp_size == total_size:
            LOGGER.info('{} has downloaded completely'.format(file_path))
            return

        if temp_size > total_size:
            LOGGER.error(
                '{} has corrupted, download it again'.format(file_path))
            os.remove(file_path)
            return self.download(url, file_path)

        LOGGER.debug('{} of {} needs to be download'.format(
            total_size - temp_size, total_size))

        # download from temp size to end
        headers = {'Range': 'bytes={}-'.format(temp_size)}

        r = requests.get(url, stream=True, verify=False, headers=headers)

        with open(file_path, "ab") as f:
            for chunk in r.iter_content(chunk_size=1024):
                if self.stop_flag:
                    break

                if chunk:
                    temp_size += len(chunk)
                    f.write(chunk)
                    f.flush()

                    # download progress
                    done = int(50 * temp_size / total_size)
                    sys.stdout.write("\r[{}{}] {}%".format(
                        '█' * done, ' ' * (50 - done),
                        100 * temp_size / total_size))
                    sys.stdout.flush()

        sys.stdout.write(os.linesep)
Esempio n. 5
0
    def cmd_and_debug(cls, cmd) -> map:
        ret = {'out': '', 'err': ''}

        LOGGER.debug(cmd)
        p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True)

        err = p.stderr.read().decode().strip()
        if err:
            LOGGER.error('shell error: ' + err)
            ret['err'] = err

        out = p.stdout.read().decode().strip()
        if out:
            LOGGER.debug('shell output: ' + out)
            ret['out'] = out

        return ret
Esempio n. 6
0
    def __init__(self, device, install: bool, port: int, regexps: list):
        super().__init__()

        if device.type == FakeDevice.type:
            # init remote device
            LOGGER.debug(
                'device {} does not support get_usb_device, changing to get_remote_device'
                .format(device.id))
            self.forward_port = port_manager.acquire_port()
            self.device = frida.get_device_manager().add_remote_device(
                '127.0.0.1:{}'.format(self.forward_port))
            self.device.id = device.id
        else:
            self.device = device

        self.install = install
        self.port = port
        self.regexps = regexps if regexps else ['.*']

        self.adb = Adb(self.device.id)

        if device.type == FakeDevice.type:
            self.adb.forward(self.forward_port, FRIDA_SERVER_DEFAULT_PORT)

        if self.port:
            self.iptables = Iptables(self.adb, self.port)

        self.arch = self.adb.unsafe_shell("getprop ro.product.cpu.abi")['out']
        # maybe get 'arm64-v8a', 'arm-v7a' ...
        if 'arm64' in self.arch:
            self.arch = 'arm64'
        elif 'arm' in self.arch:
            self.arch = 'arm'
        elif 'x86_64' in self.arch:
            self.arch = 'x86_64'
        elif 'x86' in self.arch:
            self.arch = 'x86'
        else:
            raise RuntimeError('unknown arch: ' + self.arch)

        self.server_name = 'frida-server-{}-android-{}'.format(
            frida.__version__, self.arch)

        self.stop_flag = False

        thread_manager.add_thread(self)
Esempio n. 7
0
    def cmd_and_debug(cls, cmd: str, debug=True) -> map:
        ret = {'out': '', 'err': ''}

        if debug:
            LOGGER.debug(cmd)

        p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True)

        err = p.stderr.read().decode().strip()
        if err:
            if err != 'Unable to start: Error binding to address: Address already in use':
                LOGGER.error('shell error: ' + err)
                ret['err'] = err

        out = p.stdout.read().decode().strip()
        if out:
            if debug:
                LOGGER.debug('shell output: ' + out)
            ret['out'] = out

        return ret
Esempio n. 8
0
    def run(self) -> None:
        LOGGER.info(
            "{} start with hook device: id={}, name={}, type={}".format(
                self.__class__.__name__, self.device.id, self.device.name,
                self.device.type))

        try:
            self.prepare()
            self.hook_apps()
        except Exception as e:
            LOGGER.error('device {}: {}'.format(self.device.id, e))

        try:
            self.shutdown()
        except Exception as e:
            LOGGER.error(
                'unexpected error occurred when shutdown device {}: {}'.format(
                    self.device.id, e))

        LOGGER.debug('device {} exit'.format(self.device.id))
        thread_manager.del_thread(self)
Esempio n. 9
0
    def run(self) -> None:
        LOGGER.debug('{} start'.format(self.__class__.__name__))

        while True:
            if self.stop_flag:
                break

            devices = frida.enumerate_devices()

            # usb devices from frida api
            usb_devices = [
                device for device in devices if device.type == 'usb'
            ]
            usb_devices_ids = [device.id for device in usb_devices]

            # devices strings from "adb devices"
            adb_devices_strings = Shell.cmd_and_debug(
                'adb devices', debug=False)['out'].split('\n')[1:]
            adb_devices_strings = [
                _.split('\t')[0] for _ in adb_devices_strings
            ]

            # we need to access these devices remotely
            remote_devices_strings = set(adb_devices_strings) - set(
                usb_devices_ids)
            remote_devices = []

            for _ in remote_devices_strings:
                new_device = FakeDevice()
                new_device.id = _
                remote_devices.append(new_device)

            for device in usb_devices + remote_devices:
                duplicated = False

                for t in self.frida_threads:
                    if t.device.id == device.id:
                        if not t.is_alive():
                            self.frida_threads.remove(t)
                            break

                        duplicated = True
                        break

                if duplicated:
                    continue

                try:
                    frida_thread = FridaThread(device, self.install, self.port,
                                               self.regexps)
                except RuntimeError as e:
                    LOGGER.error(
                        'error occurred when init frida thread: {}'.format(e))
                else:
                    frida_thread.start()
                    self.frida_threads.append(frida_thread)

            time.sleep(0.1)

        self.shutdown()
        LOGGER.debug('watch thread exit')