-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptrace.c
279 lines (252 loc) · 6.4 KB
/
ptrace.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
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
#include <stdio.h>
#include <string.h> //memset
#include <stdlib.h> //exit
#include <stdint.h> //types defintion
#include <getopt.h> //getopt_long
#include <unistd.h> //fork()
#include <poll.h>
#include <sys/ptrace.h> //ptrace()
#include <sys/types.h>
#include <sys/wait.h> //waitpid
#include <errno.h> //erno
#include <sys/procfs.h> //elf_regset
#include "ptrace.h"
#include "elf_utils.h"
#ifdef DBG_PRINTF
#undef DBG_PRINTF
#define DBG_PRINTF(fmt,...) \
fprintf(stderr, fmt, ##__VA_ARGS__);
#else
#define DBG_PRINTF
#endif
#define LOG_PRINTF(fmt, ...) \
fprintf(stderr, fmt, ##__VA_ARGS__)
typedef enum inf_state_t {
INF_STATE_INVALID = -1,
INF_STATE_STARTED,
INF_STATE_RUNNING,
INF_STATE_STOPPED,
INF_STATE_MAX = 256
} inf_state_t;
typedef struct inf_handle {
#ifndef MAX_FILENAME
#define MAX_FILENAME 256
#endif
char program[MAX_FILENAME];
pid_t pid;
inf_state_t state;
Elf_Ehdr *ehdr;
off_t e_sz;
} inf_handle_t;
static inf_handle_t inferior;
static inf_handle_t *inf;
static const char *GetSinfo(int signo)
{
switch (signo) {
case SIGSEGV: return "SIGSEGV"; break;
case SIGABRT: return "SIGABRT"; break;
case SIGBUS: return "SIGBUS"; break;
case SIGKILL: return "SIGKILL"; break;
case SIGINT: return "SIGINT"; break;
default: return "Unknown signal number"; break;
}
}
static void print_reg_info(elf_gregset_t regs)
{
int i = 0;
struct rorder {
int idx;
const char *reg_name;
};
static struct rorder reg_print_order[] = { \
{RAX, "rax"},
{RBX, "rbx"},
{RCX, "rcx"},
{RDX, "rdx"},
{RSI, "rsi"},
{RDI, "rdi"},
{RBP, "rbp"},
{RSP, "rsp"},
{R8, "r8"},
{R9, "r9"},
{R10, "r10"},
{R11, "r11"},
{R12, "r12"},
{R13, "r13"},
{R14, "r14"},
{R15, "r15"},
{RIP, "rip"},
{EFLAGS, "eflags"},
{CS, "cs"},
{SS, "ss"},
{DS, "ds"},
{ES, "es"},
{FS, "fs"},
{GS, "gs"},
};
for (i = 0; i < (sizeof(reg_print_order)/sizeof(reg_print_order[0])); ++i) {
uint64_t idx = reg_print_order[i].idx;
if (EFLAGS == idx) {
fprintf(stderr, "%s\t\t0x%x%10s[ PF ZF IF RF ]\n",
reg_print_order[i].reg_name,
regs[idx], " ");
continue;
}
fprintf(stderr, "%s\t\t0x%llx%20lld\n",
reg_print_order[i].reg_name,
regs[idx], regs[idx]);
}
}
Elf_Addr ReadWordInInferior(Elf_Addr offset)
{
Elf_Addr address = ptrace(PT_READ_I, inf->pid, offset, 0);
return address;
}
static void PrintBacktrace(Elf_Addr pc, Elf_Addr bp)
{
char *name;
Elf_Addr value;
Elf_Addr ra = pc;
Elf_Addr fp = bp;
fprintf(stderr, "Backtrace\n");
#define FRAME_NEXT(fp) ReadWordInInferior(fp)
#define FRAME_RA(fp) ReadWordInInferior(fp+8)
do {
ra = FRAME_RA(fp);
elf_search_symbol(inf->ehdr, (Elf_Addr)pc, &value, &name);
if (!name)
break;
fprintf(stderr, "%p %s+0x%x() \n", pc, name, pc-value);
pc = ra;
} while (fp = FRAME_NEXT(fp));
}
void handle_inf_event(void)
{
int status = 0;
if (inf->state == INF_STATE_STARTED) {
wait(&status);
if (WIFSTOPPED(status)) {
DBG_PRINTF( "The first process SIGTRAP after execve()\n"); //lets continue;
long retval = ptrace(PTRACE_CONT, inf->pid, NULL, 0);
DBG_PRINTF("ptrace cont returned %ld\n", retval);
inf->state = INF_STATE_RUNNING;
}
} else if (inf->state == INF_STATE_RUNNING) {
if (inf->pid != waitpid(inf->pid, &status, 0)) {
LOG_PRINTF("waitpid() error\n");
exit(0); // ???? kill the tracee ?
}
if (WIFSTOPPED(status)) {
int si = WSTOPSIG(status);
elf_gregset_t regs;
Elf_Sym *sym = NULL;
Elf_Addr value;
char *name;
LOG_PRINTF( "Program received signal %s\n", GetSinfo(si));
if (ptrace (PTRACE_GETREGS, inf->pid, 0, (long) ®s) < 0)
perror("couldn't get registers\n");
DBG_PRINTF("successfully read registers\n");
print_reg_info(regs);
PrintBacktrace(regs[RIP], regs[RBP]);
exit(0);
}
}
}
void do_sigchld(int signo)
{
DBG_PRINTF("sigchld received by %d task\n", getpid());
handle_inf_event();
}
static const char *optString ="e:hv";
static const struct option longOpts[] = {
{"exe", required_argument, NULL, 0 },
{"help", no_argument, NULL, 0 },
{"version", no_argument, NULL, 0 },
{ NULL, no_argument, NULL, 0}
};
static void printUsage(void)
{
printf("ptrace\n");
printf("Allowed options: \n");
printf("-h [ --help ] Display this message\n");
printf("-e [ --exe ] Exe file name\n");
printf("-v [ --version ] Display version information\n");
}
int main(int argc, char *argv[])
{
pid_t pid;
int opt = -1, retval = -1, longIndex;
inf = &inferior;
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
while (-1 != opt) {
switch(opt) {
case 'h':
printUsage();
exit(0);
break;
case 'v':
fprintf(stderr, "readnote Version 1.0 [20th Jan 2016]\n");
exit(0);
case 'e':
strncpy(inf->program, optarg, sizeof(inf->program));
inf->program[MAX_FILENAME] = 0;
break;
case '?':
printUsage();
exit(0);
break;
case 0:
if (!strcmp("file-name", longOpts[longIndex].name)) {
strncpy(inf->program, optarg, sizeof(inf->program));
inf->program[MAX_FILENAME] = 0;
} else if (!strcmp("version", longOpts[longIndex].name)) {
fprintf(stderr, "ptrace Version 1.0 [14th Jan 2016]\n");
exit(0);
} else if (!strcmp("help", longOpts[longIndex].name)) {
printUsage();
exit(0);
}
break;
default:
printUsage();
exit(0);
break;
}
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
}
if (!*inf->program) {
fprintf(stderr, "Executable not specified\n");
printUsage();
exit(0);
}
inf->ehdr = elf_map_file(inf->program, &inf->e_sz);
if (NULL == inf->ehdr) {
LOG_PRINTF("Cannot map the elf file\n");
exit(0);
}
signal(SIGCHLD, do_sigchld);
if (!(pid= fork())) {
int *ptr = NULL;
char *argv[10];
memset(argv, 0, sizeof(argv));
long retval = ptrace(PTRACE_TRACEME, 0, NULL, NULL);
DBG_PRINTF("ptrace returned %ld\n", retval);
if (0 > retval) {
DBG_PRINTF("ptrace failed with errno(%d:%s)\n", errno, strerror(errno));
exit(0);
}
DBG_PRINTF( "Now exec'ing the child\n");
int rval = execv(inf->program, argv);
if (0 > rval) {
DBG_PRINTF( "execv returned error\n");
exit(0);
}
DBG_PRINTF("Error if you are here!!!!\n");
} else {
int rval = 0;
inf->pid = pid;
poll_forever:
rval = poll(0, 0, -1);
goto poll_forever;
}
}