class TelnetPlugin(BasePlugin): def __init__(self, socket, config, framework): BasePlugin.__init__(self, socket, config, framework) self.input_type = "" self.user_input = "" self.io = SocketIO(self._skt, "rw") self._session = None def do_track(self): self.get_session() self.username() self.password() self.options() while self._skt and not self.kill_plugin: self.command() def get_session(self): self._session = str(self.get_uuid4()) def close_descriptors(self): self.io.close() def get_input(self): try: data = self.io.readline(1024).decode() except OSError: self.kill_plugin = True return data = data.strip("\r\n") data = data.strip("\n") return data def username(self): self.io.write(b"Username: "******"username" self.user_input = self.get_input() self.do_save() def password(self): self.io.write(b"Password: "******"password" self.user_input = self.get_input() self.do_save() def command(self): self.input_type = "command" self.io.write(b". ") line = self.get_input() commands = line.split(";") for i in commands: if self.kill_plugin: break self.handle(i) def handle(self, command): if len(command) == 0: return self.input_type = "command" self.user_input = command self.do_save() arguments = command.split(" ", 1) command = arguments.pop(0) if self.is_command(command): if len(arguments) == 0: getattr(self, command)() else: getattr(self, command)(arguments.pop(0)) else: self.io.write(b"%unrecognized command - type options for a list\r\n") def is_command(self, command): if hasattr(self, command): if hasattr(getattr(self, command), "get_command"): return True else: return False else: return False @set_command("basic list of options available to user") def options(self, arguments=None): self.io.write(b"\r\nWelcome, Please choose from the following options\r\n") line_count = 0 for option in dir(self): if self.is_command(option): if line_count == 5: self.io.write(b"\r\n") line_count = 0 tabs = b"" i = 0 try: for i in range(0, ceil((16 - len(option)) / 8)): tabs += b"\t" except ZeroDivisionError: tabs = ":" self.io.write(option.encode() + tabs) line_count += 1 self.io.write(b"\r\n") @set_command("detailed description of options") def help(self, arguments=None): for help in dir(self): if self.is_command(help): tabs = b":" i = 0 try: for i in range(0, ceil((16 - len(help) - 1) / 8)): tabs += b"\t" except ZeroDivisionError: tabs = ":" self.io.write(help.encode() + tabs + getattr(getattr(self, help), "get_description").encode() + b"\r\n") @set_command("prompt to echo back typing") def echo(self, arguments=None): self.input_type = "echo" if arguments: self.user_input = arguments else: self.io.write(b"Text? ") self.user_input = self.get_input() self.do_save() self.io.write(self.user_input.encode() + b"\r\n") @set_command("close telnet connection to server") def quit(self, arguments=None): self.io.write(b"\nGoodbye\n") self.io.close() self.kill_plugin = True
class HTTPPlugin(BasePlugin, BaseHTTPRequestHandler): def __init__(self, socket, config, framework): BasePlugin.__init__(self, socket, config, framework) self.rfile = SocketIO(socket, "r") self.wfile = SocketIO(socket, "w") socket.settimeout(60) def do_track(self): try: self.handle_one_request() except OSError: self.kill_plugin = True return except AttributeError: self.kill_plugin = True return except UnicodeDecodeError: self.kill_plugin = True return self.format_data() self.do_save() self.kill_plugin = True def get_body(self): too_long = False if not hasattr(self, 'headers'): return else: try: body_length = int(self.headers.get('content-length', 0)) if body_length > 65536: body_length = 65536 too_long = True except TypeError: self.body = '' return try: self.body = self.rfile.read(body_length) self.body = str(self.body, 'utf-8') if too_long: self.body += '*' except timeout: self.body = '' def get_session(self): if not hasattr(self, 'headers'): return try: cookie = self.headers.get('cookie', None) except AttributeError: cookie = self.get_uuid4() self.send_header('Set-Cookie', 'SESSION=' + cookie) return if cookie is None: cookie = self.get_uuid4() else: cookie = cookie.split("SESSION=") if len(cookie) > 1: cookie = cookie[1].split()[0] self.send_header('Set-Cookie', 'SESSION=' + cookie) self._session = cookie def format_data(self): if not hasattr(self, 'command'): self.command = '' if not hasattr(self, 'path'): self.path = '' if not hasattr(self, 'headers'): self.headers = '' else: self.headers = self.headers.as_string() if not hasattr(self, 'body'): self.body = '' def address_string(self): return self._skt.getsockname()[0] def end_headers(self): self.get_session() if self.request_version != 'HTTP/0.9': self._headers_buffer.append(b"\r\n") self.flush_headers() def do_GET(self): if self.path in GOOD_PATHS: self.do_HEAD() self.wfile.write(PAGE_LOGIN) else: self.send_error(404) def do_HEAD(self): if self.path in GOOD_PATHS: self.send_response(200) self.send_header('Content-Type', 'text/html') self.send_header('Connection', 'close') self.send_header('Content-Length', str(len(PAGE_LOGIN))) self.end_headers() else: content = (self.error_message_format % {'code': 404, 'message': _quote_html('File not found'), 'explain': _quote_html('Nothing matches the given URI')}) body = content.encode('UTF-8', 'replace') self.send_response(404, "File not found") self.send_header("Content-Type", self.error_content_type) self.send_header('Connection', 'close') self.send_header('Content-Length', int(len(body))) self.end_headers() def do_POST(self): self.get_body() if self.path in GOOD_PATHS: self.go_GET() elif self.path == '/login': self.send_error(403) else: self.send_error(404) def do_PUT(self): self.send_error(501) def do_OPTIONS(self): self.send_error(501) def do_DELETE(self): self.send_error(501) def do_TRACE(self): self.send_error(501) def do_CONNECT(self): self.send_error(501)