-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtiny-debugger.c
190 lines (151 loc) · 4.49 KB
/
tiny-debugger.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* The original author is Qi Yao. The code was listed in his book
* <Debugger not in Depth>. I port it to amd64 version and
* made a few modifications.
* */
#include <sys/ptrace.h>
#include <errno.h>
#include <asm/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/reg.h>
#include <unistd.h>
#include <stdio.h>
// dl_debug_state
//#define DL_ENTRY_ADDR 0x00007ffff7de7e90
// the address of foo() in int3func
#define DL_ENTRY_ADDR 0x40052a
union instr {
long l;
unsigned char c[sizeof(long)/sizeof(char)];
}u;
struct breakpoint {
unsigned char orig;
void *address;
};
#define DEBUG
//#undef DEBUG
extern pid_t child;
void setup_breakpoint(pid_t child, long address, struct breakpoint *b) {
long align = (address >> 2) << 2;
int idx = address % 4;
u.l = ptrace(PTRACE_PEEKTEXT, child, align, NULL);
if (errno > 0)
printf("errno = %d\n", errno);
b->address = (void*) address;
#ifdef DEBUG
printf("Get instr "
"[0x%x 0x%x 0x%x 0x%x]"
"[0x%x 0x%x 0x%x 0x%x]"
"from 0x%lx\n",
u.c[0], u.c[1], u.c[2], u.c[3],
u.c[4], u.c[5], u.c[6], u.c[7],
align);
#endif
b->orig = u.c[idx];
u.c[idx] = 0xcc;
ptrace(PTRACE_POKETEXT, child, align, u.l);
#ifdef DEBUG
printf("Set breakpoint at 0x%lx, and write "
"[0x%x 0x%x 0x%x 0x%x]"
"[0x%x 0x%x 0x%x 0x%x] at 0x%lx\n",
address,
u.c[0], u.c[1], u.c[2], u.c[3],
u.c[4], u.c[5], u.c[6], u.c[7],
align);
#endif
}
void restore_breakpoint(pid_t child, struct breakpoint *b) {
long pc;
long align = (long) b->address;
int idx = align % 4;
align = (align >> 2) << 2;
u.l = ptrace(PTRACE_PEEKTEXT, child, align, NULL);
u.c[idx] = b->orig;
ptrace(PTRACE_POKETEXT, child, align, u.l);
#ifdef DEBUG
printf("Restore the orginal instruction at 0x%lx\n",
align);
#endif
}
void hit_breakpoint(pid_t child, struct breakpoint *b) {
long pc;
long align = (long) b->address;
int idx = align % 4;
align = (align >> 2) << 2;
pc = ptrace(PTRACE_PEEKUSER, child, 8 * RIP, NULL);
#ifdef DEBUG
printf(" PC = 0x%lx\n", pc);
printf("b->address = 0x%lx\n", b->address);
#endif
if (pc != (b->address) + 1) {
printf("hit_breakpoint: pc != (b->address) + 1... RETURN NOW.\n");
return;
}
u.l = ptrace(PTRACE_PEEKTEXT, child, align, NULL);
u.c[idx] = b->orig;
ptrace(PTRACE_POKETEXT, child, align, u.l);
#ifdef DEBUG
printf("Hit breakpoint: Restore the original instr"
"[0x%x 0x%x 0x%x 0x%x]"
"[0x%x 0x%x 0x%x 0x%x] at 0x%lx\n",
u.c[0], u.c[1], u.c[2], u.c[3],
u.c[4], u.c[5], u.c[6], u.c[7],
align);
#endif
ptrace(PTRACE_POKEUSER, child, 8 * RIP, pc - 1);
ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
wait(NULL);
pc = ptrace(PTRACE_PEEKUSER, child, 8 * RIP, NULL);
#ifdef DEBUG
printf("Single Step: PC = 0x%lx\n", pc);
#endif
b->orig = u.c[idx];
u.c[idx] = 0xcc;
ptrace(PTRACE_POKETEXT, child, align, u.l);
#ifdef DEBUG
printf("Restore TRAP(0xCC) at 0x"
"[0x%x 0x%x 0x%x 0x%x]"
"[0x%x 0x%x 0x%x 0x%x] at 0x%lx\n",
u.c[0], u.c[1], u.c[2], u.c[3],
u.c[4], u.c[5], u.c[6], u.c[7],
align);
#endif
}
int main() {
pid_t child = fork();
if (child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
//execl("/bin/ls", NULL);
execl("./int3func", NULL);
} else {
int status;
long pc;
struct breakpoint b;
wait(NULL);
setup_breakpoint(child, DL_ENTRY_ADDR, &b);
ptrace(PTRACE_CONT, child, NULL, NULL);
while (1) {
wait(&status);
if (WIFEXITED(status)) {
printf("Exit status: %d\n", WEXITSTATUS(status));
return 0;
}
if (WIFSTOPPED(status)) {
printf("Stopped by: %d\n", WSTOPSIG(status));
hit_breakpoint(child, &b);
// 11 = SIGSEGV
if (status == 11)
break;
}
if (WIFSIGNALED(status)) {
printf("SIGNALED by: %d\n", WTERMSIG(status));
}
// Why is '8 * RIP' correct?
pc = ptrace(PTRACE_PEEKUSER, child, 8 * RIP, NULL);
printf("at 0x%lx\n", pc);
ptrace(PTRACE_CONT, child, NULL, NULL);
}
}
return 0;
}