CSAW'18 CTF Qualification - Algebra (100 pts).

CSAW’18 CTF Qualification: Algebra

Event Challenge Category Points Solves
CSAW’18 CTF Qualification Algebra Misc 100 410


Are you a real math wiz?

nc 9002


This challenge consists in equations being thrown at us that we have to solve and send the result in order to get the flag. Python and sage, guide us please !


A friend I was talking with while solving this chall told me :

Well, the equations are simple, let’s write a quick script to solve it !

Hearing that, I went like :

Hum… Hum no. That’s not how it works, they wouldn’t give 100 points for that…

In fact, during CSAW 2017 quals, there was an other Misc I worked on, and the questions (bank card number to compute) were getting harder and harder, so I knew we had to find a better way to solve it automatically. So I took my worst coding skills, and used popen and eval like I it was my last day on earth. Here’s the result, 10mn coding ! :)

#!/usr/bin/env python2

from pwn import *
import os

r = remote("", 9002)

while True:
        data = r.recvuntil("equal?: ")
        line = [line for line in data.splitlines() if "=" in line][0]
        print "\n" + line
        out = os.popen("sage -c \"var('X'); print solve([" + line.replace("=", "==") + "], X)\"").read()
        answer = out.split("== ")[1].split("\n")[0]
        print answer
        if "/" in answer: # Sage does not simplify, so python will do it ! :)
            answer = eval(answer.replace("/", "/float(") + ")")
            print answer
        print "out", out # In case of failure, please tell us why...

Just so you can laugh a bit, here are the first and last equations…

If I had to code something that can handle that… I would… I wouldn’t.

simple equation hard equation

The flag is : flag{y0u_s0_60od_aT_tH3_qU1cK_M4tH5}