Jaffer Sheriff Jaffer Sheriff - 3 months ago 13
Objective-C Question

Creating a software patcher in mac

I'm an IOS developer and I know objective C. I wanna to create a stand alone mac app whose sole functionality is to patch another app available in same mac.

Lets say I have an app called X in my applications folder. This app X has some undesired behaviour. So I tried to modify this behaviour. I analysed the app's executable with the help of Hopper disassembler, I came to know that I have to change assembly instructions starting at

00000001003e3790
. I changed those assembly instructions and produced the new executable. Then I replaced the old one with new executable and then that undesired behaviour now seems to be gone.

As most people would love to remove this undesired behaviour, I decided to write a patcher and distribute that patcher to them.

So how can I modify assembly instructions available inside the executable of app X in my patcher app then replace the original one with my modified version ?

It would be great if someone help me in right direction.

Answer Source
  1. In general, you should ask the user for the location of the app bundle, in case it can`t be found in /Applications/.
  2. You need to check whether the target executable inside that bundle has the same hash (it may be CRC, MD5, SHA — you name it) as the executable you had before patching it.
  3. If the hashes match, then you are to open the file for writing and seek for the pre-hardcoded place where the wrong instructions are stored; you can determine that place by searching the patched file in a hex-editor for a long enough byte string beginning with your patched bytes.
  4. And finally, you are to rewrite (a.k.a. patch) the target bytes with yours and close the file.

[UPD.] Example code for [3].

This does not require any ObjC-related mechanisms, and can be built and run using only the plain libc:

long PatchSomething(char *name, char *data, size_t offs, size_t size) {
    long file = open(name, O_WRONLY);
    if (file != -1) {
        lseek(file, offs, SEEK_SET);
        write(file, data, size);
        close(file);
    }
    return file != -1;
}

where:

  • name is the name of the file to patch
  • data is the data to be written
  • offs is the file offset where the data shall be put
  • size is the data size; exactly size of the old bytes in the file would get rewritten