Skip to content

Instantly share code, notes, and snippets.

@jameshilliard
Forked from laanwj/decrypt.py
Last active October 9, 2019 20:51
Show Gist options
  • Save jameshilliard/adf656c43fbaeafc61cf2e097a87b2df to your computer and use it in GitHub Desktop.
Save jameshilliard/adf656c43fbaeafc61cf2e097a87b2df to your computer and use it in GitHub Desktop.
Decrypt router configuration
#!/usr/bin/env python3
# W.J. van der Laan 2017, distributed under MIT license
import binascii
import base64
import struct
import json
import os, sys
from Crypto import Random
from Crypto.Cipher import AES
KEY = binascii.a2b_hex(b'fffffbffeffffbfffbbfffbfdbfff7ffffffffffffffdfffff7fffffbfffffff')
def unpad(s):
'''PKCS7 unpad.'''
padlen = s[len(s)-1]
if padlen > 16:
raise ValueError('Invalid padding')
return s[:-padlen]
def decrypt(data_in):
# first 32 bytes are IV, we only need 16 of that
iv = data_in[0:16]
cipher = AES.new(KEY, AES.MODE_CBC, iv)
data_out = cipher.decrypt(data_in[32:])
data_out = unpad(data_out)
#with open('configfile_decrypted.dat', 'wb') as f:
# f.write(data_out)
crc = data_out[0:4]
ccrc = struct.pack('I',binascii.crc32(data_out[4:]))
assert(crc == ccrc)
json_data = json.loads(data_out[4:].decode())
return json_data
def main():
if len(sys.argv) < 2:
print('Usage: %s /path/to/configfile.bin' % os.path.basename(sys.argv[0]))
exit(1)
with open(sys.argv[1], 'rb') as f:
# decode base64
data_in = base64.b64decode(f.read())
# decode second layer, create one huge JSON file with record types at top level
json_data = decrypt(data_in)
out_recs = {}
for record in json_data:
d = base64.b64decode(record['data'])
assert(record['type'] not in out_recs) # duplicate
out_recs[record['type']] = decrypt(d)
if len(sys.argv) == 3:
with open(sys.argv[2], 'w') as outfile:
json.dump(out_recs, outfile, sort_keys=True, indent=4, separators=(',', ': '))
else:
json.dump(out_recs, sys.stdout, sort_keys=True, indent=4, separators=(',', ': '))
if __name__ == '__main__':
main()
@kingoflions
Copy link

what is usage command ?
when i run it i get this
./gwdecrypt.py
Traceback (most recent call last):
File "./gwdecrypt.py", line 10, in
from Crypto import Random
ImportError: No module named 'Crypto'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment