N-PN White-Hat Project
[Python] Modify Hex in a binary - Version imprimable

+- N-PN White-Hat Project (https://dev.n-pn.fr/forum)
+-- Forum : Programmation (https://dev.n-pn.fr/forum/forumdisplay.php?fid=72)
+--- Forum : Langages interprétés (https://dev.n-pn.fr/forum/forumdisplay.php?fid=27)
+--- Sujet : [Python] Modify Hex in a binary (/showthread.php?tid=2124)



[Python] Modify Hex in a binary - fr0g - 04-08-2012

Source : http://orgasm.re/article-89--Python-Modify-hex-in-a-binary.html

Code PYTHON :

#coding=utf-8
 
import sys
 
 
##
# Première ébauche de code
# afin de modifier la chaine hexadecimale correspondant a une instruction
# ASM, (code à modifier, afin de gérer les programmes plus gros où peuvent
# se trouver plusieurs itérations de la même séquence Hexa'.
#
# (Pour cette version, il est conseillé de donner une serie de opcodes plus large
# que celle de l'instruction à proprement parlé.)
##
 
 
 
 
##
# generation de opcodes à partir du String entré par l'utilisateur
##
def opcode_gen(x):
 
        buf = ""
        finaly_str = ""
        i = 0
       
        for lettre in x:
                buf += lettre
                if (i % 2) != 0:
                        buf = chr(int(buf,16))
                        finaly_str += buf                      
                        buf = ""                          
                i +=1
 
       
        return finaly_str
 
 
 
##
# Recherches modifications & copie du code
##
def make_crack(prog, initial, final):
 
        f_read = open(prog,"rb")
        data = f_read.read()
       
        if (opcode_gen(initial) in data):
                print "[*] Instruction found"
 
                data = data.replace(opcode_gen(initial),opcode_gen(final),1)
 
                name = raw_input(" Name of the new cracked binary : ")
                f = open(name,"wb")
                f.write(data)
                f.close()
                f_read.close()
 
                print "[*] Succes !!!"
        else:
                print "[!] Error : Instruction not found"
 
 
 
##
# Fonction main()
##
def main(prog):
        print "[*] Binary name : " + prog
        i = raw_input(" Find (exmpl : 7404) : ")
        f = raw_input(" Replace by : ")
        if (i != "" and f != ""):
                make_crack(prog, i, f)
        else:
                print "[!] Error : NULL ?"
 
##
# Header
##
def header():
        print """
---------------------------------
-App : PatchR
-Powered by HWC-CREW
-Butterfly Project
-Desc : Replace opcodes in a binary (to change instructions)
---------------------------------
        """

 
 
 
if (len(sys.argv) < 2):
        print "[!] Error : please specify a binary file"
        print "[*] Use : ./patchR [binary_name]"
 
else:
        header()
        main(sys.argv[1])
 

 



RE: [Python] Modify Hex in a binary - sakiir - 08-11-2012

Intéressant !!! je vais m'en servir pour continuer mon programme !! merci copain Big Grin