Created
May 11, 2015 14:21
-
-
Save chitoge/ea3f8c2a8891e59f510a to your computer and use it in GitHub Desktop.
ASIS CTF 2015 Quals - grids
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
from scipy.spatial import ConvexHull | |
import numpy as np | |
import ast | |
import telnetlib | |
import re | |
class Telnet(telnetlib.Telnet): | |
# inherit from Telnetlib and add new method | |
def __init__(self,host,port): | |
telnetlib.Telnet.__init__(self,host,port) | |
# make easier when you want to send raw data to server | |
def writeRawData(self,data): | |
return self.get_socket().send(data) | |
def recvRawData(self,size): | |
return self.get_socket().recv(size) | |
# Calculate algebraic area of a polygon using shoelace formula | |
def area(vertices): | |
n = len(vertices) | |
area = 0.0 | |
for i in range(n): | |
j = (i + 1) % n | |
area += vertices[i][0] * vertices[j][1] | |
area -= vertices[j][0] * vertices[i][1] | |
area /= 2.0 | |
return area | |
host = '217.218.48.84' | |
port = 12432 | |
# connect | |
tel = Telnet(host, port) | |
s = tel.read_until('Are you ready for this challenge?\n') | |
print s, | |
# reply | |
print 'yes' | |
tel.writeRawData('yes\n') | |
s = tel.read_until('OK, OK, lets start\n') | |
print s, | |
# solve | |
for i in range(99): | |
print '[*] %d-th round' % i | |
s = tel.read_until("What's the area?") | |
print s, | |
l = np.array(ast.literal_eval(re.findall(r'\n(\[.*\])\n', s)[0])) | |
hull = ConvexHull(l) | |
vertices = l[hull.vertices] | |
res = abs(area(vertices)) | |
print res | |
tel.writeRawData(str(res)+'\n') | |
# get flag | |
tel.interact() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment