Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove telnetlib in py3.13 #429

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions pocsuite3/modules/listener/bind_tcp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
import sys
import socket
import zlib
import pickle
import base64
import select
import telnetlib
import threading
from pocsuite3.lib.core.poc import POCBase
from pocsuite3.lib.utils import random_str
Expand All @@ -26,7 +26,7 @@ def read_inputs(s):


def read_results(conn, inputs):
if isinstance(conn, telnetlib.Telnet):
if sys.version_info <= (3, 12) and isinstance(conn, telnetlib.Telnet):
flag = random_str(6).encode()
inputs = inputs.strip() + b';' + flag + b'\n'
results = b''
Expand All @@ -43,7 +43,7 @@ def read_results(conn, inputs):
results = os.linesep.encode().join(
results.split(flag)[0].splitlines()[0:-1])
return results.strip() + b'\n'
elif callable(conn):
if callable(conn):
results = conn(inputs.decode())
if not isinstance(results, bytes):
results = results.encode()
Expand Down Expand Up @@ -71,6 +71,16 @@ def read_results(conn, inputs):
return b'\n'


def wait_msg(conn, inputs):
try:
while True:
msg = conn.recv(1024).decode('utf-8', errors='ignore')
if inputs in msg.lower():
break
except Exception:
pass


def flow_redirect(conn):
s = socket.socket()
s.connect((conf.connect_back_host, conf.connect_back_port))
Expand Down Expand Up @@ -116,20 +126,39 @@ def bind_tcp_shell(host, port, check=True):


def bind_telnet_shell(host, port, user, pwd, check=True):
# see https://peps.python.org/pep-0594/#telnetlib
if sys.version_info <= (3, 12):
import telnetlib

if not check_port(host, port):
return False
try:
tn = telnetlib.Telnet(host, port)
tn.expect([b'Login: ', b'login: '], 10)
tn.write(user.encode() + b'\n')
tn.expect([b'Password: ', b'password: '], 10)
tn.write(pwd.encode() + b'\n')
tn.write(b'\n')
if check:
flag = random_str(6).encode()
if flag not in read_results(tn, b'echo %s' % flag):
return False
if sys.version_info <= (3, 12):
tn = telnetlib.Telnet(host, port)
tn.expect([b'Login: ', b'login: '], 10)
tn.write(user.encode() + b'\n')
tn.expect([b'Password: ', b'password: '], 10)
tn.write(pwd.encode() + b'\n')
tn.write(b'\n')
if check:
flag = random_str(6).encode()
if flag not in read_results(tn, b'echo %s' % flag):
return False

else:
tn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tn.connect((host, port))
wait_msg(tn, 'login: ')
tn.sendall((user + "\n").encode('utf-8'))
wait_msg(tn, 'password: ')
tn.sendall((pwd + "\n").encode('utf-8'))
if check:
flag = random_str(6).encode()
if flag not in read_results(tn, b'echo %s' % flag):
return False

start_listener(tn)

except Exception as e:
logger.error(str(e))

Expand Down
Loading