it turns out some of those bits weren't random

This commit is contained in:
beerpsi 2024-07-18 03:12:57 +07:00
parent 2f654ec64f
commit fce4269fc9

View File

@ -1,11 +1,14 @@
# pyright: reportMissingTypeStubs=false, reportOperatorIssue=false, reportUnknownArgumentType=false, reportUnknownMemberType=false
from math import ceil
import os
import secrets
import struct
import time
import zlib
from Crypto.Cipher import AES
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.Hash import HMAC, SHA1
from Crypto.PublicKey import RSA
from construct import Bytes, Const, Int16ul, Int32ul, Int64ul, Int8ul, Struct
# ---- Configuration
@ -65,6 +68,17 @@ BTIV = bytes.fromhex("")
# The HMAC key that ensures the app/opt/pack created is authentic.
SIGKEY = bytes.fromhex("")
HEADER_META_PUBKEY = RSA.import_key("""-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsRMLnJuczNpfoqPpHQ3o
5XNkjKXO6P3ToV/45Az5dNaHVL7uEu9vPI7a2KYFQnNYgD3UUHFahfTcljzLOkcH
1aVrhm8gaB/5mygjUJWcN+kKyB7sASqhL22RC7NlxtDY15ozli/b0MagVoaBAV5D
MytUCa73GPRGY0x9v/wTvtmFclYCWjJ9c2QzrCrQ9eNTVyETwh5q6qKEARHGZgCX
rWmdCsa/+oS+3pLbUGFlHCSZtCvvWCJgmgurlTGAGzoxrieO6XDEg2AGiRprWWL2
BGNh7gwgnSq6FWnKSf2Qe7xoFcTpV5QhNFBQjrq0KnBDRfz5EXJnMoKxNYL6reqR
uwIDAQAB
-----END PUBLIC KEY-----
""")
# ----
# ---- Constants. Don't edit.
@ -123,8 +137,10 @@ print(f"Generated IV: {iv.hex()}")
filesize = os.stat(INPUT_FILE).st_size
BOOTID["block_count"] = ceil(filesize / BOOTID["block_size"]) + 8
bullshit = secrets.token_bytes(BOOTID["block_size"])
bullshit_crc32 = zlib.crc32(bullshit)
header_meta = struct.pack("<Q", time.time()) + os.path.abspath(INPUT_FILE).encode("utf-8")
header_meta += secrets.token_bytes(BOOTID["block_size"] - len(header_meta))
header_meta = PKCS1_OAEP.new(HEADER_META_PUBKEY).encrypt(header_meta)
bullshit_crc32 = zlib.crc32(header_meta)
block_crc32s = [0, bullshit_crc32, bullshit_crc32, bullshit_crc32, bullshit_crc32, bullshit_crc32, bullshit_crc32, bullshit_crc32]
with open(INPUT_FILE, "rb") as fin, open(OUTPUT_FILE, "w+b") as fout:
@ -140,9 +156,8 @@ with open(INPUT_FILE, "rb") as fin, open(OUTPUT_FILE, "w+b") as fout:
# We'll generate random bytes for them though.
_ = fout.write(secrets.token_bytes(BOOTID["block_size"] - 0x2800))
# Bullshit out 7 random blocks for the header.
for i in range(BOOTID["header_block_count"] - 1):
_ = fout.write(bullshit)
_ = fout.write(header_meta)
# Encrypt the contents of the file.
total_written = 0