def start(self): if not self.selected: raise CmdError("No listener selected") if len( list( filter(lambda l: l['Name'] == self.selected['Name'], self.listeners))): raise CmdError( f"A listener named \'{self.selected['Name']}\' already running! (Change the name and try again)" ) try: self.selected.start() logging.info( f"Started {self.selected.name} listener ({self.selected['BindIP']}:{self.selected['Port']})" ) except Exception as e: traceback.print_exc() raise CmdError( f"Failed to start {self.selected.name} listener: {e}") else: self.listeners.append(self.selected) listener_json = dict(self.selected) self.use(self.selected.name) asyncio.create_task(self.teamserver.update_server_stats()) return dict(listener_json)
def set(self, name: str, value: str): if not self.selected: raise CmdError("No listener selected") try: self.selected[name] = value except KeyError: raise CmdError(f"Unknown option '{name}'")
def kill(self, guid): try: session = self.get_session(guid) session.jobs.add(Job(command=('Exit', []))) return {'guid': guid, 'status': 'Tasked to exit'} except SessionNotFoundError: raise CmdError(f"No session named: {guid}")
def generate(self, listener_name): if not self.selected: raise CmdError("No stager selected") for l in self.teamserver.contexts['listeners'].listeners: if l['Name'] == listener_name: guid, psk, generated_stager = self.selected.generate(l) self.teamserver.contexts['sessions']._register(guid, psk) return { "output": generated_stager, "suggestions": self.selected.suggestions, "extension": self.selected.extension } raise CmdError(f"No listener running with name '{listener_name}'")
def use(self, name: str): for s in self.loaded: if s.name.lower() == name.lower(): self.selected = deepcopy(s) return dict(self.selected) raise CmdError(f"No stager available named '{name.lower()}'")
def wrapper(*args, **kwargs): cmd_args = kwargs["args"] validated_args = {} for name, hint in get_type_hints(func).items(): if name == 'response': continue try: value = cmd_args[f'<{name}>'] except KeyError: try: value = cmd_args[f'--{name}'] except KeyError: raise CmdError( f"Unable to find '{name}' argument in command definition" ) try: validated_args[name] = hint(value) except TypeError: # I'm still not sure if there's a way to dynamically cast Lists and Dicts using type hints if hint == List[int]: validated_args[name] = [int(x) for x in value] elif hint == List[str]: validated_args[name] = [str(x) for x in value] else: raise NotImplementedError( f"Casting for type '{hint}' has not been implemented") return func(args[0], **validated_args, response=kwargs["response"] ) if args[0].__class__._remote is True else func( args[0], **validated_args)
def use(self, name: str): for l in self.loaded: if l.name.lower() == name.lower(): self.selected = deepcopy(l) #self.selected.name = f"{l.name}-{gen_random_string(6)}" return dict(self.selected) raise CmdError(f"No listener available named '{name.lower()}'")
def use(self, name: str): for m in self.loaded: if m.name.lower() == name.lower(): #self.selected = deepcopy(m) self.selected = m return dict(self.selected) raise CmdError(f"No module available named '{name.lower()}'")
def jitter(self, guid, max, min): try: session = self.get_session(guid) if min: session.jobs.add(Job(command=('Jitter', [int(max), int(min)]))) else: session.jobs.add(Job(command=('Jitter', [int(max)]))) except SessionNotFoundError: raise CmdError(f"No session named: {guid}")
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 register(self, guid, psk): if not guid: guid = uuid.uuid4() if not psk: psk = gen_stager_psk() try: uuid.UUID(str(guid)) except ValueError: raise CmdError("Invalid Guid") self._register(guid, psk) return {"guid": str(guid), "psk": psk}
def options(self): if not self.selected: raise CmdError("No listener selected") return self.selected.options
def guid_is_valid(self, guid): try: uuid.UUID(str(guid)) except ValueError: raise CmdError("Invalid Guid")
def rename(self, guid, name): try: session = self.get_session(guid) session.name = name except SessionNotFoundError: raise CmdError(f"No session with guid: {guid}")
def checkin(self, guid): try: session = self.get_session(guid) session.jobs.add(Job(command=('CheckIn', []))) except SessionNotFoundError: raise CmdError(f"No session named: {guid}")
def sleep(self, guid, interval): try: session = self.get_session(guid) session.jobs.add(Job(command=('Sleep', [int(interval)]))) except SessionNotFoundError: raise CmdError(f"No session named: {guid}")
def options(self): if not self.selected: raise CmdError("No module selected") return self.selected.options
def info(self, guid): try: return dict(self.get_session(guid)) except SessionNotFoundError: raise CmdError(f"No session named: {guid}")
def info(self): if not self.selected: raise CmdError("No module selected") return dict(self.selected)