Automatic website login | script

Post date: Mar 5, 2011 7:58:05 AM

The following script allows an automatic login into any generic http web resource as if it were human. It is supposed for robotic maintenance of internet services such as authorized internet access, keeping alive various forum and discussion board access, systematic account check-in, web site serviceing and terms renewal.

The script runs on Unix/Linux and is capable of scheduled repitition. The later function is provided by cron automation. It is a bash environment program. It requires utilites: perl and curl. To setup the script you need basic understanding of HTML code - particulary HTML forms. You need to be able review web site code and HTTP headers. The later can be done -for example- by Firefox plugin "Live HTTP headers".

The code is provided as is. There is no obligation authors enter if you use the code.

login script (html processor)

#!/bin/bash

# Script for auto-login to forum

# reactivates webhosting account automatically

#

# Ideation by Matthew Beckler beck0778 @ umn.edu

# Created by Konstantyn Spasokukotskiy 01.2009

# spasokukotskiy.info

# /home/remote/autologin/weblogin

# file name: weblogin, executable

####################################################

# site settings ####################################

# normal page to be on

SITE=''http://your.site.com/work.php''

# login page

SITETWO="http://your.site.com/login.php"

# unique indication of the logged-in status

LOGINMESSAGE="Welcome,"

# the meassage spreading in the html code

LOGINLINES=2

# values - form settings ###########################

USERNAME=your_user_name_here

PASSWORD=''your_password_here_where_spaces_as_%20''

# additional fields to submit

# REMEMBERME=0

# SUBMIT=Log%20in

H1=''''

H2=guest

H3=login

# your hashcode here if password hashed by js

# to find out the js results investigate the HTTP header you send

H4=202cb962ac59175d964b97152f453270

H5=202cb962ac59175d964b97152f453270

# field names - form settings ######################

F_USERNAME=vb_login_username

F_PASSWORD=vb_login_password

# F_REMEMBERME=cookieuser

# F_SUBMIT=

FH_1=s

FH_2=securitytoken

FH_3=do

FH_4=vb_login_md5password

FH_5=vb_login_md5password_utf

# system settings ###################################

COOKIEJAR=''cookie''

AGENT="Mozilla/4.0%20(compatible;%20MSIE%205.01;%20Windows%20NT%205.0)"

#####################################################

rm $COOKIEJAR

curl -e $SITE -A $AGENT -L -c $COOKIEJAR -o site.html $SITE &> /dev/null

if [[ $? = 0 ]]; then

# optional ##########################################

# find the form lines and filter out hidden variable values to submit

H1=`./formfind.pl < site.html | grep "NAME=\\"s\\"" | tr -s ''\\"'' ''\\n'' | grep -v "Input" | grep -v "s" | grep -v "VALUE" | grep -v "(HIDDEN)"`

ENTRIES=`./formfind.pl < site.html | grep "NAME=\\"s\\"" -c`

for i in $(seq 2 $ENTRIES)

do

H1=$(echo $H1 | tr -s '' '' ''\\n'' | sed ''1d'')

done

H2=`./formfind.pl < site.html | grep "NAME=\\"securitytoken\\"" | tr -s ''\\"'' ''\\n'' | grep -v "Input" | grep -v "securitytoken" | grep -v "VALUE" | grep -v "(HIDDEN)"`

ENTRIES=`./formfind.pl < site.html | grep "NAME=\\"securitytoken\\"" -c`

for i in $(seq 2 $ENTRIES)

do

H2=$(echo $H2 | tr -s '' '' ''\\n'' | sed ''1d'')

done

# end optional ######################################

# web site available - time to login ################

# adjust the form fileds you need to pass to the remote server

curl -e $SITE -F $F_USERNAME=$USERNAME -F $F_PASSWORD=$PASSWORD -F $FH_1=$H1 -F $FH_2=$H2 -F $FH_3=$H3 -F $FH_4=$H4 -F $FH_5=$H5 -A $AGENT -b $COOKIEJAR -c $COOKIEJAR -L -o /dev/null $SITETWO &> /dev/null

#waiting 30-120 seconds - you might want to adjust this

sleep 30

#finalize the login

curl -e $SITETWO -A $AGENT -L -b $COOKIEJAR -c $COOKIEJAR -o site.html $SITE &> /dev/null

# log the results ###################################

DATE=`date`

ENTRIES=`grep -A $LOGINLINES "$USERNAME" site.html | grep -A $LOGINLINES "$LOGINMESSAGE"`

if [[ $ENTRIES != '''' ]]; then

`echo $DATE '' ##### SUCCESS! #####'' $USERNAME ''was logged as follows:'' >> logfile`

`echo $ENTRIES >> logfile`

exit 0

else

`echo $DATE '' ### FAILURE ### the loggin to'' $SITE ''as'' $USERNAME ''failed'' >> logfile`

exit 1

fi

else

`echo $DATE ''### FAILURE ### the site:'' $SITE ''is not accessible'' >> logfile`

exit 1

fi

###### END ###########################################

controller script

#!/bin/sh

# Konstantyn -> spasokukotskiy.info

# login into forum to keep web account alive

# belongs to ubuntu server. (Jan. 2009)

# file name: autologin-cron-script, executable

#/etc/cron/cron.weekly/autologin-cron-script

# cron weekly

########################################################

# change the folder /home/remote/autologin/

# to the actual folder, where you install the script

cd /home/remote/autologin/

/home/remote/autologin/weblogin

if [[ $? = 0 ]]; then

# adjust timout (sec) for the second chance login

sleep 96000

/home/remote/forumlogin/weblogin

exit 0

fi

exit 1

html form extracting script

#!/usr/bin/env perl

# $Id: formfind,v 1.6 2004/10/19 18:36:17 bagder Exp $

# formfind.pl#

# This script gets a HTML page on stdin and presents form information on

# stdout.

#

# Author: Daniel Stenberg <daniel@haxx.se>

# Version: 0.2 Nov 18, 2002

#

# HISTORY

# 0.1 - Nov 12 1998 - Created now!

# 0.2 - Nov 18 2002 - Enhanced. Removed URL support, use only stdin.

# place in hte same dir as weblogin script

# no adjustment needed

$in="";

if($ARGV[0] eq "-h") {

print "Usage: $0 < HTML\\n";

exit;

}

sub namevalue {

my ($tag)=@_;

my $name=$tag;

if($name =~ /name *=/i) {

if($name =~ /name *= *([^\\"\\'']([^ \\">]*))/i) {

$name = $1;

}

elsif($name =~ /name *= *(\\"|\\'')([^\\"\\'']*)(\\"|\\'')/i) {

$name=$2;

}

else {

# there is a tag but we didn''t find the contents

$name="[weird]";

}

}

else {

# no name given

$name="";

}

# get value tag

my $value= $tag;

if($value =~ /[^\\.a-zA-Z0-9]value *=/i) {

if($value =~ /[^\\.a-zA-Z0-9]value *= *([^\\"\\'']([^ \\">]*))/i) {

$value = $1;

}

elsif($value =~ /[^\\.a-zA-Z0-9]value *= *(\\"|\\'')([^\\"\\'']*)(\\"|\\'')/i) {

$value=$2;

}

else {

# there is a tag but we didn''t find the contents

$value="[weird]";

}

}

else {

$value="";

}

return ($name, $value);

}

while(<STDIN>) {

$line = $_;

push @indoc, $line;

$line=~ s/\\n//g;

$line=~ s/\\r//g;

$in=$in.$line;

}

while($in =~ /[^<]*(<[^>]+>)/g ) {

# we have a tag in $1

$tag = $1;

if($tag =~ /^<!--/) {

# this is a comment tag, ignore it

}

else {

if(!$form &&

($tag =~ /^< *form/i )) {

$method= $tag;

if($method =~ /method *=/i) {

$method=~ s/.*method *= *(\\"|)([^ \\">]*).*/$2/gi;

}

else {

$method="get"; # default method

}

$action= $tag;

$action=~ s/.*action *= *(\\''|\\"|)([^ \\"\\''>]*).*/$2/gi;

$method=uc($method);

$enctype=$tag;

if ($enctype =~ /enctype *=/) {

$enctype=~ s/.*enctype *= *(\\''|\\"|)([^ \\"\\''>]*).*/$2/gi;

if($enctype eq "multipart/form-data") {

$enctype="multipart form upload [use -F]"

}

$enctype = "\\n--- type: $enctype";

}

else {

$enctype="";

}

print "--- FORM report. Uses $method to URL \\"$action\\"$enctype\\n";

$form=1;

}

elsif($form &&

($tag =~ /< *\\/form/i )) {

print "--- end of FORM\\n";

$form=0;

if( 0 ) {

print "*** Fill in all or any of these: (default assigns may be shown)\\n";

for(@vars) {

$var = $_;

$def = $value{$var};

print "$var=$def\\n";

}

print "*** Pick one of these:\\n";

for(@alts) {

print "$_\\n";

}

}

undef @vars;

undef @alts;

}

elsif($form &&

($tag =~ /^< *(input|select)/i)) {

$mtag = $1;

($name, $value)=namevalue($tag);

if($mtag =~ /select/i) {

print "Select: NAME=\\"$name\\"\\n";

push @vars, "$name";

$select = 1;

}

else {

$type=$tag;

if($type =~ /type *=/i) {

$type =~ s/.*type *= *(\\''|\\"|)([^ \\"\\''>]*).*/$2/gi;

}

else {

$type="text"; # default type

}

$type=uc($type);

if(lc($type) eq "reset") {

# reset types are for UI only, ignore.

}

elsif($name eq "") {

# let''s read the value parameter

print "Button: \\"$value\\" ($type)\\n";

push @alts, "$value";

}

else {

print "Input: NAME=\\"$name\\"";

if($value ne "") {

print " VALUE=\\"$value\\"";

}

print " ($type)\\n";

push @vars, "$name";

# store default value:

$value{$name}=$value;

}

}

}

elsif($form &&

($tag =~ /^< *textarea/i)) {

my ($name, $value)=namevalue($tag);

print "Textarea: NAME=\\"$name\\"\\n";

}

elsif($select) {

if($tag =~ /^< *\\/ *select/i) {

print "[end of select]\\n";

$select = 0;

}

elsif($tag =~ /[^\\/] *option/i ) {

my ($name, $value)=namevalue($tag);

my $s;

if($tag =~ /selected/i) {

$s= " (SELECTED)";

}

print " Option VALUE=\\"$value\\"$s\\n";

}

}

}

}

script, Linux, automation, web , login, seite, bash, program, forum, automatic, password, user'