Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Crypto.Cipher import AES
- from Crypto.Util import Counter
- import struct
- import hashlib
- from binascii import hexlify, unhexlify
- """
- typedef struct boot_dat_hdr
- {
- unsigned char ident[0x10];
- unsigned char sha2_s2[0x20];
- unsigned int s2_dst;
- unsigned int s2_size;
- unsigned int s2_enc;
- unsigned char pad[0x10];
- unsigned int s3_size;
- unsigned char pad2[0x90];
- unsigned char sha2_hdr[0x20];
- } boot_dat_hdr_t;
- """
- def aes_ctr_dec(buf, key, iv):
- ctr = Counter.new(128, initial_value=int(hexlify(iv), 16))
- return AES.new(key, AES.MODE_CTR, counter=ctr).encrypt(buf)
- def aes_ctr_enc(buf, key, iv):
- ctr = Counter.new(128, initial_value=int(hexlify(iv), 16))
- return AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(buf)
- boot = open('boot_recompiled.dat', 'wb')
- e0sHashBytes = b""
- # Get rights keys
- f = open("boot.dat", "rb")
- b = f.read()
- f.close()
- boot_ver = struct.unpack("I", b[0x0C:0x10])[0]
- print(hex(boot_ver))
- s2_base, s2_size = struct.unpack("II", b[0x30:0x38])
- stage2 = open("stage2_{0:08X}.bin".format(s2_base), 'rb').read()
- if boot_ver == 0x302E3156:
- s2_key = unhexlify("47E6BFB05965ABCD00E2EE4DDF540261")
- s2_ctr = unhexlify("8E4C7889CBAE4A3D64797DDA84BDB086")
- arm64_key = unhexlify("35D8FFC4AA1BAB9514825EB0658FB493")
- arm64_ctr = unhexlify("C38EA26FF3CCE98FD8D5ED431D9D5B94")
- arm64_off = 0x53BAB0
- arm64_size = 0x36370
- arm64_base = 0x80FFFE00
- fb_key = unhexlify("E2AC05206A701C9AA514D2B2B7C9F395")
- fb_ctr = unhexlify("46FAB59AF0E469EF116614DEC366D15F")
- fb_off = 0x17BAB0
- fb_size = 0x3C0000
- fb_base = 0xF0000000
- data_key = unhexlify("030D865B7E458B10AD5706F6E227F4EB")
- data_ctr = unhexlify("AFFC93692EBD2E3D252339F01E03416B")
- data_off = 0x5F40
- data_size = 0x175B70
- data_base = 0x80000000
- elif boot_ver == 0x312E3156:
- s2_key = unhexlify("47E6BFB05965ABCD00E2EE4DDF540261")
- s2_ctr = unhexlify("8E4C7889CBAE4A3D64797DDA84BDB086")
- arm64_key = unhexlify("51A39F0B46BAE4691AD39A698146E865")
- arm64_ctr = unhexlify("7A307ED7F1ECC792F0E821ECD6999853")
- arm64_off = 0x53BAE0
- arm64_size = 0x36370
- arm64_base = 0x80FFFE00
- fb_key = unhexlify("27BABEE3DCFEF100C744A2388B57E957")
- fb_ctr = unhexlify("0B88AC25AFAF9B92D81372331AD66E24")
- fb_off = 0x17BAE0
- fb_size = 0x3C0000
- fb_base = 0xF0000000
- data_key = unhexlify("8D6FEABE0F3936145A474D3F05D33679")
- data_ctr = unhexlify("2846EFA9DACB065C51C71C154F0E9EA2")
- data_off = 0x5F50
- data_size = 0x175B90
- data_base = 0x80000000
- else:
- exit()
- #ident
- e0sHashBytes += b'\x54\x58\x20\x42\x4F\x4F\x54\x00\x00\x00\x00\x00'
- e0sHashBytes += b[0x0C:0x10]
- #sha-256 of stage2_40020000.bin
- sha256 = hashlib.new('sha256')
- sha256.update(stage2)
- e0sHashBytes += sha256.digest()
- # todo: write s2_dst, hardcoded :\
- e0sHashBytes += b'\x00\x00\x02\x40'
- # write s2_size
- e0sHashBytes += struct.pack('I', len(stage2))
- # write s2_enc
- e0sHashBytes += struct.pack('I', 1)
- # 0x10 size padding
- e0sHashBytes += b'\x00' * 0x10
- # s3_size?
- if boot_ver == 0x302E3156:
- e0sHashBytes += b'\x50\x2B\xED\x00'
- elif boot_ver == 0x312E3156:
- e0sHashBytes += b'\x20\x2F\xED\x00'
- # 0x90 size padding
- e0sHashBytes += b'\x00' * 0x90
- # write all that data
- boot.write(e0sHashBytes)
- # calculate e0ssha256
- sha256 = hashlib.new('sha256')
- sha256.update(e0sHashBytes)
- boot.write(sha256.digest())
- # stage2
- boot.write(aes_ctr_enc(stage2, s2_key, s2_ctr))
- # data
- print("Data offset : " + hex(boot.tell()))
- boot.write(aes_ctr_enc(open("data_{0:08X}.bin".format(data_base), 'rb').read(), data_key, data_ctr))
- # fb
- print("fb offset : " + hex(boot.tell()))
- boot.write(aes_ctr_enc(open("fb_{0:08X}.bin".format(fb_base), 'rb').read(), fb_key, fb_ctr))
- # arm64
- print("arm64 offset : " + hex(boot.tell()))
- boot.write(aes_ctr_enc(open("arm64_{0:08X}.bin".format(arm64_base), 'rb').read(), arm64_key, arm64_ctr))
- # write rest of boot.dat
- with open('boot.dat', 'rb') as fh:
- fh.seek(boot.tell(), 0)
- boot.write(fh.read())
- boot.close()
Advertisement
Add Comment
Please, Sign In to add comment