Un script qui permet d'afficher ou convertir de l'hexa, dans plusieurs format utiles - pratique dans les challenges ;-)
0x01. SYNOPSIS
Usage: xx <-H|-R|-D|-C|-I> <-s string|-f file> [-o file] [-w width] -s : string to encode -f : file to encode -o : file to write output -w : maximum width of result -H : \x20\x00\xa0\xce\x49\x20\x00\x00\x11\x36\x02\x45\xb2 \xf7\x30\x11 -X : 2000a0ce4920000011360245b2f73011 -C : '\x20','\x00','\xa0','\xce','\x49','\x20','\x00','\x00','\x11', '\x36','\x02','\x45','\xb2','\xf7','\x30','\x11', -I : 0x20, 0x00, 0xa0, 0xce, 0x49, 0x20, 0x00, 0x00, 0x11, 0x36, 0x02, 0x45, 0xb2, 0xf7, 0x30, 0x11, -D : 32016020673320017542691782474817 (base10 numeric display) -M : CHAR(32,01,60,20,67,33,20,01,75,42,69,17,82,47,48,17) (mysql numeric display) -R : ceci est un test
0x02. SCRIPT
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Imports # # sys.exit() import sys # stdin from sys import stdin,stdout # getopt.getopt from getopt import getopt # base64.encodestring import base64 # # Variables globales # # Largeur maximale à l'affichage WIDTH=64 # Nom de l'application APP="xx" # Fonction ...: <string> xor(arg<string>,format<string>) # # Variables ..: arg= Données à chiffrer, format= Format d'affichage # # Retourne ...: hexed= Données encodées # # Description : Formate l'affichage # # ----------------------------------------------------------------------------- # def xx(arg,fmt): hexed = "" for i in range(0,len(arg)): res = ord(arg[i]) if i % WIDTH == 0 and i > 0: hexed = hexed + "\n" # Conversion en hexadécimal : de 00 à ff # On extrait la valeur pour mieux re-formater l'affichage if fmt[0:3] == "hex": res = hex(res).replace("0x","") if len(res) < 2: res = "0" + res if fmt[-1] == "1": hexed = hexed + "\\x" + res if fmt[-1] == "2": hexed = hexed + res if fmt[-1] == "3": hexed = hexed + "'\\x" + res + "'," if fmt[-1] == "4": hexed = hexed + " 0x" + res + "," if fmt == "raw": hexed = hexed + chr(res) # Conversion en binaire : de 000 à 255 if fmt == "digit" or fmt == "mysql": dec = str( int(res) ) if len(dec) == 2: dec = "0" + dec if len(dec) == 1: dec = "00" + dec if fmt == "digit": hexed = hexed + dec if fmt == "mysql": if i == 0: hexed = "CHAR(" hexed = hexed + dec + ", " if i == len(arg)-1: hexed = hexed + "\b\b)" return(hexed) # Fonction ...: <void> usage(err<int>) # # Variables ..: err= Numéro de code d'erreur de sortie du programme via exit() # # Retourne ...: - # # Description : Affichage de l'aide d'utilisation # # ----------------------------------------------------------------------------- # def usage(err): print """ Usage: %s <-H|-R|-D|-C|-I> <-s string|-f file> [-o file] [-w width] -s : string to encode -f : file to encode -o : file to write output -w : maximum width of result -H : \\x20\\x00\\xa0\\xce\\x49\\x20\\x00\\x00\\x11\\x36\\x02\\x45\\xb2 \\xf7\\x30\\x11 -X : 2000a0ce4920000011360245b2f73011 -C : '\x20','\x00','\xa0','\xce','\x49','\x20','\x00','\x00','\x11', '\x36','\x02','\x45','\xb2','\xf7','\x30','\x11', -I : 0x20, 0x00, 0xa0, 0xce, 0x49, 0x20, 0x00, 0x00, 0x11, 0x36, 0x02, 0x45, 0xb2, 0xf7, 0x30, 0x11, -D : 32016020673320017542691782474817 (base10 numeric display) -M : CHAR(32,01,60,20,67,33,20,01,75,42,69,17,82,47,48,17) (mysql numeric display) -R : ceci est un test """ % APP sys.exit(err) # Fonction ...: <void> error(err<int>) # # Variables ..: descr= description à afficher # err= Numéro de code d'erreur de sortie du programme via exit() # # Retourne ...: Sorts avec le code d'erreur fourni # # Description : Affichage d'un message d'erreur et sort du programme # # ----------------------------------------------------------------------------- # def error(descr,err): print "\nError: " + descr + "\n" sys.exit(err) # Variables ..: Les paramètres cités ci-dessus # # Retourne ...: Si la sortie est dans un fichier: : rien # Sinon affiche le résultat au format sélectionné # # Description : Programme principal # # ----------------------------------------------------------------------------- # if __name__ == '__main__': arg = "" out = "" target = "" format = "" operation = "" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # * On essaye de récupérer les arguments passés ... # try: opts,args = getopt(sys.argv[1:],"XMCHRDIs:f:ho:w:") for o,a in opts: if o == "-H": format="hex1" if o == "-X": format="hex2" if o == "-C": format="hex3" if o == "-I": format="hex4" if o == "-R": format="raw" if o == "-D": format="digit" if o == "-M": format="mysql" if o == "-w": WIDTH=int(a) if o == "-o": out=a # Seule l'option -s ou ... if o == "-s": if target: usage target="string" arg = a # ... l'option -f doit être choisie if o == "-f": if target: usage target="file" arg = a if o == "-h": usage(0) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # * .. sinon on y arrive pas, on affichage l'aide # except: error("error parsing arguments",1) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Si les arguments obligatoires ne sont pas définis # on affiche l'aide # if format == "": error("no format specified",1) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Si les arguments obligatoires ne sont pas définis # on affiche l'aide # if target == "" and arg != "": error("no target specified",1) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # On récupère depuis le stdin # if arg == "": target = "string" arg = "" try: c = stdin.read(1) while c: arg += c c = stdin.read(1) except KeyboardInterrupt: stdout.flush() print "\n" pass # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Si les données à chiffrer est un fichier, on récupère son contenu # if target == "file": # On essaie de lire le fichier (il peut être binaire) try: fh = open(arg,"rb") buff = fh.read() fh.close() except: error("unable to read input file",1) # Si ça n'est pas un fichier, et que ça n'est pas une chaine de caractère # (cas improbable), on gère l'erreur et on affiche l'aide elif target != "string": error("this is not a valid string",2) # On récupère l'argument passé en entrée else: buff = arg # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Opérations commune à toutes les options # # [2] -> L'opération de chiffrement est commune à toutes les options hexed = xx(buff,format) # [3] -> L'opération d'écriture dans un fichiers if out == "": print hexed else: try: fh = open(out,"wb") fh.write(hexed) fh.close() except: error("unable to write output file",1)
=> Écrit par : Nicolas, le 14 juin 2016