Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /usr/bin/env python
# hg658c.wordpress.com
# Version 1
# Deutsche Telekom Speedport Hybrid configuration file decoder
import sys
import os
from binascii import hexlify, unhexlify
from Crypto.Cipher import AES
import zlib
AES256CBC_KEY = "DD1FBB7596D0D58BBF336ABA391366ED3ED7460849BB9EFAB9649C999863BF25"
AES256CBC_IV = "B380E17A0C476E8A480B55253ABAE666"
XML_VERSION_STRING = b'<?xml version="1.0" ?>'
def print_usage():
print("Usage : " + sys.argv[0] + " {encrypt | decrypt} input_file output_file")
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):
wfile = open(dest_file,"wb")
wfile.write(data)
wfile.close()
def decrypt_config(input_file, output_file):
enc_config=load_config(input_file)
print("Decrypting...")
iv = unhexlify(AES256CBC_IV)
key= unhexlify(AES256CBC_KEY)
cipher = AES.new(key, AES.MODE_CBC, iv)
try:
decrypted_data = cipher.decrypt(enc_config)
decompressed_data=""
decompressed_data = zlib.decompress(decrypted_data)
except:
print("Bad config file...exiting")
sys.exit(1)
decompressed_data = decompressed_data[:-1]
check_config(decompressed_data)
print("Saving decrypted config to " + output_file + "...")
save_to_file(output_file, decompressed_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 encrypt_config(input_file, output_file):
new_config_data=load_config(input_file)
check_config(new_config_data)
new_config_data += b'\0'
print("Compressing config...")
compressed_data = zlib.compress(new_config_data, 9)
padding_amount = len(compressed_data) % 16
print("" + str(padding_amount) + " bytes padding needed")
print("Adding padding...")
compressed_data=compressed_data + b'\0'*(16-padding_amount)
print("Encrypting config...")
iv = unhexlify(AES256CBC_IV)
key= unhexlify(AES256CBC_KEY)
aes = AES.new(key, AES.MODE_CBC, iv)
enc_new_config = aes.encrypt(compressed_data)
print("Saving encrypted config to " + output_file + "...")
save_to_file(output_file, enc_new_config)
def main():
if len(sys.argv) < 4:
print_usage()
input_file = sys.argv[2]
output_file = sys.argv[3]
command = sys.argv[1]
if (command == "encrypt"):
encrypt_config(input_file, output_file)
elif (command == "decrypt"):
decrypt_config(input_file, output_file)
else:
print_usage()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.