OpenSecurity/bin/proxy_getter.py
changeset 234 216da9017f8f
child 240 d7ef04254e9c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/bin/proxy_getter.py	Thu Oct 02 13:08:09 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +import socket
     1.5 +import os
     1.6 +import httplib
     1.7 +import sys
     1.8 +import _winreg
     1.9 +import re
    1.10 +
    1.11 +DNS_WPAD_FILENAME = "wpad.dat"
    1.12 +
    1.13 +def check_for_wpad_file(server, path):
    1.14 +
    1.15 +    wpad_url = "http://%s/%s"%(server, path)
    1.16 +    print "checking", wpad_url
    1.17 +    try:
    1.18 +        conn = httplib.HTTPConnection(server)
    1.19 +        conn.request("HEAD", "/%s"%path)
    1.20 +        r = conn.getresponse()
    1.21 +        if r.status == 200:
    1.22 +            return wpad_url
    1.23 +    except Exception, e:
    1.24 +        return None
    1.25 +
    1.26 +    return None
    1.27 +
    1.28 +def get_wpad_server_searchlist():
    1.29 +    #get fully-qualified hostname
    1.30 +    fqhn = socket.getfqdn().split(" ")[0]
    1.31 +    
    1.32 +    #do we really have a fully-qualified name?
    1.33 +    #if not, linux offers a second possibility
    1.34 +    if fqhn.count(".") == 0 and os.name == 'posix':
    1.35 +        #weird method to get own ip address and fqhn 
    1.36 +        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    1.37 +        s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    1.38 +        s.connect(('<broadcast>', 0))
    1.39 +        my_ip = s.getsockname()[0]
    1.40 +        s.close()
    1.41 +        fqhn = socket.gethostbyaddr(my_ip)[0]
    1.42 +
    1.43 +    parts = fqhn.split(".")
    1.44 +    if len(parts) < 3:
    1.45 +        return []
    1.46 +
    1.47 +    servers = []
    1.48 +    parts[0] = "wpad"
    1.49 +    servers.append(".".join(parts))
    1.50 +    parts = parts[0:1]+parts[2:]
    1.51 +    #not correct for some suffixes, but everyone does it that way, see wikipedia for details
    1.52 +    while len(parts) >= 3:
    1.53 +        servers.append(".".join(parts))
    1.54 +        parts = parts[0:1]+parts[2:]
    1.55 +
    1.56 +    return servers
    1.57 +
    1.58 +def search_for_wpad_url():
    1.59 +    #TODO: According to RFC we should check DHCP first, but it isn't used often
    1.60 +    #check most common case first
    1.61 +    wpad_server = "wpad"
    1.62 +    wpad_url = check_for_wpad_file(wpad_server, DNS_WPAD_FILENAME)
    1.63 +    if wpad_url:
    1.64 +        return wpad_url
    1.65 +
    1.66 +    #wpad DNS search procedure
    1.67 +    possible_wpad_servers = get_wpad_server_searchlist()
    1.68 +    for server in possible_wpad_servers:
    1.69 +        wpad_url = check_for_wpad_file(server, DNS_WPAD_FILENAME)
    1.70 +        if wpad_url:
    1.71 +            return wpad_url
    1.72 +        
    1.73 +    #no wpad url found
    1.74 +    return None
    1.75 +    
    1.76 +def getProxySettings():
    1.77 +    # try to autodetect domain wpad file
    1.78 +    wpad_url = search_for_wpad_url()
    1.79 +    if wpad_url:
    1.80 +        return {'ProxyAutoConfigURL': wpad_url}
    1.81 +    
    1.82 +    # get Proxy settings from registry      
    1.83 +    aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
    1.84 +    aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
    1.85 +    _, valueCount, _ = _winreg.QueryInfoKey(aKey)
    1.86 +    reg_entries = dict()
    1.87 +    for i in range(valueCount):                                           
    1.88 +        try:
    1.89 +            n,v,_ = _winreg.EnumValue(aKey,i)
    1.90 +            reg_entries[n] = v
    1.91 +        except EnvironmentError:                                               
    1.92 +            break
    1.93 +    _winreg.CloseKey(aKey)
    1.94 +    
    1.95 +    # return configured WPAD url
    1.96 +    if 'AutoConfigURL' in reg_entries.keys():
    1.97 +        return {'ProxyAutoConfigURL': reg_entries['AutoConfigURL']}
    1.98 +
    1.99 +    # return manually configured proxy
   1.100 +    if 'ProxyEnable' in reg_entries.keys() and reg_entries['ProxyEnable'] == 1:
   1.101 +        proxy_search = re.search(r"(?<=http=)(?P<ProxyServer>.*?)(?=;)", reg_entries['ProxyServer'])
   1.102 +        if proxy_search:
   1.103 +            proxies = proxy_search.groupdict()
   1.104 +            if 'ProxyServer' in proxies.keys(): # found http proxy
   1.105 +                return {'ProxyServer': proxies['ProxyServer']}  
   1.106 +        return {'ProxyServer': reg_entries['ProxyServer']}
   1.107 +        
   1.108 +    return None
   1.109 \ No newline at end of file