03 Feb 2017

AlexCTF Write-up

On Friday, I took part in AlexCTF. It was pretty good fun since nothing in it was related to web app security and more focused around reverse engineering and cryptography.

Warning: this write-up contains CTF quality code.

Cryptography 1: Ultracoded

We get given a text file with the words ZERO and ONE down and the message:

Fady didn’t understand well the difference between encryption and encoding, so instead of encrypting some secret message to pass to his friend, he encoded it! Hint: Fady’s encoding doesn’t handle any special character

I figured it would be worth converting the zeros and ones into actual binary so I wrote a quick script

f = open('zero_one', 'r')

data = ""
for line in f:
  for word in line.split():
    if word == "ZERO":
    elif word == "ONE":
print data

# from http://stackoverflow.com/a/11599702
x = ''.join(chr(int(data[i:i+8], 2)) for i in xrange(0, len(data), 8))

print x

which gives us:


which looks like a base64 string due to the == at the end. I then converted that string to ASCII but it didn’t seem to return anything good. base64 decoding that string returns only special characters and spaces so I was confused. Here’s whats returned:

.- .-.. . -..- -.-. - ..-. - .... .---- ..... --- .---- ...
--- ..... ..- .--. ...-- .-. --- ..... . -.-. .-. ...-- - 
--- - -..- -  

I have to admit, it too me a while to figure out that it’s Moores code. I ran it through an online converter and got the following flag:


because we don’t have any special characters we can substitute the 0’s for underscores and finally get our flag:


for 50 points.

Cryptography 2: Many time secrets

This challenge had a file with a few lines of hex and the following description:

This time Fady learned from his old mistake and decided to use onetime pad as his encryption technique, but he never knew why people call it one time pad!

The problem

“but he new know why people call it one time pad” gave a pretty good hint to the problem, implementing the solution was slightly tougher.

In general msg xor key is good as long as it’s just used once. In this problem the key had been used multiple times which caused the issue.

def xors(a, b):
    return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(a, b))

data = [

for line in data:
    print xors(line.decode('hex'), 'ALEXCTF{HERE_GOES_THE_KEY}')  

I for the flag ALEXCTF{HERE_GOES_THE_KEY} by manually “crib dragging” until the plain text made sense.

Plain text:

Dear Friend, This time I u
nderstood my mistake and u
sed One time pad encryptio
n scheme, I heard that it
is the only encryption met
hod that is mathematically
 proven to be not cracked
ever if the key is kept se
cure, Let Me know if you a
gree with me to use this e
ncryption scheme always.  

Scripting 1: Math bot

The instructions for this challenge were straight forward:

It is well known that computers can do tedious math faster than human.

When we use netcat to connect we get the following screen:

root@726a61e75cb8:/ctf# nc 1337
         ______/ ________ \______
       _/      ____________      \_
     _/____________    ____________\_
    /  ___________ \  / ___________  \
  /  /############/    \############\  \
__|\_____   ___   //  \\   ___   _____/|__
[_       \     \  X    X  /     /       _]
__|     \ \                    / /     |__
[____  \ \ \   ____________   / / /  ____]
     \  \ \ \/||.||.||.||.||\/ / /  /
      \_ \ \  ||.||.||.||.||  / / _/
        \ \   ||.||.||.||.||   / /
         \_   ||_||_||_||_||   _/
           \     ........     /

Our system system has detected human traffic from your IP!
Please prove you are a bot
Question  1 :
1992697762612302550072538 + 2115286419135408893338581 =  

It looks like we have to script a bot to solve maths problems. I initially considered using the pwntools library for python but ended up going with plain sockets because it was easier.

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 1337))

def count_lines(data):
	lines = 0
	for char in data:
		if char == "\n":
	return lines

def get_final_line(data):
	counter = 0
	final_line = ""

	for char in data:
		if char == "\n":
			counter = counter+1

		if counter==(count_lines(data)-1):

	return final_line

def send_answer(answer):
	s.send(str(answer) + "\n")

def format_question(final_line):
	return final_line.replace("=", "")

def calculate_answer(question):
	# dangerous but running
	# inside docker so
	# its okay
	return eval(question)

while True:
	data = s.recv(4000)
	question = format_question(get_final_line(data))
	print question
	answer = calculate_answer(question)
	print answer

The script runs for a few seconds to solve 250 maths problems and finally we get the following message:

Tell your human operator flag is: ALEXCTF{1_4M_l33t_b0t}  

and we get the flag for a cool 100 points.

Reverse Engineering 1: Gifted

I got a binary, since the challenge was only 50 points I went ahead and tried:

strings gifted | grep AlexCTF  

which returned (along with other strings)


for easy 50 points.

I'm on twitter @_amanvir