Guest User

Untitled

a guest
Jun 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. from Crypto.Cipher import AES
  2. from Crypto.Util import Counter
  3. import struct
  4.  
  5. import hashlib
  6. from binascii import hexlify, unhexlify
  7.  
  8. """
  9. typedef struct boot_dat_hdr
  10. {
  11. unsigned char ident[0x10];
  12. unsigned char sha2_s2[0x20];
  13. unsigned int s2_dst;
  14. unsigned int s2_size;
  15. unsigned int s2_enc;
  16. unsigned char pad[0x10];
  17. unsigned int s3_size;
  18. unsigned char pad2[0x90];
  19. unsigned char sha2_hdr[0x20];
  20. } boot_dat_hdr_t;
  21. """
  22.  
  23. def aes_ctr_dec(buf, key, iv):
  24. ctr = Counter.new(128, initial_value=int(hexlify(iv), 16))
  25. return AES.new(key, AES.MODE_CTR, counter=ctr).encrypt(buf)
  26.  
  27. def aes_ctr_enc(buf, key, iv):
  28. ctr = Counter.new(128, initial_value=int(hexlify(iv), 16))
  29. return AES.new(key, AES.MODE_CTR, counter=ctr).decrypt(buf)
  30.  
  31. boot = open('boot_recompiled.dat', 'wb')
  32. stage2 = open('stage2_40020000.bin', 'rb').read()
  33. e0sHashBytes = b""
  34.  
  35. #ident
  36. e0sHashBytes += b'\x54\x58\x20\x42\x4F\x4F\x54\x00\x00\x00\x00\x00\x56\x31\x2E\x30'
  37. #sha-256 of stage2_40020000.bin
  38. sha256 = hashlib.new('sha256')
  39. sha256.update(stage2)
  40. e0sHashBytes += sha256.digest()
  41. # todo: write s2_dst, hardcoded :\
  42. e0sHashBytes += b'\x00\x00\x02\x40'
  43. # write s2_size
  44. e0sHashBytes += struct.pack('I', len(stage2))
  45. # write s2_enc
  46. e0sHashBytes += struct.pack('I', 1)
  47. # 0x10 size padding
  48. e0sHashBytes += b'\x00' * 0x10
  49. # s3_size?
  50. e0sHashBytes += b'\x50\x2B\xED\x00'
  51. # 0x90 size padding
  52. e0sHashBytes += b'\x00' * 0x90
  53. # write all that data
  54. boot.write(e0sHashBytes)
  55. # calculate e0ssha256
  56. sha256 = hashlib.new('sha256')
  57. sha256.update(e0sHashBytes)
  58. boot.write(sha256.digest())
  59. # stage2
  60. boot.write(aes_ctr_enc(stage2, unhexlify("47E6BFB05965ABCD00E2EE4DDF540261"), unhexlify("8E4C7889CBAE4A3D64797DDA84BDB086")))
  61. # data
  62. boot.write(aes_ctr_enc(open('data_80000000.bin', 'rb').read(), unhexlify("030D865B7E458B10AD5706F6E227F4EB"), unhexlify("AFFC93692EBD2E3D252339F01E03416B")))
  63. # fb
  64. boot.write(aes_ctr_enc(open('fb_F0000000.bin', 'rb').read(), unhexlify("E2AC05206A701C9AA514D2B2B7C9F395"), unhexlify("46FAB59AF0E469EF116614DEC366D15F")))
  65. # arm64
  66. boot.write(aes_ctr_enc(open('arm64_80FFFE00.bin', 'rb').read(), unhexlify("35D8FFC4AA1BAB9514825EB0658FB493"), unhexlify("C38EA26FF3CCE98FD8D5ED431D9D5B94")))
  67. # write rest of boot.dat og from 0x571e20 onwards
  68. with open('boot.dat', 'rb') as fh:
  69. fh.seek(0x571E20, 0)
  70. boot.write(fh.read())
  71.  
  72. boot.close()
Add Comment
Please, Sign In to add comment