ECTF 2016: Cryptofun (Crypto, 150 pts)

Description:
Try out the e3™ beta storage service today!
cryptofun.py
Hosted at: 35.161.87.33:9999

Flag:
ECTF{ihopeyouarehavingfunlikeiam}

In the welcome of the server, you will notice that to save space they compress the data you send it:

welcome = """  
Welcome to e3 storage interface. This is a beta version so bare with us :)  
Storage costs are as cheap as Rs.1 per byte ;).  
Note:  
* The bytes used are calculated after zlib compression.
* The storage used by the metadata is also charged
"""

Since our data is appended to the flag and then sent to the server, the closer our data is to the flag, the lower the cost will be since the compression will make the data smaller. Thus, I wrote a script that compares the cost of all printable characters:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
s.connect((HOST, PORT))  
s.recv(1024)  
s.recv(1024)  
s.sendall("1\n")  
s.recv(1024)  
s.sendall(secret + "\x20")  
cost = int(s.recv(1024).split(":")[-1])  
print cost

low = 0x20  
for i in range(0x20, 128):  
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.recv(1024)
    s.recv(1024)
    s.sendall("1\n")
    s.recv(1024)
    s.sendall(secret + chr(i))
    cost_new = int(s.recv(1024).split(":")[-1])

    if cost_new <= cost:
        costs.append([cost_new, chr(i)])
        cost = cost_new

    print i
print costs  

Whatever is the most logical option is in the costs list, I add that to the secret variable. By no surprise, secret starts off with ECTF{. After that, I just went through every character until I got secret to be secret = "ECTF{ihopeyouarehavingfunlikeiam}".