#!/usr/bin/env python from urllib2 import urlopen from urllib import urlencode import libxml2dom import socket from urlparse import urlparse from ipy import * data = {} # Username and password data["username"] = "username" data["password"] = "password" # Set this to the entry you choose from the "Authentication" options data["provider"] = "Guest Users" # Set this to the hostname of the captive portal portal = "cas.example.com" # Set this to the IP range of the unauthenticated subnet unauth = IPRange('10.10.10.0/24') # Check if the current IP address is unauthenticated ip = IPAddress(socket.getaddrinfo(socket.gethostname(), None)[0][4][0]) if ip in unauth: # Open any page to get the URL of the portal f = urlopen("https://" + portal) s = f.read() f.close() # Extract the redirect URL from the page doc = libxml2dom.parseString(s, html=1) url = doc.getElementsByTagName("meta")[0].getAttribute("content").split('=', 1) # Extract the "cm" parameter from the redirect URL parsed = urlparse(url[1]) params = dict([part.split('=') for part in parsed[4].split('&')]) cm = params["cm"] # Open the page with the Java applet f = urlopen(url[1]) s = f.read() f.close() # Extract the session key and ip from the applet tag doc = libxml2dom.parseString(s, html=1) applet = doc.getElementsByTagName("applet")[0] for node in applet.childNodes: if node.getAttribute("name") == "s6": session = node.getAttribute("value") if node.getAttribute("name") == "userip": userip = node.getAttribute("value") # Setup the POST data for the login form data["reqFrom"] = "perfigo_login.jsp" data["uri"] = "http://google.com" data["registerGuest"] = "NO" data["cm"] = cm data["session"] = session data["userip"] = userip data["pm"] = "" data["pageid"] = "-1" data["compact"] = "false" # Submit the login information f = urlopen("https://" + portal + "/auth/perfigo_cm_validate.jsp", urlencode(data)) s = f.read() f.close() # Check if it worked if (s.find("Renewing IP Address") == -1): print "Authentication unsuccessful" print "URL: " + url[1] print "Session: " + session print "cm: " + cm print "User IP: " + userip print s exit(1)