HTTPS proxy tunneling with the ssl module

后端 未结 5 1122
耶瑟儿~
耶瑟儿~ 2021-01-02 01:51

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 i

5条回答
  •  迷失自我
    2021-01-02 02:25

    Finally I got somewhere expanding on @kravietz and @02strich answers.

    Here's the code

    import threading
    import select
    import socket
    import ssl
    
    server = 'mail.google.com'
    port = 443
    PROXY = ("localhost", 4433)
    CONNECT = "CONNECT %s:%s HTTP/1.0\r\nConnection: close\r\n\r\n" % (server, port)
    
    
    class ForwardedSocket(threading.Thread):
        def __init__(self, s, **kwargs):
            threading.Thread.__init__(self)
            self.dest = s
            self.oursraw, self.theirsraw = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
            self.theirs = socket.socket(_sock=self.theirsraw)
            self.start()
            self.ours = ssl.wrap_socket(socket.socket(_sock=self.oursraw), **kwargs)
    
        def run(self):
            rl, wl, xl = select.select([self.dest, self.theirs], [], [], 1)
            print rl, wl, xl
            # FIXME write may block
            if self.theirs in rl:
                self.dest.send(self.theirs.recv(4096))
            if self.dest in rl:
                self.theirs.send(self.dest.recv(4096))
    
        def recv(self, *args):
            return self.ours.recv(*args)
    
        def send(self, *args):
            return self.outs.recv(*args)
    
    
    def test():
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(PROXY)
        s = ssl.wrap_socket(s, ciphers="ALL:aNULL:eNULL")
        s.send(CONNECT)
        resp = s.read(4096)
        print (resp, )
    
        fs = ForwardedSocket(s, ciphers="ALL:aNULL:eNULL")
        fs.send("foobar")
    

    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 it ssagd and depth-2 ssl negotiation and binary rubbish:

    [dima@bmg ~]$ openssl s_server  -nocert -cipher "ALL:aNULL:eNULL"
    Using default temp DH parameters
    Using default temp ECDH parameters
    ACCEPT
    -----BEGIN SSL SESSION PARAMETERS-----
    MHUCAQECAgMDBALAGQQgmn6XfJt8ru+edj6BXljltJf43Sz6AmacYM/dSmrhgl4E
    MOztEauhPoixCwS84DL29MD/OxuxuvG5tnkN59ikoqtfrnCKsk8Y9JtUU9zuaDFV
    ZaEGAgRSnJ81ogQCAgEspAYEBAEAAAA=
    -----END SSL SESSION PARAMETERS-----
    Shared ciphers: [snipped]
    CIPHER is AECDH-AES256-SHA
    Secure Renegotiation IS supported
    CONNECT mail.google.com:443 HTTP/1.0
    Connection: close
    
    sagq
    �u\�0�,�(�$��
    �"�!��kj98���� �m:��2�.�*�&���=5�����
    ��/�+�'�#��     ����g@32��ED���l4�F�1�-�)�%���

提交回复
热议问题