# Try every username and password from a list on a website

This code will be brute force specific site using http POST method.

 def BruteForce(url, username, password):
userlist = userlist.split("\n")

passlist = passlist.split("\n")

for i in userlist:
for j in passlist:
data = urllib.urlencode(data)
request = urllib2.Request(url, data)

response = urllib2.urlopen(request)

isconnect = "True"
return

else:
pass


Is this code efficient? How I can improve my code? Should I use multithreading mechanism to speed up my script?

• It's probably a good idea to close() your files explicitly. – Joel Cornett Apr 5 '12 at 9:52

First, improving the code:

1. Reading lines from a file is as easy as open(name, 'r').readlines(). No need for three lines where one is enough.

2. data and data2, are on the face of it redundant (and are badly named). The redundancy is only resolved because you reuse data – not a good idea. Use proper variable naming and use new variables for a new purpose, don’t reuse variables.

(And in general, choose more relevant names.)

3. If you don’t use the else branch in a conditional, don’t write it.

4. Separation of concerns! This method tries to find a way into a website, not print stuff to the console. Return the result instead. That way, the function can be properly used.

This leaves us with:

def BruteForce(url, userlist_file, passlist_file):

encoded = urllib.urlencode(data)
request = urllib2.Request(url, encoded)
response = urllib2.urlopen(request)

isconnect = 'True'
return data

return { } # Nothing found


Is this code efficient?

Not really. The problem isn’t so much the code in itself, it’s more the fact that HTTP requests are generally quite slow, but the most time is spent waiting for data. If you do them sequentially (and you do), most of the script’s running time is spent waiting.

This can be drastically improved by sending several requests concurrently. This doesn’t even need multithreading – you can just open several connections in a loop, and then start reading from them. Even better would be to read asynchronously. Unfortunately, urllib doesn’t support this – but other libraries do.

That said, you cannot simply bombard the web server with tons of requests. After all, your script is malicious and you don’t want the web server to notice you. So you need to fly under the radar – send just as many concurrent requests as the server allows without being blocked immediately.

Finding this number is tricky and requires guesswork and a bit of luck. If you are lucky, the server is badly configured and won’t mind lots of connections. If you want to stay on the safe side, stay way low.

I have no idea, nor much interest in, how to engineer such an attack.

• thx! I have learned a lot from your comment! – improvemycode Mar 31 '12 at 21:36
• There is no asynchttp library documentation :( – improvemycode Apr 4 '12 at 15:21
• One minor thing. It's usually a good idea to explicitly close your file objects. OP could do this with a with bloc however. – Joel Cornett Apr 5 '12 at 9:53

1.

def BruteForce(url, userlist_file, passlist_file):


PEP8 recommends you to name functions in lowercase/underscore style.

def bruteforce(url, userlist_file, passlist_file):


2.

isconnect = "True"


If it's a constant move it out of function, at least out of cycle. Name it UPPERCASE.

SUCCESS_RESPONSE = 'True'


3.

if(payload.find(isconnect)>=0):


Python has build-in function in for your case which returns True/False. Brackets are also redundant.

if SUCCESS_RESPONSE in payload:


4.

You don't check exceptions that are very likely when you send HTTP requests. In this case your script stop and you have to attack server from the beginning. I also advise you to close connections. With-statement does it automatically.

from contextlib import closing

try:
with closing(urllib2.urlopen(request)) as response: