Set Up a Basic HTTP Server with Authentication
In this step, you will set up a simple HTTP server that requires basic authentication. This server will serve as our target for the Hydra attack in the next step. We will use a Python script for this.
First, ensure you are in the ~/project directory:
cd ~/project
Now, create a new Python file named webserver.py using nano:
nano webserver.py
Paste the following Python code into the editor:
import http.server
import socketserver
import base64
PORT = 8000
class AuthHandler(http.server.SimpleHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_AUTHHEAD(self):
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm="My Realm"')
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
auth = self.headers.get('Authorization')
if auth == None:
self.do_AUTHHEAD()
self.wfile.write(b"Authentication Required")
elif auth == 'Basic YWRtaW46cGFzc3dvcmQ=': ## admin:password base64 encoded
http.server.SimpleHTTPRequestHandler.do_GET(self)
else:
self.do_AUTHHEAD()
self.wfile.write(b"Authentication Failed")
Handler = AuthHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
This script creates a basic HTTP server on port 8000. It requires basic authentication. The correct username is admin and the password is password. The base64 encoded string YWRtaW46cGFzc3dvcmQ= represents admin:password.
Save the file (Ctrl+O, then Enter) and exit nano (Ctrl+X).
Now, run the Python script to start the HTTP server. We will run it in the background so you can continue using the terminal:
nohup python3 webserver.py > /dev/null 2>&1 &
The nohup command allows the process to continue running even if you close the terminal. > /dev/null 2>&1 redirects standard output and standard error to /dev/null, preventing the server's output from cluttering your terminal. The & at the end runs the command in the background.
To confirm the server is running, you can check if a process is listening on port 8000:
ss -ltn | grep ':8000'
You should see output similar to this, indicating a process is listening on port 8000:
LISTEN 0 4096 0.0.0.0:8000 0.0.0.0:*
Keep this server running for the next step.