235 lines
4.3 KiB
Perl
Executable file
235 lines
4.3 KiB
Perl
Executable file
#!/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;
|
|
|
|
# variables
|
|
my $delta;
|
|
my $x1;
|
|
my $x2;
|
|
my $sol;
|
|
my $sol_trunc;
|
|
|
|
print("\n" . $equation . "\n");
|
|
|
|
# calcul delta
|
|
($delta, $a, $b) = delta($equation);
|
|
|
|
# if it's < 0, no solution
|
|
if ($delta < 0) {
|
|
print("\t There is no solution...\n");
|
|
exit;
|
|
}
|
|
|
|
# if delta == 0
|
|
if ($delta == 0) {
|
|
$sol = - $b / (2 * $a);
|
|
}
|
|
|
|
# else (delta > 0), 2 solutions
|
|
else {
|
|
# calcul x1 and x2
|
|
$x1 = - ($b - sqrt($delta)) / (2 * $a);
|
|
$x2 = - ($b + sqrt($delta)) / (2 * $a);
|
|
|
|
# determine the one who is valid for the answer of the challenge
|
|
if ($x1 > $x2) {
|
|
$sol = $x1;
|
|
}
|
|
|
|
else {
|
|
$sol = $x2;
|
|
}
|
|
}
|
|
|
|
# truncate (2 decimals)
|
|
$sol_trunc = (int ($sol * 100));
|
|
$sol_trunc /= 100;
|
|
|
|
# return the solution
|
|
return $sol_trunc;
|
|
|
|
}
|
|
|
|
# calcul delta
|
|
sub delta
|
|
{
|
|
# variables
|
|
my $delta;
|
|
my $a;
|
|
my $b;
|
|
my $c;
|
|
|
|
# shift the equation
|
|
my $equation = shift;
|
|
|
|
# funny time
|
|
print("\t Regex time boy !\n");
|
|
|
|
# regex
|
|
$equation =~ m/(\d*)x. (.) (\d*)x (.) (\d*)/;
|
|
|
|
print("\t Regex time is done ! Well done\n");
|
|
|
|
# reorder all the coefficients
|
|
$a = $1;
|
|
|
|
if ($a eq "") {
|
|
$a = 1;
|
|
}
|
|
|
|
$b = $3;
|
|
|
|
if ($3 eq "") {
|
|
$b = 1;
|
|
}
|
|
|
|
if ($2 eq "-") {
|
|
$b *= -1;
|
|
}
|
|
|
|
$c = $5;
|
|
|
|
if ($c eq "") {
|
|
$c = 0;
|
|
}
|
|
|
|
if ($4 eq "-") {
|
|
$c *= -1;
|
|
}
|
|
|
|
# calcul delta
|
|
$delta = $b * $b - 4 * $a * $c;
|
|
|
|
# return needed values
|
|
return $delta, $a, $b;
|
|
|
|
}
|
|
|
|
# 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 : ./2nd_degre.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");
|
|
|