Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can fishhook hook c function written by ourself? #25

Closed
intheway opened this issue Mar 7, 2016 · 2 comments
Closed

can fishhook hook c function written by ourself? #25

intheway opened this issue Mar 7, 2016 · 2 comments

Comments

@intheway
Copy link

intheway commented Mar 7, 2016

myFunction is a c function written by me in my project, I want to hook it, but I tried this code failed.

int myFunction(int a){
    return a+1;
}

static int (*orig_myFunction)(int);

int hook_myFunction(int a){
    return a+2;
}

int main(int argc, char * argv[])
{
    @autoreleasepool {
        rebind_symbols((struct rebinding[1]){{"myFunction", hook_myFunction, (void *)&orig_myFunction}}, 1);
        printf("%d\n" , myFunction(1));
       return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

This means fishhook can only hook system api or how to hook c function written by ourself ?
Thanks.

@fpotter
Copy link

fpotter commented Mar 7, 2016

fishhook can only hook functions that exist in other libraries. It cannot
hook functions that exist in the same image (library or executable) as your
currently running code.

The reason for this is that there's no indirection that happens when you
call a function in your own executable. It's just a plain jump to another
code address in your executable.

That's very different from calling a function in an external library, where
your executable uses dyld to figure out the address of the function being
called before jumping to it.

On Sun, Mar 6, 2016 at 10:59 PM, Zhengwei Yin notifications@github.com
wrote:

myFunction is a c function written by me in my project, I want to hook
it, but I tried this code failed.

int myFunction(int a){
return a+1;
}

static int (*orig_myFunction)(int);

int hook_myFunction(int a){
return a+2;
}

int main(int argc, char * argv[])
{
@autoreleasepool {
rebind_symbols((struct rebinding[1]){{"myFunction", hook_myFunction, (void *)&orig_myFunction}}, 1);
printf("%d\n" , myFunction(1));
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

This means fishhook can only hook system api or how to hook c function
written by ourself ?
Thanks.


Reply to this email directly or view it on GitHub
#25.

@intheway
Copy link
Author

intheway commented Mar 7, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants