Esempio n. 1
0
 def run_driver_cmd(self, cmd, *args, **kwargs):
     '''执行驱动命令
     
     :param cmd:  命令
     :type  cmd:  string
     '''
     args = [('"%s"' % (it.replace('"', r'\"') if isinstance(it, str) else it)) for it in args]
     result = self.adb.run_shell_cmd('sh %s/SpyHelper.sh %s %s' % (qt4a_path, cmd, ' '.join(args)), **kwargs)
     if 'No such file or directory' in result:
         raise QT4ADriverNotInstalled('Please install QT4A driver first')
     return result
Esempio n. 2
0
 def run_driver_cmd(self, cmd, *args, **kwargs):
     """执行驱动命令
     
     :param cmd:  命令
     :type  cmd:  string
     """
     args = [
         ('"%s"' % (it.replace('"', r"\"") if isinstance(it, str) else it))
         for it in args
     ]
     result = self.adb.run_shell_cmd(
         "sh %s/SpyHelper.sh %s %s" % (qt4a_path, cmd, " ".join(args)),
         **kwargs)
     if "No such file or directory" in result:
         raise QT4ADriverNotInstalled("Please install QT4A driver first")
     return result
Esempio n. 3
0
    def _init_driver(self):
        """初始化测试桩
        """
        self._client = self._create_client()
        if self._client != None:
            # 字段赋值
            self._process["name"] = self._process_name
            self._process["id"] = 0  # process id may change
            if self.hello() != None:
                self._process["id"] = self._adb.get_pid(self._process_name)
                return

        timeout = 20
        time0 = time.time()
        proc_exist = False
        while time.time() - time0 < timeout:
            if not proc_exist:
                pid = self._adb.get_pid(self._process_name)
                if pid > 0:
                    proc_exist = True
                    self._process["name"] = self._process_name
                    self._process["id"] = pid
                    break

            time.sleep(1)

        if not proc_exist:
            raise RuntimeError("进程:%s 在%d秒内没有出现" %
                               (self._process_name, timeout))

        inject_file = "inject"
        if self._adb.is_app_process64(
                pid if self._adb.is_rooted() else self._process_name):
            # 64 bit process
            inject_file += "64"
        timeout = 30

        try:
            if self._adb.is_art():
                # Android 5.0上发现注入容易导致进程退出
                self._wait_for_cpu_low(20, 10)

            time0 = time.time()
            cmdline = "%s/%s %s" % (
                self._get_driver_root_path(),
                inject_file,
                self._process_name,
            )
            while time.time() - time0 < timeout:
                if self._adb.is_rooted():
                    ret = self._adb.run_shell_cmd(cmdline,
                                                  True,
                                                  timeout=120,
                                                  retry_count=1)
                else:
                    ret = self._adb.run_as(self._process_name,
                                           cmdline,
                                           timeout=120,
                                           retry_count=1)
                logger.debug("inject result: %s" % ret)
                if "not found" in ret:
                    raise QT4ADriverNotInstalled(
                        "QT4A driver damaged, please reinstall QT4A driver")
                if "Inject Success" in ret:
                    break
                elif "Operation not permitted" in ret:
                    # 可能是进程处于Trace状态
                    pid = self._adb.get_pid(self._process_name)
                    status = self._adb.get_process_status(pid)
                    tracer_pid = int(status["TracerPid"])
                    if tracer_pid > 0:
                        if int(status["PPid"]) == tracer_pid:
                            # 使用TRACEME方式防注入
                            raise Exception("应用使用了防注入逻辑,注入失败")
                        logger.warn("TracerPid is %d" % tracer_pid)
                        self._adb.kill_process(tracer_pid)
                elif "Function not implemented" in ret:
                    raise Exception(
                        "Please install repacked app on this device")
                time.sleep(1)

        except RuntimeError as e:
            logger.exception("%s\n%s" % (e, self._adb.run_shell_cmd("ps")))
            if self._adb.is_rooted():
                logger.info(self._adb.dump_stack(self._process_name))
            raise e
        timeout = 10
        time0 = time.time()
        while time.time() - time0 < timeout:
            if self._client == None:
                self._client = self._create_client()
            if self._client != None and self.hello() != None:
                return
            time.sleep(0.1)
        raise RuntimeError("连接测试桩超时")
Esempio n. 4
0
    def _init_driver(self):
        '''初始化测试桩
        '''
        self._client = self._create_client()
        if self._client != None:
            # 字段赋值
            self._process['name'] = self._process_name
            self._process['id'] = 0  # process id may change
            if self.hello() != None:
                self._process['id'] = self._adb.get_pid(self._process_name)
                return

        timeout = 20
        time0 = time.time()
        proc_exist = False
        while time.time() - time0 < timeout:
            if not proc_exist:
                pid = self._adb.get_pid(self._process_name)
                if pid > 0:
                    proc_exist = True
                    self._process['name'] = self._process_name
                    self._process['id'] = pid
                    break

            time.sleep(1)

        if not proc_exist:
            raise RuntimeError('进程:%s 在%d秒内没有出现' %
                               (self._process_name, timeout))

        inject_file = 'inject'
        if self._adb.is_app_process64(
                pid if self._adb.is_rooted() else self._process_name):
            # 64 bit process
            inject_file += '64'
        timeout = 30

        try:
            if self._adb.is_art():
                # Android 5.0上发现注入容易导致进程退出
                self._wait_for_cpu_low(20, 10)

            time0 = time.time()
            cmdline = '%s/%s %s' % (self._get_driver_root_path(), inject_file,
                                    self._process_name)
            while time.time() - time0 < timeout:
                if self._adb.is_rooted():
                    ret = self._adb.run_shell_cmd(cmdline,
                                                  True,
                                                  timeout=120,
                                                  retry_count=1)
                else:
                    ret = self._adb.run_as(self._process_name,
                                           cmdline,
                                           timeout=120,
                                           retry_count=1)
                logger.debug('inject result: %s' % ret)
                if 'not found' in ret:
                    raise QT4ADriverNotInstalled(
                        'QT4A driver damaged, please reinstall QT4A driver')
                if 'Inject Success' in ret:
                    break
                elif 'Operation not permitted' in ret:
                    # 可能是进程处于Trace状态
                    pid = self._adb.get_pid(self._process_name)
                    status = self._adb.get_process_status(pid)
                    tracer_pid = int(status['TracerPid'])
                    if tracer_pid > 0:
                        if int(status['PPid']) == tracer_pid:
                            # 使用TRACEME方式防注入
                            raise Exception('应用使用了防注入逻辑,注入失败')
                        logger.warn('TracerPid is %d' % tracer_pid)
                        self._adb.kill_process(tracer_pid)
                time.sleep(1)

        except RuntimeError as e:
            logger.exception('%s\n%s' % (e, self._adb.run_shell_cmd('ps')))
            if self._adb.is_rooted():
                logger.info(self._adb.dump_stack(self._process_name))
            raise e
        timeout = 10
        time0 = time.time()
        while time.time() - time0 < timeout:
            if self._client == None:
                self._client = self._create_client()
            if self._client != None and self.hello() != None:
                return
            time.sleep(0.1)
        raise RuntimeError('连接测试桩超时')