-
Notifications
You must be signed in to change notification settings - Fork 55
/
ui.c
392 lines (288 loc) · 7.79 KB
/
ui.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
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
391
392
#define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <histedit.h>
#include "assemble.h"
#include "common.h"
#include "arch.h"
#include "display.h"
#include "exedir.h"
#include "elf_gen.h"
#include "ptrace.h"
#include "ptrace_arch.h"
#include "ui.h"
extern struct options_t options;
extern int exiting;
static int in_block;
static
char const* _prompt(
EditLine *const e)
{
if (in_block)
return "_> ";
else
return "> ";
}
static
void _help(void)
{
printf("Commands:\n");
printf(".quit - quit\n");
printf(".help - display this help\n");
printf(".info - display registers\n");
printf(".begin - start a block, input will not be assembled/run until '.end'\n");
printf(".end - assemble and run the prior block\n");
printf(".showmap - shortcut for cat /proc/<pid>/maps\n");
printf(".allregs <on|off> - toggle all register display\n");
printf(".read <address> [amount] - read <amount> bytes of data from address using ptrace [16]\n");
printf(".write <address> <data> - write data starting at address using ptrace\n");
}
static
void _ui_read(
const pid_t child_pid,
const char *line)
{
char *dupline = strdup(line);
if (!dupline) {
perror("strdup");
return;
}
char *saveptr;
const char *dotread = strtok_r(dupline, " ", &saveptr);
if (!dotread || strcasecmp(dotread, ".read"))
goto bail;
const char *addr_str = strtok_r(NULL, " ", &saveptr);
if (!addr_str)
goto bail;
errno = 0;
const unsigned long addr = strtoul(addr_str, NULL, 0x10);
if (addr == ULONG_MAX && errno) {
perror("strtoul");
goto bail;
}
const char *sz_str = strtok_r(NULL, " ", &saveptr);
unsigned long sz = 0x10;
if (sz_str && strlen(sz_str)) {
errno = 0;
sz = strtoul(sz_str, NULL, 0);
if (sz == ULONG_MAX && errno) {
perror("strtoul");
goto bail;
}
}
uint8_t *buf = xmalloc(sz);
if (!ptrace_read(child_pid, (void *)addr, buf, sz))
dump(buf, sz, addr);
free(buf);
bail:
free(dupline);
}
static
void _ui_write(
const pid_t child_pid,
const char *line)
{
char *dupline = strdup(line);
if (!dupline) {
perror("strdup");
return;
}
char *saveptr;
const char *dotread = strtok_r(dupline, " ", &saveptr);
if (!dotread || strcasecmp(dotread, ".write"))
goto bail;
const char *addr_str = strtok_r(NULL, " ", &saveptr);
if (!addr_str)
goto bail;
errno = 0;
const unsigned long addr = strtoul(addr_str, NULL, 0x10);
if (addr == ULONG_MAX && errno) {
perror("strtoul");
goto bail;
}
const char *val_str = strtok_r(NULL, " ", &saveptr);
if (!val_str) goto bail;
char *p = strchr(val_str, '\n');
if (p) *p = 0;
const size_t val_len = strlen(val_str);
if (val_len % 2) {
printf("Memory write values should be hex encoded, even length strings\n");
goto bail;
}
const size_t sz = val_len / 2;
uint8_t *buf = xmalloc(sz);
memset(buf, 0, sz);
for (size_t ii = 0; ii < val_len; ii += 2) {
uint8_t a = hex_hashmap[(uint8_t)val_str[ii + 0]];
uint8_t b = hex_hashmap[(uint8_t)val_str[ii + 1]];
if (a == 0xff || b == 0xff) {
printf("Memory write values should be hex encoded, even length strings\n");
}
buf[ii / 2] = a << 4 | b;
}
ptrace_write(child_pid, (void *)addr, buf, sz);
free(buf);
bail:
free(dupline);
}
static const
pid_t _gen_child(void) {
uint8_t buf[PAGE_SIZE];
mem_assign(buf, PAGE_SIZE, TRAP, TRAP_SZ);
uint8_t *elf;
const size_t elf_sz = gen_elf(&elf, options.start, (uint8_t *)buf, PAGE_SIZE);
const int exe_fd = write_exe(elf, elf_sz, options.savefile);
free(elf);
const pid_t tracee = fork();
if (tracee < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (tracee == 0) {
ptrace_child(exe_fd);
abort();
}
// Parent
close(exe_fd);
return tracee;
}
void interact(
const char *const argv_0)
{
EditLine *const el = el_init(argv_0, stdin, stdout, stderr);
el_set(el, EL_PROMPT, &_prompt);
el_set(el, EL_EDITOR, "emacs");
History *const hist = history_init();
if (!hist) {
fprintf(stderr, "Could not initalize history\n");
exit(EXIT_FAILURE);
}
HistEvent ev;
history(hist, &ev, H_SETSIZE, 100);
char hist_path[PATH_MAX] = { 0 };
int ret = snprintf(hist_path, sizeof(hist_path), "%s/history", options.rappel_dir);
if (ret < 0) {
fprintf(stderr, "Path exceeds max path length: %s/history", options.rappel_dir);
exit(EXIT_FAILURE);
}
history(hist, &ev, H_LOAD, hist_path);
el_set(el, EL_HIST, history, hist);
const pid_t child_pid = _gen_child();
verbose_printf("child process is %d\n", child_pid);
if (options.verbose) _help();
char buf[PAGE_SIZE] = { 0 };
size_t buf_sz = 0;
int end = 0, child_died = 0;
struct proc_info_t info = { 0 };
ARCH_INIT_PROC_INFO(info);
ptrace_launch(child_pid);
ptrace_cont(child_pid, &info);
ptrace_reap(child_pid, &info);
display(&info);
for (;;) {
int count;
const char *const line = el_gets(el, &count);
if (count == -1) {
perror("el_gets");
exit(EXIT_FAILURE);
}
// count is 0 == ^d
if (!count || strcasestr(line, ".quit") || strcasestr(line, ".exit")) break;
// We have input, add it to our history
history(hist, &ev, H_ENTER, line);
// If we start with a ., we have a command
if (line[0] == '.') {
if (strcasestr(line, "help")) {
_help();
continue;
}
if (strcasestr(line, "info")) {
display(&info);
continue;
}
if (strcasestr(line, "showmap")) {
char cmd[PATH_MAX] = { 0 };
snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", child_pid);
if (system(cmd))
fprintf(stderr, "sh: %s failed\n", cmd);
continue;
}
if (strcasestr(line, "read")) {
_ui_read(child_pid, line);
continue;
}
if (strcasestr(line, "write")) {
_ui_write(child_pid, line);
continue;
}
if (strcasestr(line, "allregs")) {
if (strcasestr(line, "on"))
options.allregs = 1;
if (strcasestr(line, "off"))
options.allregs = 0;
continue;
}
if (strcasestr(line, "begin")) {
in_block = 1;
continue;
}
// Note the lack of continue. Need to fall through...
if (strcasestr(line, "end")) {
in_block = 0;
end = 1;
}
}
if (buf_sz + count > sizeof(buf)) {
printf("Buffer full (max: 0x%zx), please use '.end'\n", sizeof(buf));
continue;
}
// Since we fell through, we want to avoid adding .end to our buffer
if (!end) {
memcpy(buf + buf_sz, line, count);
buf_sz += count;
}
if (!in_block) {
verbose_printf("Trying to assemble (%zu):\n%s", buf_sz, buf);
uint8_t bytecode[PAGE_SIZE];
const size_t bytecode_sz = assemble(bytecode, sizeof(bytecode), buf, buf_sz);
verbose_printf("Got asm (%zu):\n", bytecode_sz);
verbose_dump(bytecode, bytecode_sz, -1);
if (!bytecode_sz) {
fprintf(stderr, "assembled to 0 length bytecode:\n%s", buf);
}
memset(buf, 0, sizeof(buf));
buf_sz = 0;
end = 0;
if (!bytecode_sz) {
continue;
}
// round up to nearest ptr_sz + size of at least one trap
const size_t code_buf_sz = ROUNDUP(bytecode_sz + TRAP_SZ, sizeof(long));
uint8_t *code_buf = xmalloc(code_buf_sz);
mem_assign((uint8_t *)code_buf, code_buf_sz, TRAP, TRAP_SZ);
memcpy(code_buf, bytecode, bytecode_sz);
ptrace_write(child_pid, (void *)options.start, code_buf, code_buf_sz);
free(code_buf);
ptrace_reset(child_pid, options.start);
ptrace_cont(child_pid, &info);
if (ptrace_reap(child_pid, &info)) {
child_died = 1;
break;
}
display(&info);
}
}
if (!child_died)
ptrace_detatch(child_pid, &info);
printf("\n");
// we close this one with a file pointer so we can truncate the file
FILE *hist_save = fopen(hist_path, "wb");
REQUIRE (hist_save != NULL);
history(hist, &ev, H_SAVE_FP, hist_save);
REQUIRE (fclose(hist_save) == 0);
history_end(hist);
el_end(el);
}