Venom ctf 密码题 狂飙

题目如下,懒得找题目qaq
import os
from flag import flag
from Crypto.Util.number import *
from Crypto.Cipher import AES
m = 88007513702424243702066490849596817304827839547007641526433597788800212065249
key = os.urandom(24)
key = bytes_to_long(key)
n = m % key
flag += (16 - len(flag) % 16) * b'\x00'
iv = os.urandom(16)
aes = AES.new(key,AES.MODE_CBC,iv)
enc_flag = aes.encrypt(flag)

print(n)
print(enc_flag)
print(iv)

103560843006078708944833658339172896192389513625588

b'\xfc\x87\xcb\x8e\x9d\x1a\x17\x86\xd9~\x16)\xbfU\x98D\xfe\x8f\xde\x9c\xb0\xd1\x9e\xe7\xa7\xefiY\x95C\x14\x13C@j1\x9d\x08\xd9\xe7W>F2\x96cm\xeb'

b'UN\x1d\xe2r<\x1db\x00\xdb\x9a\x84\x1e\x82\xf0\x86'

刚开始第一个想法是爆破,遍历key值,利用n = m % key的关系求key值然后求aes,显然,key太大了,代码运算不出来

后面经高人指点,重新整理一下思路!
既然是AES加密,那么先写解密代码 decrypt_flag = aes.decrypt(enc_flag).rstrip(b"\x00") # .rstrip(b"\x00")用来补位
又因为题目aes = AES.new(key,AES.MODE_CBC,iv),所以现在求key就可以
又因为题目给了 n = m % key , 那就有关系 n = m - x * key (m > n , x取得未知数), 所以x * key = m - n
在m , n已知的情况下, 我们可以得到x * key的值,然后亚夫分解一下,得到3, 37, 439, 3939851, 5036645362649, 265898280367, 342291058100503482469327892079792475478873这几个数字的乘积
所以,写个函数将上面的数字的所有分成两组的乘积可能性列出来就是所有可能的key值,然后库库遍历,flag就是最后几个结果
`from itertools import combinations
from Crypto.Util.number import bytes_to_long
from Crypto.Cipher import AES

iv = b'UN\x1d\xe2r<\x1db\x00\xdb\x9a\x84\x1e\x82\xf0\x86'
enc_flag = b'\xfc\x87\xcb\x8e\x9d\x1a\x17\x86\xd9~\x16)\xbfU\x98D\xfe\x8f\xde\x9c\xb0\xd1\x9e\xe7\xa7\xefiY\x95C\x14\x13C@j1\x9d\x08\xd9\xe7W>F2\x96cm\xeb'

给定的因数列表

factors = [3, 37, 439, 3939851, 5036645362649, 265898280367, 342291058100503482469327892079792475478873]

def factor_combinations(factors):
all_combinations = set()

# 单独存在的因数
all_combinations.update(factors)

# 两两相乘的组合
for combo in combinations(factors, 2):
    all_combinations.add(combo[0] * combo[1])

# 三个相乘的组合
for combo in combinations(factors, 3):
    all_combinations.add(combo[0] * combo[1] * combo[2])

# 更多因数相乘的组合
for r in range(4, len(factors) + 1):
    for combo in combinations(factors, r):
        all_combinations.add(eval('*'.join(map(str, combo))))

return sorted(all_combinations)

生成所有可能的组合

all_combinations = factor_combinations(factors)
zifu = []
for i in all_combinations:

# 打印所有组合
# for combo in (all_combinations, 1):
#     print(f"{combo}")
# 科学计数法表示的大整数
scientific_notation = i

# 将科学计数法转换为整数
integer_value = int(scientific_notation)

# 将整数转换为字节表示
byte_representation = integer_value.to_bytes((integer_value.bit_length() + 7) // 8, 'big')

print(byte_representation)
zifu.append(byte_representation)

for i in zifu:
if len(i)==24:
aes = AES.new(i, AES.MODE_CBC, iv)
decrypted_flag = aes.decrypt(enc_flag).rstrip(b"\x00")
print(decrypted_flag)
`
结果如下:flag{cf735a4d-f9d9-5110-8a73-5017fc39b1b0}

posted @ 2024-03-18 21:57  附体欢欢  阅读(131)  评论(0)    收藏  举报