def _shell(self): time.sleep(1) while True: try: cmd = raw_input("bettersploit> ") if self.commands.has_key(cmd.split(" ")[0]): method = self.commands[cmd.split(" ")[0]]['method'] cmd_args = len(cmd.split(" ")) - 1 if cmd_args == 2: arg1, arg2 = cmd.split(" ")[1], cmd.split(" ")[2] msg = method(arg1, arg2) elif cmd_args == 1: arg1 = cmd.split(" ")[1] msg = method(arg1) else: msg = method() if msg is not None: status = msg[0] print_str = msg[1] if status == 'success': good(print_str) elif status == 'error': error(print_str) elif cmd == 'help': help_menu(self.commands) elif len(cmd) > 0: print "%s: command not found. Type help for a "\ "list of commands" % cmd except: continue
def read_write_directory(directory): if os.path.exists(directory): if os.access(directory, os.W_OK and os.R_OK): return directory else: error('The output is not readable and/or writable') else: error('The specified directory does not exist')
async def on_command_error(error, ctx): channel = ctx.message.channel if isinstance(error, commands.MissingRequiredArgument): await send_cmd_help(ctx) elif isinstance(error, commands.BadArgument): await send_cmd_help(ctx) elif isinstance(error, commands.CommandInvokeError): output.error("Exception in command '{}', {}".format(ctx.command.qualified_name, error.original)) oneliner = "Error in command '{}' - {}: {}\nIf this issue persists, Please report it in the support server.".format( ctx.command.qualified_name, type(error.original).__name__, str(error.original)) await ctx.bot.send_message(channel, oneliner)
async def on_ready(): output.info("Loading {} extension(s)...".format(len(startup_extensions))) for extension in startup_extensions: try: bot.load_extension("cogs.{}".format(extension.replace(".py", ""))) loaded_extensions.append(extension) except Exception as e: exc = '{}: {}'.format(type(e).__name__, e) output.error('Failed to load extension {}\n\t->{}'.format(extension, exc)) output.success('Successfully loaded the following extension(s): {}'.format(', '.join(loaded_extensions))) output.info('You can now invite the bot to a server using the following link: https://discordapp.com/oauth2/authorize?client_id={}&scope=bot'.format(bot.user.id))
async def on_ready(): output.info("Loading {} extension(s)...".format(len(startup_extensions))) for extension in startup_extensions: try: bot.load_extension("cogs.{}".format(extension.replace(".py", ""))) loaded_extensions.append(extension) except Exception as e: exc = '{}: {}'.format(type(e).__name__, e) output.error('Failed to load extension {}\n\t->{}'.format(extension, exc)) output.success('Successfully loaded the following extension(s); {}'.format(loaded_extensions))
async def pull(self, ctx): """ Update the bot [ADMIN ONLY] """ await self.bot.say("Pulling...") try: returned = os.system("git pull") await self.bot.say(":+1:Returned code "+ str(returned)) except Exception as e: exc = '{}: {}'.format(type(e).__name__, e) output.error('{} has attempted to update the bot, but the following ' 'exception occurred;\n\t->{}'.format(ctx.message.author, exc))
async def shutdown(ctx): """Shut down the bot""" author = str(ctx.message.author) try: await bot.say("Shutting down...") await bot.logout() bot.loop.stop() output.info('{} has shut down the bot...'.format(author)) except Exception as e: exc = '{}: {}'.format(type(e).__name__, e) output.error('{} has attempted to shut down the bot, but the following ' 'exception occurred;\n\t->{}'.format(author, exc))
async def reload(ctx, module: str): """Load a cog located in /cogs [ADMIN ONLY]""" author = str(ctx.message.author) module = module.strip() try: bot.reload_extension("cog.{}".format(module)) output.info('{} reloaded module: {}'.format(author, module)) await ctx.send("Successfully reloaded {}.py".format(module)) except Exception as e: exc = '{}: {}'.format(type(e).__name__, e) output.error('{} attempted to reload module \'{}\' but the following ' 'exception occured;\n\t->{}'.format(author, module, exc)) await ctx.send('Failed to reload extension {}\n\t->{}'.format( module, exc))
async def restart(ctx): """Restart the bot""" author = str(ctx.message.author) try: await bot.say("Restarting...") await bot.logout() bot.loop.stop() output.info('{} has restarted the bot...'.format(author)) os.system('sh restart.sh') except Exception as e: exc = '{}: {}'.format(type(e).__name__, e) output.error('{} has attempted to restart the bot, but the following ' 'exception occurred;\n\t->{}'.format(author, exc))
def prompt(self): """ Interaction shell with the session """ def get_cwd(): self.send_cmd({ 'command': '', }) self.recv_output() cmd = None get_cwd() while cmd != "exit": try: cmd = raw_input(self.cwd + '> ') if cmd == 'exit': break msg = None fw = cmd.split(" ")[0] if self.commands.has_key(fw): method = self.commands[fw]['method'] cmd_args = len(cmd.split(" ")) - 1 if cmd_args == 2: arg1, arg2 = cmd.split(" ")[1], cmd.split(" ")[2] msg = method(self, arg1, arg2) elif cmd_args == 1: arg1 = cmd.split(" ")[1] msg = method(self, arg1) elif not cmd_args: msg = method(self) else: help_menu(self.commands) if msg is not None: status = msg[0] print_str = msg[1] if status == 'success': good(print_str) elif status == 'error': error(print_str) elif cmd == 'help': help_menu(self.commands) else: if self.send_cmd({'command': cmd}): if cmd == 'kill': break try: cmd_output = self.recv_output()['output'] except struct.error: error('Slave connection died') break except Exception as e: raise else: if cmd_output: print(cmd_output) else: error('Could not send command') except (socket.timeout): error("Connection closed by remote host") break except KeyboardInterrupt: info("Closing session prompt") break
def _accept_conns(self): """ Background thread accepts client connections """ info("Listening for connections...") for c in self.connections: c.close() self.connections = [] self.addresses = [] while 1: try: conn, addr = self.socket.accept() conn.setblocking(1) hostname = conn.recv(1024).decode("utf-8") address = addr + (hostname, ) except Exception as e: error("Failed accepting connections", e[0]) continue self.connections.append(conn) self.addresses.append(address) good("Connection established from %s (%s)" % (addr[-1], addr[0]))
def _select_slave(self, slave): slave = int(slave) try: conn = self.connections[slave] except IndexError: error('Not a valid selection', 'use %slist%s to list the clients' \ % (Colors.red, Colors.end)) raise good('Connected to %s' % self.addresses[slave][2]) session = Session(conn, self.addresses[slave]) try: session.prompt() except Exception as e: msg = "Something went wrong:\n%s" % e status = "error" else: del session msg = "Exited cleanly" status = "success" finally: return (status, msg)
async def on_command_error(ctx, error): if isinstance(error, commands.MissingRequiredArgument): if ctx.message.guild is not None: await ctx.message.delete() await send_cmd_help(ctx, commands.MissingRequiredArgument, error) elif isinstance(error, commands.BadArgument): if ctx.message.guild is not None: await ctx.message.delete() await send_cmd_help(ctx, commands.BadArgument, error) elif isinstance(error, commands.CommandInvokeError): output.error("Exception in command '{}', {}".format(ctx.command.qualified_name, error.original)) oneliner = "Error in command '{}' - {}: {}\nIf this issue persists, Please report it in the support server.".format( ctx.command.qualified_name, type(error.original).__name__, str(error.original)) await ctx.send(oneliner) elif isinstance(error, commands.CommandOnCooldown): if ctx.message.guild is not None: await ctx.message.delete() await ctx.author.send(f'This command is on cooldown. Please wait {error.retry_after:.2f}s :alarm_clock: ')
def handler(signal, frame): print() error('Exiting!')