Created
July 7, 2013 07:54
-
-
Save Xavier75/5942740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#-*- encoding: Utf-8 -*- | |
from hashlib import md5 | |
from re import match | |
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 " Diagnostic decrypter 07/07/2013 " | |
print " " | |
# Vérifier que les bons arguments ont été renseignés. | |
if not (('--decrypt' in argv and len(argv) == 4) or ('--encrypt' in argv and len(argv) == 5)): | |
print 'Usage: %s --decrypt <infile> <outfile>' % argv[0] | |
print ' %s --encrypt <macaddr> <infile> <outfile>' % argv[0] | |
print 'Examples: %s --decrypt rapport_diag_nb6_1371236872 decrypted_diag' % argv[0] | |
print ' %s --encrypt e0:a1:d7:12:34:56 decrypted_diag rapport_diag_nb6_1371236872' % 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('') | |
###################### | |
# Fonctions communes # | |
###################### | |
def nb_encode(before): | |
after = '' | |
for i in before: | |
after += chr(ord(i) >> 4) | |
after += chr(ord(i) & 0xf) | |
return after | |
def check_macaddr(macaddr): | |
if len(macaddr) == 16: | |
macaddr = macaddr[:8] + ':' + macaddr[8:] | |
regex = '^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$' | |
if not match(regex, macaddr): | |
error('Adresse MAC invalide : ' + repr(macaddr)) | |
return macaddr | |
################# | |
# Déchiffrement # | |
################# | |
if '--decrypt' in argv: | |
argv.remove('--decrypt') | |
# Lecture du fichier | |
try: | |
infile_fd = open(argv[1], 'r') | |
infile = infile_fd.read() | |
except: | |
error('Impossible d\'ouvrir le fichier "' + argv[1] + '"') | |
# Déchiffrement avec la 1ère clé | |
decrypted = AES.new('a7e821fa91253b27', AES.MODE_CBC, '9d37b43294574aed').decrypt(infile) | |
# Déchiffrement avec la 2ème clé | |
macaddr = decrypted[:16] | |
info('Adresse MAC : ' + check_macaddr(macaddr)) | |
key2 = nb_encode(md5(macaddr[:8]).digest())[:16] | |
iv2 = nb_encode(md5(macaddr).digest())[:16] | |
decrypted = AES.new(key2, AES.MODE_CBC, iv2).decrypt(decrypted[16:]) | |
# Vérifier que le dernier bloc est valide (il doit être identique à | |
# l'avant-dernier mais chiffré deux fois) | |
infile_fd.seek(-32, 2) | |
avantDernier = infile_fd.read(16) | |
dernier = decrypted[-16:] | |
if dernier != avantDernier: | |
error('Dernier bloc invalide') | |
# É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() | |
infile_fd.close() | |
info('Diagnostic déchiffré dans "' + argv[2] + '"') | |
############### | |
# Chiffrement # | |
############### | |
elif '--encrypt' in argv: | |
argv.remove('--encrypt') | |
# Lecture du fichier | |
try: | |
infile_fd = open(argv[2], 'r') | |
infile = infile_fd.read() | |
except: | |
error('Impossible d\'ouvrir le fichier "' + argv[2] + '"') | |
# Ajout de padding | |
infile += '\x00' * ((16 - len(infile) % 16) % 16) | |
# Chiffrement avec la 2ème clé | |
macaddr = argv[1].strip().lower() | |
macaddr = macaddr[:8] + macaddr[9:] | |
check_macaddr(macaddr) | |
key2 = nb_encode(md5(macaddr[:8]).digest())[:16] | |
iv2 = nb_encode(md5(macaddr).digest())[:16] | |
aes2 = AES.new(key2, AES.MODE_CBC, iv2) | |
encrypted = aes2.encrypt(infile) | |
# Chiffrement avec la 1ère clé | |
aes1 = AES.new('a7e821fa91253b27', AES.MODE_CBC, '9d37b43294574aed') | |
encrypted = aes1.encrypt(macaddr + encrypted) | |
# Génération du dernier bloc | |
dernier = encrypted[-16:] | |
dernier = aes2.encrypt(dernier) | |
dernier = aes1.encrypt(dernier) | |
encrypted += dernier | |
# Écriture du fichier déchiffré | |
try: | |
outfile = open(argv[3], 'w') | |
except: | |
error('Impossible d\'ouvrir le fichier "' + argv[3] + '"') | |
outfile.write(encrypted) | |
outfile.close() | |
infile_fd.close() | |
info('Diagnostic chiffré dans "' + argv[3] + '"') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment