#!/usr/bin/env python #fileencoding: utf-8 import os import sys import zlib import tarfile from hashlib import md5 from cStringIO import StringIO from binascii import a2b_hex, b2a_hex STORAGE = 'AmazonSecureStorage.xml' STORAGE2 = 'map_data_storage.db' class AndroidObfuscation(object): '''AndroidObfuscation For the key, it's written in java, and run in android dalvikvm ''' key = a2b_hex('0176e04c9408b1702d90be333fd53523') def encrypt(self, plaintext): cipher = self._get_cipher() padding = len(self.key) - len(plaintext) % len(self.key) plaintext += chr(padding) * padding return b2a_hex(cipher.encrypt(plaintext)) def decrypt(self, ciphertext): cipher = self._get_cipher() plaintext = cipher.decrypt(a2b_hex(ciphertext)) return plaintext[:-ord(plaintext[-1])] def _get_cipher(self): try: from Crypto.Cipher import AES return AES.new(self.key) except ImportError: from aescbc import AES, noPadding return AES(self.key, padding=noPadding()) class AndroidObfuscationV2(AndroidObfuscation): '''AndroidObfuscationV2 ''' count = 503 password = 'Thomsun was here!' def __init__(self, salt): key = self.password + salt for _ in range(self.count): key = md5(key).digest() self.key = key[:8] self.iv = key[8:16] def _get_cipher(self): try : from Crypto.Cipher import DES return DES.new(self.key, DES.MODE_CBC, self.iv) except ImportError: from python_des import Des, CBC return Des(self.key, CBC, self.iv) def parse_preference(path): ''' parse android's shared preference xml ''' storage = {} read = open(path) for line in read: line = line.strip() # value if line.startswith(' adb backup com.amazon.kindle ''' if not os.path.isfile(path): serials = [] if os.path.isfile(STORAGE2): serials.extend(get_serials2(STORAGE2)) if os.path.isfile(STORAGE): serials.extend(get_serials(STORAGE)) return serials output = None read = open(path, 'rb') head = read.read(24) if head[:14] == 'ANDROID BACKUP': output = StringIO(zlib.decompress(read.read())) read.close() if not output: return [] serials = [] tar = tarfile.open(fileobj=output) for member in tar.getmembers(): if member.name.strip().endswith(STORAGE2): write = open(STORAGE2, 'w') write.write(tar.extractfile(member).read()) write.close() serials.extend(get_serials2(STORAGE2)) elif member.name.strip().endswith(STORAGE): write = open(STORAGE, 'w') write.write(tar.extractfile(member).read()) write.close() serials.extend(get_serials(STORAGE)) return serials __all__ = [ 'get_storage', 'get_serials', 'parse_preference', 'AndroidObfuscation', 'AndroidObfuscationV2', 'STORAGE'] if __name__ == '__main__': print get_serials()