def disconnect(self, close_code): try: async_to_sync(self.channel_layer.group_discard)("msfconsole", self.channel_name) Xcache.clean_msfconsoleinputcache() except Exception as E: logger.exception(E) pass
def receive(self, text_data=None, bytes_data=None): """接收前端用户输入""" message = json.loads(text_data) input_data = message.get("data") # \r \t \x7f # ctrl+c \x03 # ctrl+z \x1a cache_str = Xcache.get_msfconsoleinputcache() # 输入处理 if input_data == "\r" or input_data == "\r\n": Xcache.add_to_msfconsole_history_cache(cache_str) if cache_str.lower() == "exit -f": cache_str = "exit" elif cache_str.lower() == "viper": Console.reset_active_console() cache_str = "" Console.write(cache_str + "\r\n") Xcache.clean_msfconsoleinputcache() self.send_input_feedback("\r\n") Thread(target=self.send_msfrpc_read).start() elif input_data == "\x7f": # 删除键 return_str = Xcache.del_one_from_msfconsoleinputcache() self.send_input_feedback(return_str) elif input_data == "\t": # tab键 flag, result = Console.tabs(cache_str) if flag is not True: extra_str = "\r\nConnect Error >" self.send_input_feedback(extra_str) return tabs = result.get("tabs") if tabs == None or len(tabs) == 0: return elif len(tabs) == 1: extra_str = tabs[0][len(cache_str):] self.send_input_feedback(extra_str) Xcache.add_to_msfconsoleinputcache(extra_str) else: tmp = self.deal_tabs_options(cache_str, tabs) if tmp is None or tmp == cache_str: extra_str = "\r\n" for one in tabs: extra_str = extra_str + one + "\r\n" prompt = result.get("prompt") extra_str = extra_str + prompt + cache_str self.send_input_feedback(extra_str) else: extra_str = tmp[len(cache_str):] self.send_input_feedback(extra_str) Xcache.add_to_msfconsoleinputcache(extra_str) elif input_data == "\x1b[A": # 上键 clear_cmd = Xcache.clear_oneline_from_msfconsoleinputcache() self.send_input_feedback(clear_cmd) last = Xcache.get_last_from_msfconsole_history_cache() if last is None: pass else: Xcache.add_to_msfconsoleinputcache(last) self.send_input_feedback(last) elif input_data == "\x1b[B": # 上键 clear_cmd = Xcache.clear_oneline_from_msfconsoleinputcache() self.send_input_feedback(clear_cmd) last = Xcache.get_next_from_msfconsole_history_cache() if last is None: pass else: Xcache.add_to_msfconsoleinputcache(last) self.send_input_feedback(last) elif input_data == '\x03': # ctrl+c Console.session_kill() Xcache.clean_msfconsoleinputcache() Console.write("\r\n") self.send_input_feedback("\r\n") Thread(target=self.send_msfrpc_read).start() elif input_data == '\x1a': # ctrl+z Console.session_detach() Xcache.clean_msfconsoleinputcache() Console.write("\r\n") self.send_input_feedback("\r\n") Thread(target=self.send_msfrpc_read).start() else: Xcache.add_to_msfconsoleinputcache(input_data) self.send_input_feedback(input_data)