Title : [writeups] Internetwache CTF 2016 - It's Prime Time! #code60
Released : 2016-02-22 02:11:54 -0500
Viewed : 487

https://ctf.internetwache.org/tasks/code/60

Description: We all know that prime numbers are quite important in cryptography. Can you help me to find some?

Service: 188.166.133.53:11059

Sample input/output:

$ nc 188.166.133.53 11059
Hi, you know that prime numbers are important, don't you? Help me calculating the next prime!
Level 1.: Find the next prime number after 4:
5
Yay, that's right!
Level 2.: Find the next prime number after 3:

Solved using gmp_nextprime()

#!/usr/bin/php -q
<?php
	$host = "188.166.133.53";
	$port=11059;

	$fp = fsockopen($host, $port, $erno, $errstr, 30);
	if (!$fp) {
		echo $errstr." (".$errno.")\n";
	} else {
		while (!feof($fp)) {
			$line=fgets($fp,95);
			if(strpos($line,'after')){
				$e=explode(' ',$line);
				$f=array_reverse($e);
				$g=str_replace(':','',$f[0]);
				$h=getnextprime($g);
				fwrite($fp,$h);
			}
		}
	}

	function getnextprime($x){
		$prime = gmp_nextprime($x);
		return gmp_strval($prime);
	}

Flag: IW{Pr1m3s_4r3_!mp0rt4nt}