diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-15 19:38:05 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-15 19:38:05 +0300 |
| commit | 0be46c952901aaafe76e1fb5a8faecb38323d549 (patch) | |
| tree | 9ec9e7a2c99bd5cd81e17f1578a7be725558f88e | |
| parent | Initial commit (diff) | |
| download | linux-game-trainer-0be46c952901aaafe76e1fb5a8faecb38323d549.tar.gz linux-game-trainer-0be46c952901aaafe76e1fb5a8faecb38323d549.zip | |
Attaching & Detaching from process
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Makefile | 13 | ||||
| -rw-r--r-- | src/main.c | 34 |
3 files changed, 49 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc22e3a --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | *~ | ||
| 2 | *.out | ||
| 3 | *.o \ No newline at end of file | ||
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..844cc7b --- /dev/null +++ b/Makefile | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | CC = gcc | ||
| 2 | CFLAGS = -Wall -g | ||
| 3 | OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c)) | ||
| 4 | OUT = hack.out | ||
| 5 | |||
| 6 | $(OUT): $(OBJS) | ||
| 7 | $(CC) $(CFLAGS) -o $@ $(OBJS) | ||
| 8 | |||
| 9 | %.o: %.c | ||
| 10 | $(CC) $(CFLAGS) -c $? -o $@ | ||
| 11 | |||
| 12 | clean: | ||
| 13 | rm -f src/*.o $(OUT) | ||
| @@ -1,6 +1,38 @@ | |||
| 1 | #include <stdlib.h> | ||
| 1 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <sys/ptrace.h> | ||
| 4 | #include <sys/wait.h> | ||
| 5 | #include <unistd.h> | ||
| 6 | |||
| 7 | #define ERROR(...) \ | ||
| 8 | do { \ | ||
| 9 | fprintf(stderr, __VA_ARGS__); \ | ||
| 10 | exit(1); \ | ||
| 11 | } while (0) | ||
| 12 | |||
| 13 | #define LOG(...) \ | ||
| 14 | fprintf(stderr, __VA_ARGS__) | ||
| 2 | 15 | ||
| 3 | int main(int argc, char *argv[]) | 16 | int main(int argc, char *argv[]) |
| 4 | { | 17 | { |
| 5 | 18 | if (argc < 2) { | |
| 19 | ERROR("Usage: %s <tracee_pid>\n", argv[0]); | ||
| 20 | } | ||
| 21 | |||
| 22 | int pid; | ||
| 23 | if ((pid = atoi(argv[1])) == 0) { | ||
| 24 | ERROR("Invalid pid '%s'\n", argv[1]); | ||
| 25 | } | ||
| 26 | |||
| 27 | ptrace(PTRACE_ATTACH, pid, NULL, NULL); | ||
| 28 | waitpid(pid, NULL, __WALL); | ||
| 29 | LOG("Attached to process %d\n", pid); | ||
| 30 | |||
| 31 | /* Do stuff ... */ | ||
| 32 | sleep(5); | ||
| 33 | |||
| 34 | ptrace(PTRACE_DETACH, pid, NULL, NULL); | ||
| 35 | LOG("Detached from process %d\n", pid); | ||
| 36 | |||
| 37 | return 0; | ||
| 6 | } | 38 | } |
