create server with flask using python and print all TLS info about clients
- User
create server with flask using python and print all TLS info about clients
- Assistant
create server with flask using python and print all TLS info about clients
To create a Flask server in Python that prints out details regarding the Transport Layer Security (TLS) information for incoming client connections, you'll need to serve the Flask app using a WSGI server that supports HTTPS, such as werkzeug.serving. It's important to note that logging sensitive TLS details like the secrets should be done with caution and only in a secure, non-production environment.
Before starting, ensure you have Flask installed, or you can install it using pip:
pip install flask
You will also need OpenSSL to generate self-signed certificates or use any valid certificates if you already have them available.
For testing purposes, you can generate your self-signed SSL certificate and key. Run these commands in your terminal:
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
You will be prompted to enter information for the certificate. This certificate will be valid for 365 days.
Here is the sample code to create a Flask server that logs TLS information of incoming connections. We'll modify the server to enable extraction of that security detail.
from flask import Flask, request
import ssl
from werkzeug.serving import run_simple
app = Flask(__name__)
@app.route('/')
def index():
# If TLS information, extract and print it
if request.environ.get('werkzeug.socket'):
socket = request.environ['werkzeug.socket']
if hasattr(socket, 'getpeercert'):
cert = socket.getpeercert()
print("Client Certificate Details:")
print(cert)
cipher = socket.cipher() if hasattr(socket, 'cipher') else None
print("Cipher Used:")
print(cipher)
ssl_version = getattr(socket, 'version', lambda: "(Not available)")()
print("SSL Version:")
print(ssl_version)
return 'Welcome to Secure Flask server!'
if __name__ == '__main__':
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('cert.pem', 'key.pem')
# Pass SSL context to run_simple to use HTTPS
run_simple('localhost', 4000, app, ssl_context=context)
Generate a Self-Signed Certificate: We make our own certificate for HTTPS testing.
Flask App: We create a small Flask application.
TLS Info Extraction: Inside the index route, we try to extract the socket associated with the client connection using request.environ['werkzeug.socket']. From this socket, we attempt to extract TLS details like the certificate, cipher, and SSL version used for that connection.
Running the Server: We utilize werkzeug.serving.run_simple with an SSL context to ensure that our Flask app serves over HTTPS.
Execute the Python file. Open a browser or use a tool like curl to make an HTTPS request to https://localhost:4000/. Since this is using a self-signed certificate, you might have to bypass the security warning in your browser or use an appropriate flag in curl to ignore SSL checks (curl -k https://localhost:4000/).
Not yet included in a published release.