Visit Our UNIX and Linux User Community


Q:Perl Extracting & Printing Security Token


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Q:Perl Extracting & Printing Security Token
# 1  
Old 07-27-2011
[Perl] Extracting & Printing Security Token

I have a script which is supposed to log in to my vB account and print my security token, however doesn't seem to work globally. The logging in works perfectly just will not retrieve and print the security token for every forum I log in to. Code Below:

Code:
#!/usr/bin/perl
use LWP::UserAgent;

my $ua = LWP::UserAgent->new(agent => q{Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)});
$ua->cookie_jar({});

print("Specify site URL(e.g http://site.com/): ");
chomp(my $url = <>);
print("Username: ");
chomp(my $username = <>);
print("Password: ");
chomp(my $password = <>);

my $req = HTTP::Request->new(POST => $url . '/login.php?do=login');
$req->content_type('application/x-www-form-urlencoded');
$req->content("vb_login_username=$username&vb_login_password=$password&do=login&securitytoken=guest&vb_login_md5password=&vb_login_md5password_utf=&s=");
$ua->request($req);
my $content = $ua->get("$url/index.php")->content;
my ($securityToken) = $content =~ /value="\w{10}-\w{40}" /g;
print "Security token: $securityToken\n";

Just view your page source to find an example of the vB security token while logged in.

Last edited by AndrewTwain; 07-27-2011 at 05:31 AM.. Reason: full source
# 2  
Old 07-27-2011
Try something like this:
Code:
% cat script.pl
$_ = '<input type="hidden" value="1311696616-766a50d6240bd38183dd57b51a345bf916a634a9" name="securitytoken">';
my $securityToken = $1 if  /securitytoken/ && /value="(\w+-\w+)"/;
print "Security token: $securityToken\n";

% perl script.pl
Security token: 1311696616-766a50d6240bd38183dd57b51a345bf916a634a9

# 3  
Old 07-27-2011
Think you misunderstood me. Disregard that specific value it's a generic string. I need it to grab that generic string from the page.

First post updated so you can test on a few vB forums to see what I mean, works on some, won't work on others.

Last edited by AndrewTwain; 07-27-2011 at 02:38 AM..
# 4  
Old 07-28-2011
In $content you have all the page but we need only an input element with the attribute 'name="securitytoken"'. There are can be a few but they are the same (on unix.com at least). So it's enough to get just the first one:
Code:
my $input = $1 if $content =~ /(<input.*?securitytoken.*?>)/s;

(/s - just in case an input elements takes several lines.)
Then from this input we can get the value in the same vein:
Code:
my $securityToken = $1 if $input =~ /value="(\w{10}-\w{40})"/;

And it's necessary to check whether we really get the token - something like this:
Code:
if ($securityToken) { 
  print $securityToken;
} else {
  print "oops\n";
  exit -1;
}

Hope this helps. If not - write me to the private (so I can be sure to see your questions).

PS Sorry for my English.

---------- Post updated at 11:00 AM ---------- Previous update was at 10:50 AM ----------

I think this will be enough:
Code:
my $securityToken = $1 if $input =~ /value="(.*?)"/;

For example, on some forums where i'm a guest it gives me the value "guest".
This User Gave Thanks to yazu For This Post:
# 5  
Old 07-28-2011
Thank you for the response. Seems to work for a few more forums, however still not for every vB forum. I'll send you another example, read the comments in your banking.

---------- Post updated at 11:47 PM ---------- Previous update was at 11:10 PM ----------

After checking your post update the last example seems to have fixed things. But it seems I've ran in to another problem. I need the usage of gzip, deflate but with the script looking like it does below, it puts the security token extraction back where it was before.

Code:
#!/usr/bin/perl
use LWP::UserAgent;

my $ua = LWP::UserAgent->new(agent => q{Mozilla/5.0 (Windows; U;  Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET  CLR 3.5.30729)});
$ua->cookie_jar({});

print("Specify site URL(e.g http://site.com/): ");
chomp(my $url = <>);
print("Username: ");
chomp(my $username = <>);
print("Password: ");
chomp(my $password = <>);

$ua->default_header('Accept' =>  "text/xml,application/xml,applicati+on/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",  
    'Accept-Language' => "en-us,en;q=0.5",
    'Accept-Charset' => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
    'Accept-Encoding' => 'gzip,deflate',
    'Keep-Alive' => "300",
    'Connection' => "keep-alive", 
    'Referer' => "$url/index.php");

push @{ $ua->requests_redirectable }, 'POST';

my $req = HTTP::Request->new(POST => $url . '/login.php?do=login');
$req->content_type('application/x-www-form-urlencoded');
$req->content("vb_login_username=$username&vb_login_password=$password&do=login&securitytoken=guest&vb_login_md5password=&vb_login_md5password_utf=&s=");
$ua->request($req);
my $content = $ua->get("$url/index.php")->content;
my $input = $1 if $content =~ /(<input.*?securitytoken.*?>)/m;
my $securityToken = $1 if $input =~ /value="(.*?)"/;
print "Security token: $securityToken\n";

# 6  
Old 07-28-2011
Don't quite understand. What is the content of the var "$content"? Is it gzipped?
# 7  
Old 07-28-2011
No it's for other purposes such as extraction of posts/titles. But I don't see why it stops the extraction of the security token.

Previous Thread | Next Thread
Test Your Knowledge in Computers #562
Difficulty: Easy
Normally, the control flow of a program is sequential: each statement is executed right-to-left the next statement before the current statement.
True or False?

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Forum Support Area for Unregistered Users & Account Problems

Security Token

I got this message while submitting a reply to a thread. I could submitt the replies till yesterday but today it shows me the following message "Your submission could not be processed because a security token was missing." What should I do to resolve this? (1 Reply)
Discussion started by: Akang
1 Replies

3. Shell Programming and Scripting

Extracting character between 2 token - using only first match

Hello. ps -ae return I would like that the following command return 3214 echo " 3214 ? 00:00:01 acroread" | grep -o "]]]*" (5 Replies)
Discussion started by: jcdole
5 Replies

4. Emergency UNIX and Linux Support

Perl - Retrieving and Printing Security Token

My script below is supposed to log in to my vB account on any vB forum I'm registered on and retrieve + print my security token. However it seems to be hit and miss. The logging in works perfectly just will not retrieve and print the security token for every forum I log in to. Code Below: ... (3 Replies)
Discussion started by: AndrewTwain
3 Replies

5. Shell Programming and Scripting

Extracting and printing data

Hi I have the following data : <Cell id="34A" ref="ds:/BTS:34/Cells/Cell:34A"/> <Cell id="34B" ref="ds:/BTS:34/Cells/Cell:34B"/> <Cell id="34C" ref="ds:/BTS:34/Cells/Cell:34C"/> I would like to print this data in the following format : BTS:34 Cell:34A.I'm... (9 Replies)
Discussion started by: Prega
9 Replies

6. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

7. UNIX for Advanced & Expert Users

Network Shell Script & Blade Logic & Network Security

I am going to take up a position in Data & Network Security. I would need to write network shell scripts doing the following task: Going to around 2000 servers and findout which groups has access to each servers and which ids are there in each group that has access. I need to implement... (1 Reply)
Discussion started by: pinnacle
1 Replies

Featured Tech Videos