题目来源:2026主队面试 题干:
from Crypto.Cipher import ARC4
FLAG = b"..."
N = 256
MENU = """\
+---------------------------+
| [E] Encrypt text |
| [I] Show public info |
+---------------------------+
"""
def encrypt(nonce: bytes, text: bytes) -> bytes:
cipher = ARC4.new(nonce + FLAG)
return cipher.encrypt(text)
nonce_len = 3
print(MENU)
print("[*] RC4 FMS Attack demo oracle")
print("[*] secret_len =", len(FLAG))
print("[*] required_nonce_len =", nonce_len)
while True:
choice = input("[+] choice:\n>").strip().upper()
if choice == "E":
line = input("[+] nonce hex:\n>").strip()
try:
nonce = bytes.fromhex(line)
except Exception:
print("[!] invalid hex")
continue
if len(nonce) != nonce_len:
print(f"[!] nonce length must be exactly {nonce_len} bytes")
continue
text = bytes.fromhex(input("[+] text to encrypt(hex):\n>").strip())
c = encrypt(nonce, text)
print("[+] ciphertext =", c.hex())
elif choice == "I":
print("[*] secret_len =", len(FLAG))
print("[*] required_nonce_len =", nonce_len)
else:
print("[!] invalid choice")
WP: 题目背景:可以输入得到加密结果,可以控制IV的前24bit,后面是flag,组成IV 先回顾RC4的基本运转:
1.KSA(打乱S盒):先是2个部分:IV(KEY盒),S盒,S盒最开始是(0,1,2,…,255),对于i[0,255],计算每一个j=i+S[i]+K[i]mod length,交换S[i],S[j]位置
2.PGRA(伪随机):
初始化:初始化i=1,j=0;i=i+1mod256,j=(j+S[i])mod256;交换S[i],S[j];输出S[S[i]+S[j]]mod256,最后xor得到的输出
结合情景:可以控制nonce前3位,自己选择输入, 直接选择0,xor无效化,得到输出,接下来就是求flag,手段只有对nonce前3的控制
这里选择[3,255,X],X是[0,255]逐个选取的获得信息手段,
接下来讲解为什么是[3,255,X] 3:
从长远来看, 3是为了控制j的位置处于3,使得轮到flag的nonce时j的计算更明朗 ,现在取3,会有j=0+S[0]+K[0]=3,现在换位S[0],S[3],S[0]=2,S[3]=0,S[3]的位置得到控制,j也仍然在3 255:这里取255利用模把j仍然限定在3,j=1+S[1]+K[1]=3,现在交换S[1],S[3],S[1]=0,S[3]=1,这里运营的结果出来了,j仍然停留在3,S[3]的位置得到控制,flag初始字节调用时S盒变得可以预测 X:现在的j=X+5,目的在于获得更多样本 处理:
到flag时,j=X+6+flag[0],但是光运营2步,3步不可能忽视接下更多打乱的熵,要想解出只能靠一些统计规律等经验结论,RC4 FMT攻击有如下结论:
从PGRA的步骤可以得知, 第一个输出的是3位,也就是j=X+6+flag[0],但这有一个前提,就是后续的步骤没有打乱关键计算步骤PGRA的需要位置:0,1,3, 但这个概率很小,这里我们有统计学结论: 正确的候选会比错误的候选出现频率高5%左右 ,所以这里我们要X收集信息,统计每一次的结果,正确的是最多出现的,我们选择[3,255]是为了把出现的结果公式更加可视化, 但这个结果不一定能出,也不一定精确
脚本:
from pwn import *
import sys
HOST = ''
PORT =
NONCE_LEN = 3
SECRET_LEN = 32
SAMPLES = 256
context.log_level = 'error'
def solve():
try:
io = remote(HOST, PORT)
except:
print("[!] 无法连接服务器")
return
known_key = []
print("[*] 正在从头开始破解 RC4 FMS 攻击...")
print("-" * 60)
for byte_idx in range(SECRET_LEN):
votes = [0] * 256
A = NONCE_LEN + byte_idx
sys.stdout.write(f"[*] 破解 Byte {byte_idx:02d} (Key[{A}]): ")
sys.stdout.flush()
for v in range(SAMPLES):
nonce = [A, 255, v]
nonce_hex = "".join(f"{x:02x}" for x in nonce)
io.sendline(b"E")
io.sendline(nonce_hex.encode())
io.sendline(b"00")
try:
io.recvuntil(b"ciphertext = ")
z = int(io.recvline().strip(), 16)
except:
io.close()
io = remote(HOST, PORT)
continue
S = list(range(256))
j = 0
cur_key = nonce + known_key
for i in range(A):
j = (j + S[i] + cur_key[i]) % 256
S[i], S[j] = S[j], S[i]
predicted = (z - j - S[A]) % 256
votes[predicted] += 1
if v % 50 == 0: sys.stdout.write("."); sys.stdout.flush()
top_3 = sorted(range(256), key=lambda x: votes[x], reverse=True)[:3]
best_guess = top_3[0]
known_key.append(best_guess)
char = chr(best_guess) if 32 <= best_guess <= 126 else "?"
current_flag = "".join(chr(x) if 32 <= x <= 126 else "?" for x in known_key)
sys.stdout.write("\r")
print(f"[+] 字节 {byte_idx:02d}: {hex(best_guess)} ('{char}') | 票数: {votes[best_guess]} | 候选: {[chr(x) if 32<=x<=126 else hex(x) for x in top_3]}")
print(f" 当前串: {current_flag}")
print("-" * 60)
io.close()
if __name__ == "__main__":
solve()