I'd like to manually (using the socket and ssl modules) make an HTTPS
request through a proxy which itself uses HTTPS
.
I can perform the initial CONNECT
exchange just fine:
import ssl, socket
PROXY_ADDR = ("proxy-addr", 443)
CONNECT = "CONNECT example.com:443 HTTP/1.1\r\n\r\n"
sock = socket.create_connection(PROXY_ADDR)
sock = ssl.wrap_socket(sock)
sock.sendall(CONNECT)
s = ""
while s[-4:] != "\r\n\r\n":
s += sock.recv(1)
print repr(s)
The above code prints HTTP/1.1 200 Connection established
plus some headers, which is what I expect. So now I should be ready to make the request, e.g.
sock.sendall("GET / HTTP/1.1\r\n\r\n")
but the above code returns
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
Reason: You're speaking plain HTTP to an SSL-enabled server port.<br />
Instead use the HTTPS scheme to access this URL, please.<br />
</body></html>
This makes sense too, since I still need to do an SSL handshake with the example.com
server to which I'm tunneling. However, if instead of immediately sending the GET
request I say
sock = ssl.wrap_socket(sock)
to do the handshake with the remote server, then I get an exception:
Traceback (most recent call last):
File "so_test.py", line 18, in <module>
ssl.wrap_socket(sock)
File "/usr/lib/python2.6/ssl.py", line 350, in wrap_socket
suppress_ragged_eofs=suppress_ragged_eofs)
File "/usr/lib/python2.6/ssl.py", line 118, in __init__
self.do_handshake()
File "/usr/lib/python2.6/ssl.py", line 293, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [Errno 1] _ssl.c:480: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
So how can I do the SSL handshake with the remote example.com
server?
EDIT: I'm pretty sure that no additional data is available before my second call to wrap_socket
because calling sock.recv(1)
blocks indefinitely.
Finally I got somewhere expanding on @kravietz and @02strich answers.
Here's the code
Don't mind custom
cihpers=
, that only because I didn't want to deal with certificates.And there's depth-1 ssl output, showing
CONNECT
, my response to itssagd
and depth-2 ssl negotiation and binary rubbish:Judging from the API of the OpenSSL and GnuTLS library, stacking a SSLSocket onto a SSLSocket is actually not straightforwardly possible as they provide special read/write functions to implement the encryption, which they are not able to use themselves when wrapping a pre-existing SSLSocket.
The error is therefore caused by the inner SSLSocket directly reading from the system socket and not from the outer SSLSocket. This ends in sending data not belonging to the outer SSL session, which ends badly and for sure never returns a valid ServerHello.
Concluding from that, I would say there is no simple way to implement what you (and actually myself) would like to accomplish.
This should work if the CONNECT string is rewritten as follows:
Not sure why this works, but maybe it has something to do with the proxy I'm using. Here's an example code:
Note how the socket is first opened and then open socket placed in SSL context. Then I manually initialize SSL handshake. And output:
It's based on pyOpenSSL because I needed to fetch invalid certificates too and Python built-in ssl module will always try to verify the certificate if it's received.
It doesn't sound like there's anything wrong with what you're doing; it's certainly possible to call
wrap_socket()
on an existingSSLSocket
.The 'unknown protocol' error can occur (amongst other reasons) if there's extra data waiting to be read on the socket at the point you call
wrap_socket()
, for instance an extra\r\n
or an HTTP error (due to a missing cert on the server end, for instance). Are you certain you've read everything available at that point?If you can force the first SSL channel to use a "plain" RSA cipher (i.e. non-Diffie-Hellman) then you may be able to use Wireshark to decrypt the stream to see what's going on.
Building on @kravietz answer. Here is a version that works in Python3 through a Squid proxy:
This works in Python 2 also.