#!/usr/bin/env python3 from random import randint import sys import os
flag = str(os.getenv('FLAG')).encode()
defdie(*args): print_something(*args) quit()
defprint_something(*args): s = " ".join(map(str, args)) sys.stdout.write(s + "\n") sys.stdout.flush()
defscan(): return sys.stdin.buffer.readline()
defsubset_process(n, l, K, A): A, K = set(A), set(K) return [pow(_, 2, n) + randint(0, 1) for _ in A - K]
defmain(): border_plus = "[+]" border_sub = "[-]" print_something(border_plus, "You need to complete something before I can give you what you ultimately need! Are you ready? ") print_something(border_plus, "eg: 1,2,3,4") _flag = False n, l = 127, 20 N = set(list(range(n))) K = [randint(0, n-1) for _ inrange(l)] COUNT, STEP = 0, 2 * n // l - 1
whileTrue: ans = scan().decode().strip() try: _A = [int(_) for _ in ans.split(',')] iflen(_A) <= l andset(_A).issubset(N): someting = subset_process(n, l, K, _A) print_something(border_plus, f'someting = {someting}') ifset(_A) == set(K): _flag = True else: die(border_sub, 'Exception! Bye!!') except Exception: die(border_sub, 'Your input is not valid! Bye!!') if _flag: die(border_plus, f'Congrats! the flag: {flag}') if COUNT > STEP: die(border_sub, 'Too many tries, bye!') COUNT += 1
if __name__ == '__main__': main()
0x02 解题思路
题目主要通过K = [randint(0, n-1) for _ in range(l)]生成一个长度为l,元素范围0到(n-1)的set集合,该语句等效以下代码段:
1 2 3
K = [] for _ inrange(l): #循环l次 K.append(randint(0, n-1))
r = remote("IP_address", 114514) context.log_level = 'debug' n, l = 127, 20 current_K = set() fullset = set(range(1,128))
for _ inrange(11): my_set = set() no_set = set() for i in fullset: t1 = pow(i,2,n) t2 = pow(i,2,n) + 1 if t1 in no_set or t2 in no_set: continue no_set.add(t1) no_set.add(t2) my_set.add(i) iflen(my_set) == l: fullset = fullset - my_set break r.sendline(",".join(map(str, list(my_set)))) r.recvuntil(b"[+] someting = ") result_list = eval(r.recvline().decode().strip())
invalid = set() for i in my_set: ifpow(i,2,n) in result_list: invalid.add(i) ifpow(i,2,n) in [b-1for b in result_list]: invalid.add(i) for i in my_set - invalid: current_K.add(i) iflen(current_K) == 20: r.sendline(",".join(map(str, list(current_K)))) r.interactive()