import re import base64 defrc4_decrypt(ciphertext,key): S=list(range(256)) j=0 res=[] #初始化S for i inrange(256): j=(j+S[i]+key[i%len(key)])%256 S[i],S[j]=S[j],S[i] #解密 i=j=0 for char in ciphertext: i=(i+1)%256 j=(j+S[i])%256 S[i],S[j]=S[j],S[i] res.append(char^S[(S[i]+S[j])%256]) returnbytes(res) data='wr3ClVcSw7nCmMOcHcKgacOtMkvDjxZ6asKWw4nChMK8IsK7KMOOasOrdgbDlx3DqcKqwr0hw701Ly57w63CtcOl' data=base64.b64decode(data.encode()).decode() data_list=[ord(j) for j in data] key='As_we_do_as_you_know' key_list=[ord(c) for c in key] flag=rc4_decrypt(data_list,key_list) print(flag)
初始化S和T: for i=0 to do S[i]=i; T[i]=K[i mod keylen];
初始排列S: j=0; for i=0 to 255 do j = (j+S[i]+T[i]mod256; swap(S[i],S[j]; 生成密钥流,利用密钥流和明文进行加密: i,j=0; for r=0 to len do //r为明文长度,r字节 i=(i+1)mod 256; j=(j+S[i])mod 256; swap(S[i],S[j]); t=(S[i]+S[j])mod 256; K[r]=S[t]; data[r]^=K[r];
[长城杯 2021 政企组]魔鬼凯撒的RC4茶室
第一部分:自定义凯撒加密
第二部分:需要从给定的两个文件中通过凯撒和rc4恢复key,发现key找不到定义…
放弃rc4逆向,直接调试得key[0]地址esp+f4h,发现为0xde,exp:
1 2 3 4
key=0xde enc=[0x0d,0x0a,0xb2,0xbf,0xb9,0xa5,0xa6,0xef,0xbf,0xb1,0xb3,0xef,0xb0,0xb9,0x81,0xef,0xad,0x81,0xed,0xb1,0x81,0xbb,0xbf,0xad,0xa7,0x81] for i in enc: print(chr((i^key)&0xff),end='')
import re import base64 defrc4_decrypt(ciphertext,key): S=list(range(256)) j=0 res=[] #初始化S for i inrange(256): j=(j+S[i]+key[i%len(key)])%256 S[i],S[j]=S[j],S[i] #解密 i=j=0 for char in ciphertext: i=(i+1)%256 j=(j+S[i])%256 S[i],S[j]=S[j],S[i] res.append(char^S[(S[i]+S[j])%256]) returnbytes(res) s1="w4s1PUYsJ8OYwpRXVjvDkVPCgzIEJ27Dt2I=" s2=base64.b64decode(s1.encode()).decode() data_list=[ord(i) for i in s2] key='e3d68926fe0ec451105275d838847bd1' key_list=[ord(c) for c in key] flag=rc4_decrypt(data_list,key_list) print(flag)