Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

3.8 KB
Raw Blame

A dictionary attack is a type of cryptanalytic attack where the attacker attempts to break a password or encrypted message by trying common words or phrases, usually drawn from a precompiled list called a dictionary. This type of attack exploits the fact that many people use easily guessable passwords, such as common words, names, or phrases.

In the context of a cryptosystem, a dictionary attack is typically performed against a password hash (e.g., when an attacker tries to crack a password hash stored in a database). The attacker hashes each word in the dictionary and compares it to the stored hash. If a match is found, the attacker has successfully cracked the password.

Below is an example of a dictionary attack using Python, where we will try to crack a password hash by comparing it to hashes of words from a dictionary file.

Python Code for a Dictionary Attack on a Password Hash

In this example, we'll use the SHA-256 hash function (commonly used in cryptosystems) to hash each word in the dictionary and compare it to a given hash. The attacker doesn't know the original password but knows the hash.

import hashlib

# Function to perform dictionary attack
def dictionary_attack(target_hash, dictionary_file):
    # Open the dictionary file (list of potential passwords)
    with open(dictionary_file, 'r') as file:
        for word in file:
            word = word.strip()  # Remove any surrounding whitespace or newlines
            hashed_word = hashlib.sha256(word.encode()).hexdigest()  # Hash the word using SHA-256

            # Compare the hash with the target hash
            if hashed_word == target_hash:
                print(f"Password found: {word}")
                return word  # Return the cracked password

    print("Password not found in dictionary.")
    return None

# Example usage:
# The hash of the password "password123" (SHA-256)
target_hash = 'ef92b778bafe771e89245b05005e3601b6a1edc8a41986b0b8109a2a56b3b101'  # Precomputed SHA-256 hash of "password123"

# Path to the dictionary file (a list of possible passwords)
dictionary_file = 'dictionary.txt'  # Replace this with the actual path to your dictionary file

# Perform the dictionary attack
cracked_password = dictionary_attack(target_hash, dictionary_file)

if cracked_password:
    print(f"Cracked password: {cracked_password}")
else:
    print("No match found.")

How the Code Works

  1. Dictionary File: The dictionary file (dictionary.txt) contains a list of potential passwords (one per line). The attacker tries to match each password in the dictionary by hashing it and comparing it to the target hash.
  2. SHA-256 Hashing: The code uses the hashlib library to compute the SHA-256 hash of each word from the dictionary.
  3. Hash Comparison: For each word, the attacker hashes it and checks if the hash matches the target hash.
  4. Success: If a match is found, the code prints the cracked password. Otherwise, it reports that the password wasn't found.

Example Dictionary File

The dictionary file should contain common passwords, such as:

password123
123456
qwerty
letmein
admin
welcome
password

Considerations for Defending Against Dictionary Attacks

To protect against dictionary attacks, it is recommended to use:

  • Strong, complex passwords that do not appear in dictionaries.
  • Salting: Adding a random value (salt) to the password before hashing ensures that the same password will have different hashes, making precomputed dictionary attacks ineffective.
  • Key Stretching: Techniques like PBKDF2, bcrypt, and scrypt slow down the hashing process, making dictionary attacks less feasible.

This code provides a basic demonstration of how dictionary attacks can work and serves as a way to test the strength of password hashes against simple attacks.