CTF easy_hash 题目解析:多项式与自定义哈希逆向
一、题目信息
- 题目名称:easy_hash
- 比赛名称:DASCTF NOV X 联合出题人 2022 年度积分榜争夺赛
- 题目描述:闲来无事自己写了个 hash,你能看懂嘛?
- 附件:
interesting.py
二、题目分析
拿到题目后,首先分析附件代码的逻辑。题目主要包含两个核心函数:myhash 和 encode。
1. 自定义哈希函数 myhash
这个函数是题目的核心难点之一,它的处理流程如下:
def myhash(x):
res = []
end = b""
bytescipher = long_to_bytes(x)
# 1. 处理前缀(无法被 8 整除的剩余部分)
a = bytescipher[:len(bytescipher) % 8]
res.append(a)
res.append(long_to_bytes(crc32(a)))
# 添加前缀的 CRC32 校验值
# 2. 处理 8 字节对齐的数据块
t = (len(bytescipher) // 8)
bytescipher = bytescipher[len(bytescipher) % 8:]
for i in range(t):
a = bytescipher[i*8:i*8+8]
res.append(a)
res.append(long_to_bytes(crc32(a)))
# 添加每个数据块的 CRC32 校验值
# 3. 拼接并转换
for i in res:
end += i
res = bytes_to_long(end)
# 4. 截断操作
res = (res + (res >> 500)) & (2**500 - 1)
return res
关键点分析:
- 输入数据被分割成若干块:一个长度为
len % 8的前缀,和若干个 8 字节的块。

