Created
April 26, 2017 21:55
-
-
Save juaxix/8d70373d00856e9155e1e237f0eb2176 to your computer and use it in GitHub Desktop.
Hacker rank - infinite links - Capture the Flag contest - April 2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(0); | |
function download($Url) | |
{ | |
if (!function_exists('curl_init')){ | |
die('Sorry cURL is not installed!'); | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $Url); | |
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
return $output; | |
} | |
function goInside($page,&$urls,&$secrets) | |
{ | |
if(in_array($page,$urls)) { | |
return; | |
} | |
array_push($urls,$page); | |
echo 'Going into '.$page.'<br>'; | |
$html = download("https://cdn.hackerrank.com/hackerrank/static/contests/capture-the-flag/infinite/".$page); | |
$a = array(); | |
if(preg_match("/<font color=blue><b>(.*)<\/font>/", $html, $a)) | |
{ | |
$n = count($a); | |
if($n>0) | |
{ | |
for($i=1;$i<$n;$i++){ | |
$a[$i] = str_replace("Secret Phrase: ","",$a[$i]); | |
array_push($secrets,$a[$i]); | |
echo "Found secret: ".$a[$i].'<br>'; | |
} | |
} | |
} | |
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; | |
if(preg_match_all("/$regexp/siU", $html, $matches, PREG_SET_ORDER)) { | |
foreach($matches as $match) { | |
goInside($match[2],$urls,$secrets); | |
// $match[3] = link text | |
} | |
} | |
} | |
$urls = array(); | |
$secrets = array(); | |
goInside('qds.html',$urls,$secrets); | |
sort($secrets); | |
echo '<h1>Results:</h2><hr>'; | |
foreach($secrets as $secret){ | |
echo $secret.'<br>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment