Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- from binascii import hexlify, unhexlify
- # pip install pycrypto
- from Crypto.Cipher import AES
- from Crypto.Hash import SHA256
- from Crypto.Util import number
- # HG352
- modem_keys = {
- "HG352": {
- "RSA_D":
- "1B18D0048611500CA489C51D7389B19A"
- "F977E6F5BB8DD5E61A62E339499E6237"
- "C234740129EBD25EF226AB7E498A0830"
- "DF0A5D45F19F5055B906EBC5E71C16C5"
- "A99E36D4F369701FAE2403E445BA3CAE"
- "4B0C9526A82EDD90FECD78B7EDD5EA5E"
- "6C98A0C4CABF3148E99E78DA0D5EB972"
- "6F1533A6738F47C790037D532F403C0D",
- "RSA_N":
- "A93591A1BFCB7615555C12CFE3AF0B68"
- "5A6B94E8604A9441ABF7A5F268D4CBF9"
- "6022E2F0694D679D2C8E4C2D4C3C0C44"
- "60C5646E852A51EF7EBC2F0C88F08E80"
- "6D991446348EB7AF280E607DDA363F4F"
- "322E9B5005503F31F60353219F86443A"
- "04E573FFEF541D21ADD1043E478D81B1"
- "E79A5B434C5F64B3D5B141D7BEB59D71",
- "AES128CBC_KEY": "3E4F5612EF64305955D543B0AE350880",
- "AES128CBC_IV": "8049E91025A6B54876C3B4868090D3FC",
- },
- "HG658": {
- "RSA_D":
- "2345ADB2C06E54D7373E2A233A50150A"
- "4044E417FBF76FB1AC8A444E72A345AA"
- "14B7C349A4824C96DF9ECF7D8CC50425"
- "32930DBD40D86FDCD892398702E3EA51"
- "41C90F10494BB91440E89B104626CCCB"
- "E45A5133362359732954BD63FCA58929"
- "E3D890014FDF83847E6B19F0D9E1117E"
- "9706984EAA57E114934B273366C4DBD1",
- "RSA_N": "C206CF93A9E6EE1CE17984DD54422AC4"
- "561A4EEB969D1BA81432626D6409FA03"
- "3B3738F8BBA046ACEF3BAC35094B70AF"
- "231D9DC43C1D68EDBEBE983E267B72FD"
- "3C2A7614D60FA7457B92B6A45C49F307"
- "EA23DE51E7E0C36D6440FC4F62C44CCB"
- "4169914E43DBFDAE536F002B2D670CE0"
- "A2A11FD1AF4C484C1A6FED9C228199A3",
- "AES128CBC_KEY": "48EE9D8621739F26C215C49071e2438A",
- "AES128CBC_IV": "A68FBBCA44BB1F5364A530608BCDEAAB",
- }
- }
- RSA_D = None # not required
- RSA_N = None
- AES128CBC_IV = None
- AES128CBC_KEY = None
- XML_VERSION_STRING = b'<?xml version="1.0" ?>'
- def print_usage():
- print("Usage : python " + sys.argv[0] + " config_file_path modem_model")
- print("modem_model only HG352 and HG658.")
- sys.exit(1)
- def load_config(config_file):
- if os.path.isfile(config_file):
- cf = open(config_file, "rb")
- config = cf.read()
- cf.close()
- else:
- print("Config file not found..exiting")
- sys.exit(1)
- return config
- def save_to_file(dest_file, data):
- with open(dest_file, "wb") as write_file:
- write_file.write(data)
- def get_sha256_hash_from_sig(sig):
- sig_int = int(hexlify(sig), 16)
- rsa_n = int(RSA_N, 16)
- dec_sig_as_int = pow(sig_int, 0x10001, rsa_n)
- decrypted_sig = number.long_to_bytes(dec_sig_as_int, 128)
- target_sha256 = hexlify(decrypted_sig)[-64:]
- return target_sha256
- def calc_actual_sha256_hash(enc_config_body):
- sha256 = SHA256.new()
- sha256.update(enc_config_body)
- actual_sha256_sig = sha256.hexdigest()
- actual_sha256_sig = str.encode(actual_sha256_sig)
- return actual_sha256_sig
- def decrypt_body(enc_config_body):
- iv = unhexlify(AES128CBC_IV)
- key = unhexlify(AES128CBC_KEY)
- cipher = AES.new(key, AES.MODE_CBC, iv)
- decrypted_data = cipher.decrypt(enc_config_body)
- # Strip block padding
- decrypted_data = decrypted_data.rstrip(b'\0')
- return decrypted_data
- def decrypt_config(input_file, output_file):
- enc_config = load_config(input_file)
- sig = enc_config[:0x80]
- enc_config_body = enc_config[0x80:]
- print("verifying signature...")
- target_sha256_hash = get_sha256_hash_from_sig(sig)
- actual_sha256_hash = calc_actual_sha256_hash(enc_config_body)
- if actual_sha256_hash == target_sha256_hash:
- print("Signature ok...")
- else:
- print("Signature not ok...exiting")
- sys.exit(1)
- print("Decrypting...")
- decrypted_data = decrypt_body(enc_config_body)
- check_config(decrypted_data)
- print("Saving decrypted config to " + output_file + "...")
- save_to_file(output_file, decrypted_data)
- def check_config(new_config_file):
- head = new_config_file[0:len(XML_VERSION_STRING)]
- if head != XML_VERSION_STRING:
- print("Not a valid config file...exiting")
- sys.exit(1)
- def main():
- if len(sys.argv) < 3:
- print_usage()
- input_file = sys.argv[1]
- global modem_keys, RSA_D, RSA_N, AES128CBC_IV, AES128CBC_KEY
- sys.argv[2] = sys.argv[2].upper() # required to be upper case.
- if sys.argv[2] not in modem_keys.keys():
- print("the only version available are : " + modem_keys.keys())
- sys.exit(1)
- keys = modem_keys[sys.argv[2]]
- RSA_D = keys["RSA_D"]
- RSA_N = keys["RSA_N"]
- AES128CBC_IV = keys["AES128CBC_IV"]
- AES128CBC_KEY = keys["AES128CBC_KEY"]
- output_file = "config.xml"
- decrypt_config(input_file, output_file)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment