Skip to content

Commit 929248e

Browse files
committed
initial commit
0 parents  commit 929248e

9 files changed

Lines changed: 634 additions & 0 deletions

File tree

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# General
2+
.DS_Store
3+
4+
# Thumbnails
5+
._*
6+
7+
# Files that might appear in the root of a volume
8+
.DocumentRevisions-V100
9+
.fseventsd
10+
.Spotlight-V100
11+
.TemporaryItems
12+
.Trashes
13+
.VolumeIcon.icns
14+
.com.apple.timemachine.donotpresent
15+
16+
# Useless project files
17+
.zed
18+
*.dSYM

Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
SYS_ARCH := $(shell uname -m)
2+
3+
CC = clang
4+
CFLAGS = -Iinclude -O3 -Wall
5+
6+
SRC = src/memory.c src/tinyhook.c
7+
OBJ = $(SRC:.c=.o)
8+
LIB = lib/libtinyhook.a
9+
TST = tests/example
10+
11+
ifeq ($(SYS_ARCH), x86_64)
12+
OBJ += src/fde64/fde64.o
13+
endif
14+
15+
build: $(LIB)
16+
17+
test: $(TST)
18+
19+
all: $(LIB) $(TST)
20+
21+
$(LIB): $(OBJ)
22+
ar rcs $@ $^
23+
24+
%.o: %.c
25+
$(CC) $(CFLAGS) -c $< -o $@
26+
27+
tests/example: tests/example.c $(LIB)
28+
$(CC) $(CFLAGS) -ltinyhook -Llib $< -o $@
29+
30+
.PHONY: clean
31+
clean:
32+
rm -f $(LIB) $(TST) src/memory.o src/tinyhook.o

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# tinyhook
2+
3+
A tiny macOS inline hook framework
4+
5+
**Now support both ARM64 and x86_64!**
6+
7+
## functions
8+
9+
### read and write memory
10+
11+
```c
12+
int read_mem(void *destnation, const void *source, size_t len);
13+
int write_mem(void *destnation, const void *source, size_t len);
14+
```
15+
16+
### insert a jump at an address
17+
18+
`tiny_insert()` uses `bl` or `b` (depends on `link` flag) on ARM64, and `jmp` or `call` on x86_64
19+
20+
`tiny_isnert_far()` uses `adrp` + `blr`/`br` on ARM64, and `jmp` or call on x86_64
21+
22+
```c
23+
int tiny_insert(void *address, void *destnation, bool link);
24+
int tiny_insert_far(void *address, void *destnation, bool link);
25+
```
26+
27+
### inline hook
28+
29+
`origin` can be `NULL`
30+
31+
```c
32+
int tiny_hook(void *function, void *destnation, void **origin);
33+
```
34+
35+
## examples
36+
37+
```c
38+
#include <stdio.h>
39+
#include <mach-o/dyld.h>
40+
41+
#include "tinyhook.h"
42+
43+
uint32_t (*orig_dyld_image_count)(void);
44+
uint32_t my_dyld_image_count(void) {
45+
uint32_t image_count = orig_dyld_image_count();
46+
printf("hooked _dyld_image_count: %d!\n", image_count);
47+
return 5;
48+
}
49+
50+
int main() {
51+
int image_count = _dyld_image_count();
52+
for (int i = 0; i < image_count; i++) {
53+
printf("image[%d]: %s\n", i, _dyld_get_image_name(i));
54+
}
55+
56+
tiny_hook(_dyld_image_count, my_dyld_image_count, (void **)&orig_dyld_image_count);
57+
58+
int image_count = _dyld_image_count();
59+
for (int i = 0; i < image_count; i++) {
60+
printf("image[%d]: %s\n", i, _dyld_get_image_name(i));
61+
}
62+
return 0;
63+
}
64+
```
65+
66+
## references
67+
68+
Thanks to these projects for their inspiring idea and code!
69+
70+
- https://github.com/rodionovd/rd_route
71+
- https://github.com/GiveMeZeny/fde64

include/tinyhook.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef tinyhook_h
2+
#define tinyhook_h
3+
4+
#include <stddef.h>
5+
#include <stdbool.h>
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
int read_mem(void *destnation, const void *source, size_t len);
12+
13+
int write_mem(void *destnation, const void *source, size_t len);
14+
15+
int tiny_hook(void *function, void *destnation, void **origin);
16+
17+
int tiny_insert(void *address, void *destnation, bool link);
18+
19+
int tiny_insert_far(void *address, void *destnation, bool link);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
25+
#endif

0 commit comments

Comments
 (0)