Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Antibioticss committed Aug 11, 2024
0 parents commit 929248e
Show file tree
Hide file tree
Showing 9 changed files with 634 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
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
32 changes: 32 additions & 0 deletions Makefile
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
71 changes: 71 additions & 0 deletions README.md
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
25 changes: 25 additions & 0 deletions include/tinyhook.h
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
Loading

0 comments on commit 929248e

Please sign in to comment.