From 0be46c952901aaafe76e1fb5a8faecb38323d549 Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Mon, 15 Apr 2024 19:38:05 +0300 Subject: Attaching & Detaching from process --- .gitignore | 3 +++ Makefile | 13 +++++++++++++ src/main.c | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc22e3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +*.out +*.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 @@ +CC = gcc +CFLAGS = -Wall -g +OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c)) +OUT = hack.out + +$(OUT): $(OBJS) + $(CC) $(CFLAGS) -o $@ $(OBJS) + +%.o: %.c + $(CC) $(CFLAGS) -c $? -o $@ + +clean: + rm -f src/*.o $(OUT) diff --git a/src/main.c b/src/main.c index b54dbf1..1d23902 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,38 @@ +#include #include +#include +#include +#include + +#define ERROR(...) \ + do { \ + fprintf(stderr, __VA_ARGS__); \ + exit(1); \ + } while (0) + +#define LOG(...) \ + fprintf(stderr, __VA_ARGS__) int main(int argc, char *argv[]) { - + if (argc < 2) { + ERROR("Usage: %s \n", argv[0]); + } + + int pid; + if ((pid = atoi(argv[1])) == 0) { + ERROR("Invalid pid '%s'\n", argv[1]); + } + + ptrace(PTRACE_ATTACH, pid, NULL, NULL); + waitpid(pid, NULL, __WALL); + LOG("Attached to process %d\n", pid); + + /* Do stuff ... */ + sleep(5); + + ptrace(PTRACE_DETACH, pid, NULL, NULL); + LOG("Detached from process %d\n", pid); + + return 0; } -- cgit v1.2.3