-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 929248e
Showing
9 changed files
with
634 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# General | ||
.DS_Store | ||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Useless project files | ||
.zed | ||
*.dSYM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
SYS_ARCH := $(shell uname -m) | ||
|
||
CC = clang | ||
CFLAGS = -Iinclude -O3 -Wall | ||
|
||
SRC = src/memory.c src/tinyhook.c | ||
OBJ = $(SRC:.c=.o) | ||
LIB = lib/libtinyhook.a | ||
TST = tests/example | ||
|
||
ifeq ($(SYS_ARCH), x86_64) | ||
OBJ += src/fde64/fde64.o | ||
endif | ||
|
||
build: $(LIB) | ||
|
||
test: $(TST) | ||
|
||
all: $(LIB) $(TST) | ||
|
||
$(LIB): $(OBJ) | ||
ar rcs $@ $^ | ||
|
||
%.o: %.c | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
tests/example: tests/example.c $(LIB) | ||
$(CC) $(CFLAGS) -ltinyhook -Llib $< -o $@ | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f $(LIB) $(TST) src/memory.o src/tinyhook.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# tinyhook | ||
|
||
A tiny macOS inline hook framework | ||
|
||
**Now support both ARM64 and x86_64!** | ||
|
||
## functions | ||
|
||
### read and write memory | ||
|
||
```c | ||
int read_mem(void *destnation, const void *source, size_t len); | ||
int write_mem(void *destnation, const void *source, size_t len); | ||
``` | ||
### insert a jump at an address | ||
`tiny_insert()` uses `bl` or `b` (depends on `link` flag) on ARM64, and `jmp` or `call` on x86_64 | ||
`tiny_isnert_far()` uses `adrp` + `blr`/`br` on ARM64, and `jmp` or call on x86_64 | ||
```c | ||
int tiny_insert(void *address, void *destnation, bool link); | ||
int tiny_insert_far(void *address, void *destnation, bool link); | ||
``` | ||
|
||
### inline hook | ||
|
||
`origin` can be `NULL` | ||
|
||
```c | ||
int tiny_hook(void *function, void *destnation, void **origin); | ||
``` | ||
## examples | ||
```c | ||
#include <stdio.h> | ||
#include <mach-o/dyld.h> | ||
#include "tinyhook.h" | ||
uint32_t (*orig_dyld_image_count)(void); | ||
uint32_t my_dyld_image_count(void) { | ||
uint32_t image_count = orig_dyld_image_count(); | ||
printf("hooked _dyld_image_count: %d!\n", image_count); | ||
return 5; | ||
} | ||
int main() { | ||
int image_count = _dyld_image_count(); | ||
for (int i = 0; i < image_count; i++) { | ||
printf("image[%d]: %s\n", i, _dyld_get_image_name(i)); | ||
} | ||
tiny_hook(_dyld_image_count, my_dyld_image_count, (void **)&orig_dyld_image_count); | ||
int image_count = _dyld_image_count(); | ||
for (int i = 0; i < image_count; i++) { | ||
printf("image[%d]: %s\n", i, _dyld_get_image_name(i)); | ||
} | ||
return 0; | ||
} | ||
``` | ||
|
||
## references | ||
|
||
Thanks to these projects for their inspiring idea and code! | ||
|
||
- https://github.com/rodionovd/rd_route | ||
- https://github.com/GiveMeZeny/fde64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef tinyhook_h | ||
#define tinyhook_h | ||
|
||
#include <stddef.h> | ||
#include <stdbool.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
int read_mem(void *destnation, const void *source, size_t len); | ||
|
||
int write_mem(void *destnation, const void *source, size_t len); | ||
|
||
int tiny_hook(void *function, void *destnation, void **origin); | ||
|
||
int tiny_insert(void *address, void *destnation, bool link); | ||
|
||
int tiny_insert_far(void *address, void *destnation, bool link); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
Oops, something went wrong.