add solution for nbcontest, progra, n°3

This commit is contained in:
Wilfried OLLIVIER 2014-11-18 23:52:27 +01:00
parent 2be1683253
commit 6cd233f728
3 changed files with 150 additions and 0 deletions

View File

View File

@ -0,0 +1,150 @@
#!/usr/bin/env perl
# uses #########################################################################
use diagnostics;
use warnings;
use strict;
# Modules
# database
use DBI;
# http
use LWP::UserAgent;
use HTTP::Cookies;
# subs #########################################################################
# extract cookie from SQLite db
sub extract_cookie
{
# shift db_path
my $db_path = shift;
# variables usefull for database connection and handler
my $dbh;
my $sth;
my $rv;
my @row;
# string containing the query
my $query;
# connect to the db
$dbh = DBI->connect("dbi:SQLite:dbname=$db_path","","") or die $DBI::errstr;
# prepare the query
$query = "SELECT name, value FROM moz_cookies WHERE host LIKE\
'%newbiecontest.org%' AND name LIKE '%SMFCookie%'";
$sth = $dbh->prepare($query);
# execute
$rv = $sth->execute() or die $DBI::errstr;
# fetch the only row found
@row = $sth->fetchrow_array();
# close the database
$sth->finish();
$dbh->disconnect();
return $row[0], $row[1];
}
# Send http request
sub send_request
{
# shift args
my ($cookie_name, $cookie_val, $url) = @_;
# create variables
my $ua = LWP::UserAgent->new;
my $cookies = HTTP::Cookies->new();
# create the cookie
$cookies->set_cookie(0, $cookie_name, $cookie_val, '/', 'newbiecontest.org', 80, 0, 0,\
0, 0);
# add the cookie to the jar
$ua->cookie_jar($cookies);
# send request
my $response = $ua->get($url);
return $response->decoded_content;
}
# solve equation
sub solve_equation
{
# shift equation
my $equation = shift;
my $result;
# funny time
print("\t Regex time boy !\n");
print("\t\t " . $equation . "\n");
# regex
$equation =~ m/racine\((\d+)\)\*(\d+)²\+(\d+)/;
print("\t\t a = " . $1 . " b = " . $2 . " c = " . $3 . "\n");
print("\t Regex time is done ! Well done\n");
return int(sqrt($1) * ($2 * $2) + $3);
}
# send answer
sub send_answer
{
my ($cookie_name, $cookie_val, $url, $result) = @_;
# create variables
my $ua = LWP::UserAgent->new;
my $cookies = HTTP::Cookies->new();
# modify the url
$url = $url . "?solution=" . $result;
# create the cookie
$cookies->set_cookie(0, $cookie_name, $cookie_val, '/', 'newbiecontest.org', 80, 0, 0,\
0, 0);
# add the cookie to the jar
$ua->cookie_jar($cookies);
# send request
my $response = $ua->get($url);
print $response->decoded_content;
}
# main #########################################################################
# check argument number
if (@ARGV != 3) {
print("Get. The. Fuck. Out\n");
print("[i] Usage : ./equation.pl cookie.db url_in url_out\n");
exit;
}
my $cookie_name;
my $cookie_val;
my $equation;
my $result;
($cookie_name, $cookie_val) = extract_cookie($ARGV[0]);
$equation = send_request($cookie_name, $cookie_val, $ARGV[1]);
$result = solve_equation($equation);
send_answer($cookie_name, $cookie_val, $ARGV[2], $result);
print("Done.\n");