0%

myGDB

简单GDB

之前学习了 ptrace 系统调用,看了别人写的各种 demo 和简单调试器

我想自己尝试写一个简单的 GDB

目前实现的功能如下:

  • 单步执行(步入)
  • 显示寄存器
  • 显示内存
  • 打断点
  • 显示断点

代码如下:

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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include<sys/wait.h>
#include<sys/reg.h>
#include<sys/user.h>
#include<sys/ptrace.h>
#include<unistd.h>
#include<sys/syscall.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

enum ERR{
NOL,
INPUT_ERR,
FORK_ERR,
MALLOC_ERR,
OPEN_ERR,
NOTFIND_ERR,
};

const int word_size = sizeof(size_t);
char breakcode[] = {0xcd,0x80,0xcc,0};

struct breakinfo{
size_t addr;
struct breakinfo* next;
char backcode[9];
};

struct breakinfohead{
size_t total;
struct breakinfo* next;
};

struct breakinfohead* bh;

size_t hex2int(const char* str){
size_t ans=0;
int mm[128]={0};
mm['0']=0; mm['1']=1; mm['2']=2; mm['3']=3; mm['4']=4;
mm['5']=5; mm['6']=6; mm['7']=7; mm['8']=8; mm['9']=9;
mm['a']=10; mm['b']=11; mm['c']=12; mm['d']=13; mm['e']=14; mm['f']=15;
mm['A']=10; mm['B']=11; mm['C']=12; mm['D']=13; mm['E']=14; mm['F']=15;

if(str==NULL)
return 0;
while (*str){
ans*=16;
ans+=mm[*str];
str++;
}
return ans;
}

void errPrint(char * msg){
puts(msg);
exit(-1);
}

void msgPrint(char * msg){
puts(msg);
}

void bytePrint(char* codes, int len) {
int i;
for (i = 0; i < len; ++i) {
printf("%02x ", (unsigned char) codes[i]);
if ((i + 1) % 8 == 0)
printf("\n");
}
printf("\n");
}

void putdata(pid_t pid, size_t addr, char *str, int len) {
char *code;
int i, j;
union u{
size_t val;
char word[word_size];
}data;
i = 0;
j = len / word_size;
code = str;
while(i < j) {
memcpy(data.word, code, word_size);
ptrace(PTRACE_POKEDATA, pid, addr + i * 8, data.val);
++i;
code += word_size;
}
j = len % word_size;
if(j != 0) {
memcpy(data.word, code, j);
ptrace(PTRACE_POKEDATA, pid, addr + i * 8, data.val);
}
}

void getdata(pid_t pid, size_t addr, char* str, int len) {
char *code;
int i, j;
union u{
size_t val;
char word[word_size];
}data;
i = 0;
j = len / word_size;
code = str;
while (i < j) {
data.val = ptrace(PTRACE_PEEKDATA, pid, addr + i * 8, NULL);
memcpy(code, data.word, word_size);
++i;
code += word_size;
}
j = len % word_size;
if (j != 0) {
data.val = ptrace(PTRACE_PEEKDATA, pid, addr + i * 8, NULL);
memcpy(code, data.word, j);
}
str[len] = '\0';
}

int showReg(pid_t child, int key){
if(child==-1){
return -FORK_ERR;
}
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, child, 0, &regs);
printf("───────────────────────────────[ REGISTERS ]──────────────────────────────────\n");
printf("rax:\t%llx\nrbx:\t%llx\nrcx:\t%llx\nrdx:\t%llx\nrsi:\t%llx\nrdi:\t%llx\nrbp:\t%llx\n"
"rsp:\t%llx\nrip:\t%llx\neflags:\t%llx\ncs:\t%llx\nss:\t%llx\nds:\t%llx\nes:\t%llx\n",
regs.rax, regs.rbx, regs.rcx, regs.rdx, regs.rsi, regs.rdi, regs.rbp,
regs.rsp, regs.rip, regs.eflags, regs.cs, regs.ss, regs.ds, regs.es);
if(key == 1)
printf("──────────────────────────────────────────────────────────────────────────────\n");

return 0;
}

int showCode(pid_t child, int key){
if(child==-1){
return -FORK_ERR;
}
char code[0x40]={0};
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, child, 0, &regs);
getdata(child,regs.rip,code,0x40);
printf("───────────────────────────────[ CODE BYTES ]─────────────────────────────────\n");
bytePrint(code,0x40);
if(key == 1)
printf("──────────────────────────────────────────────────────────────────────────────\n");
}

int showMemory(pid_t child){
if(child==-1){
return -FORK_ERR;
}
char code[0x40] = {0};
size_t addr;
struct user_regs_struct regs;

scanf("%lx",&addr);
getchar();
ptrace(PTRACE_GETREGS, child, 0, &regs);
getdata(child,addr,code,0x40);
printf("──────────────────────────────────────────────────────────────────────────────\n");
printf("search in %lx:\n",addr);
bytePrint(code,0x40);
printf("──────────────────────────────────────────────────────────────────────────────\n");
}

int inputLine(char *cmd){
if(cmd == NULL){
return -INPUT_ERR;
}
char tmp[0x40];
char ch;
int i=0;
while(1){
ch = getchar();
if(ch == '\n'){
if(i == 0){
return 0;
}
tmp[i]='\0';
break;
}
tmp[i++] = ch;
}
strcpy(cmd,tmp);
return 0;
}

int setBreak(pid_t child){
if(child==-1){
return -FORK_ERR;
}
struct breakinfo* bk = (struct breakinfo*)malloc(sizeof(struct breakinfo));
if(bk==NULL){
return -MALLOC_ERR;
}
size_t addr;
scanf("%lx",&addr);
getchar();
bk->addr = addr;
printf("break at :0x%lx\n",bk->addr);
getdata(child, addr, bk->backcode, 8);
bytePrint(bk->backcode,8);
putdata(child, addr, breakcode, 3);

bk->next = bh->next;
bh->next = bk;
return 0;
}

int searchBreak(pid_t child,size_t addr,struct breakinfo** bkp){
struct breakinfo* tmp=bh->next;
while(tmp!=NULL){
if(addr == tmp->addr){
*bkp = tmp;
return 0;
}
tmp = tmp->next;
}
msgPrint("Miss Breakpoint");
return -NOTFIND_ERR;
}

int unlinkBreak(pid_t child,struct breakinfo** bkp){
if(*bkp == NULL){
return -INPUT_ERR;
}
struct breakinfo* tmp=bh->next;
if(*bkp == bh->next){
bh->next = bh->next->next;
free(*bkp);
return 0;
}

while(tmp->next!=*bkp){
tmp = tmp->next;
}
tmp->next = tmp->next->next;
free(*bkp);
return 0;
}

int showBreak(pid_t child){
struct breakinfo *tmp = bh->next;

while(tmp!=NULL){
printf("Break addr = 0x%lx\n",tmp->addr);
bytePrint(tmp->backcode,8);
tmp = tmp->next;
}
return 0;
}

int runBreak(pid_t child, int status){
if(child==-1){
return -FORK_ERR;
}
struct user_regs_struct regs;
struct breakinfo *bk;
if (WIFEXITED(status)){
msgPrint("Process finished");
exit(0);
}
if (WSTOPSIG(status) == SIGTRAP) {
ptrace(PTRACE_GETREGS, child, 0, &regs);

if(searchBreak(child,regs.rip-3,&bk) < 0){
return -NOTFIND_ERR;
}
putdata(child, bk->addr, bk->backcode, 8);
ptrace(PTRACE_SETREGS, child, 0, &regs);
regs.rip = bk->addr;
ptrace(PTRACE_SETREGSET, child, 0, &regs);
ptrace(PTRACE_SETREGS, child, 0, &regs);
showReg(child,0);
showCode(child,1);
printf("Hit Breakpoint at: 0x%lx\n", bk->addr);
unlinkBreak(child, &bk);
return 0;
}
}

int runStepIn(pid_t child){
if(child==-1){
return -FORK_ERR;
}
int status;
showReg(child,0);
showCode(child,1);
ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
wait(&status);
if (WIFEXITED(status)){
msgPrint("Process finished");
exit(0);
}
return 0;
}

int getBase(pid_t child,size_t* probase,size_t* libcbase){
if(child==-1){
return -FORK_ERR;
}
int fd;
char path[0x40];
char cmd[0x50];
char buf[0x40];
char *ret;
sprintf(path,"/proc/%d/maps",child);
printf("Path: %s\n",path);
sprintf(cmd,"cat %s",path);
printf("───────────────────────────────[ VMA MAP ]───────────────────────────────────\n");
msgPrint("LEGEND: STACK | HEAP | CODE | DATA | RWX | RODATA");
system(cmd);
printf("──────────────────────────────────────────────────────────────────────────────\n");
fd = open(path,0);
if(fd < 0){
return -OPEN_ERR;
}

lseek(fd,0,SEEK_SET);
read(fd,buf,0x40);
ret = strchr(buf,'-');
*ret = '\x00';
*probase = hex2int(buf);
lseek(fd,0x19c,SEEK_SET);
read(fd,buf,0x40);
ret = strchr(buf,'-');
*ret = '\x00';
*libcbase = hex2int(buf);

printf("probase: 0x%lx\n",*probase);
printf("libcbase: 0x%lx\n",*libcbase);
close(fd);
return 0;
}

int main(int argc, char *argv[]){
pid_t child;
int status;
size_t probase;
size_t libcbase;
struct user_regs_struct regs;
char cmd[0x40];

if(argv[1]==NULL){
errPrint("need target");
}
child = fork();
if(child==0){
ptrace(PTRACE_TRACEME,0,0);
execl(argv[1],argv[1],NULL);
}
else{
getBase(child,&probase,&libcbase);
bh = (struct breakinfohead*)malloc(sizeof(struct breakinfohead));
if(bh==NULL){
return -MALLOC_ERR;
}
while(1){
printf("YHellow > ");
inputLine(cmd);
if(strcmp(cmd,"c") == 0){
ptrace(PTRACE_CONT, child, NULL, NULL);
wait(&status);
runBreak(child,status);
}
else if(strcmp(cmd,"b") == 0 || strcmp(cmd,"break") == 0){
setBreak(child);
}
else if(strcmp(cmd,"b info") == 0 || strcmp(cmd,"break info") == 0){
showBreak(child);
}
else if(strcmp(cmd,"n") == 0 || strcmp(cmd,"ni") == 0){
runStepIn(child);
}
else if(strcmp(cmd,"r") == 0 || strcmp(cmd,"reg") == 0){
showReg(child,1);
}
else if(strcmp(cmd,"s") == 0 || strcmp(cmd,"show") == 0){
showMemory(child);
}
else if(strcmp(cmd,"q") == 0 || strcmp(cmd,"quit") == 0){
errPrint("Exit\nThanks");
}
}
}
}