Guest User

dg8045 decrypt

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