Created
September 7, 2016 19:46
-
-
Save eenblam/8649e5ba74ae7d478a8a0f5bab8a40d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from pwn import * | |
from itertools import permutations | |
def check_palindrome(s): | |
length = len(s) | |
half = length / 2 | |
return s[:half] == s[-half:][::-1] | |
def get_palindrome(inp): | |
perms = permutations(inp) | |
perms = ((p, ''.join(p)) for p in perms) | |
for p in perms: | |
if check_palindrome(p[1]): | |
return ' '.join(p[0]) | |
return False | |
r = remote('ppc1.chal.ctf.westerns.tokyo', 31111) | |
r.recvuntil("Let's play!") | |
r.recvlines(2) | |
while True: | |
line = r.recvline() | |
print line | |
try: | |
left, right = line.split(':') | |
except ValueError: | |
# Case: Ben's an idiot | |
print "Bad line..." | |
continue | |
if left == 'Case': | |
continue | |
if left == 'Input': | |
right = right.strip().split() | |
# skip count | |
answer = get_palindrome(right[1:]) | |
if not answer: | |
print 'No palindrome found for:\t{}'.format(right[1:]) | |
break # Failed | |
r.sendlineafter('Answer: ', answer) | |
# else Judge (or Answer if bug) | |
r.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment