def push_and_start_frida_server(adb: ADB): """ Push and start adb server on device Parameters ---------- adb Returns ------- """ frida_server = os.path.join(os.getcwd(), "resources", "frida-server", "frida-server") try: adb.execute(['root']) except Exception as e: adb.kill_server() logger.error("Error on adb {}".format(e)) logger.info("Push frida server") try: adb.push_file(frida_server, "/data/local/tmp") except Exception as e: pass logger.info("Add execution permission to frida-server") chmod_frida = ["chmod 755 /data/local/tmp/frida-server"] adb.shell(chmod_frida) logger.info("Start frida server") start_frida = ["cd /data/local/tmp && ./frida-server &"] adb.shell(start_frida, is_async=True) time.sleep(4)
def push_and_start_frida_server(adb: ADB): """ Push and start adb server on device Parameters ---------- adb Returns ------- """ frida_server = os.path.join(os.path.dirname(__file__), "resources", "frida-server", "frida-server") cmd_output = adb.shell("ps -e | grep frida".split()) if "frida-server" in cmd_output: logger.warning("[*] frida-server is already running on device") return try: adb.execute(["root"]) except Exception as e: adb.kill_server() logger.error("Error on adb {}".format(e)) logger.info("[*] Push frida server") try: adb.push_file(frida_server, "/data/local/tmp") except Exception as e: pass logger.info("[*] Add execution permission to frida-server") chmod_frida = ["chmod 755 /data/local/tmp/frida-server"] adb.shell(chmod_frida) logger.info("Start frida server") start_frida = ["cd /data/local/tmp && ./frida-server &"] adb.shell(start_frida, is_async=True) time.sleep(4)
def pull_api_monitor_xposed(adb: ADB, package_name: str, result_directory: str, md5_app: str = None): """ Parameters ---------- adb package_name result_directory md5_app Returns ------- """ extracted_log_path = os.path.join(result_directory, 'monitoring_api_{}.log'.format(md5_app)) try: adb.execute(['root']) except Exception: adb.kill_server() adb.pull_file('/data/data/{0}/TalosApiMonitor/apimonitor.log'.format(package_name), extracted_log_path)
class logManager(object): def __init__(self, p2adb, logtype="logcat", pollrate=0.5): self.adb = ADB(p2adb) self.logtype = logtype self.pollrate = pollrate self._event_list = [] self._isRunning = False self.logcat_monitor_tread = None self.cacheMutex = Lock() self.cacheLineNum = 0 def __logcat_monitor_func(self): (stdout, stderr) = self.adb.execute(' shell "logcat -c"') while self._isRunning: sleep(self.pollrate) (stdout, stderr) = self.adb.execute(' shell "logcat -d && logcat -c"') self.cacheMutex.acquire() self.cacheLineNum += len(stdout.splitlines()) with open(cacheFilePath, "a+") as f: f.write(stdout) self.cacheMutex.release() for event in self._event_list: match = event.regex.search(stdout) if match: event.cb(match) def start(self): # create cache file if os.path.exists(cacheFilePath): os.remove(cacheFilePath) # create thread for logcat monitor self.logcat_monitor_tread = Thread(target=self.__logcat_monitor_func) self.logcat_monitor_tread.daemon = False self._isRunning = True self.logcat_monitor_tread.start() def stop(self): self._isRunning = False self.logcat_monitor_tread.join() if os.path.exists(cacheFilePath): os.remove(cacheFilePath) def registerEvent(self, cb=None, params=None, regex=""): if cb == None or regex == "": return -1 self.cacheMutex.acquire() e = event(cb, params, regex, self.cacheLineNum) self._event_list.append(e) ret = len(self._event_list) - 1 self.cacheMutex.release() return ret def getLogHistory(self, eventhandle): self.cacheMutex.acquire() # assume evenhandle always exist, need to refine with open(cacheFilePath, "r") as f: loghist = f.readlines()[self._event_list[eventhandle].startline:] self.cacheMutex.release() return ''.join(loghist)