题目
题目描述: py又cc
附件:(资源已上传)
pyc文件是是py的编译文件,使用反编译工具还原文件
1. 反编译pyc文件
在线工具:http://tools.bugscaner.com/decompyle/
本地工具:uncompyle6
pip install uncompyle6
uncompyle6 xxx.pyc > xxx.py
安装参考链接:https://blog.csdn.net/qq_26328861/article/details/136188163
得到py文件:
# uncompyle6 version 3.9.1
# Python bytecode version base 3.8.0 (3413)
# Decompiled from: Python 3.11.8 (tags/v3.11.8:db85d51, Feb 6 2024, 22:03:32) [MSC v.1937 64 bit (AMD64)]
# Embedded file name: ./pyc.py
# Compiled at: 2024-01-23 16:32:18
# Size of source mod 2**32: 503 bytes
import random
def encrypt_file(file_path):
random.seed(114514)
with open(file_path, "rb") as file:
data = file.read()
encrypted_data = b''
for byte in data:
key = random.randint(0, 128)
encrypted_data += chr(byte ^ key).encode()
else:
encrypted_file_path = file_path + ".enc"
with open(encrypted_file_path, "wb") as encrypted_file:
encrypted_file.write(encrypted_data)
return encrypted_file_path
file_path = "./flag"
encrypt_file(file_path)
# okay decompiling secret.pyc
2. 解密
加密时,对明文每个字节byte与随机key进行异或运算,然后加密
encrypted_data += chr(byte ^ key).encode()
由于随机种子固定,则random得到的随机数固定,直接对字节进行解密:
decrypted_data += bytes([byte ^ key])
import random
def decrypted_file(file_path):
random.seed(114514)
with open(file_path, 'rb') as (file):
data = file.read()
decrypted_data = b''
for byte in data:
key = random.randint(0, 128)
decrypted_data += bytes([byte ^ key])
decrypted_file_path = file_path[:-4]
with open(decrypted_file_path, 'wb') as (decrypted_file):
decrypted_file.write(decrypted_data)
return decrypted_file_path
file_path = './flag.enc'
decrypted_file(file_path)
查看解密结果flag{U_R_g00d_at_do1n_pyc}