Trend Micro 2015 Write Up CTF: Programming 200

The article will discuss the Trend Micro 2015 Write Up CTF: Programming 200′.

Problem

There is a service that provides a wide range of basic arithmetic problems. But the numbers that appear not only integer only, but the roman numeral numbers and mention the word also appears.

Goal: Solve all tasks

nc ctfquest.trendmicro.co.jp 51740

Discussion

In my solution using a programming language Pyhon. For the first phase, the basic arithmetic of numbers could be solved with the following functions:

def common(strings):
	x = 'p = ' + strings[:-2]
	print x
	x = x.replace(',','')
	exec(x)
	return p

In the second phase, the Roman numeral can be solved with a library roman.pyand mention of numbers with words could be solved with functions within word2num.py. In addition, there is also a matter of calculation is sent as a list (), then I use def listed ().

Then we just combine all of these functions, and be solver.py

import socket
import word2num
from roman import *

s = socket.socket()
host = "ctfquest.trendmicro.co.jp"
port = 51740
s.connect((host, port))

def saynum(strings):
	strings = strings[:-3]
	strings = strings.replace(',','').replace('(','  (  ').replace(')','  )  ').replace('*','  *  ').replace('-','  -  ').replace('+','  +  ').replace('/','  /  ')
	strings = strings.split('   ')
	b = []
	for st in strings:
		st = st.strip('  ')
		try:
			if 'I' in st or 'V' in st or 'X' in st or 'M' in st or 'C' in st or 'L' in st:
				x = fromRoman(st)
			else:
				x = word2num.text2int(st)
		except:
			x = st
		b.append(str(x))
	b = ''.join(b)
	print b
	x = 'p = ' + b
	exec(x)
	return p

def listed(strings):
	strings = strings.replace('[','').replace(']','').replace("'", '').replace(',','')
	strings = strings[:-2]
	strings = strings.split(' ')

	b = []
	for st in strings:
		if st == 'zero':
			x = 0
		try:
			x = fromRoman(st)
		except:
			x = st
		b.append(str(x))

	b = ''.join(b)
	print b
	x = 'p = ' + b
	exec(x)
	return p

def common(strings):
	x = 'p = ' + strings[:-2]
	print x
	x = x.replace(',','')
	exec(x)
	return p

for i in range(100):
	strings = s.recv(8192)
	print '\nData ke-', i
	print 'Isi data', strings
	print 'Panjang data', len(strings)
	
	if 'I' in strings or 'V' in strings or 'X' in strings or 'M' in strings or 'C' in strings or 'L' in strings:
		y = saynum(strings)
		s.send(str(y))
	elif '[' in strings:
		y = listed(strings)
		s.send(str(y))
	elif 'a' in strings or 'i' in strings or 'u' in strings or 'e' in strings or 'o' in strings:
		y = saynum(strings)
		s.send(str(y))
	else:
		y = common(strings)
		s.send(str(y))

strings = s.recv(8192)
print strings
strings = s.recv(8192)
print strings
strings = s.recv(8192)
print strings

prog200

Flag: TMCTF{U DID 17!}

Advertisements

One thought on “Trend Micro 2015 Write Up CTF: Programming 200

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s