[Python] Modify hex in a binary

Download | Vote Up (1) | Vote Down (0)
#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])




fr0g


Be the first to give feedback !

Please login to comment !