Guest User

Untitled

a guest
Jun 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 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. boot = open('boot_recompiled.dat', 'wb')
  28. stage2 = open('stage2_40020000 (edited with hash of databin).bin', 'rb').read()
  29.  
  30. e0sHashBytes = b""
  31.  
  32. # write ident
  33. boot.write(b'\x54\x58\x20\x42\x4F\x4F\x54\x00\x00\x00\x00\x00\x56\x31\x2E\x30')
  34. e0sHashBytes += b'\x54\x58\x20\x42\x4F\x4F\x54\x00\x00\x00\x00\x00\x56\x31\x2E\x30'
  35.  
  36. # write sha2-256 of stage2_40020000.bin
  37. sha256 = hashlib.new('sha256')
  38. sha256.update(stage2)
  39. boot.write(sha256.digest())
  40. e0sHashBytes += sha256.digest()
  41.  
  42. # todo: write s2_dst, hardcoded :\
  43. boot.write(b'\x00\x00\x02\x40')
  44. e0sHashBytes += b'\x00\x00\x02\x40'
  45.  
  46. # write s2_size
  47. boot.write(struct.pack('I', len(stage2)))
  48. e0sHashBytes += struct.pack('I', len(stage2))
  49.  
  50. # write s2_enc
  51. boot.write(struct.pack('I', 1))
  52. e0sHashBytes += struct.pack('I', 1)
  53.  
  54. # 0x10 size padding
  55. boot.write(b'\x00' * 0x10)
  56. e0sHashBytes += b'\x00' * 0x10
  57.  
  58. # s3_size?
  59. boot.write(b'\x50\x2B\xED\x00')
  60. e0sHashBytes += b'\x50\x2B\xED\x00'
  61.  
  62. # 0x90 size padding
  63. boot.write(b'\x00' * 0x90)
  64. e0sHashBytes += b'\x00' * 0x90
  65.  
  66. # calculate e0ssha256
  67. sha256 = hashlib.new('sha256')
  68. sha256.update(e0sHashBytes)
  69. boot.write(sha256.digest())
  70.  
  71. # stage2 section
  72. boot.write(aes_ctr_dec(stage2, unhexlify("47E6BFB05965ABCD00E2EE4DDF540261"), unhexlify("8E4C7889CBAE4A3D64797DDA84BDB086")))
  73.  
  74. # data section
  75. with open('data_80000000 (edited with pub key).bin', 'rb') as fh:
  76. boot.write(aes_ctr_dec(fh.read(), unhexlify("030D865B7E458B10AD5706F6E227F4EB"), unhexlify("AFFC93692EBD2E3D252339F01E03416B")))
  77.  
  78. # fb section
  79. with open('fb_F0000000.bin', 'rb') as fh:
  80. boot.write(aes_ctr_dec(fh.read(), unhexlify("E2AC05206A701C9AA514D2B2B7C9F395"), unhexlify("46FAB59AF0E469EF116614DEC366D15F")))
  81.  
  82. # write arm64
  83. with open('arm64_80FFFE00.bin', 'rb') as fh:
  84. boot.write(aes_ctr_dec(fh.read(), unhexlify("35D8FFC4AA1BAB9514825EB0658FB493"), unhexlify("C38EA26FF3CCE98FD8D5ED431D9D5B94")))
  85.  
  86. # write rest of boot.dat og from 0x571e20 onwards
  87. with open('boot.dat', 'rb') as fh:
  88. fh.seek(0x571E20, 0)
  89. boot.write(fh.read())
  90.  
  91. boot.close()
Add Comment
Please, Sign In to add comment