Attempt Attack on Fake Service
In this step, we will simulate an attack on a fake service using Hydra. This will help you understand how Hydra works and how to use it to crack passwords. We'll start with a simple scenario to get you familiar with the tool.
First, let's create a simple "service" that requires a username and password. We'll use a simple Python script for this purpose.
Navigate to your project directory:
cd ~/project
Create a file named fake_service.py
:
nano fake_service.py
Paste the following Python code into the fake_service.py
file:
#!/usr/bin/env python3
import socket
import sys
HOST = '127.0.0.1' ## Standard loopback interface address (localhost)
PORT = 65432 ## Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
username = conn.recv(1024).decode().strip()
password = conn.recv(1024).decode().strip()
if username == 'testuser' and password == 'password123':
conn.sendall(b"Login successful!")
else:
conn.sendall(b"Login failed.")
Save the file and exit the editor. Make the script executable:
chmod +x fake_service.py
Now, run the fake service in the background:
./fake_service.py &
This starts the Python script, which listens for connections on port 65432. The &
puts the process in the background.
Next, let's create a username and password list for Hydra to use. Create a file named users.txt
:
nano users.txt
Add the following username to the file:
testuser
Save the file and exit the editor.
Create a file named passwords.txt
:
nano passwords.txt
Add the following passwords to the file:
password
password123
wrongpassword
Save the file and exit the editor.
Now, let's use Hydra to attack our fake service. We'll use the generic
module since we've created a custom service.
hydra -l testuser -P passwords.txt 127.0.0.1 generic "USER <USER> PASS <PASS> RET Login successful!" -s 65432 -vV
Let's break down this command:
hydra
: The Hydra command.
-l testuser
: Specifies the username to use. We're using a single username in this example.
-P passwords.txt
: Specifies the password list file.
127.0.0.1
: The target IP address (localhost in this case).
generic
: Specifies the generic module, which allows us to define the protocol.
"USER <USER> PASS <PASS> RET Login successful!"
: This is the protocol definition. <USER>
and <PASS>
are placeholders that Hydra replaces with the username and password from the lists. RET Login successful!
tells Hydra to look for "Login successful!" in the response to determine a successful login.
-s 65432
: Specifies the port number.
-vV
: Verbose mode, which shows the attempts in real-time.
You should see Hydra attempting different passwords. After a few seconds, it should find the correct password:
Hydra v9.6 (c) 2024 by van Hauser/THC & David Maciejak - Please use caution!
Hydra starting at 2024-10-27 14:30:00
[DATA] 1 task/1 service (1 connection per task, 1 thread per task)
[DATA] attacking service 127.0.0.1 on port 65432
[DATA] testing user: 'testuser' password: 'password'
[DATA] testing user: 'testuser' password: 'password123'
[65432] [generic] host: 127.0.0.1 login: testuser password: password123
Hydra is finishing at 2024-10-27 14:30:02 after 00:00:02
1 task completed, 1 valid password found
This output shows that Hydra successfully found the password password123
for the user testuser
.
Finally, let's stop the fake service. First, find its process ID:
ps aux | grep fake_service.py
You'll see a line similar to this:
labex 1234 0.1 0.2 12345 6789 pts/0 Sl 14:29 0:00 ./fake_service.py
The second number (1234 in this example) is the process ID (PID). Replace 1234
with the actual PID from your output.
Now, kill the process:
kill 1234
This stops the fake service.