-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.c
More file actions
65 lines (59 loc) · 1.73 KB
/
main.c
File metadata and controls
65 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <sys/user.h>
#define SYS_CUSTOM_write 10000
void print_custom(char *str) {
syscall(SYS_CUSTOM_write, str, 1, strlen(str));
}
void tracee() {
ptrace(PTRACE_TRACEME, 0, 0, 0);
raise(SIGCONT);//to make it actually stop
printf("You shouldn't be able to see anything down below if you managed to attach your own debugger :P\n");
for (int i = 0; i < 10; i++) {
print_custom("fuck me up pls~\n");
}
}
void tracer(pid_t child_pid) {
int status;
waitpid(child_pid, &status, 0);
if (!WIFSTOPPED(status)) {
printf("Incorrect state.\n");
return;
}
ptrace(PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_EXITKILL);
struct user_regs_struct regs;
while (WIFSTOPPED(status)) {
ptrace(PTRACE_SYSCALL, child_pid, 0, 0);
waitpid(child_pid, &status, 0);
ptrace(PTRACE_GETREGS, child_pid, 0, ®s);
if (regs.orig_rax == SYS_CUSTOM_write) {
//printf("CUSTOM_write found, patched.\n");
regs.orig_rax = SYS_write;
//swap arg1 & arg2
unsigned long long int orig_rdi = regs.rdi;
regs.rdi = regs.rsi;
regs.rsi = orig_rdi;
ptrace(PTRACE_SETREGS, child_pid, 0, ®s);
}
ptrace(PTRACE_SYSCALL, child_pid, 0, 0);
waitpid(child_pid, &status, 0);
}
}
int main() {
printf("Weird Linux Anti-debugging Demo\n");
pid_t child_pid = fork();
if (child_pid < 0) {
printf("Fork failed.\n");
return 1;
}
if (child_pid == 0) {
tracee(); //child
} else {
tracer(child_pid);
}
return 0;
}