-
Notifications
You must be signed in to change notification settings - Fork 55
/
exp_1.c
117 lines (104 loc) · 3.7 KB
/
exp_1.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <unistd.h>
#include <sys/types.h>
#include<sys/ptrace.h>
#include<sys/reg.h>
#include<sys/wait.h>
#include<sys/user.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/syscall.h>
#define long_size sizeof(long)
void print_regs(struct user_regs_struct regs)
{
printf("+++++++ return from syscall ++++++++\n");
printf("RAX: %#lx\n", regs.rax);
printf("RBX: %#lx\n", regs.rbx);
printf("RCX: %#lx\n", regs.rcx);
printf("RDX: %#lx\n", regs.rdx);
printf("RSI: %#lx\n", regs.rsi);
printf("RDI: %#lx\n", regs.rdi);
printf("RBP: %#lx\n", regs.rbp);
printf("RSP: %#lx\n", regs.rsp);
printf("RIP: %#lx\n", regs.rip);
printf("R8: %#lx\n", regs.r8);
printf("R9: %#lx\n", regs.r9);
printf("R10: %#lx\n", regs.r10);
printf("R11: %#lx\n", regs.r11);
printf("R12: %#lx\n", regs.r12);
printf("R13: %#lx\n", regs.r13);
printf("R14: %#lx\n", regs.r14);
printf("R15: %#lx\n", regs.r15);
printf("EFLAGS: %#lx\n", regs.eflags);
printf("CS: %#lx\n", regs.cs);
printf("SS: %#lx\n", regs.ss);
printf("DS: %#lx\n", regs.ds);
printf("ES: %#lx\n", regs.es);
printf("FS: %#lx\n", regs.fs);
printf("GS: %#lx\n", regs.gs);
printf("----------- end of regs ----------\n");
}
int main(){
pid_t child;
long orig_rax;
child=fork();
if(child==0){
ptrace(PTRACE_TRACEME,0,NULL,NULL);
// execl("/home/v1me/workspace/hackergame2022-challenges/no_open/files/bin/chall",NULL,NULL);
execl("/chall",NULL,NULL);
}else{
struct user_regs_struct regs, regs1;
int i = 0;
void *text_base = 0;
wait(NULL);
orig_rax = ptrace(PTRACE_PEEKUSER,child,8*ORIG_RAX,NULL);
printf("orig_rax: %#llx\n",orig_rax);
ptrace(PTRACE_GETREGS,child,NULL,®s);
printf("rip %#llx\n", regs.rip);
while (1) {
// ptrace(PTRACE_SYSCALL,child,NULL,NULL);
ptrace(PTRACE_SYSCALL,child,NULL,NULL);
wait(NULL);
orig_rax = ptrace(PTRACE_PEEKUSER, child, 8 * ORIG_RAX, NULL);
ptrace(PTRACE_GETREGS, child, NULL, ®s);
// if (regs.rip < 0x555555554000 + 0x100000000000)
if (orig_rax == SYS_mprotect && regs.rdi / 0x100000000000 == 5) {
printf("text_base: %#llx\n", regs.rdi-0x4000);
break;
}
}
text_base = regs.rdi-0x4000;
printf("text_base: %#llx\n", text_base);
while (1) {
ptrace(PTRACE_SINGLESTEP,child,NULL,NULL);
wait(NULL);
ptrace(PTRACE_GETREGS, child, NULL, ®s);
// if (regs.rip>=text_base && regs.rip<text_base+0x10000) {
// print_regs(regs);
// }
if (regs.rip == text_base+0x1EF9) {
print_regs(regs);
memcpy(®s1, ®s, sizeof(regs));
}
if (regs.rip == text_base+0x1F1B) {
print_regs(regs);
regs.rax = regs1.rax;
regs.rdi = regs1.rdi;
regs.rsi = text_base;
regs.rdx = 0x6000;
ptrace(PTRACE_SETREGS, child, NULL, ®s);
break;
}
// if (orig_rax == SYS_read && regs.rdi == 3 && regs.rsi / 0x100000000000 == 5) {
// printf("flag addr: %#llx\n", text_base+0x2020);
// uint64_t val = ptrace(PTRACE_PEEKDATA, child, text_base+0x2020, NULL);
// printf("%#llx\n", &val);
// break;
// }
}
}
}