Skip to main content
https://www.youtube.com/watch?v=s9xk77X4m5c

Project #1

Brute Force Attacks

Cracking Passwords: A Beginner’s Guide to Brute Force

In cybersecurity, understanding how passwords are cracked sheds light on the need for robust security measures. One such method is brute force, where every possible combination of characters is systematically tried until the correct password is found. Let’s delve into the basics of this technique using Python.

What is Brute Force?

Brute force is a straightforward yet resource-intensive approach to cracking passwords. It systematically generates every possible combination of characters until the correct password is identified. This method is effective but time-consuming, especially for longer and more complex passwords.

Implementing Brute Force in Python

To illustrate this concept, let’s break down a straightforward implementation using Python:

  1. Reading the Target Password:
    • The code begins by reading a target password stored in a file.
    • It compares this password against a predefined string.
  2. Executing the Brute Force Attack:
    • The script initiates a brute force attack if no direct match is found.
    • It systematically tries combinations of characters, starting with shorter lengths and progressing to more complex ones.

Critical Components of the Code

  • Word Matching: The script first attempts to find an exact match for a predefined password within a list of words.
  • Brute Force Function: If no match is found, it employs a brute force function to systematically generate and test password combinations.

Conclusion

Understanding brute force attacks provides insights into the importance of using strong, unique passwords and implementing robust security measures. While effective in some scenarios, brute force is just one method used in cybersecurity. It underscores the ongoing challenge between defenders and attackers in safeguarding digital information.

Individuals and organizations can better protect themselves against potential security threats by learning about these techniques.

Password Cracking Python Code

Password Cracking Python Code

import itertools # imp itertools - use the tools
import string # imp string
import time # imp time

def word(): # method word
    with open('write.txt', 'r') as word: # open a file to read as word
        word_list = word.read().splitlines() # word list has to be read and splitlines

    for i, match in enumerate(word_list, start = 1): # for loop i, match in enumerate word list start=1
        if match == word: # if match equal word
            return f'Common match: {match} (#{i})' # return common match: match #i

def brute_force(word, length, digits = False, symbols = False): # method brute_force # attr - word, length, digits, symbols / false
    chars = string.ascii_lowercase # chars equal string.ascii_lowercase

    if digits: # if digits chars += string digits
        chars += string.digits
    if symbols: # if digits chars += string.punctuation
        chars += string.punctuation 

    attempts = 0 # count attempts = 0
    for guess in itertools.product(chars, repeat = length): # for loop guess intertools.product(char, repeat=length)
        attempts += 1 # attempt += 1
        guess = ''.join(guess) # guess = ''.join(guess)

        if guess == word: # if guess equal word
            return f"{word} was cracked in {attempts} attempts: {guess}" # return word was cracked in attempts: guesses

def main(): # method main
    print('Searching...') # print Searching...
    password = 'abc2' # password = 'abc1'

    start_time = time.perf_counter() # start time = time.perf_counter()

    if common_match := word(): # if common match := common guess inside password
        print(common_match) # print common match 
    else: # else 
        for i in range(3, 6): # for i in range(3, 8)
            if cracked := brute_force(password, length = i, digits = True, symbols = True): # if cracked := brute_force(password, length i, digits true, symbols true)
                print(cracked) # print cracked
            else: # else
                print('There was no match....') # print there was no match...

    end_time = time.perf_counter() # end time = time.perf_counter()
    print(round(end_time - start_time, 2), 's') # print(round(end_time - start_time, 2), 's')

if __name__ == '__main__': # if __name__ == '__main__':
    main()