Skip to content

Instantly share code, notes, and snippets.

@xinan
Last active October 13, 2016 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xinan/dda0402e75885b8cab84e9b1b70db001 to your computer and use it in GitHub Desktop.
Save xinan/dda0402e75885b8cab84e9b1b70db001 to your computer and use it in GitHub Desktop.
Solution to HITCON CTF 16 -- [PPC] Beelzemon
#!/usr/bin/python
'''
xinan@nusgreyhats.org
'''
from copy import deepcopy
from pwn import *
a1 = [0, 3]
b1 = [1, 2]
base = {}
# Pre-compute solution for all the n=k cases. Takes about 4 seconds.
for i in range(1, 21):
A1 = map(lambda x: x - 2**i, a1)
B1 = map(lambda x: x - 2**i, b1)
base[i] = (A1, B1)
a1_temp = deepcopy(a1)
for b in b1:
a1.append(b + 2**(i + 1))
for a in a1_temp:
b1.append(a + 2**(i + 1))
r = remote('52.198.217.117', 6666)
while True:
try:
line = r.readline_regex('\d+ \d+').split(' ')
except:
print r.recvall()
break
x = int(line[0])
y = int(line[1])
n = max(x, y)
k = min(x, y)
print 'n: ' + str(n) + ' k: ' + str(k)
# Get the solution for n = k, and generate the solution for n > k.
current_base = base[k]
a1 = deepcopy(current_base[0])
b1 = deepcopy(current_base[1])
while k < n:
k += 1
a1_temp = deepcopy(a1)
for b in b1:
next = b + 2**k if b < 0 else b - 2**k
a1.append(next)
for a in a1_temp:
next = a + 2**k if a < 0 else a - 2**k
b1.append(next)
answer = ' '.join([str(i) for i in a1])
r.sendline(answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment