[LitCTF 2023]babyLCG
题目分数:463
题目评分:
1.5
题目标签: RSALCG
题目描述:
- 题目描述:主打一个baby难度的LCG
- 出题人:3tefanie丶zhouflag以NSSCTF{}形式提交
题目附件:
附件信息
from Crypto.Util.number import *
from secret import flag
m = bytes_to_long(flag)
bit_len = m.bit_length()
a = getPrime(bit_len)
b = getPrime(bit_len)
p = getPrime(bit_len+1)
seed = m
result = []
for i in range(10):
seed = (a*seed+b)%p
result.append(seed)
print(result)
"""
result = [699175025435513913222265085178805479192132631113784770123757454808149151697608216361550466652878, 193316257467202036043918706856603526262215679149886976392930192639917920593706895122296071643390, 1624937780477561769577140419364339298985292198464188802403816662221142156714021229977403603922943, 659236391930254891621938248429619132720452597526316230221895367798170380093631947248925278766506, 111407194162820942281872438978366964960570302720229611594374532025973998885554449685055172110829, 1415787594624585063605356859393351333923892058922987749824214311091742328340293435914830175796909, 655057648553921580727111809001898496375489870757705297406250204329094679858718932270475755075698, 1683427135823894785654993254138434580152093609545092045940376086714124324274044014654085676620851, 492953986125248558013838257810313149490245209968714980288031443714890115686764222999717055064509, 70048773361068060773257074705619791938224397526269544533030294499007242937089146507674570192265]
"""
解题:
本人在Visual Studio Code里面跑的
建一个文件LitCTF-LCG,里面两个py,如下:
enc.py:
import uuid
from Crypto.Util.number import *
flag = f"MUMU11{{{uuid.uuid4()}}}"
seed = bytes_to_long(flag.encode())
length = seed.bit_length()
print(length)
a = getPrime(length)
b = getPrime(length)
m = getPrime(length)
results = []
for _ in range(10):
seed = (a * seed + b) % m
results.append(seed)
print(f"results = {results}")
main.py:
import sympy
import libnum
import contextlib
def lcg_get_m(output):
t = [output[i] - output[i - 1] for i in range(1, len(output))]
return [int(sympy.gcd(t[i + 3] * t[i + 1] - t[i + 2] * t[i + 2], t[i + 2] * t[i] - t[i + 1] * t[i + 1])) for i in range(len(t) - 3)]
def lcg_get_a(output, m):
lis = []
with contextlib.suppress(Exception):
lis.extend((output[i + 2] - output[1]) * int(sympy.mod_inverse((output[i + 1] - output[i]), m)) % m for i in range(len(output) - 2))
return lis
def lcg_get_b(output, a, m):
return [(output[i] - a * output[i-1]) % m for i in range(1, len(output))]
def lcg_get_seed(output, a, b, m):
with contextlib.suppress(Exception):
a_inv = int(sympy.mod_inverse(a, m))
return a_inv * (output[0] - b) % m
def lcg_solver(output, isPrint=True, isSet=False):
lis = []
for m in lcg_get_m(output):
for a in lcg_get_a(output, m):
for b in lcg_get_b(output, a, m):
if (seed := lcg_get_seed(output, a, b, m)):
lis.append(seed)
if isSet:
lis = list(set(lis))
if isPrint:
for seed in lis:
print(libnum.n2s(seed))
return lis
if __name__ == '__main__':
#这里是题目给出的输出列表
output = [699175025435513913222265085178805479192132631113784770123757454808149151697608216361550466652878, 193316257467202036043918706856603526262215679149886976392930192639917920593706895122296071643390, 1624937780477561769577140419364339298985292198464188802403816662221142156714021229977403603922943, 659236391930254891621938248429619132720452597526316230221895367798170380093631947248925278766506, 111407194162820942281872438978366964960570302720229611594374532025973998885554449685055172110829, 1415787594624585063605356859393351333923892058922987749824214311091742328340293435914830175796909, 655057648553921580727111809001898496375489870757705297406250204329094679858718932270475755075698, 1683427135823894785654993254138434580152093609545092045940376086714124324274044014654085676620851, 492953986125248558013838257810313149490245209968714980288031443714890115686764222999717055064509, 70048773361068060773257074705619791938224397526269544533030294499007242937089146507674570192265]
lcg_solver(output, isPrint=True, isSet=False)
跑主函数得到LitCTF{31fcd7832029a87f6c9f760fcf297b2f}