当前位置:网站首页>2022 Tiangong cup ctf--- crypto1 WP
2022 Tiangong cup ctf--- crypto1 WP
2022-07-25 06:58:00 【3tefanie, Zhou】
List of articles
AES-CBC
aes-cbc Pattern encryption requires an initialization vector for encryption and decryption (Initialization Vector, IV), Before every encryption or after every decryption , Use initialization vectors to XOR plaintext or ciphertext .
encryption
When encrypting , Plaintext first and IV Exclusive or , Then block encrypt the result , The output is ciphertext , At the same time, the output ciphertext of this time is encrypted as the next block IV.
Decrypt
When decrypting , First decrypt the first block of the ciphertext , Then compare the results with IV Exclusive or , You can get plaintext , meanwhile , The input ciphertext of this decryption is decrypted as the next block IV.
subject
from Crypto.Cipher import AES
import binascii
import hashlib
from secret import flag
assert flag[:5] == "flag{" and flag[-1:] == "}"
key = b"J1fx2g1jDak1c***"
l = len(key)
message = b"I have had my invitation to this world's festival, and thus my life has been blessed" + binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10]
iv = flag[5:-1]
message = message + bytes((l - len(message) % l) * chr(l - len(message) % l), encoding = "utf-8")
aes = AES.new(key, AES.MODE_CBC, iv)
print(binascii.hexlify(aes.encrypt(message)))
#******************************************************************************************************************************************************6ece036e495d363b647d7f2749c4c2f3dd78f8637b
process analysis
According to the title, we can know the following information :
key The first thirteen bytes of are b'J1fx2g1jDak1c', The last three are unknown
The first 84 bytes of plaintext are b"I have had my invitation to this world's festival, and thus my life has been blessed"
The middle cross is unknown , And finally bytes((l - len(message) % l) * chr(l - len(message) % l), encoding = "utf-8"), That is to say bytes((16-94%16)*chr(16-94%16))==>bytes(2*chr(2)), namely b'\x02\x02'
After the ciphertext 21 bytes binascii.unhexlify('6ece036e495d363b647d7f2749c4c2f3dd78f8637b')
According to the above aes-cbc Encryption process and decryption process
We can do it through the last known ciphertext block aes-ecb Mode decryption is XOR with the last plaintext block , You can get the last iv, It is also the ciphertext after the last block encryption The code logic is as follows :
from Crypto.Cipher import AES
# Ciphertext
enc = 'xxxxxx*****'
# Plaintext
m = 'yyyyyy******'
aes_ecb = AES.new(key,AES.MODE_ECB)
#aes ecb Mode decryption
xor_result = aes_ecb.decrypt(enc)
# Decryption result and plaintext bitwise XOR
iv = bytes([i^j for i,j in zip(m,xor_result)])
ps: Because the final message And key Is related to the value of , So we need to make sure key Can be determined message Value , Then push back in turn to solve the initial iv( namely flag)
So we need Mr. Cheng key Dictionary , And then blow it up Key
import string
dic = string.printable[:62]
with open('key_table.txt','wb') as file:
for i in dic:
for j in dic:
for k in dic:
key = b"J1fx2g1jDak1c"+i.encode()+j.encode()+k.encode()
file.write(key+b'\n')
file.close()

stay aes-ecb The size of each block in the pattern is 16byte, The known ciphertext is later 21byte, That is to say, the penultimate ciphertext block is known 5byte. Thus, we can decrypt the value of the last block according to the result of the exclusive or of the last plaintext block 5byte Make a comparison , If the value obtained is the same , The key It's what you want key
from Crypto.Cipher import AES
from tqdm import tqdm
import binascii
import hashlib
def xor(m: bytes, c: bytes):
return bytes([i ^ j for i, j in zip(m, c)])
enc = binascii.unhexlify('5d363b647d7f2749c4c2f3dd78f8637b')
five_part = binascii.unhexlify(b"6ece036e49")
f = open("key_table.txt","rb+")
pbar = tqdm(range(238328))
for i in f:
key = i[:16]
aes = AES.new(key, AES.MODE_ECB)
dec = aes.decrypt(enc)
m = b"ssed" +binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10]+b'\x02\x02'
xor_result = xor(m,dec)
pbar.update(1)
if five_part in xor_result:
print(key)
break
file.close()

To get by blasting Key:
b'J1fx2g1jDak1c7s4'

ok, determine key After that, our plaintext will be determined
key = 'J1fx2g1jDak1c7s4'
message = b"I have had my invitation to this world's festival, and thus my life has been blessed" + binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10]
message = message + bytes((l - len(message) % l) * chr(l - len(message) % l), encoding="utf-8")
#b'I have had my invitation to this world\'s festival, and thus my life has been blessed\xedVG\xfd"\xe6\x9d\xd5\xb9\xe2\x02\x02'
Then group the plaintext (16byte A group of ), Then solve the previous group to enc use key To decrypt , obtain xor_result, then xor_result Exclusive or with the same group of plaintext can get this group iv And the previous group enc. To repeat , Push forward step by step to calculate the initial iv, namely flag.
Next is the happy script time (bushi
l = len(key)
message = b"I have had my invitation to this world's festival, and thus my life has been blessed" + binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10]
message = message + bytes((l - len(message) % l) * chr(l - len(message) % l), encoding="utf-8")
for i in range(0,len(message),16):
aes_ecb = AES.new(key,AES.MODE_ECB)
dec_c = aes_ecb.decrypt(enc)
enc = xor(message[len(message)-i-16:len(message)-i],dec_c)
print(enc)
Problem solving script
import binascii
import hashlib
import string
from Crypto.Cipher import AES
from tqdm import tqdm
def xor(m: bytes, c: bytes):
return bytes([i ^ j for i, j in zip(m, c)])
dic = string.printable[:62]
with open('key_table.txt','wb') as file:
for i in dic:
for j in dic:
for k in dic:
key = b"J1fx2g1jDak1c"+i.encode()+j.encode()+k.encode()
file.write(key+b'\n')
file.close()
enc = binascii.unhexlify('5d363b647d7f2749c4c2f3dd78f8637b')
five_part = binascii.unhexlify(b"6ece036e49")
f = open("key_table.txt","rb+")
pbar = tqdm(range(238328))
for i in f:
key = i[:16]
aes = AES.new(key, AES.MODE_ECB)
dec = aes.decrypt(enc)
m = b"ssed" +binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10]+b'\x02\x02'
xor_result = xor(m,dec)
pbar.update(1)
if five_part in xor_result:
print(key)
break
file.close()
l = len(key)
message = b"I have had my invitation to this world's festival, and thus my life has been blessed" + binascii.unhexlify(hashlib.sha256(key).hexdigest())[:10]
message = message + bytes((l - len(message) % l) * chr(l - len(message) % l), encoding="utf-8")
for i in range(0,len(message),16):
aes_ecb = AES.new(key,AES.MODE_ECB)
dec_c = aes_ecb.decrypt(enc)
enc = xor(message[len(message)-i-16:len(message)-i],dec_c)
print(enc)
flag:
flag{
welcome_1234_igd}
【 Probably there is a kind of self infliction in the world , It's called putting yourself in the right place , Think of others everywhere .】
边栏推荐
- Qt实战案例(53)——利用QDrag实现拖拽拼图功能
- Prevention strategy of Chang'an chain Shuanghua transaction
- GIS实战应用案例100篇(十七)-基于DEM制作三维地图
- Leetcode 206. reverse linked list I
- 【transformer】DeiT
- 常吃发酵馒头是否会伤害身体
- Baidu Post Bar crawler gets web pages
- Baidu xirang's first yuan universe auction ended, and Chen Danqing's six printmaking works were all sold!
- [daily question 1] 1184. Distance between bus stops
- Mathematics Olympiad vs Informatics Olympiad (July 19, 2022)
猜你喜欢

百度希壤首场元宇宙拍卖落槌,陈丹青六幅版画作品全部成交!

"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 supplementary problem solution (incomplete)

Cointelegraph撰文:依托最大的DAO USDD成为最可靠的稳定币

Do you know the same period last year in powerbi

章鱼网络 Community Call #1|开启 Octopus DAO 构建

Labelme labels different objects, displays different colors and batch conversion

【terminal】x86 Native Tools Command Prompt for VS 2017

What are the hazards of insufficient sleep?

大话西游服务端启动注意事项

QT actual combat case (53) -- using qdrag to realize the drag puzzle function
随机推荐
[jailhouse article] base architectures for virtual physical computing (2018)
Insight into mobile application operation growth in 2022 white paper: the way to "break the situation" in the era of diminishing traffic dividends
[C language] document processing and operation
Rongyun launched a real-time community solution and launched "advanced players" for vertical interest social networking
Not only log collection, but also the installation, configuration and use of project monitoring tool sentry
Upload and download multiple files using web APIs
ArgoCD 用户管理、RBAC 控制、脚本登录、App 同步
Talk about practice, do solid work, and become practical: tour the digitalized land of China
Builder pattern
Kyligence Li Dong: from the data lake to the index middle stage, improve the ROI of data analysis
Microorganisms are healthy. Don't exclude microorganisms in the human body
Rust标准库-实现一个TCP服务、Rust使用套接字
Save the sqoop run template
Mlx90640 infrared thermal imager temperature measurement module development notes (I)
When the graduation season comes, are you ready? What are we going to do
Analysis of the calling principle of Changan chain solid smart contract
What are the hazards of insufficient sleep?
Yolov7 model reasoning and training its own data set
C language -c51 compilation warning "* * * warning l1: unresolved external symbol" and extern
The relationship between Informatics, mathematics and Mathematical Olympiad (July 19, 2022) C