How to get VNC security type from server with sockets on Python?
Question: How to create a script for Python , which will be connect to specific ip with
port , and print/return , which security type VNC connection have?

    
    
    import socket
    def check(ip,port):
        vnc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        vnc.connect((ip,int(port)))
        vnc_ver = vnc.recv(12)
        print(vnc_ver)
        vnc.send(vnc_ver)
        print(vnc.recv(1024))
    check("127.0.0.1","5900")
    

I just get output:"RFB 003.008"


Answer: IETF RFC 6143 (The Remote Framebuffer Protocol) defines that security
handshake occurs after version handshake (see
<https://tools.ietf.org/rfc/rfc6143.txt> paragraph 7.1.2).

In the first phase of the security handshake, the VNC server is supposed to:

  * send 1 byte containing the number of supported security types
  * followed by the supported security types (each security type coded on one byte)

So a Python script (similar to the one in your question) could display the
supported security types that way:

    
    
    import socket
    
    def displaySecurityType(sec_type):
        switcher = {
            0: "Invalid",
            1: "NONE",
            2: "VNC Authentication"
        } 
        print(' security type: ' + str(sec_type) + ' (' + switcher.get(sec_type,"Not defined by IETF") +')' )
    
    def check(ip,port):
        vnc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        vnc.connect((ip,int(port)))
        vnc_ver = vnc.recv(12)
        print(vnc_ver)
        vnc.send(vnc_ver)
        nb_sec_types = ord(vnc.recv(1))
        print("Nb security types: " + str(nb_sec_types))
        for i in xrange(0,nb_sec_types):
            sec_type = ord(vnc.recv(1))
            displaySecurityType(sec_type) 
    check("127.0.0.1","5900")
    

