(path, what, value)
Replace a given name (what) in any LC_LOAD_DYLIB command found in
    the given binary with a new name (value), provided it's shorter.
Replace a given name (what) in any LC_LOAD_DYLIB command found in
    the given binary with a new name (value), provided it's shorter.
Replace a given name ( what ) in any LC_LOAD_DYLIB command found in the given binary with a new name ( value ) provided it s shorter .
def mach_o_change(path, what, value):
    """
    Replace a given name (what) in any LC_LOAD_DYLIB command found in
    the given binary with a new name (value), provided it's shorter.
    """

    def do_macho(file, bits, endian):
        # Read Mach-O header (the magic number is assumed read by the caller)
        cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags = read_data(file, endian, 6)
        # 64-bits header has one more field.
        if bits == 64:
            read_data(file, endian)
        # The header is followed by ncmds commands
        for n in range(ncmds):
            where = file.tell()
            # Read command header
            cmd, cmdsize = read_data(file, endian, 2)
            if cmd == LC_LOAD_DYLIB:
                # The first data field in LC_LOAD_DYLIB commands is the
                # offset of the name, starting from the beginning of the
                # command.
                name_offset = read_data(file, endian)
                file.seek(where + name_offset, os.SEEK_SET)
                # Read the NUL terminated string
                load = file.read(cmdsize - name_offset).decode()
                load = load[:load.index('\0')]
                # If the string is what is being replaced, overwrite it.
                if load == what:
                    file.seek(where + name_offset, os.SEEK_SET)
                    file.write(value.encode() + '\0'.encode())
            # Seek to the next command
            file.seek(where + cmdsize, os.SEEK_SET)

    def do_file(file, offset=0, size=maxint):
        file = fileview(file, offset, size)
        # Read magic number
        magic = read_data(file, BIG_ENDIAN)
        if magic == FAT_MAGIC:
            # Fat binaries contain nfat_arch Mach-O binaries
            nfat_arch = read_data(file, BIG_ENDIAN)
            for n in range(nfat_arch):
                # Read arch header
                cputype, cpusubtype, offset, size, align = read_data(file, BIG_ENDIAN, 5)
                do_file(file, offset, size)
        elif magic == MH_MAGIC:
            do_macho(file, 32, BIG_ENDIAN)
        elif magic == MH_CIGAM:
            do_macho(file, 32, LITTLE_ENDIAN)
        elif magic == MH_MAGIC_64:
            do_macho(file, 64, BIG_ENDIAN)
        elif magic == MH_CIGAM_64:
            do_macho(file, 64, LITTLE_ENDIAN)

    assert(len(what) >= len(value))
    do_file(open(path, 'r+b'))
def mach_o_change ( path , what , value ) : def do_macho ( file , bits , endian ) : # Read Mach-O header (the magic number is assumed read by the caller) cputype , cpusubtype , filetype , ncmds , sizeofcmds , flags = read_data ( file , endian , 6 ) # 64-bits header has one more field. if bits == 64 : read_data ( file , endian ) # The header is followed by ncmds commands for n in range ( ncmds ) : where = file . tell ( ) # Read command header cmd , cmdsize = read_data ( file , endian , 2 ) if cmd == LC_LOAD_DYLIB : # The first data field in LC_LOAD_DYLIB commands is the # offset of the name, starting from the beginning of the # command. name_offset = read_data ( file , endian ) file . seek ( where + name_offset , os . SEEK_SET ) # Read the NUL terminated string load = file . read ( cmdsize - name_offset ) . decode ( ) load = load [ : load . index ( '\0' ) ] # If the string is what is being replaced, overwrite it. if load == what : file . seek ( where + name_offset , os . SEEK_SET ) file . write ( value . encode ( ) + '\0' . encode ( ) ) # Seek to the next command file . seek ( where + cmdsize , os . SEEK_SET ) def do_file ( file , offset = 0 , size = maxint ) : file = fileview ( file , offset , size ) # Read magic number magic = read_data ( file , BIG_ENDIAN ) if magic == FAT_MAGIC : # Fat binaries contain nfat_arch Mach-O binaries nfat_arch = read_data ( file , BIG_ENDIAN ) for n in range ( nfat_arch ) : # Read arch header cputype , cpusubtype , offset , size , align = read_data ( file , BIG_ENDIAN , 5 ) do_file ( file , offset , size ) elif magic == MH_MAGIC : do_macho ( file , 32 , BIG_ENDIAN ) elif magic == MH_CIGAM : do_macho ( file , 32 , LITTLE_ENDIAN ) elif magic == MH_MAGIC_64 : do_macho ( file , 64 , BIG_ENDIAN ) elif magic == MH_CIGAM_64 : do_macho ( file , 64 , LITTLE_ENDIAN ) assert ( len ( what ) >= len ( value ) ) do_file ( open ( path , 'r+b' ) )