Visual_Studio

ADB Backup Creator/Extractor

May 26th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. #credits
  4. __title__       = "ADB Backup Creator/Extractor"
  5. __author__      = "Visual Studio @ se7ensins.com"
  6. __version__     = "1.0.0.0"
  7. __description__ = "A script to create/extract ADB backups from Android devices"
  8.  
  9. #built-in imports
  10. import zlib
  11. from io import BytesIO
  12. from os.path import isfile
  13. from binascii import unhexlify
  14. from argparse import ArgumentParser
  15.  
  16. #module imports
  17. from Cryptodome.Cipher import AES
  18. from Cryptodome.Util.Padding import unpad
  19. from Cryptodome.Protocol.KDF import PBKDF2
  20.  
  21. #globals
  22. KEY_BITS = 256
  23. KEY_BYTES = int(KEY_BITS / 8)
  24. PBKDF2_HASH_ROUNDS = 10000
  25. BACKUP_FILE_HEADER_MAGIC = b"ANDROID BACKUP"
  26.  
  27. if __name__ == "__main__":
  28.     #parse arguments
  29.     parser = ArgumentParser(description="A script to decrypt/decompress android ADB backups")
  30.     parser.add_argument("-e", "--create", action="store_true", help="Create a backup from scratch")
  31.     parser.add_argument("-d", "--extract", action="store_true", help="Extract an existing backup")
  32.     parser.add_argument("-i", "--in-file", type=str, help="The input file")
  33.     parser.add_argument("-o", "--out-file", type=str, help="The output file")
  34.     parser.add_argument("-k", "--key", type=str, help="The encryption key for the backup")
  35.     args = parser.parse_args()
  36.  
  37.     #arg constraints
  38.     assert args.create or args.extract, "No mode selected, use --encrypt or --decrypt!"
  39.     assert isfile(args.in_file), "Input file doesn't exist!"
  40.     assert not args.create, "Not implemented!"
  41.  
  42.     with open(args.in_file, "rb") as f:
  43.         if args.extract:
  44.             #read the header and make sure the magic exists
  45.             assert f.readline().rstrip() == BACKUP_FILE_HEADER_MAGIC, "Invalid backup file magic!"
  46.  
  47.             #read out first half of the header
  48.             backup_file_version = int(f.readline().rstrip())
  49.             backup_file_compressed = bool(f.readline().rstrip())
  50.             backup_file_encrypted = f.readline().rstrip() == b"AES-256"
  51.  
  52.             #the output
  53.             backup_file_data = b""
  54.  
  55.             #if the file's encrypted then decrypt it
  56.             if backup_file_encrypted:
  57.                 assert args.key, "No key provided and backup is encrypted, use --key!"
  58.  
  59.                 #read out the rest of the header
  60.                 backup_file_user_password_salt = unhexlify(f.readline().rstrip())
  61.                 backup_file_master_key_checksum_salt = unhexlify(f.readline().rstrip())
  62.                 backup_file_pbkdf2_rounds = int(f.readline().rstrip())
  63.                 backup_file_user_password_iv = unhexlify(f.readline().rstrip())
  64.                 backup_file_enc_key_blob = unhexlify(f.readline().rstrip())
  65.                 backup_file_enc_contents = f.read()
  66.  
  67.                 #derive the user password
  68.                 user_password = PBKDF2(args.key, backup_file_user_password_salt, KEY_BYTES, backup_file_pbkdf2_rounds)
  69.  
  70.                 #decrypt the key blob
  71.                 backup_file_dec_key_blob = AES.new(user_password, AES.MODE_CBC, iv=backup_file_user_password_iv).decrypt(backup_file_enc_key_blob)
  72.                 with BytesIO(backup_file_dec_key_blob) as bio:
  73.                     #read the key blob data
  74.                     key_blob_iv = bio.read(bio.read(1)[0])
  75.                     key_blob_master_key = bio.read(bio.read(1)[0])
  76.                     key_blob_checksum = bio.read(bio.read(1)[0])
  77.  
  78.                     #check the master key's checksum against the stored one
  79.                     #master_key_checksum = PBKDF2(key_blob_master_key, backup_file_master_key_checksum_salt, KEY_BYTES, PBKDF2_HASH_ROUNDS, hmac_hash_module=SHA1)
  80.  
  81.                     backup_file_data = unpad(AES.new(key_blob_master_key, AES.MODE_CBC, iv=key_blob_iv).decrypt(backup_file_enc_contents), AES.block_size)
  82.  
  83.             #if the file's compressed then decompress it
  84.             if backup_file_compressed:
  85.                 #decompress the data
  86.                 backup_file_data = zlib.decompress(backup_file_data)
  87.  
  88.             #write the output file
  89.             open(args.out_file, "wb").write(backup_file_data)
Advertisement
Add Comment
Please, Sign In to add comment