题目
题目链接:https://github.com/pogTeam/writeups/tree/master/2018/Pragyan/crypto/xmen_or_the_avengers
The legion of doom is expecting an impending attack from a group of superheroes. they are not sure if it is the Xmen OR the avengers. They have received some information from a spy, a zip file containing the following files:
info_crypt.txt
info_clear.txt
superheroes_group_info_crypt.txt
Help the legion of doom in decrypting the last file so they can prepare themselves and prevent their impending doom.
审题
一看就是一个加解密的题,给了三个文件,告诉读者是要解密第三个文件获得结果,点进去第三个文件一看是一段奇怪的文字,有些敏感度都能觉察出来这是被base64加密过的,所以这里肯定要把它解密一下
然后就是第一二个文件了,第一段是些文字,没有大用,第二段是一些奇怪的字符,根本不知道干嘛用的.
到这里似乎就被卡住了,然后寻思一下加解密的算法吧
加解密的算法有:DES,AES,RSA,DH,我觉得用的比较多的就是这些吧.RSA跟DH从构造上就被这道题排除了,然后还剩DES跟AES.看题目吧! “Xmen OR the avengers” .题目肯定不会空穴来风的, XOR 找到一个了,avengers —>”AES”
我觉得大概就是这样了吧,先不管了,试试再说,不行再换.
写代码的思路
由于没有位移向量,所以这个AES肯定就是ECB模式了,然后网上搜索一下关于AES跟DES的东西,找到了新大陆,引用如下:
使用AES需要注意下面几点:
1) 确保都使用AES_128 + ECB;
2) 确保明文填充都使用的是Pkcs7;
3) 加密key在AES_128长度必须是16, 24, 或者 32 字节(bytes);如果不够长必须填充,过长必须截取,建议直接md5;
4) 加密向量iv与加密key有同样的约定,但在ECB可以忽略该值(用不到)。
5) 注意加密结果建议都使用base64编码。
只有以上都保持一样,各个语言里最终加密的密文才能保持一致,否则会出现:
1) 每次加密的密文不一样,但是能解密;(iv随机生成导致的)
2) 不同语言加密出来的密文不一致。
思路就很清楚了:
1,第一二个文件异或,然后MD5一下得到key
2,第三个文件BASE64解密得到密文
3,用AES的ECB模式解密估计就可以得到结果了
代码
import base64
import hashlib
from Crypto.Cipher import AES
def readfile(path):
with open(path, 'r') as f:
return f.read()
def xor(s1, s2):
return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))
#read the files
clear = readfile('info_clear.txt')
crypt = readfile('info_crypt.txt')
superhero = readfile('superheroes_group_info_crypt.txt')
superhero = base64.b64decode(superhero)
#get the key
#dec = xor(clear,crypt).rstrip('\n').encode('utf-8')
dec = xor(clear,crypt)
print(dec)
key = hashlib.md5(dec).hexdigest().encode()
print(key)
#decrypt aes-ecb
cipher = AES.new(key, AES.MODE_ECB)
msg = cipher.decrypt(superhero)
print("the key is---->" + msg)
解密结果为:pctf{it’s_the_justice_league_DC_for_life_hellya}
格式出现了ctf的字样,然后文字可读,那就差不多是做对了0.0