-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdb.c
243 lines (216 loc) · 6.89 KB
/
mdb.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
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <bfd.h>
#include <dis-asm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <assert.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <errno.h>
#include <readline/readline.h>
#include "uthash.h"
typedef struct{
char *sym; /*symbol*/
long addr; /*symbol address*/
long opc; /*wrod at symbol*/
UT_hash_handle hh; /*uthash handle*/
}SymbolTable, Symbol;
/*debugger commands*/
typedef enum {CONTINUE, BREAK, KILL, NEXT, REGISTER}mdb_cmd;
/*reads command from user*/
mdb_cmd next_cmd(char **argv)
{
mdb_cmd cmd= -1;
char *cmdstr;
if(!(cmdstr = readline("(mdb) "))) return cmd;
if(!strcmp(cmdstr, "continue")){
cmd = CONTINUE;
}else if(!strcmp(cmdstr, "kill")){
cmd = KILL;
}else if(!strcmp(cmdstr, "next")){
cmd = NEXT;
}else if(!strcmp(cmdstr, "registers")){
cmd = REGISTER;
}else{
const char *op = strtok(cmdstr, " ");
const char *arg = strtok(NULL, " ");
if( op && arg && !strcmp(op, "break"))
{
cmd = BREAK;
*argv = strdup(arg);
}
}
free(cmdstr);
return cmd;
}
/*disassemble instruction(s) from buf*/
void disassemble(void *buf, unsigned long size)
{
struct disassemble_info info;
init_disassemble_info (&info, stdout, (fprintf_ftype)fprintf);
info.mach = bfd_mach_x86_64;
info.endian = BFD_ENDIAN_LITTLE;
info.buffer = buf;
info.buffer_length = size;
print_insn_i386(0, &info);
printf("\n");
}
int main(int argc, char **argv)
{
if(argc == 1)
{
fprintf(stderr, "usage mdb <bin>");
exit(-1);
}
const char *path = argv[1];
const char *name = strrchr(path, '/');
if(name){
name += 1;
}else{
name = path;
}
pid_t pid;
switch(pid = fork())
{
case -1: /*error*/
{
perror("fork()");
exit(-1);
}
case 0:/*child process*/
{
ptrace(PTRACE_TRACEME, NULL, NULL); /*allow child process to be traced*/
execl(path, name, NULL); /*child will be stopped here*/
perror("execl()");
exit(-1);
}
/*parent continues execution*/
}
/* load symbol table*/
long size;
long nsym;
asymbol **asymtab;
bfd_init();
bfd *abfd = bfd_openr(path, NULL);
bfd_check_format(abfd, bfd_object);
size = bfd_get_symtab_upper_bound(abfd);
asymtab = malloc(size);
nsym = bfd_canonicalize_symtab(abfd, asymtab); /*reads symtab*/
/*create symbol table hash*/
long i;
SymbolTable *symtab=NULL, *symbol;
for(i = 0; i < nsym; i++)
{
symbol = malloc(sizeof(Symbol));
symbol->sym = bfd_asymbol_name(asymtab[i]);
symbol->addr = bfd_asymbol_value(asymtab[i]);
HASH_ADD_KEYPTR(hh, symtab, symbol->sym, strlen(symbol->sym), symbol);
}
char *arg;
int status;
long opcode[2];
long old_rip;
SymbolTable *brptab=NULL, *brp;
wait(&status);
while(1)
{
if(WIFEXITED(status) || (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL))
{
printf("process %d terminated\n", pid);
exit(0);
}
if(WIFSTOPPED(status))
{
long rip = ptrace(PTRACE_PEEKUSER, pid, 8 * RIP, NULL)-1;
HASH_FIND_INT(brptab, &rip, brp);
if(brp){
HASH_DEL(brptab, brp);
/*restore instruction(s)*/
ptrace(PTRACE_POKETEXT, pid, brp->addr, brp->opc);
/*decrement eip*/
ptrace(PTRACE_POKEUSER, pid, 8 * RIP, rip);
printf("process %d stopped at 0x%lx<%s>\n", pid, rip, brp->sym);
}else{
printf("process %d stopped at 0x%lx\n", pid, rip);
}
}
switch(next_cmd(&arg))
{
case BREAK: /*set break point*/
{
/*look up the symbol in the symbol table*/
HASH_FIND_STR(symtab, arg, symbol);
if(symbol)
{
/*insert new break point*/
brp = malloc(sizeof(Symbol));
brp->sym = symbol->sym;
brp->addr = symbol->addr;
/*save instruction at eip*/
brp->opc = ptrace(PTRACE_PEEKTEXT, pid, symbol->addr, NULL);
/*insert break point*/
ptrace(PTRACE_POKETEXT, pid, symbol->addr, 0xcc);
/*add break point to hash*/
HASH_ADD_INT(brptab, addr, brp);
printf("breakpoint at %lx<%s>\n", brp->addr, brp->sym);
}
else
{
printf("symbol not found <%s>\n", arg);
}
free(arg);
break;
}
case NEXT: /*next instruction*/
{
/*read instruction pointer*/
long rip = ptrace(PTRACE_PEEKUSER, pid, 8 * RIP, NULL);
if(old_rip)
{
/*calculate instruction size*/
long oplen = rip - old_rip;
if(oplen > 0 && oplen < 16)
disassemble(&opcode, oplen);
}
/*read two words at eip*/
opcode[0] = ptrace(PTRACE_PEEKDATA, pid, rip, NULL);
opcode[1] = ptrace(PTRACE_PEEKDATA, pid, rip+sizeof(long), NULL);
old_rip = rip;
/*next instruction*/
ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL);
wait(&status);
break;
}
case REGISTER:/*dump registers*/
{
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
printf("rax 0x%lx\n", regs.rax);
printf("rbx 0x%lx\n", regs.rbx);
printf("rcx 0x%lx\n", regs.rcx);
printf("rdx 0x%lx\n", regs.rdx);
printf("rsi 0x%lx\n", regs.rsi);
printf("rdi 0x%lx\n", regs.rdi);
printf("rbp 0x%lx\n", regs.rbp);
printf("rsp 0x%lx\n", regs.rsp);
printf("rip 0x%lx\n", regs.rip);
break;
}
case CONTINUE:/*continue execution*/
ptrace(PTRACE_CONT, pid, NULL, NULL);
wait(&status);
break;
case KILL: /*kill process*/
kill(pid, SIGKILL);
wait(&status);
break;
default:/*error*/
fprintf(stderr, "invalid or unknown command\n");
}
}
}