Exemple #1
0
    def connect(self, host, port, socketport, username, password):
        self.host = host
        self.port = port
        try:
            response = requests.post(url=f'{host}:{port}/api/admin/login',
                                     json={
                                         'username': username,
                                         'password': password
                                     },
                                     verify=False)
        except Exception as e:
            return e

        if response.status_code == 200:
            self.token = response.json()['token']
            self.connected = True

            self.sio = socketio.Client(ssl_verify=False,
                                       reconnection_attempts=3)
            self.sio.connect(f'{host}:{socketport}?token={self.token}')

            # Wait for version to be returned
            self.empire_version = self.get_version()['version']

            self.init()
            self.init_handlers()
            self.notify_connected()
            print_util.title(self.empire_version, len(self.modules),
                             len(self.listeners), len(self.active_agents))
            return response

        elif response.status_code == 401:
            return response
Exemple #2
0
    def parse_command_line(self,
                           text: str,
                           cmd_line: List[str],
                           resource_file=False):
        if len(cmd_line) == 0:
            return
        if not state.connected and not cmd_line[0] == "connect":
            if cmd_line[0] == "exit":
                choice = input(print_util.color("[>] Exit? [y/N] ", "red"))
                if choice.lower() == "y":
                    raise CliExitException
                else:
                    return
            else:
                return

        # Switch Menus
        if text.strip() == "main":
            state.get_modules()
            state.get_listeners()
            print_util.title(
                state.empire_version,
                len(state.modules),
                len(state.listeners),
                len(state.get_active_agents()),
            )
            menu_state.push(self.menus["MainMenu"])
        elif text.strip() == "listeners":
            menu_state.push(self.menus["ListenerMenu"])
        elif text.strip() == "chat":
            menu_state.push(self.menus["ChatMenu"])
        elif menu_state.current_menu_name == "ChatMenu":
            menu_state.current_menu.send_chat(text)
        elif text.strip() == "agents":
            menu_state.push(self.menus["AgentMenu"])
        elif text.strip() == "sponsors":
            menu_state.push(self.menus["SponsorsMenu"])
        elif text.strip() == "credentials":
            menu_state.push(self.menus["CredentialMenu"])
        elif text.strip() == "plugins":
            menu_state.push(self.menus["PluginMenu"])
        elif text.strip() == "admin":
            menu_state.push(self.menus["AdminMenu"])
        elif cmd_line[0] == "uselistener" and len(cmd_line) > 1:
            if cmd_line[1] in state.listener_types:
                menu_state.push(self.menus["UseListenerMenu"],
                                selected=cmd_line[1])
            else:
                print(f"No listener {cmd_line[1]}")
        elif cmd_line[0] == "usestager" and len(cmd_line) > 1:
            if cmd_line[1] in state.stagers:
                menu_state.push(self.menus["UseStagerMenu"],
                                selected=cmd_line[1])
            else:
                print(f"No stager {cmd_line[1]}")
        elif cmd_line[0] == "interact" and len(cmd_line) > 1:
            if cmd_line[1] in state.agents:
                menu_state.push(self.menus["InteractMenu"],
                                selected=cmd_line[1])
            else:
                print(f"No agent {cmd_line[1]}")
        elif cmd_line[0] == "useplugin" and len(cmd_line) > 1:
            if cmd_line[1] in state.plugins:
                menu_state.push(self.menus["UsePluginMenu"],
                                selected=cmd_line[1])
            else:
                print(f"No plugin {cmd_line[1]}")
        elif cmd_line[0] == "usecredential" and len(cmd_line) > 1:
            if cmd_line[1] in state.credentials or cmd_line[1] == "add":
                menu_state.push(self.menus["UseCredentialMenu"],
                                selected=cmd_line[1])
            else:
                print(f"[!] No credential {cmd_line[1]}")
        elif cmd_line[0] == "usemodule" and len(cmd_line) > 1:
            if cmd_line[1] in state.modules:
                if menu_state.current_menu_name == "InteractMenu":
                    menu_state.push(
                        self.menus["UseModuleMenu"],
                        selected=cmd_line[1],
                        agent=menu_state.current_menu.selected,
                    )
                else:
                    menu_state.push(self.menus["UseModuleMenu"],
                                    selected=cmd_line[1])
            else:
                print(f"No module {cmd_line[1]}")
        elif cmd_line[0] == "editlistener" and len(cmd_line) > 1:
            if menu_state.current_menu_name == "ListenerMenu":
                if cmd_line[1] in state.listeners:
                    menu_state.push(self.menus["EditListenerMenu"],
                                    selected=cmd_line[1])
            else:
                print(f"No listener {cmd_line[1]}")
        elif text.strip() == "shell":
            if menu_state.current_menu_name == "InteractMenu":
                menu_state.push(self.menus["ShellMenu"],
                                selected=menu_state.current_menu.selected)
            else:
                pass
        elif menu_state.current_menu_name == "ShellMenu":
            if text == "exit":
                menu_state.push(
                    self.menus["InteractMenu"],
                    selected=menu_state.current_menu.selected,
                )
            else:
                menu_state.current_menu.shell(menu_state.current_menu.selected,
                                              text)
        elif text.strip() == "proxy":
            if menu_state.current_menu_name == "InteractMenu":
                if menu_state.current_menu.agent_options["language"] not in [
                        "python",
                        "ironpython",
                ]:
                    print(
                        print_util.color(
                            f'[!] Agent proxies are not available in {menu_state.current_menu.agent_options["language"]} agents'
                        ))
                    pass
                elif state.listeners[menu_state.current_menu.agent_options[
                        "listener"]]["module"] not in [
                            "http", "http_hop", "redirector"
                        ]:
                    print(
                        print_util.color(
                            f"[!] Agent proxies are not available in {state.listeners[menu_state.current_menu.agent_options['listener']]['module']} listeners"
                        ))
                else:
                    menu_state.push(
                        self.menus["ProxyMenu"],
                        selected=menu_state.current_menu.selected,
                    )
            else:
                pass
        elif text.strip() == "back":
            menu_state.pop()
        elif text.strip() == "exit":
            if resource_file:
                raise CliExitException
            choice = input(print_util.color("[>] Exit? [y/N] ", "red"))
            if choice.lower() == "y":
                raise CliExitException
            else:
                pass
        else:
            func = None
            try:
                func = getattr(
                    menu_state.current_menu
                    if hasattr(menu_state.current_menu, cmd_line[0]) else self,
                    cmd_line[0],
                )
            except:
                pass

            if func:
                try:
                    # If the command is set, wrap the value in quotes so docopt
                    # doesn't interpret it as a parameter. Also concatenate all the words
                    # after the 3rd word for easier autofilling with suggested values that have spaces
                    # There may be a better way to do this.
                    if cmd_line[0] == "set":
                        cmd_line[2] = f'"{" ".join(cmd_line[2:])}"'
                        del cmd_line[3:]
                    args = self.strip(docopt(func.__doc__, argv=cmd_line[1:]))
                    new_args = {}
                    # todo casting for type hinted values?
                    for key, hint in get_type_hints(func).items():
                        # if args.get(key) is not None:
                        if key != "return":
                            new_args[key] = args[key]
                    # print(new_args)
                    func(**new_args)
                except Exception as e:
                    print(e)
                    pass
                except SystemExit as e:
                    pass
            elif not func and menu_state.current_menu_name == "InteractMenu":
                if cmd_line[0] in shortcut_handler.get_names(
                        self.menus["InteractMenu"].agent_language):
                    menu_state.current_menu.execute_shortcut(
                        cmd_line[0], cmd_line[1:])
Exemple #3
0
    def parse_command_line(self, text: str, cmd_line: List[str], resource_file=False):
        if len(cmd_line) == 0:
            return
        if not state.connected and not cmd_line[0] == 'connect':
            if cmd_line[0] == 'exit':
                choice = input(print_util.color("[>] Exit? [y/N] ", "red"))
                if choice.lower() == "y":
                    raise CliExitException
                else:
                    return
            else:
                return

        # Switch Menus
        if text.strip() == 'main':
            state.get_modules()
            state.get_listeners()
            print_util.title(state.empire_version, len(state.modules), len(state.listeners),
                             len(state.get_active_agents()))
            menu_state.push(self.menus['MainMenu'])
        elif text.strip() == 'listeners':
            menu_state.push(self.menus['ListenerMenu'])
        elif text.strip() == 'chat':
            menu_state.push(self.menus['ChatMenu'])
        elif menu_state.current_menu_name == 'ChatMenu':
            menu_state.current_menu.send_chat(text)
        elif text.strip() == 'agents':
            menu_state.push(self.menus['AgentMenu'])
        elif text.strip() == 'sponsors':
            menu_state.push(self.menus['SponsorsMenu'])
        elif text.strip() == 'credentials':
            menu_state.push(self.menus['CredentialMenu'])
        elif text.strip() == 'plugins':
            menu_state.push(self.menus['PluginMenu'])
        elif text.strip() == 'admin':
            menu_state.push(self.menus['AdminMenu'])
        elif cmd_line[0] == 'uselistener' and len(cmd_line) > 1:
            if cmd_line[1] in state.listener_types:
                menu_state.push(self.menus['UseListenerMenu'], selected=cmd_line[1])
            else:
                print(f'No listener {cmd_line[1]}')
        elif cmd_line[0] == 'usestager' and len(cmd_line) > 1:
            if cmd_line[1] in state.stagers:
                menu_state.push(self.menus['UseStagerMenu'], selected=cmd_line[1])
            else:
                print(f'No stager {cmd_line[1]}')
        elif cmd_line[0] == 'interact' and len(cmd_line) > 1:
            if cmd_line[1] in state.agents:
                menu_state.push(self.menus['InteractMenu'], selected=cmd_line[1])
            else:
                print(f'No agent {cmd_line[1]}')
        elif cmd_line[0] == 'useplugin' and len(cmd_line) > 1:
            if cmd_line[1] in state.plugins:
                menu_state.push(self.menus['UsePluginMenu'], selected=cmd_line[1])
            else:
                print(f'No plugin {cmd_line[1]}')
        elif cmd_line[0] == 'usecredential' and len(cmd_line) > 1:
            if cmd_line[1] in state.credentials or cmd_line[1] == 'add':
                menu_state.push(self.menus['UseCredentialMenu'], selected=cmd_line[1])
            else:
                print(f'[!] No credential {cmd_line[1]}')
        elif cmd_line[0] == 'usemodule' and len(cmd_line) > 1:
            if cmd_line[1] in state.modules:
                if menu_state.current_menu_name == 'InteractMenu':
                    menu_state.push(self.menus['UseModuleMenu'], selected=cmd_line[1],
                                    agent=menu_state.current_menu.selected)
                else:
                    menu_state.push(self.menus['UseModuleMenu'], selected=cmd_line[1])
            else:
                print(f'No module {cmd_line[1]}')
        elif cmd_line[0] == 'editlistener' and len(cmd_line) > 1:
            if menu_state.current_menu_name == 'ListenerMenu':
                if cmd_line[1] in state.listeners:
                    menu_state.push(self.menus['EditListenerMenu'], selected=cmd_line[1])
            else:
                print(f'No listener {cmd_line[1]}')
        elif text.strip() == 'shell':
            if menu_state.current_menu_name == 'InteractMenu':
                menu_state.push(self.menus['ShellMenu'], selected=menu_state.current_menu.selected)
            else:
                pass
        elif menu_state.current_menu_name == 'ShellMenu':
            if text == 'exit':
                menu_state.push(self.menus['InteractMenu'], selected=menu_state.current_menu.selected)
            else:
                menu_state.current_menu.shell(menu_state.current_menu.selected, text)
        elif text.strip() == 'back':
            menu_state.pop()
        elif text.strip() == 'exit':
            if resource_file:
                raise CliExitException
            choice = input(print_util.color("[>] Exit? [y/N] ", "red"))
            if choice.lower() == "y":
                raise CliExitException
            else:
                pass
        else:
            func = None
            try:
                func = getattr(menu_state.current_menu if hasattr(menu_state.current_menu, cmd_line[0]) else self,
                               cmd_line[0])
            except:
                pass

            if func:
                try:
                    # If the command is set, wrap the value in quotes so docopt
                    # doesn't interpret it as a parameter. Also concatenate all the words
                    # after the 3rd word for easier autofilling with suggested values that have spaces
                    # There may be a better way to do this.
                    if cmd_line[0] == 'set':
                        cmd_line[2] = f'"{" ".join(cmd_line[2:])}"'
                        del cmd_line[3:]
                    args = self.strip(docopt(
                        func.__doc__,
                        argv=cmd_line[1:]
                    ))
                    new_args = {}
                    # todo casting for type hinted values?
                    for key, hint in get_type_hints(func).items():
                        # if args.get(key) is not None:
                        if key != 'return':
                            new_args[key] = args[key]
                    # print(new_args)
                    func(**new_args)
                except Exception as e:
                    print(e)
                    pass
                except SystemExit as e:
                    pass
            elif not func and menu_state.current_menu_name == 'InteractMenu':
                if cmd_line[0] in shortcut_handler.get_names(self.menus['InteractMenu'].agent_language):
                    menu_state.current_menu.execute_shortcut(cmd_line[0], cmd_line[1:])