Skip to content

Instantly share code, notes, and snippets.

@mgedigian
Created October 12, 2009 05:23
Show Gist options
  • Save mgedigian/208167 to your computer and use it in GitHub Desktop.
Save mgedigian/208167 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""
Requires BeautifulSoup (http://www.crummy.com/software/BeautifulSoup/)
Reads totally insecure configuration file:
~/.airbears/airbears_config.py
With content (don't forget the quotes):
username="My Username"
password="My Password"
"""
import os, sys, commands, urllib
homedir = os.path.expanduser("~")
airbears_url = "https://wireless-gw1.berkeley.edu:443/logon"
def get_login_page():
f = urllib.urlopen(airbears_url)
doc = f.read()
f.close()
return doc
def extract_vars(html):
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(''.join(html))
forms = soup.find("form")
inputs = forms.findAllNext("input")
form_values = {}
for input in inputs:
try:
value = input["value"]
form_values[ input["name"] ] = input["value"]
except:
pass
return form_values
def inject_config(form_values):
configdir = homedir + os.sep + ".airbears"
sys.path.append(configdir)
try:
import airbears_config
for var in dir(airbears_config):
if var[0] != "_":
form_values[var] = vars(airbears_config)[var]
except ImportError:
import getpass
form_values["username"] = raw_input("Enter username: ")
form_values["password"] = getpass.getpass("Enter password: ")
response = raw_input("Do you want to save these in a **totally insecure** config file? ")
if response[0].lower() != "y":
if not os.path.exists(configdir):
os.makedirs(configdir)
f = open(configdir + os.sep + "airbears_config.py", "w")
f.write("username=" + form_values["username"] + "\n")
f.write("password=" + form_values["password"] + "\n")
f.close()
return form_values
def main():
html = get_login_page()
form = extract_vars(html)
if form["logon_action"] == "Logoff":
response = raw_input("It looks like you're already logged in. Do you want to logoff? ")
if response[0].lower() != "y":
return
form = inject_config(form)
cmd = "curl "
for key,value in form.items():
try:
cmd += '-d %s=%s ' % (urllib.quote(key), urllib.quote(value))
except Exception, e:
print >> sys.stderr, e.message
print >> sys.stderr, key, "=", value
cmd += " " + airbears_url
output = commands.getoutput(cmd)
print output
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment