Sobes.tech
Junior — Middle

What methods can be used to prevent man-in-the-middle (MITM) attacks?

sobes.tech AI

Answer from AI

To prevent Man-in-the-Middle (MITM) attacks, the following methods are used:

  • Using secure data transfer protocols like HTTPS (TLS/SSL), which provide encryption and server authentication.
  • Verifying server certificates on the client side to ensure the connection is established with a trusted source.
  • Applying HSTS (HTTP Strict Transport Security) to enforce HTTPS usage.
  • Using VPNs to create a secure communication channel.
  • Implementing two-factor authentication to reduce the risk of credential compromise.
  • Using data integrity mechanisms such as digital signatures.

Example in Python for certificate verification during HTTPS requests using the requests library:

import requests

response = requests.get('https://example.com', verify=True)  # verify=True checks the certificate
print(response.status_code)

If the server's certificate is invalid or fake, the request will raise an exception, helping to prevent MITM.