Advertisement
smea

xor 'n write

Jan 2nd, 2014
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. #arg1 : CTRFAT image filename
  2. #arg2 : CTRFAT XORpad filename
  3. #change physical drive
  4. #use at your own risk (wrong PD could mean losing data)
  5.  
  6. import os
  7. import sys
  8.  
  9. SECTOR_SIZE = 0x200
  10. MULTIPLE = 0x50
  11.  
  12. print("opening device...")
  13. drive = open("\\\\.\\PhysicalDrive1","rb+")
  14. drive.seek(SECTOR_SIZE*0x3CA800)
  15.  
  16. #check
  17. print("checking device...")
  18. sector=bytearray(drive.read(SECTOR_SIZE))
  19. check=open("checkSector.bin", "rb").read()
  20. if(sector!=check):
  21.     print("sector0 doesn't match !")
  22.     exit(1)
  23. print("device confirmed.")
  24.  
  25. #write our shit
  26. drive.seek(SECTOR_SIZE*(0x3CA800+0x5CAE5))
  27.  
  28. src1fn=sys.argv[1] # nand image (CTRFAT)
  29. src2fn=sys.argv[2] # xorpad
  30.  
  31. print("writing...")
  32. k=0
  33. with open(src1fn, "rb") as f1:
  34.     with open(src2fn, "rb") as f2:
  35.         sector = bytearray(f1.read(SECTOR_SIZE*MULTIPLE))
  36.         sectorXOR = bytearray(f2.read(SECTOR_SIZE*MULTIPLE))
  37.         while sector != b"" and sectorXOR != b"":
  38.             # XORing here is uber slow
  39.             for i in range(len(sector)):
  40.                 sector[i]=sector[i]^sectorXOR[i]
  41.             drive.write(sector)
  42.             sys.stdout.write("Write progress: %f%%  (sector %s) \r" % (float(100*k)/0x179F1B, hex(k)) ); sys.stdout.flush();
  43.             sector = bytearray(f1.read(SECTOR_SIZE*MULTIPLE))
  44.             sectorXOR = bytearray(f2.read(SECTOR_SIZE*MULTIPLE))
  45.             k+=MULTIPLE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement