def packets(): secret = unhexlify( '82bba514e6d19521114940bd65121af234c53654a8e67add7710b3725db44f77' '30ed8e3da7015a09fe0f08e9bef3853c0506327eb77c9951769d923d863a2f5e') sg_crypto = Crypto.from_shared_secret(secret) # Who cares about RAM anyway? data = {} data_path = os.path.join(os.path.dirname(__file__), 'data', 'packets') for f in os.listdir(data_path): with open(os.path.join(data_path, f), 'rb') as fh: data[f] = packer.unpack(fh.read(), sg_crypto) return data
def test_from_shared_secret(shared_secret_bytes): from xbox.sg.crypto import Crypto c = Crypto.from_shared_secret(shared_secret_bytes) # invalid length with pytest.raises(ValueError): c.from_shared_secret(shared_secret_bytes[1:]) # invalid parameter with pytest.raises(ValueError): c.from_shared_secret(123) assert c._encrypt_key == unhexlify(b'82bba514e6d19521114940bd65121af2') assert c._iv_key == unhexlify(b'34c53654a8e67add7710b3725db44f77') assert c._hash_key == unhexlify( b'30ed8e3da7015a09fe0f08e9bef3853c0506327eb77c9951769d923d863a2f5e')
def main(): parser = argparse.ArgumentParser( description='Parse PCAP files and show SG sessions') parser.add_argument('file', help='Path to PCAP') parser.add_argument('secret', help='Expanded secret for this session.') args = parser.parse_args() secret = args.secret if os.path.exists(secret): # Assume a file containing the secret with open(secret, 'rb') as fh: secret = fh.read() if all(chr(c) in string.hexdigits for c in secret): secret = unhexlify(secret) else: secret = unhexlify(secret) crypto = Crypto.from_shared_secret(secret) parse(args.file, crypto)
def crypto(shared_secret_bytes): return Crypto.from_shared_secret(shared_secret_bytes)
def crypto(): secret = unhexlify( '82bba514e6d19521114940bd65121af234c53654a8e67add7710b3725db44f77' '30ed8e3da7015a09fe0f08e9bef3853c0506327eb77c9951769d923d863a2f5e') return Crypto.from_shared_secret(secret)
def main(): parser = argparse.ArgumentParser( description='Re-Encrypt raw smartglass packets from a given filepath') parser.add_argument('src_path', type=str, help='Path to sourcefiles') parser.add_argument('src_secret', type=str, help='Source shared secret in hex-format') parser.add_argument('dst_path', type=str, help='Path to destination') parser.add_argument('dst_secret', type=str, help='Target shared secret in hex-format') args = parser.parse_args() src_secret = unhexlify(args.src_secret) dst_secret = unhexlify(args.dst_secret) src_path = args.src_path dst_path = args.dst_path if len(src_secret) != 64: print('Source key of invalid length supplied!') sys.exit(1) elif len(dst_secret) != 64: print('Destination key of invalid length supplied!') sys.exit(1) source_crypto = Crypto.from_shared_secret(src_secret) dest_crypto = Crypto.from_shared_secret(dst_secret) source_path = os.path.dirname(src_path) dest_path = os.path.dirname(dst_path) for f in os.listdir(source_path): src_filepath = os.path.join(source_path, f) with open(src_filepath, 'rb') as sfh: encrypted = sfh.read() if Int16ub.parse(encrypted[:2]) != PacketType.Message.value: print('Invalid magic, %s not a smartglass message, ignoring' % src_filepath) continue # Slice the encrypted data manually header = encrypted[:26] payload = encrypted[26:-32] hash = encrypted[-32:] if not source_crypto.verify(encrypted[:-32], hash): print('Hash mismatch, ignoring') continue # Decrypt with source shared secret iv = source_crypto.generate_iv(header[:16]) decrypted_payload = source_crypto.decrypt(iv, payload) # Encrypt with destination parameters new_iv = dest_crypto.generate_iv(header[:16]) recrypted = dest_crypto.encrypt(new_iv, decrypted_payload) new_hash = dest_crypto.hash(header + recrypted) new_packet = header + recrypted + new_hash with open(os.path.join(dest_path, f), 'wb') as dfh: dfh.write(new_packet)