Esempio n. 1
0
    def BackConnect(self):
        print info.get_information()  # printing information banner
        self.cwd = info.cwd
        readline.set_completer(
            completer.complete)  # calling auto-complete method
        cmdcount = 1
        while True:
            try:
                try:
                    # Getting command to be executed from the user
                    command = raw_input(
                        "[" + info.current_user + colored('@', 'red') +
                        colored(info.hostname, 'green') + colored(
                            ' {0}'.format(self.cwd[:10] + '...' +
                                          self.cwd[-10:] if len(self.cwd) > 20
                                          else self.cwd), 'magenta') +
                        ' ]$ ').strip()
                # If something went wrong screw the list
                except IndexError:
                    command = raw_input('WebHandler@server:$ ').strip()

                command_list = command.split()

                # Updating command history
                self.history.append(unquote(command))

                if command not in ('exit', 'quit', 'bye'):
                    if command == 'clear':
                        Popen('clear', shell=True).wait()
                    elif command and command[0] == '!':
                        self.execute(command)
                    elif command and command[0] == '#':
                        continue
                    # Execute a module
                    elif command and command[0].startswith(('@', ':')):
                        try:
                            self.commands[command_list[0]](command_list)
                        except KeyError:
                            cprint(
                                '[+] {0} module does not exist!'.format(
                                    command_list[0]), 'red')
                    else:
                        try:
                            #Handle the current working directory 'cwd'
                            if command_list[0] == 'cd' and len(
                                    command_list) > 1:
                                cwd = self.cwd
                                if '../' in command_list[
                                        -1] or '..' in command_list[-1]:
                                    self.cwd = '/'.join(
                                        cwd.split('/')
                                        [:-len(command_list[-1].split('..')) +
                                         1])
                                    if not self.cwd:
                                        self.cwd = '/'
                                else:
                                    if command_list[-1].startswith('/'):
                                        cmd = '[ -d {0} ] && echo is_valid'.format(
                                            command_list[-1])
                                        if make_request.get_page_source(
                                                cmd)[0] == 'is_valid':
                                            self.cwd = command_list[-1]
                                        else:
                                            print 'bash: cd: {0}: No such file or directory'.format(
                                                command_list[-1])
                                    else:
                                        cmd = '[ -d {0}/{1} ] && echo is_valid'.format(
                                            cwd, command_list[-1])
                                        if make_request.get_page_source(
                                                cmd)[0] == 'is_valid':
                                            self.cwd = '{0}/{1}'.format(
                                                cwd, command_list[-1])
                                        else:
                                            print 'bash: cd: {0}: No such file or directory'.format(
                                                command_list[-1])

                            elif command_list[0] == 'cd' and len(
                                    command_list) == 1:
                                self.cwd = info.cwd  # dirty patch to get the original cwd

                            else:
                                # Setting aliases for some commands to avoid
                                # Issues realted to empty directories
                                # command = command.replace('ls', 'ls -lh --color') if command_list[0] == 'ls' else command
                                # command = command.replace('rm', 'rm -v') if command_list[0] == 'rm' else command
                                # command = command.replace('cp', 'cp -v') if command_list[0] == 'cp' else command
                                # command = command.replace('ifconfig', '/sbin/ifconfig')
                                if self.aliased_commands.get(command):
                                    command = self.aliased_commands[
                                        command].strip("'")

                                # Get the source code cotenets
                                cmd = 'cd {0}&&{1} 2>&1'.format(
                                    self.cwd, command)
                                source = make_request.get_page_source(cmd)
                                if source:
                                    for line in source:
                                        print line

                                # If the executed command doesn't exist
                                else:
                                    errmsg = '{0}: command not found '.format(
                                        unquote(command))
                                    errmsg += 'or I don\'t have permission to execute it'
                                    if command_list[0] == 'echo':
                                        pass
                                    else:
                                        cprint(errmsg, 'red')
                        except IndexError:
                            pass
                # Exit WebHandler if user provides exit as a command
                else:
                    #on_exit = '\n[+] Preformed "{0}" commands on the server, {1}'.format(cmdcount, info.host_ip.split(',')[0])
                    on_exit = '\n[*] Connection closed'
                    cprint(on_exit, 'red')
                    break

            # If recieved a break (^c)... Do nothing!
            except KeyboardInterrupt:
                print ""
            cmdcount += 1
Esempio n. 2
0
    def BackConnect(self):
        print info.get_information()    # printing information banner
        self.cwd = info.cwd
        readline.set_completer(completer.complete)      # calling auto-complete method
        cmdcount = 1
        while True:
            try:
                try:
                    # Getting command to be executed from the user
                    command = raw_input(info.current_user +
                            colored('@', 'red') +
                            colored(info.host_ip.split(',')[0], 'green') + ':~' +
                            colored('({0})'.format(self.cwd), 'yellow') + ':$ ').strip()
                # If something went wrong screw the list
                except IndexError:
                    command = raw_input('WebHandler@server:$ ').strip()

                command_list = command.split()

                # Updating command history
                self.history.append(unquote(command))

                if command not in ('exit', 'quit', 'bye'):
                    if command == 'clear':
                        Popen('clear', shell=True).wait()
                    elif command and command[0] == '!':
                        self.execute(command)
                    # Execute a module
                    elif command and command[0] == '@':
                        try:
                            self.commands[command_list[0]](command_list)
                        except KeyError:
                            cprint('[+] {0} module does not exist!'.format(command_list[0]), 'red')
                    else:
                        try:
                            #Handle the current working directory 'cwd'
                            if command_list[0] == 'cd' and len(command_list) > 1:
                                cwd = self.cwd
                                if '../' in command_list[-1] or '..' in command_list[-1]:
                                    self.cwd = cwd.rstrip(cwd.split('/').pop()).rstrip('/')
                                else:
                                    if command_list[-1].startswith('/'):
                                        cmd = '[ -d {0} ] && echo is_valid'.format(command_list[-1])
                                        if make_request.get_page_source(cmd)[0] == 'is_valid':
                                            self.cwd = command_list[-1]
                                        else:
                                            print 'bash: cd: {0}: No such file or directory'.format(command_list[-1])
                                    else:
                                        cmd = '[ -d {0}/{1} ] && echo is_valid'.format(cwd, command_list[-1])
                                        if make_request.get_page_source(cmd)[0] == 'is_valid':
                                            self.cwd = '{0}/{1}'.format(cwd, command_list[-1])
                                        else:
                                            print 'bash: cd: {0}: No such file or directory'.format(command_list[-1])

                            elif command_list[0] == 'cd' and len(command_list) == 1:
                                self.cwd = info.cwd  # dirty patch to get the original cwd

                            else:
                                # Setting aliases for some commands to avoid
                                # Issues realted to empty directories
                                command = command.replace('ls', 'ls -lh --color') if command_list[0] == 'ls' else command
                                command = command.replace('rm', 'rm -v') if command_list[0] == 'rm' else command
                                command = command.replace('cp', 'cp -v') if command_list[0] == 'cp' else command
                                command = command.replace('ifconfig', '/sbin/ifconfig')

                                # Get the source code cotenets
                                cmd = 'cd {0};{1}'.format(self.cwd, command)
                                source = make_request.get_page_source(cmd)
                                if source:
                                    for line in source:
                                        print line

                                # If the executed command doesn't exist
                                else:
                                    errmsg = '{0}: command not found '.format(unquote(command))
                                    errmsg += 'or I don\'t have permission to execute it'
                                    if command_list[0] == 'echo':
                                        pass
                                    else:
                                        cprint(errmsg, 'red')
                        except IndexError:
                            pass
                # Exit WebHandler if user provides exit as a command
                else:
                    #on_exit = '\n[+] Preformed "{0}" commands on the server, {1}'.format(cmdcount, info.host_ip.split(',')[0])
                    on_exit = '\n[*] Connection closed'
                    cprint(on_exit, 'red')
                    break

            # If recieved a break (^c)... Do nothing!
            except KeyboardInterrupt:
                print ""
            cmdcount += 1