Posted on by and filed under MMA CTF 2016.

The challenge reads as follows:

Your task is to make a palindrome string by rearranging and concatenating given words.

Input Format: N ...

Answer Format: Rearranged words separated by space.

Each words contain only lower case alphabet characters.

Example Input: 3 ab cba c

Example Answer: ab c cba

You have to connect to ppc1.chal.ctf.westerns.tokyo:31111(TCP) to answer the problem.

$ nc ppc1.chal.ctf.westerns.tokyo 31111

Time limit is 3 minutes.

The maximum number of words is 10.

There are 30 cases. You can get flag 1 on case 1. You can get flag 2 on case 30.

Here is my solution:

    
from itertools import permutations
from pwn import *
import time

def ispalindrome(word):
    return word == word[::-1]


con = remote('ppc1.chal.ctf.westerns.tokyo',31111)

for i in range(1,31):
    con.recvuntil("Case: #%d\nInput: " % i)
    strings =  con.recvline().strip('\n')
    inputList= strings.split(' ')
    con.recvuntil('Answer: ')

    for p in permutations(inputList[1:]):
        concat = ""
        for string in p:
            concat += string
        if  ispalindrome(concat) == True:
            final = ""
            for w in p:
                final += w + " "
            final += '\n'
            print "[+] sending " + final
            con.send(final)
            print con.recvline()
            break