Skip to content

Instantly share code, notes, and snippets.

@Pretz
Created February 26, 2009 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pretz/71183 to your computer and use it in GitHub Desktop.
Save Pretz/71183 to your computer and use it in GitHub Desktop.
#!/opt/local/bin/perl -w
# Scrapes the AirBears logon page for interesting variables and authenticates
# you with a given SID and password. Should work with minor modifications
# for other Vernier based systems.
#
# Licensed under the WTFPL, see http://sam.zoy.org/wtfpl/
#
# (C) 2006 Joshua Kwan <joshk@ocf.berkeley.edu>
use HTTP::Cookies;
use LWP::UserAgent;
use HTML::Tree;
use Crypt::SSLeay;
print "AirBears authentication agent started.\n";
my $ua = LWP::UserAgent->new(agent => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.2) Gecko/20070208 Iceweasel/2.0.0.2");
my $uri = "http://www.google.com/";
push @{ $ua->requests_redirectable }, 'POST'; # This URL returns 302 Found
my $rc = "/etc/calnet.cf";
if (defined $ENV{'HOME'}) {
my $prc = $ENV{'HOME'} . "/.calnet";
$rc = $prc if (-e $prc);
}
open RC, '<', $rc or die "Please provide your CalNet info in the format:\n\nsid = 12345678\npassword = billybob123\n\nin the file \"$rc\". Terminated";
my ($sid, $pass);
while (<RC>)
{
chomp;
my ($k, $v) = split(/ = /, $_, 2);
$sid = $v if ($k eq 'sid');
$pass = $v if ($k eq 'password');
}
close RC;
die "Required information (either 'sid' or 'password') missing from $rc. Terminated" if (!$sid or !$pass);
# First retrieve tokens from logon page
print "Downloading AirBears login page...\n";
my $gateway = "https://wireless-gw1.berkeley.edu";
my $url = "${gateway}/logon?$uri";
my $response = $ua->get($url);
$response->is_success or die "Couldn't get logon page: ".$response->status_line;
# If already logged in, then
if ($response->content =~ /Logged on as: ([^<]*)/)
{
print "Already logged on as user $1.\n";
exit 0;
}
my $tree = HTML::TreeBuilder->new_from_content($response->content);
my ($action, $enctype);
# Parse the tree and grab all the form variables from form name 'logonForm'
foreach my $tform ($tree->find_by_tag_name('form'))
{
if ($tform->attr('name') eq 'logonForm')
{
foreach my $input ($tform->find_by_tag_name('input'))
{
$form{$input->attr('name')} = $input->attr('value');
}
}
}
# Fill in username/password
$form{'username'} = $sid;
$form{'password'} = $pass;
print "Sending authentication request...\n";
$action = "$gateway/logon";
$response = $ua->post($action, \%form); # should redirect to Google
$response->is_success or die "Couldn't authenticate: " .$response->status_line;
if ($uri eq $response->request->uri())
{
print "Authentication complete!\n";
exit 0;
} elsif ($response->content =~ /Bad username/) {
print "Bad username or password.\n";
exit 1;
} else {
print "Something odd happened, dumping content:\n";
print $response->content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment