def unregister(self, guid): self.guid_is_valid(guid) if guid in self.sessions: raise CmdError("You can't unregister an active session. Kill then purge the session first.") with STDatabase() as db: db.remove_session(guid) logging.info(f"Unregistering session: {guid}") return {"guid": str(guid)}
def test_database_ops(): guid = uuid.uuid4() psk = gen_stager_psk() with STDatabase(db_path=TEST_DB_PATH) as db: _psk = db.add_session(guid, psk) assert _psk == psk # Test to make sure nothing errors out if we try to add a second session with the same guid & psk _no_psk = db.add_session(guid, psk) assert _no_psk == None _psk = db.get_session_psk(guid) assert _psk == psk sessions = db.get_sessions() assert len(sessions) == 1
def __init__(self, teamserver): self.teamserver = teamserver self.selected = None self.sessions = set() ipc_server.attach(Events.KEX, self.kex) ipc_server.attach(Events.ENCRYPT_STAGE, self.gen_encrypted_stage) ipc_server.attach(Events.SESSION_STAGED, self.notify_session_staged) ipc_server.attach(Events.SESSION_REGISTER, self._register) ipc_server.attach(Events.SESSION_CHECKIN, self.session_checked_in) ipc_server.attach(Events.NEW_JOB, self.add_job) ipc_server.attach(Events.JOB_RESULT, self.job_result) with STDatabase() as db: for registered_session in db.get_sessions(): _, guid, psk = registered_session self._register(guid, psk)
def get_session(self, guid, attempt_auto_reg=True): try: return list(filter(lambda x: x == guid, self.sessions))[0] except IndexError: logging.error(f"Tried to lookup non registered session {guid}") if attempt_auto_reg: logging.info("Attempting automatic registration from database") with STDatabase() as db: psk = db.get_session_psk(guid) if psk: self._register(guid, psk) logging.info("Automatic registration successful") return self.get_session(guid) logging.error(f"Could not automatically register session {guid}, PSK not in database") logging.warning(colored("This could be an orphaned session or somebody could be messing with the teamserver!", "red")) raise SessionNotFoundError(f"Session with guid {guid} was not found")
def _register(self, guid, psk): session = Session(guid, psk) self.sessions.add(session) with STDatabase() as db: db.add_session(guid, psk) logging.info(f"Registering session: {session}")
def getpsk(self, guid): self.guid_is_valid(guid) with STDatabase() as db: psk = db.get_session_psk(guid) return {"psk": psk}
def test_database_creation(): '''Create the database''' STDatabase.create_db_and_schema(db_path=TEST_DB_PATH) assert os.path.exists(TEST_DB_PATH) == True