def _get_cmd_bin(): adb_path = '' try: adb_msg = 'not found adb binary' if find_bin_by(settings.ADB_BINARY): adb_path = settings.ADB_BINARY if platform.system() == 'Windows': if find_bin_by('adb.exe'): adb_path = 'adb.exe' else: if find_bin_by('adb'): adb_path = 'adb' return adb_path except Exception as e: if not adb_path: msflogger.warning( f'Exception: Cannot find adb executable! {adb_msg} {str(e)}. ') finally: if adb_path: os.environ['MOBSF_ADB'] = adb_path else: os.environ['MOBSF_ADB'] = '' msflogger.warning('not found adb executable! %s. ', adb_msg) return adb_path
def cmd_execute(cmd: str, device_id: str, timeout: int = 5): ''' Execute ADB Commands. :param cmd: :param device_id: :param timeout: :return: ''' if cmd: if device_id: args = [_get_cmd_bin(), '-s', device_id] else: args = [_get_cmd_bin()] try: end_time = datetime.datetime.now() + datetime.timedelta( seconds=timeout) sub = subprocess.Popen(args + cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: if sub.poll() is not None: break time.sleep(0.1) if timeout and end_time <= datetime.datetime.now(): sub.kill() return '', 'cmd execute timeout' return sub.stdout.read(), sub.stderr.read() except Exception as e: msflogger.exception(f'Executing ADB Commands. {str(e)}') else: msflogger.warning( f'cmd execute params error cmd({cmd}) device_id({device_id})') return '', 'cmd execute exception'
def delete(self, cls, where, limit=1, offset=0): try: with db.get_session() as session: return session.query(cls).filter_by(**where).delete().limit(limit).offset(offset) > 0 except Exception as e: msflogger.warning(f'delete data fail. e:{str(e)}') return False
def update(self, cls, where, update_data, limit=1): try: with db.get_session() as session: return session.query(cls).filter_by(**where).update(**update_data).limit(limit) > 0 except Exception as e: msflogger.warning(f'update data fail. e:{str(e)}') return False
def add(self, cls, add_data): try: with db.get_session() as session: return session.add(cls(**add_data)) > 0 except Exception as e: msflogger.warning(f'add data fail. e:{str(e)}') return False
def send_message(self, topic, msg): try: f = self.producer.send(topic=topic, value=msg) print(f.exception, f.is_done) print(f.succeeded()) except Exception as e: msflogger.warning( f'send kafka message topic({topic}) error: {str(e)}') return None
def select(self, cls, where, one=False, limit=1, offset=0): try: with db.get_session() as session: if one: return session.query(cls).filter_by(**where).limit(limit).offset(offset).first() else: return session.query(cls).filter_by(**where).limit(limit).offset(offset).all() except Exception as e: msflogger.warning(f'select data fail. e:{str(e)}') return None