So I am trying to connect to different sites on the Internet through the proxy that I am currently trying to build.
If I try to connect to a website on port 80 EVERYTHING works perfectly. But when I try to connect to any https link it does not work.
I tried using a self signed certificate but that did not work.
I am now using the certifi lib and it looks like I made so progress.
The function I use for TLS handshake:
def do_handshake():
ca_certificates = certifi.where()
print("ca-certificate mozilla", ca_certificates)
CIPHERS = 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:AES256-SHA'
conn_s = ssl.wrap_socket(conn, ssl_version=ssl.PROTOCOL_TLSv1_2, cert_reqs=ssl.CERT_OPTIONAL, ca_certs=ca_certificates, do_handshake_on_connect=False)
print("Initiating Handshake!")
conn_s.do_handshake()
print("Handshake Completed!")
I accept connection and start a new thread to process the request.
while True:
conn, addr = s.accept()
request = conn.recv(4704)
print(request)
thread = threading.Thread(target=url_string, args=(conn, addr))
thread.setDaemon(True)
thread.start()
The socket server runs on port 9000.
Now If I send a curl request I get some weird text showing in the terminal and it gets stuck on conn_s.do_handshake()
forever and eventually gets timed out.
The headers I send through my socket server back to client:
HTTP/1.1 200 Connection established
< Content-Type: text/plain
< Connection: close
< Set-Cookie: cookie=verified, Expires=Saturday, 8 May 2021 03:10:29
The curl request:
curl https://google.com --proxy 127.0.0.1:9000 -v
If I don't to the TLS handshake I get this error (and many more AAA):
curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number
I use the requests library to first send the get or post request to the url and then return it to conn
.
How do I do the handshake successfully and send the request back to conn
?
UPDATE:
So the handshake problem was fixed by sending the HTTP/1.1 200 Connection Established
header. I checked and the headers were not being sent before the
TLS handshake was done.
But still I get the same error:
curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number
the code:
import socket, ssl
import os, sys, threading
import requests
import datetime
import calendar
HOST = "0.0.0.0"
PORT = os.environ.get("PORT") or 9000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(9)
print("Server listening on port {}".format(PORT))
def url_string(conn, addr):
string = request.decode("UTF-8")
data = string.encode("UTF-8")
print("Request String:", data)
first_line = string.split("\n")[0]
print(first_line)
url = first_line.split(" ")[1]
is_post_request = False
if(first_line.split(" ") [0] == "POST"):
is_post_request = True
http_pos = url.find("://")
if(http_pos == -1):
temp = url
else:
temp = url[(http_pos + 3):]
port_pos = temp.find(":")
webserver = ""
port = -1
path_pos = temp.find("/")
path = ""
if(path_pos == -1):
path_pos = len(temp)
else:
path = temp[path_pos : len(temp)]
if(port_pos == -1 or path_pos < port_pos):
port = 80
webserver = temp[:path_pos]
else:
port = int(temp[port_pos + 1 : len(temp)])
webserver = temp[:port_pos]
print(webserver + path, port)
proxy_request(webserver=webserver + path, port=port)
def proxy_request(webserver, port):
headers = get_HTTP_headers()
data_recv = ""
try:
print("Proxying request to ", addr[0])
if(port == 443):
r = requests.get("https://" + webserver)
data_recv = r.text
send_headers_test()
do_handshake()
else:
r = requests.get("http://" + webserver)
data_recv = r.text
except Exception as e:
print(str(e))
data_recv = str(e).encode()
finally:
print("finally block entered")
merge_HTTP_headers_and_content(headers, data_recv)
import certifi
def do_handshake():
ca_certificates = certifi.where()
print("ca-certificate mozilla", ca_certificates)
CIPHERS = 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:AES256-SHA'
conn_s = ssl.wrap_socket(conn, ssl_version=ssl.PROTOCOL_TLS, cert_reqs=ssl.CERT_OPTIONAL, ca_certs=ca_certificates, do_handshake_on_connect=False)
#conn_s = context.wrap_socket(conn, do_handshake_on_connect=False)
print("Initiating Handshake!")
conn_s.do_handshake()
print("Handshake Completed!")
while True:
conn, addr = s.accept()
request = conn.recv(4704)
print(request)
thread = threading.Thread(target=url_string, args=(conn, addr))
thread.setDaemon(True)
thread.start()