Skip to content

Instantly share code, notes, and snippets.

@u1735067
Forked from Xavier75/nbconfig.py
Created May 5, 2021 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u1735067/00c5fa236024e986815dcaf540dde79b to your computer and use it in GitHub Desktop.
Save u1735067/00c5fa236024e986815dcaf540dde79b to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#-*- encoding: Utf-8 -*-
from hashlib import md5
from sys import argv
try:
from Crypto.Cipher import AES
except ImportError:
exit('Erreur : la bibliothèque "PyCryto" n\'est pas installée')
print " ___ __ "
print " .-----.-----.--.--.' _| |--.-----.--.--. "
print " | | -__| | | _| _ | _ |_ _| "
print " |__|__|_____|_____|__| |_____|_____|__.__| "
print " "
print " Configuration decrypter 04/07/2013 "
print " "
# Vérifier que les bons arguments ont été renseignés.
if len(argv) != 4 or ('--decrypt' not in argv and '--encrypt' not in argv):
print 'Usage: %s [--decrypt|--encrypt] <infile> <outfile>' % argv[0]
print 'Examples: %s --decrypt nb6_1371236872.conf user.xml' % argv[0]
print ' %s --encrypt user.xml nb6_1371236872.conf' % argv[0]
exit('')
# Fonctions pour afficher les informations dans la console.
def info(text):
print '\033[37m' + '[+] ' + '\033[0m' + text
def error(text):
print '\033[31m' + '[!] ' + '\033[0m' + text
exit('')
#################
# Déchiffrement #
#################
if '--decrypt' in argv:
argv.remove('--decrypt')
# Lecture du fichier
try:
infile_fd = open(argv[1], 'r')
infile = infile_fd.read()
infile_fd.close()
except:
error('Impossible d\'ouvrir le fichier "' + argv[1] + '"')
# Déchiffrement du fichier de configuration
decrypted = AES.new('fa93550849a8a238', AES.MODE_CBC, '9d918ecc1d14767c').decrypt(infile)
# Vérification du checksum
if md5(decrypted[:-16]).digest() != decrypted[-16:]:
error('Checkum MD5 invalide, abandon')
else:
info('Checksum MD5 valide')
# Écriture du fichier déchiffré
try:
outfile = open(argv[2], 'w')
except:
error('Impossible d\'ouvrir le fichier "' + argv[2] + '"')
outfile.write(decrypted[:-16].strip('\x00'))
outfile.close()
info('Fichier de configuration déchiffré dans "' + argv[2] + '"')
###############
# Chiffrement #
###############
elif '--encrypt' in argv:
argv.remove('--encrypt')
# Lecture du fichier
try:
infile_fd = open(argv[1], 'r')
infile = infile_fd.read()
infile_fd.close()
except:
error('Impossible d\'ouvrir le fichier "' + argv[1] + '"')
# Ajout du padding et du checksum
infile += '\x00' * ((16 - len(infile) % 16) % 16)
infile += md5(infile).digest()
# Chiffrement du fichier de configuration
encrypted = AES.new('fa93550849a8a238', AES.MODE_CBC, '9d918ecc1d14767c').encrypt(infile)
# Écriture du fichier chiffré
try:
outfile = open(argv[2], 'w')
except:
error('Impossible d\'ouvrir le fichier "' + argv[2] + '"')
outfile.write(encrypted)
outfile.close()
info('Fichier de configuration chiffré dans "' + argv[2] + '"')
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment