def wrapper(self): global operation_done cprint('\n' + INDENT) printer(self) cprint(INDENT + '\n') operation_done = True
def dsm(self): if self.mode != DSM_MODE: self.mode = DSM_MODE self.pexec = self.__dsm cprint( '\n<green>[+]</> Changed to <white,underline>dsm</> (disassembly) mode\n' )
def asm(self): if self.mode != ASM_MODE: self.mode = ASM_MODE self.pexec = self.__asm cprint( '\n<green>[+]</> Changed to <white,underline>asm</> (assembly) mode\n' )
def start(self): self._host = self._get_host_ip() if self._host is None: return False cprint(IP_ADDR.format(self._host[0], str(self._host[1]))) self.add_thread(self._handle_recv) self.add_thread(self.check_connection) if self._server_host is not None: if not self._get_connected(): return False while not self.user_id_assigned: pass self._handle_username() self._connect() else: self._handle_username() self.host2user_id[self._host] = self.user_id self.user_id2host[self.user_id] = self._host # Add self public key in keys dictionary self.encryptor.add_pubkey(self.user_id, None, _self=True) return True
def main(): shell = Shellen() cprint(('<magenta>[*]</> You can type <white, bold>help</> to see the list of available commands.\n' '<magenta>[*]</> Also, to close this session you should enter <white,bold>q</> or <white,bold>quit</>\n')) shell.irun()
def shell(self, pattern, count): count = count.strip() if count == '': count = DEFUALT_COUNT else: count = int(count) cprint('\n' + self.__shellstorm.fetch_table(pattern, os=self.os, arch=self.__get_arch(), count=count).table + '\n')
def help(self, *args): cprint(( '\n' ' <white,bold>* help</>: Show this help\n' ' <white,bold>* back, Ctrl+C</>: Back to the main menu.\n' ' <white,bold>* file [filepath]</>: Send a file.\n' ))
def remove_room(self, room_name): self.stop_printing = True self.send_room_message(room_name, "Room was deleted", remove_room='Yes') self.db_helper.remove_room(room_name) cprint(ROOM_REMOVED.format(room_name))
def setos(self, os): try: OS_MATCHING[os] self.os = os cprint('\n<green>[+]</> OS changed to {}.\n'.format(os)) except KeyError: cprint('\n<red,bold>[-]</> There isn\'t such OS.\n')
def specify_root_path(self, root_path): if not os.path.isdir(root_path): cprint(NOT_A_DIRECTORY) return False root_path = os.path.join(root_path, '') self.root_path = root_path return True
def parse_root_path(self, parse): new_root_path = parse.group(1) if not os.path.isdir(new_root_path): cprint(NOT_A_DIRECTORY) if new_root_path[-1] != '/': new_root_path += '/' self.db_helper.set_root_path(new_root_path) cprint(PATH_CHANGED)
def setos(self, ros): os = ros.lower() try: OS_MATCHING[os] self.os = os cprint('\n<green>[+]</> OS changed to {}.\n'.format(os)) except KeyError: cprint('\n<red,bold>[-]</> There isn\'t such OS.\n')
def parse_user(self, parse): username = parse.group(1) if self.db_helper.user_exists(username) and \ self.db_helper.get_visibility(username) and \ self.is_online(username=username): UserChat(username=username, client=self.client).open() else: cprint(NO_SUCH_USER)
def print_last_messages(self, dst, room=False): for message in list(self.db_helper.get_history(dst, 10, room))[::-1]: if message is None or message[1] == -1: continue cprint( LAST_MESSAGE.format(message[3], self.db_helper.get_username(message[2]), message[0]))
def help(self, *args): cprint(( '\n' ' <white,bold>* help</>: Show this help\n' ' <white,bold>* back, Ctrl+C</>: Change the current username.\n' ' <white,bold>* adduser [usrname]</>: Send a file.\n' ' <white,bold>* rmroom</>: Remove current room.\n' ' <white,bold>* file [filepath]</>: Send a file.\n' ))
def main(): shell = Shellen() cprint(( '<magenta>[*]</> You can type <white, bold>help</> to see the list of available commands.\n' '<magenta>[*]</> Also, to close this session you should enter <white,bold>q</> or <white,bold>quit</>\n' )) shell.irun()
def add_user2room(self, username, room_name): if not self.db_helper.user_exists(username): cprint(NO_SUCH_USED) return False self.db_helper.add_user2room(username=username, room_name=room_name) # Invites user to the room by sending # empty message self.send_room_message(room_name, EMPTY, room_user=username) cprint(YOU_INVITED.format(username, room_name)) return True
def setarch(self, arch): if not self.pexec.setarch(arch): cprint( '\n<red,bold>[-]</> Incorrect architecture. Enter <white,bold>archs</> to see a list of available archs.\n' ) return cprint( '\n<green>[+]</> Architecture of <white,underline>{}</> changed to <white,underline>{}</>\n' .format(self.mode, arch)) self.arch = arch
def _check_correctness(self, msg, err_msg, obj, is_correct): ''' Check if object is correct ''' if not obj: obj = input(msg) while not is_correct(obj): cprint('<lred>[-]</lred> Incorrect {}'.format(err_msg)) obj = input(msg) return obj
def handle_command(self, command): for pattern, handler in self.handlers: match = pattern.match(command) if match: handler(match) return True send_file = self.R_SEND_FILE.match(command) if send_file: self.parse_sending_file(send_file, username=self.username) else: if not self.send_message(username=self.username, text=command): cprint(ERROR_WHILE_SENDING)
def help(self, *args): cprint(( '\n<white,bold>PROMPT INFO</>\n' ' The get_colored_prompt format is <white,bold>OS</>:<white,bold>mode</>:<white,bold>arch</>\n' ' <white,bold>* OS</> is a current <white,underline>Operating System</>.\n' ' <white,bold>* L</> is <white,underline>Linux</>\n' ' <white,bold>* W</> is <white,underline>Windows</>\n' ' <white,bold>* M</> is <white,underline>MacOS</>\n' ' <white,bold>* mode</> is a current <white,underline>assembly mode</> (by default it\'s asm). See below for more information.\n' ' <white,bold>* arch</> is a chosen processor <white,underline>architecture</> (by default it\'s x86_32).\n' '\n<white,bold>BASIC</>\n' ' Basic commands are listed below:\n' ' <white,bold>* clear</>: Clear the terminal screen.\n' ' <white,bold>* help</>: Show this help message.\n' ' <white,bold>* quit, q, exit</>: Finish the current session and quit.\n' '\n<white,bold>MODES</>\n' ' If you want to change a current mode, then just type the name of a mode.\n' ' There are two assembly modes (each is described below):\n' ' <white,bold>* asm</>: Assembler mode.\n' ' <white,bold>* dsm</>: Disassembler mode.\n' '\n<white,bold>COMMON COMMANDS FOR MODES</>\n' ' Common commands can be used for both <white, underline>asm</> and <white, underline>dsm</> modes.\n' ' <white,bold>* archs</>: Print a table of available architectures for a current mode.\n' ' <white,bold>* run, r, go</>: Jump to the last shellcode in a subprocess. What could go wrong?\n' ' Note that you don\'t get to control the base address your code gets loaded at,\n' ' and this assumes that the instructions will make sense to your CPU.\n' ' <white,bold>* setarch [arch]</>: Change current processor architecture.\n' ' <white,bold>* setos [OS]</>: Change current operation system: <white,underline>windows/linux/macos</>.\n' ' <white,bold>* sys [pattern]</>: Search a syscall depending on OS, architecture and specified pattern.\n' ' <white,bold>* sysv [pattern]</>: It\'s <white,underline>sys</> command, but with verbose output.\n' ' <white,bold>* shell [keyword] [count]</>: List of shellcodes with URL that suit a given keyword.\n' ' [count] parameter can be <white,underline>ommited</>\n' ' This function requests shellcodes from http://shell-storm.org\n' '\n<white,bold>ASSEMBLY MODE</>\n' ' <white,bold>asm</> mode is intended for assembling instructions.\n' ' To assembly instuctions, write them separated by colons.\n' ' If your shellcode has a null bytes, then they will be highlighted after assembling.\n' ' Remember to use appropriate <white,bold>arch</> for assembling!\n' ' <white,underline>Example of using</>:\n' ' asm:x86_32 > <white,bold>mov edx, eax; xor eax, eax; inc edx; int 80;</>\n' ' [+] Bytes count: 7\n' ' Raw bytes: "\\x89\\xc2\\x31\\xc0\\x42\\xcd\\x50"\n' ' Hex string: "89c231c042cd50"\n' '\n<white,bold>DISASSEMBLY MODE</>\n' ' <white,bold>dsm</> mode allows you to disassembly bytes into instructions, based on the <white,bold>arch</>.\n' ' <white,underline>Example of using</>:\n' ' dsm:x86_32 > <white,bold>89c231c042cd50</>\n' ' 0x00080000: mov edx, eax\n' ' 0x00080002: xor eax, eax\n' ' 0x00080004: inc edx\n' ' 0x00080005: int 0x50\n' '\n' ))
def print_entered_users(self): last_users = set(self.client.host2user_id.values()) while not self.stop_printing_users: cur_users = set(self.client.host2user_id.values()) if last_users != cur_users: try: for new_user in cur_users.difference(last_users): cprint(USER_JOINED.format(self.db_helper.get_username(new_user))) for rem_user in last_users.difference(cur_users): cprint(USER_LEAVED.format(self.db_helper.get_username(rem_user))) last_users = cur_users except TypeError: pass
def exit(self): global operation_done operation_done = True try: self.client.disconnect(exit=True) except TypeError as e: pass self.stop_printing = True self.stop_printing_users = True for thread in self.inner_threads: thread.join() cprint(BYE) sys.exit()
def help(self, *args): cprint(( '\n<white,bold>PROMPT INFO</>\n' ' The prompt format is <white,bold>OS</>:<white,bold>mode</>:<white,bold>arch</>\n' ' <white,bold>* OS</> is a current <white,underline>Operating System</>.\n' ' <white,bold>* L</> is <white,underline>Linux</>\n' ' <white,bold>* W</> is <white,underline>Windows</>\n' ' <white,bold>* M</> is <white,underline>MacOS</>\n' ' <white,bold>* mode</> is a current <white,underline>assembly mode</> (by default it\'s asm). See below for more information.\n' ' <white,bold>* arch</> is a chosen processor <white,underline>architecture</> (by default it\'s x86_32).\n' '\n<white,bold>BASIC</>\n' ' Basic commands are listed below:\n' ' <white,bold>* clear</>: Clear the terminal screen.\n' ' <white,bold>* help</>: Show this help message.\n' ' <white,bold>* quit, q, exit</>: Finish the current session and quit.\n' '\n<white,bold>MODES</>\n' ' If you want to change a current mode, then just type the name of a mode.\n' ' There are two assembly modes (each is described below):\n' ' <white,bold>* asm</>: Assembler mode.\n' ' <white,bold>* dsm</>: Disassembler mode.\n' '\n<white,bold>COMMON COMMANDS FOR MODES</>\n' ' Common commands can be used for both <white, underline>asm</> and <white, underline>dsm</> modes.\n' ' <white,bold>* archs</>: Print a table of available architectures for a current mode.\n' ' <white,bold>* setarch [arch]</>: Change current processor architecture.\n' ' <white,bold>* setos [OS]</>: Change current operation system: <white,underline>windows/linux/macos</>.\n' ' <white,bold>* sys [pattern]</>: Search a syscall depending on OS, architecture and specified pattern.\n' ' <white,bold>* sysv [pattern]</>: It\'s <white,underline>sys</> command, but with verbose output.\n' ' <white,bold>* shell [keyword] [count]</>: List of shellcodes with URL that suit a given keyword.\n' ' [count] parameter can be <white,underline>ommited</>\n' ' This function requests shellcodes from http://shell-storm.org\n' '\n<white,bold>ASSEMBLY MODE</>\n' ' <white,bold>asm</> mode is intended for assembling instructions.\n' ' To assembly instuctions, write them separated by colons.\n' ' If your shellcode has a null bytes, then they will be highlighted after assembling.\n' ' Remember to use appropriate <white,bold>arch</> for assembling!\n' ' <white,underline>Example of using</>:\n' ' asm:x86_32 > <white,bold>mov edx, eax; xor eax, eax; inc edx; int 80;</>\n' ' [+] Bytes count: 7\n' ' Raw bytes: "\\x89\\xc2\\x31\\xc0\\x42\\xcd\\x50"\n' ' Hex string: "89c231c042cd50"\n' '\n<white,bold>DISASSEMBLY MODE</>\n' ' <white,bold>dsm</> mode allows you to disassembly bytes into instructions, based on the <white,bold>arch</>.\n' ' <white,underline>Example of using</>:\n' ' dsm:x86_32 > <white,bold>89c231c042cd50</>\n' ' 0x00080000: mov edx, eax\n' ' 0x00080002: xor eax, eax\n' ' 0x00080004: inc edx\n' ' 0x00080005: int 0x50\n' '\n' ))
def print_recv_message(self, dst, room=False): last_msg = self.get_last_message(dst, room) while not self.stop_printing: cur_msg = self.get_last_message(dst, room) if last_msg[1] != cur_msg[1]: messages = self.db_helper.get_history(dst, cur_msg[1] - last_msg[1], room) for message in messages: if self.self_chat or message[2] != self.client.user_id: cprint(RECV_MESSAGE .format(message[3], self.db_helper.get_username(message[2]), message[0])) last_msg = cur_msg
def print_recv_message(self, dst, room=False): last_msg = self.get_last_message(dst, room) while not self.stop_printing: cur_msg = self.get_last_message(dst, room) if last_msg[1] != cur_msg[1]: messages = self.db_helper.get_history(dst, cur_msg[1] - last_msg[1], room) for message in messages: if self.self_chat or message[2] != self.client.user_id: cprint( RECV_MESSAGE.format( message[3], self.db_helper.get_username(message[2]), message[0])) last_msg = cur_msg
def help(self): cprint(( '\n' ' <white,bold>* help</>: Show this output\n' ' <white,bold>* username [usrname]</>: Change current username.\n' ' <white,bold>* rooms</>: Show available rooms.\n' ' <white,bold>* users</>: Show online users.\n' ' <white,bold>* user [username]</>: Switch to the user private message mode. Enter in a private messages.\n' ' <white,bold>* room [room_name]</>: Switch to the room message mode. Enter in a room.\n' ' <white,bold>* rmroom [roomname]</>: Remove current user from a room. Room itself still exists.\n' ' <white,bold>* adduser [user] [room]</>: Add an user to a room.\n' ' <white,bold>* mkroom [roomname]</>: Create new room.\n' ' <white,bold>* exit, q, quit</>: Finish current chat session.\n' ' <white,bold>* chvis</>: Change your visibility in the chat.\n' ' <white,bold>* chpath [path]</>: Change the directory for storing files.\n' ))
def print_entered_users(self): last_users = set(self.client.host2user_id.values()) while not self.stop_printing_users: cur_users = set(self.client.host2user_id.values()) if last_users != cur_users: try: for new_user in cur_users.difference(last_users): cprint( USER_JOINED.format( self.db_helper.get_username(new_user))) for rem_user in last_users.difference(cur_users): cprint( USER_LEAVED.format( self.db_helper.get_username(rem_user))) last_users = cur_users except TypeError: pass
def send_file(self, file_location, username, room=''): if username != '': user_id = self.db_helper.get_user_id(username) filename = file_location.replace('/', ' ').replace('\\', ' ') \ .split()[-1] message = self.client.create_file_data(file_location, filename, user_id=self.client.user_id, room_name=room) if message is None: cprint(NO_SUCH_FILE) return if room != '': room_id = self.db_helper.get_room_id(room) for user_id in self._get_users(room, room_id): self._send_message(user_id, message, room) else: self._send_message(user_id, message)
def print_res(self, res): encoding, count = res raw_hex = hexlify(bytearray(encoding)).decode('utf-8') raw_bytes = hex2bytes(raw_hex) dec_sc_bytes = self.__decorate_shellcode(raw_bytes) dec_sc_hex = self.__decorate_shellcode(raw_hex, False) is_zeroed = NULLBYTE in raw_bytes prefix = '' if is_zeroed: prefix += ' <yellow,bold>[!]</> Warning! Your shellcode contains <white,underline>null bytes</>!\n' cprint(prefix + ( ' <green,bold>[+]</> Bytes count: <white,bold>{}</>\n' ' Raw bytes: "{}"\n' ' Hex string: "{}"\n' ).format(len(encoding), dec_sc_bytes, dec_sc_hex))
def print_res(self, res): encoding, count = res raw_hex = hexlify(bytearray(encoding)).decode('utf-8') raw_bytes = hex2bytes(raw_hex) dec_sc_bytes = self.__decorate_shellcode(raw_bytes) dec_sc_hex = self.__decorate_shellcode(raw_hex, False) is_zeroed = NULLBYTE in raw_bytes prefix = '' if is_zeroed: prefix += ' <yellow,bold>[!]</> Warning! Your shellcode contains <white,underline>null bytes</>!\n' cprint(prefix + (' <green,bold>[+]</> Bytes count: <white,bold>{}</>\n' ' Raw bytes: "{}"\n' ' Hex string: "{}"\n' ).format(len(encoding), dec_sc_bytes, dec_sc_hex))
def help(self, *args): cprint(( '\n<white,bold>PROMPT INFO</>\n' ' The prompt format is <white,bold>OS</>:<white,bold>mode</>:<white,bold>arch</>\n' ' <white,bold>* mode</> is a current <white,underline>assembly mode</> (by default it\'s asm). See below for more information.\n' ' <white,bold>* arch</> is a chosen processor <white,underline>architecture</> (by default it\'s x86_32).\n' '\n<white,bold>BASIC</>\n' ' Basic commands are listed below:\n' ' <white,bold>* clear</>: Clear the terminal screen.\n' ' <white,bold>* help</>: Show this help message.\n' ' <white,bold>* quit, q, exit</>: Finish the current session and quit.\n' '\n<white,bold>MODES</>\n' ' If you want to change a current mode, then just type the name of a mode.\n' ' There are two assembly modes (each is described below):\n' ' <white,bold>* asm</>: Assembler mode.\n' ' <white,bold>* dsm</>: Disassembler mode.\n' '\n<white,bold>COMMON COMMANDS FOR MODES</>\n' ' Common commands can be used for both <white, underline>asm</> and <white, underline>dsm</> modes.\n' ' <white,bold>* archs</>: Print a table of available architectures for a current mode.\n' ' <white,bold>* setarch [arch]</>: Change current processor architecture.\n' ' <white,bold>* setos [OS]</>: Change current operation system (windows/linux/macos).\n' ' <white,bold>* sys [pattern]</>: Search a syscall depending on OS, architecture and specified pattern.\n' '\n<white,bold>ASSEMBLY MODE</>\n' ' <white,bold>asm</> mode is intended for assembling instructions.\n' ' To assembly instuctions, write them separated by colons.\n' ' If your shellcode has a null bytes, then they will be highlighted after assembling.\n' ' Remember to use appropriate <white,bold>arch</> for assembling!\n' ' <white,underline>Example of using</>:\n' ' asm:x86_32 > <white,bold>mov edx, eax; xor eax, eax; inc edx; int 80;</>\n' ' [+] Bytes count: 7\n' ' Raw bytes: "\\x89\\xc2\\x31\\xc0\\x42\\xcd\\x50"\n' ' Hex string: "89c231c042cd50"\n' '\n<white,bold>DISASSEMBLY MODE</>\n' ' <white,bold>dsm</> mode allows you to disassembly bytes into instructions, based on the <white,bold>arch</>.\n' ' <white,underline>Example of using</>:\n' ' dsm:x86_32 > <white,bold>89c231c042cd50</>\n' ' 0x00080000: mov edx, eax\n' ' 0x00080002: xor eax, eax\n' ' 0x00080004: inc edx\n' ' 0x00080005: int 0x50\n' '\n'))
def run(self): shellcode = self.last_shellcode() if not shellcode: cprint('\n<red,bold>[-]</> Assemble or disassemble something first!\n') return result = native.run(shellcode) if result < 0: sig_info = signal.Signals(-result) cprint('\n<red,bold>[-]</> Exited with signal <white>{}</> (<white,underline>{}</>)\n'.format(sig_info.name, sig_info.value)) elif result == 0: cprint('\n<green>[+]</> Exited with status code 0.\n') else: # result > 0 cprint('\n<yellow>[*]</> Exited with status code {}.\n'.format(result))
def command_mode(self): cprint(START_CHAT) while True: try: cprint(MAIN_CHAT_PROMPT.format(self.client.username), end='') with lock: command = input(' ') if command == '': continue if not self.handle_command(command): cprint(INVALID_COMMAND) except KeyboardInterrupt as e: cprint('')
def print_users(self, *args): cprint('\n<white,bold>{}</>'.format(INDENT)) for user_id in self.client.host2user_id.values(): if (self.db_helper.get_visibility(user_id=user_id) or user_id == self.db_helper.get_cur_user_id()) and \ self.is_online(user_id=user_id): cprint(PRINT_USER.format(self.db_helper.get_username(user_id))) cprint('<white,bold>{}</>\n'.format(INDENT))
def run(self): if not self.cur_user_exists(): self.specify_username() self.specify_root_path() else: cprint(HELLO_AGAIN.format(self.client.username)) cprint(STORAGE.format(self.db_helper.get_root_path())) self.db_helper.specify_username(self.client) if not self.client.start(): cprint(CONNECTION_ERROR) self.exit() self.init_print_users() self.command_mode()
def irun(self): while True: try: cmd = self.prompt() if cmd == '': continue else: if not self.handle_command(cmd): cprint('\n<red,bold>[-]</> Invalid command.\n') except Exception as e: cprint('\n<red,bold>[-]</> Error occured: {}\n'.format(e)) except KeyboardInterrupt: cprint()
def irun(self): while True: try: self.prompt() cmd = input(' ') if cmd == '': continue else: if not self.handle_command(cmd): cprint('\n<red,bold>[-]</> Invalid command.\n') except Exception as e: cprint('\n<red,bold>[-]</> Error occured: {}\n'.format(e)) except KeyboardInterrupt: cprint()
def dsm(self): if self.mode != DSM_MODE: self.mode = DSM_MODE self.pexec = self.__dsm cprint('\n<green>[+]</> Changed to <white,underline>dsm</> (disassembly) mode\n')
def prompt(self): cprint('<red,bold>{}</>:<blue, bold>{}</>:<blue>{}</> <yellow,bold>></>'.format(OS_MATCHING[self.os], self.mode, self.pexec.arch), end='')
def sys(self, pattern, verbose=False): if self.os == LINUX_OS: cprint('\n' + self.__linuxsys.get_printable_table(self.__get_arch(), pattern, colored=True, verbose=verbose) + '\n')
def setarch(self, arch): if not self.pexec.setarch(arch): cprint('\n<red,bold>[-]</> Incorrect architecture. Enter <white,bold>archs</> to see a list of available archs.\n') return cprint('\n<green>[+]</> Architecture of <white,underline>{}</> changed to <white,underline>{}</>\n'.format(self.mode, arch)) self.arch = arch
def asm(self): if self.mode != ASM_MODE: self.mode = ASM_MODE self.pexec = self.__asm cprint('\n<green>[+]</> Changed to <white,underline>asm</> (assembly) mode\n')
def archs(self): cprint('\n' + self.pexec.archs() + '\n')