Skip to content
/ chook Public

A new approach to hook C functions by using dyld info.

License

Notifications You must be signed in to change notification settings

LucasXu0/chook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chook

A new approach to hook lazy binding functions by using dyld info.

Usage

Objective-C or C

#include "simple_chook.h"

static char* (*ori_getenv)(const char *);

char* my_getenv(const char *s)
{
    printf("🚀 chook bigo!\n");
    return ori_getenv(s);
}

void c_hook_func(const struct mach_header *header, intptr_t slide)
{
    c_hook(header, slide, "getenv", my_getenv, (void *)&ori_getenv);
}

void reset_c_hook_func(const struct mach_header *header, intptr_t slide)
{
    reset_c_hook(header, slide, "getenv");
}

int main(int argc, char * argv[])
{
    _dyld_register_func_for_add_image(c_hook_func);

    printf("path = %s\n", getenv("PATH"));

    _dyld_register_func_for_add_image(reset_c_hook_func);

    printf("path = %s\n", getenv("PATH"));
}

// output
// 🚀 chook bigo!
// path = path = /usr/bin:/bin:/usr/sbin:/sbin
// path = path = /usr/bin:/bin:/usr/sbin:/sbin

Swift

import Foundation

//_$sSa6appendyyxnF

typealias AppendType = @convention(c) (__owned Int) -> Void
var myAppend: AppendType = { e in
    print("🚀 bigo!")
}
var replacement = unsafeBitCast(myAppend, to: UnsafeMutableRawPointer.self)
var replaced: UnsafeMutableRawPointer?

var arr = [1]

arr.append(2)

_dyld_register_func_for_add_image { (header, slide) in
    c_hook(header, slide, "$sSa6appendyyxnF", replacement, &replaced)
}

arr.append(3)

// output
// 🚀 bigo!

How it works

hook lazy binding function

chook.png

reset lazying bound function

chook.png

TODO

  • reset hooked C functions.
  • support Swift completely.
  • support hook C functions in non-lazy symbol.
  • support more simple API.
  • support hook multiple C functions in one line call.

About

A new approach to hook C functions by using dyld info.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages