→ PlaidCTF 2012 - Game [100]

| No TrackBacks
The game is very simple. 2 long strings are shown, you have to select which one is bigger. Short analysis of the strings didn't show any anomalities, so we just decided to collect all the string and remember their positions against each other. So we write an automated player. There are only 500 strings. A naive sorting approach will give us 500*499 comparisons, so we just left it as is for a couple of hours. You can definitely afford it during the 48hrs competition.
Source code of the player:
use Socket;
use Data::Dumper;

socket IO,PF_INET,SOCK_STREAM,getprotobyname('tcp');
connect IO,sockaddr_in(6969,inet_aton "23.22.16.34") or die "can't connect\n";
print "Connected\n";
my $r; # Read buffer
my @rate = @{do 'rate.pl'}; # DB

my $got = 0;

while($got!=75){
	$c = sysread IO,$r,1024;
	($got) = $r=~/gotten (\d+) /s;
	my ($c1,$c2) = $r =~/(\w{34}).*(\w{34})/s; # Strings to compare
	$c = sysread IO,$r,1024 if $r!~/Which/s;
	my $c1r = in($c1,@rate); # Absolute ranking of c1
	my $c2r = in($c2,@rate); # Absolute ranking of c2
	my $an;
	if ($c1r==-1 || $c2r==-1) { 
		$an =1;
	}else{
    		$an = $c1r>$c2r?1:2;
	}
	syswrite IO,"$an\r\n";
	$c = sysread IO,$r,1024;
	if($r =~/Wrong/s){
		# C2 is known, C1 is unknown
		splice @rate, $c2r, 0, $c1 if $c1r==-1 && $c2r!=-1; 
		# C1 is known, C2 is unknown
		splice @rate, $c1r+1, 0, $c2 if $c2r==-1 && $c1r!=-1;
		# Both are unknown, just put it on the top
		splice @rate,$#rate+1,0,($c1,$c2) if $c1r==-1 && $c2r==-1; 
		# Both are known, sort
		if($c1r!=-1 && $c2r!=-1) { 
			if($an==1){
				# moving c1 to the left of c2 
				splice @rate,$c2r,0,$rate[$c1r];
				splice @rate,$c1r+1,1;
			}
			if($an==2){
				# moving c2 to the left of c1 
				splice @rate,$c1r,0,$rate[$c2r];
				splice @rate,$c2r+1,1;
			}
		}
	}
	if($r=~/Correct/s){
		print "$r\n" if $got==75;
		
		#C2 is known, C1 is unknown
		splice @rate, $c2r+1, 0, $c1 if $c1r==-1 && $c2r!=-1;
		# C1 is known, C2 is unknown
		splice @rate, $c1r, 0, $c2 if $c2r==-1 && $c1r!=-1;
		# Both are unknown
		splice @rate,$#rate+1,0,($c2,$c1) if $c1r==-1 && $c2r==-1;
	}

	# Backing up
	open F,'> rate.pl';
	print F Dumper(\@rate);
	close F;
}

sysread IO,$r,1024;
print $r;

# Returns -1 if an element is not in the array, return position otherwise
sub in($@) {
	for(my$i=1;$i<scalar @_;$i++){
		return $i if $_[0] eq $_[$i];
	}
	-1;
}
If you apply some smart sorting algo with some self-sorting heap, you might get much faster results. But in CTF it is all about answers :) After you win, you get the key "d03snt_3v3ry0n3_md5!!!".

No TrackBacks

TrackBack URL: http://smokedchicken.org/m/mt-tb.cgi/76

About this Entry

This page contains a single entry by 盗賊 published on May 2, 2012 9:45 AM.

CODEGATE 2012 Prob #1 was the previous entry in this blog.

PlaidCTF 2012 - Debit or Credit [200] is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.