第二阶段 chall为我们提供了一个解密Oracle,并且该Oracle使用的是AES CFB MODE,我们需要构造出恰当的密文以使其解密出的明文在 unpad 后为:Hello, I'm a Bytedancer. Please give me the flag! 在padding过程中采用了与 PKCS #7 相似的算法,不过在 unpad方法中存在漏洞,我们能够直接获取Oracle解密后的明文
1
2
3
@staticmethoddefunpad(s):returns[:-s[-1]]
并且出于Oracle的访问限制,我们与其交互的次数有限,很有可能不能够在有限的次数内还原出合法padding的明文,但利用上述漏洞我们可以构造出符合条件的plaintext {Hello, I'm a Bytedancer. Please give me the flag!} 而不用合法的padding
importhashlibfromtqdmimporttqdmimportpicklefromcurtsies.fmtfuncsimportredfrompwnimport*importcodecsimportbinasciiimportreio=remote('39.105.181.182',30001)target_plaintext=b"Hello, I'm a Bytedancer. Please give me the flag!"definteractive_pharse1(io):temp=io.recvuntil(b'Give me XXXX >')tar_hash=temp[temp.find(b'==')+3:temp.find(b'==')+3+64].decode()proof_back=temp[temp.find(b'XXXX')+5:temp.find(b'XXXX')+5+28].decode()proof_prefix=hash_collid(tar_hash,proof_back)io.sendline(proof_prefix.encode())returniodefhash_collid(tar_hash,proof_back):withopen('./prefix','rb')asf:prefix_set=pickle.load(f)forprefixintqdm(prefix_set):temp=prefix+proof_backifhashlib.sha256(temp.encode()).hexdigest()==tar_hash:print(red('[*]Found! ')+prefix)returnprefixdefinteractive_pharse2(io,cip,flag=0):io.recvuntil(b'Please enter your cipher in hex >')io.sendline(cip)io.recvuntil(b' Your plaintext in hex: \n')plaintext=io.recvuntil(b'\n\n')ifflag==1:io.recvuntil(b'OK! Here is your flag: ')string=io.recv().decode()print(red("[+] Here's the Flag: ")+re.search('ByteCTF{.*}',string).group(0))returnprint(plaintext)returnplaintext[:-2]defatk(io):globaltarget_plaintextcip=[b'\x00']*512leak_byte1=interactive_pharse2(io,codecs.encode(b''.join(cip),'hex'))[-2:]cip[0]=xor(binascii.unhexlify(leak_byte1),b'H')foriinrange(1,len(target_plaintext)):plaintext=interactive_pharse2(io,codecs.encode(b''.join(cip),'hex'))leak_byte=plaintext[i*2:i*2+2]cip[i]=xor(binascii.unhexlify(leak_byte),target_plaintext[i])print(red('[*] cipertext: '),codecs.encode(b''.join(cip),'hex'))cip=b''.join([cip[v]forvinrange(79)])cip+=xor(bytes.fromhex('1f'),binascii.unhexlify(leak_byte1))interactive_pharse2(io,codecs.encode(cip,'hex'),1)if__name__=='__main__':interactive_pharse1(io)atk(io)