Skip to content

Commit

Permalink
use exscript for faster telnet prompt parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-sahlmann committed Jan 5, 2024
1 parent b0b5516 commit 7c3206f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions mb_netmgmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with mb-netmgmt. If not, see <https://www.gnu.org/licenses/

"""Network Management Protocols for Mountebank"""
__version__ = "0.0.78"
__version__ = "0.0.79"

import os
import subprocess
Expand Down Expand Up @@ -107,7 +107,7 @@ def read_imposters(name):
def proxy_imposters(to, snmp_port=161, telnet_port=23, netconf_port=830):
return [
proxy_imposter(snmp_port, "snmp", to, "oids"),
proxy_imposter(telnet_port, "telnet", to, "command"),
proxy_imposter(telnet_port, "telnet", f"telnet://{to}", "command"),
proxy_imposter(
netconf_port,
"netconf",
Expand Down
33 changes: 20 additions & 13 deletions mb_netmgmt/telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
# along with mb-netmgmt. If not, see <https://www.gnu.org/licenses/

import re
import telnetlib
import time
from socketserver import StreamRequestHandler
from socketserver import ThreadingTCPServer as Server

from mb_netmgmt.__main__ import Protocol, get_cli_patterns
from Exscript.protocols import Telnet

from mb_netmgmt.__main__ import Protocol


class Handler(StreamRequestHandler, Protocol):
Expand All @@ -35,8 +36,9 @@ def handle(self):
self.read_password()
self.command_prompt = b"#"
try:
self.telnet.write(self.password)
self.command_prompt = self.telnet.read_until(b"#").split(b"\n")[-1]
self.telnet.send(self.password)
self.command_prompt = self.telnet.expect_prompt()[1][0].encode()
self.telnet.app_authenticated = True
except AttributeError:
pass
self.wfile.write(self.command_prompt)
Expand All @@ -52,7 +54,7 @@ def handle_command(self, command, request_id):
return self.handle_request({"command": command.decode()}, request_id)

def send_upstream(self, request, request_id):
self.telnet.write(request["command"].encode())
self.telnet.execute(request["command"])

def read_request(self):
return self.rfile.readline(), None
Expand All @@ -63,17 +65,17 @@ def respond(self, response, request_id):
self.stopped = True

def read_proxy_response(self):
prompt_patterns = [self.command_prompt]
prompt_patterns += get_cli_patterns()
_, _, response = self.telnet.expect(prompt_patterns, timeout=600)
return {"response": response.decode()}
return {"response": self.telnet.response}

def handle_username_prompt(self):
username_prompt = b"Username: "
to = self.get_to()
if to:
self.telnet = telnetlib.Telnet(to.hostname)
username_prompt = self.telnet.read_until(username_prompt)
t = Telnet(debug=4)
t.connect(to.hostname)
result = t.expect(t.get_username_prompt())
username_prompt = (t.response[:-1] + result[1][0]).encode()
self.telnet = t
self.wfile.write(username_prompt)

def read_password(self):
Expand All @@ -83,8 +85,13 @@ def read_password(self):
self.password = self.rfile.readline()

def handle_username(self):
self.command_prompt = b"Password: "
self.handle_request({"command": self.username.decode()}, None)
try:
t = self.telnet
t.send(self.username)
password_prompt = t.expect(t.get_password_prompt())[1][0].encode()
except AttributeError:
password_prompt = b"Password: "
self.wfile.write(password_prompt)


def extract_username(byte_string):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ dependencies = [
"ruamel.yaml",
"requests",
"scapy",
"exscript",
]

0 comments on commit 7c3206f

Please sign in to comment.