日常鼓励自己:成功是陡峭的阶梯,两手插在裤袋里是爬不上去的。
Vote for Pedro(150 pt)
感谢队内师傅们的提示
题目描述
首先查看给出的key文件
发现这里e很小为3
题目源代码
#!/usr/bin/env python3
from Crypto.Util.number import bytes_to_long, long_to_bytes
from utils import listener
FLAG = "crypto{????????????????????}"
class Challenge():
def __init__(self):
self.before_input = "Place your vote. Pedro offers a reward to anyone who votes for him!\n"
def challenge(self, your_input):
if 'option' not in your_input:
return {"error": "You must send an option to this server"}
elif your_input['option'] == 'vote':
vote = int(your_input['vote'], 16)
verified_vote = long_to_bytes(pow(vote, ALICE_E, ALICE_N))
# remove padding
vote = verified_vote.split(b'\00')[-1]
if vote == b'VOTE FOR PEDRO':
return {"flag": FLAG}
else:
return {"error": "You should have voted for Pedro"}
else:
return {"error": "Invalid option"}
listener.start_server(port=13375)
题目分析
根据代码分析,要想拿到flag需要我们构造vote,使得这个vote在经过Alice加密之后得到的密文"\00"字节之后的一段内容为“VOTE FOR PEDR0”.
这个题可以操作的地方就是这段不严格的截断。
因为本题限制所有的“\00”之前的内容都会被自动截断丢弃,我们将需要构造的内容设定为
x
x
x,此时我们可以将
x
x
x表示为
x
=
a
∗
2
120
+
k
x=a*2^{120}+k
x=a∗2120+k
此处,a可以为任意数字,k的大小为bytes_to_long(b"VOTE FOR PEDRO")
然后借助sage求解
x
3
=
k
m
o
d
(
2
120
)
x^3=k\ mod(2^{120})
x3=k mod(2120),将构造的x作为输入就能得到flag
具体操作
k=1750572331061789800727934052618831 #bytes_to_long(b"VOTE FOR PEDRO")
x = mod(k, 2^120).nth_root(3)
print(x)
print(hex(x))
#855520592299350692515886317752220783
#0xa4c46bfb65e7eccc4e76a1ce2afc6f