题目来源PolarisCTF
题干:
解析题干:要求输入9组数据,使得H(A)=H(B)=H(C),(H(D)=)H(A+D)(=H(E))=H(E+B)=(H(F))=H(F+C),(H(G)=)H(G+D)=(H(I)=)H(I+E)=(H(J)=)H(J+F)
from hashlib import md5
from secret import flag
_H = lambda m: md5(m).digest()
S = set()
for _ in range(10):
A, B, C = bytes.fromhex(input('A > ')), bytes.fromhex(input('B > ')), bytes.fromhex(input('C > '))
ha, hb, hc = _H(A), _H(B), _H(C)
assert ha == hb == hc
D, E, F = bytes.fromhex(input('D > ')), bytes.fromhex(input('E > ')), bytes.fromhex(input('F > '))
hd, he, hf = _H(ha + D), _H(hb + E), _H(hc + F)
assert hd == he == hf
G, H, I = bytes.fromhex(input('G > ')), bytes.fromhex(input('H > ')), bytes.fromhex(input('I > '))
assert _H(hd + G) == _H(he + H) == _H(hf + I)
cur = (A, B, C, D, E, F, G, H, I)
assert len(set(cur)) == 9
assert not any(x in S for x in cur)
S.update(cur)
print(f'good: {flag}')
WP:
详细的工具下载在密码相关工具下载中
打开工具:
1.cmd或者powershell中打开wsl,直接输入wsl就行
2.定位下载地址:
cd /mnt/c/Users/Lenovo/Desktop
3.打开虚拟环境:
source .venv/bin/activate
4.运行下载地址同文件夹的脚本:
python slove.py
需要的工具:
fastcocl:可以根据前缀,得到相同哈希的信息输入的后半段信息
2条
MD5(PREFIX+S1)=MD5(PREFIX+S2)
MD5(PREFIX+S1)=MD5(PREFIX+S2)
Joux多路径原理
可以得到相同MD5的4条信息
先根据固定前缀P,先利用工具得到后缀x0,x1,因为哈希此时得到的输出就是内部状态,我们P+x0,P+x1的输入是相同的状态,输出也是相同的状态,设得到的后缀是y0,y1,
可以得到:
MD5(P+X0)=MD5(P+X1)
MD5(P+X0)=MD5(P+X1)
MD5(P+X0+Y0)=MD5(P+X0+Y1)
MD5(P+X0+Y0)=MD5(P+X0+Y1)
MD5(P+X1+Y0)=MD5(P+X1+Y1)
MD5(P+X1+Y0)=MD5(P+X1+Y1)
最后得到输入z00=x0+y0,
z01=x1+y0,
z10=x0+y1,
z11=x1+y1
这4条信息的哈希相同
示例脚本WP:
import os
import hashlib
import tempfile
import subprocess
from pwn import remote
HOST = "nc1.ctfplus.cn"
PORT = 14457
FASTCOLL = "/mnt/c/Users/Lenovo/fastcoll/fastcoll"
def H(x: bytes) -> bytes:
return hashlib.md5(x).digest()
def run_fastcoll(prefix: bytes):
with tempfile.TemporaryDirectory() as td:
pfile = os.path.join(td, "prefix.bin")
o1 = os.path.join(td, "m1.bin")
o2 = os.path.join(td, "m2.bin")
with open(pfile, "wb") as f:
f.write(prefix)
print(f"[*] fastcoll start, prefix_len = {len(prefix)}")
subprocess.check_call([FASTCOLL, "-p", pfile, "-o", o1, o2])
print(f"[*] fastcoll done, prefix_len = {len(prefix)}")
with open(o1, "rb") as f:
m1 = f.read()
with open(o2, "rb") as f:
m2 = f.read()
assert m1.startswith(prefix)
assert m2.startswith(prefix)
s1 = m1[len(prefix):]
s2 = m2[len(prefix):]
assert s1 != s2
assert H(prefix + s1) == H(prefix + s2)
return s1, s2
def make_4way_collision(prefix: bytes):
x0, x1 = run_fastcoll(prefix)
y0, y1 = run_fastcoll(prefix + x0)
z00 = x0 + y0
z01 = x0 + y1
z10 = x1 + y0
z11 = x1 + y1
h = H(prefix + z00)
assert H(prefix + z01) == h
assert H(prefix + z10) == h
assert H(prefix + z11) == h
assert len({z00, z01, z10, z11}) == 4
return [z00, z01, z10, z11]
def choose3(lst4):
a, b, c, _ = lst4
assert len({a, b, c}) == 3
return a, b, c
def send_hex(io, prompt: bytes, data: bytes):
io.recvuntil(prompt)
io.sendline(data.hex().encode())
def solve():
used = set()
io = remote(HOST, PORT)
for rnd in range(10):
print(f"[*] preparing round {rnd+1}")
while True:
print("[*] layer 1")
ABC4 = make_4way_collision(b"")
A, B, C = choose3(ABC4)
ha = H(A)
assert H(B) == ha and H(C) == ha
print("[*] layer 2")
DEF4 = make_4way_collision(ha)
D, E, F = choose3(DEF4)
hd = H(ha + D)
assert H(ha + E) == hd and H(ha + F) == hd
print("[*] layer 3")
GHI4 = make_4way_collision(hd)
G, HH, I = choose3(GHI4)
h3 = H(hd + G)
assert H(hd + HH) == h3 and H(hd + I) == h3
cur = [A, B, C, D, E, F, G, HH, I]
if len(set(cur)) != 9:
print("[!] duplicate inside current round, retry")
continue
if any(x in used for x in cur):
print("[!] repeated with previous rounds, retry")
continue
used.update(cur)
break
print(f"[+] round {rnd+1}/10 ready")
send_hex(io, b"A > ", A)
send_hex(io, b"B > ", B)
send_hex(io, b"C > ", C)
send_hex(io, b"D > ", D)
send_hex(io, b"E > ", E)
send_hex(io, b"F > ", F)
send_hex(io, b"G > ", G)
send_hex(io, b"H > ", HH)
send_hex(io, b"I > ", I)
print(io.recvall(timeout=5).decode(errors="ignore"))
if __name__ == "__main__":
solve()
注意几个def包装函数,make_4way_collision,choose3,这些可以直接在其他同类题调用