Guest User

hg630 v2 decrypt

a guest
Dec 7th, 2020
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. # hg658c.wordpress.com
  4. # This is for the Huawei HG633
  5. """
  6. To decrypt encrypted values in the config, use
  7. echo 67OEAbeekHIuQUTQZqvfxh== > 1.txt | certutil -decode 1.txt 2.txt | openssl enc -d -aes-128-cbc -K 65CE89619D8929CDCF998D42D74C59E3 -iv 85AE4578D5A1BD0758E8349AE0335F0A -nopad ; echo
  8. """
  9.  
  10. import sys
  11. import os
  12. from binascii import hexlify, unhexlify
  13. from Crypto.Cipher import AES
  14. from Crypto.Hash import MD5
  15. from Crypto.Util import number
  16. import zlib
  17.  
  18. #30003/100020
  19. RSA_D = (
  20.     "1A9FCE09FD5B6FAA0CEC5C9841B4105D"
  21.     "0AD9D1DD0324817413EEF77267C56DBE"
  22.     "E76C135B63160F759FD5333B060AFC08"
  23.     "71C813D0FDDFA3AA3CA5DD4C0430940D"
  24.     "B14A9DF8A31B4B38B4690BE84E9A38D0"
  25.     "FBB405244648CED3A8EAFE7F5451752E"
  26.     "F201B94A13C1DF915012302F12C6946B"
  27.     "230474034A665B2CAAD23794BF7B6381")
  28.  
  29. #30003/40010
  30. RSA_N = (
  31.     "B1AAE240AE798C9051E97745519F737A"
  32.     "4F5E82902CAA630420EC5B0F6FE64926"
  33.     "CA9D766811C354C951E58B4796037159"
  34.     "45F344DEA71A72106E1BA10FF72721DF"
  35.     "C7C5273A808ADF2F8A39BEB2D03370A8"
  36.     "7749E778F2135A1625FFFBBFB17631A3"
  37.     "207C9E129105F3E7DCE0AFA654321597"
  38.     "F9B2026BD197AEABF2F03B1EB8137F1D")
  39.  
  40. RSA_E = "010001"        
  41.  
  42. SIG_TEMPLATE = ("0001ffffffffffffffffffffffffffff"
  43.                 "ffffffffffffffffffffffffffffffff"
  44.                 "ffffffffffffffffffffffffffffffff"
  45.                 "ffffffffffffffffffffffffffffffff"
  46.                 "ffffffffffffffffffffffffffffffff"
  47.                 "ffffffffffffffffffffffffff003020"
  48.                 "300c06082a864886f70d020505000410")
  49.  
  50. #30000/10040
  51. AES256CBC_KEY = "65CE89619D8929CDCF998D42D74C59E3B4E11CFCAE5927760A78E18061D2145C"
  52. #30000/20040
  53. AES256CBC_IV  = "85AE4578D5A1BD0758E8349AE0335F0A"
  54.  
  55. XML_VERSION_STRING = b'<?xml version="1.0" ?>'
  56.  
  57. def print_usage():
  58.     print(("Usage : " + sys.argv[0] + " {encrypt | decrypt} input_file output_file"))
  59.    
  60.  
  61. def load_config(config_file):
  62.     if os.path.isfile(config_file):
  63.         cf = open(config_file, "rb")
  64.         config = cf.read()
  65.         cf.close()
  66.     else:
  67.         print("Config file not found..exiting")
  68.          
  69.     return config
  70.  
  71. def save_to_file(dest_file, data):
  72.     wfile = open(dest_file,"wb")
  73.     wfile.write(data)
  74.     wfile.close()
  75.  
  76. def get_md5_hash_from_sig(sig):
  77.     sig_int = int(hexlify(sig),16)
  78.     rsa_n = int(RSA_N,16)
  79.     dec_sig_as_int = pow(sig_int, 0x10001, rsa_n );
  80.     decrypted_sig = number.long_to_bytes(dec_sig_as_int, 128)
  81.     target_md5 = hexlify(decrypted_sig)[-64:]
  82.     return target_md5
  83.  
  84. def calc_actual_md5_hash(enc_config_body):
  85.     md5 = MD5.new()
  86.     md5.update(enc_config_body)
  87.     actual_md5_sig = md5.hexdigest()
  88.     actual_md5_sig = str.encode(actual_md5_sig)
  89.     return actual_md5_sig
  90.  
  91. def decrypt_config(input_file, output_file):
  92.     enc_config=load_config(input_file)
  93.  
  94.     print("Decrypting...")
  95.     iv = unhexlify(AES256CBC_IV)
  96.     key= unhexlify(AES256CBC_KEY)
  97.     cipher = AES.new(key, AES.MODE_CBC, iv)
  98.     try:
  99.         decrypted_data = cipher.decrypt(enc_config)
  100.         decompressed_data=""
  101.  
  102.         decompressed_data = zlib.decompress(decrypted_data)
  103.     except:
  104.         print("Bad config file...exiting")
  105.  
  106.     config_text = decompressed_data[:-0x80]
  107.     actual_md5_hash = calc_actual_md5_hash(config_text)
  108.  
  109.     print("Verifying signature...")
  110.     sig = decompressed_data [-0x80:]
  111.     sig_int = int(hexlify(sig),16)
  112.     rsa_n = int(RSA_N,16)
  113.     dec_sig_as_int = pow(sig_int, 0x10001, rsa_n );
  114.     decrypted_sig = number.long_to_bytes(dec_sig_as_int, 128)
  115.     target_md5_hash = hexlify(decrypted_sig)[-32:]
  116.  
  117.     if (actual_md5_hash == target_md5_hash):
  118.         print("Signature ok...")        
  119.     else:
  120.         print("Signature not ok...exiting")
  121.        
  122.  
  123.     config_text = config_text[:-1]
  124.     check_config(config_text)
  125.  
  126.     print(("Saving decrypted config to " + output_file + "..."))
  127.     save_to_file(output_file, config_text)
  128.  
  129. def check_config(new_config_file):
  130.     head = new_config_file[0:len(XML_VERSION_STRING)]
  131.     if head != XML_VERSION_STRING:
  132.         print("Not a valid config file...exiting")
  133.        
  134.  
  135. def encrypt_config(input_file, output_file):
  136.     new_config_data=load_config(input_file)
  137.     check_config(new_config_data)
  138.     new_config_data += '\0'.encode()
  139.  
  140.     print("Calculating MD5 hash...")
  141.     h = MD5.new()
  142.     h.update(new_config_data)
  143.     actual_md5_sig = h.hexdigest()
  144.  
  145.     sig = SIG_TEMPLATE + actual_md5_sig;
  146.  
  147.     print("Adding Signature...")
  148.     sig_int = int(sig,16)
  149.     rsa_d = int(RSA_D,16)
  150.     rsa_n = int(RSA_N,16)
  151.     enc_sig_int = pow(sig_int, rsa_d, rsa_n);
  152.     encrypted_sig = number.long_to_bytes(enc_sig_int, 128)
  153.     new_config_data = new_config_data + encrypted_sig
  154.  
  155.     print("Compressing config...")
  156.     compressed_data = zlib.compress(new_config_data, 9)
  157.  
  158.     padding_amount = len(compressed_data) % 16
  159.     print(("" + str(padding_amount) + " bytes padding needed"))
  160.     print("Adding padding...")
  161.     compressed_data=compressed_data + b'\0'*(16-padding_amount)
  162.  
  163.     print("Encrypting config...")
  164.     iv = unhexlify(AES256CBC_IV)
  165.     key= unhexlify(AES256CBC_KEY)
  166.     aes = AES.new(key, AES.MODE_CBC, iv)
  167.     enc_new_config = aes.encrypt(compressed_data)
  168.  
  169.     print(("Saving encrypted config to " + output_file + "..."))
  170.     save_to_file(output_file, enc_new_config)
  171.  
  172. def main():
  173.     if len(sys.argv) < 4:
  174.         print_usage()
  175.  
  176.     input_file = sys.argv[2]
  177.     output_file = sys.argv[3]
  178.     command = sys.argv[1]
  179.  
  180.     if (command == "encrypt"):
  181.         encrypt_config(input_file, output_file)
  182.     elif (command == "decrypt"):
  183.         decrypt_config(input_file, output_file)
  184.     else:
  185.         print_usage()
  186.  
  187.  
  188. if __name__ == "__main__":
  189.     main()
Advertisement
Add Comment
Please, Sign In to add comment