-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
706 lines (632 loc) · 30.7 KB
/
main.py
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
"""
This is a sandbox of experimental code to use and refine the DebugCore Debugger module
"""
import ctypes
from datetime import datetime
import DebugCore
# disassembler https://pypi.org/project/iced-x86/
import iced_x86
import queue
import tkinter as tk
import json
def _convert_input_number(literal: str):
if literal.lower().startswith('0x'):
return int(literal, 16)
return int(literal)
class DebuggerApp(DebugCore.Debugger):
"""
The main debugger application
DebugCore.Debugger drives the connection and commands to and from the kernel while this class
deals (mostly) with UX and data analysis
"""
__BASE_FONT = ('Consolas', 9)
__DARK_BG = 'gray20'
__DARK_FG = 'lightgray'
__DARK_FRAME_BG = 'gray30'
__TOP_PANE_HEIGHT = 600
__BOTTOM_PANE_HEIGHT = 300
__PANEL_WIDTH = 500 + 728
__DEBUGGER_NOT_CONNECTED_MSG = f'Debugger not connected...'
def __init__(self):
super().__init__()
self._root = tk.Tk()
self._root.title("josKDbg")
self._root.config(bg="pink")
# for now
self._root.resizable(False, False)
# top
self._top_pane = tk.Frame(self._root, height=self.__TOP_PANE_HEIGHT, width=self.__PANEL_WIDTH,
bg=self.__DARK_FRAME_BG)
self._top_pane.pack(fill=tk.BOTH, expand=True, side=tk.TOP)
self._top_pane.pack_propagate(0)
# trace
self.__trace_frame_width = self.__PANEL_WIDTH / 3
self._trace_frame = tk.LabelFrame(self._top_pane, width=self.__trace_frame_width, text="Trace")
self._trace_frame.pack(fill=tk.BOTH, expand=True, side=tk.RIGHT)
self._trace_frame.pack_propagate(0)
self._trace_pane = tk.Frame(self._trace_frame, bg=self.__DARK_FRAME_BG)
self._trace_pane.pack(fill=tk.BOTH, expand=True, side=tk.RIGHT)
self._trace_window = tk.Text(self._trace_pane, wrap=tk.WORD, font=self.__BASE_FONT,
bg=self.__DARK_BG, fg=self.__DARK_FG)
self._trace_window.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)
self._trace_window.configure(state=tk.DISABLED)
self._trace_sb = tk.Scrollbar(self._trace_window)
self._trace_sb.pack(side=tk.RIGHT, fill=tk.BOTH)
self._trace_window.config(yscrollcommand=self._trace_sb.set)
self._trace_sb.config(command=self._trace_window.yview)
# output
self.__output_frame_width = self.__PANEL_WIDTH - self.__trace_frame_width
self._output_frame = tk.LabelFrame(self._top_pane, width=self.__output_frame_width, text="Command")
self._output_frame.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)
self._output_frame.pack_propagate(0)
self._output_pane = tk.Frame(self._output_frame, bg=self.__DARK_FRAME_BG)
self._output_pane.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)
self._output_window = tk.Text(self._output_pane, wrap=tk.WORD, font=self.__BASE_FONT,
bg=self.__DARK_BG, fg=self.__DARK_FG)
self._output_window.tag_configure('assert', foreground='red')
self._output_window.tag_configure('error', foreground='red')
self._output_window.tag_configure('warning', foreground='orange')
self._output_window.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)
self._output_sb = tk.Scrollbar(self._output_window)
self._output_sb.pack(side=tk.RIGHT, fill=tk.BOTH)
self._output_window.config(yscrollcommand=self._output_sb.set)
self._output_sb.config(command=self._output_window.yview)
# bottom
self._bottom_pane = tk.Frame(self._root, height=self.__BOTTOM_PANE_HEIGHT, bg=self.__DARK_FRAME_BG)
self._bottom_pane.pack(fill=tk.BOTH, expand=True, side=tk.BOTTOM)
self._bottom_pane.pack_propagate(0)
# cli pane sits between top and bottom pane, strictly
self._bottom_top_pane = tk.Frame(self._root, bg=self.__DARK_FRAME_BG)
self._bottom_top_pane.pack(fill=tk.X, expand=True, side=tk.BOTTOM)
self._bottom_bottom_pane = tk.Frame(self._bottom_pane, bg=self.__DARK_FRAME_BG)
self._bottom_bottom_pane.pack(fill=tk.BOTH, expand=True, side=tk.BOTTOM)
self._cli_frame = tk.LabelFrame(self._bottom_top_pane, text='CMD')
self._cli_frame.pack(fill=tk.BOTH, expand=True, side=tk.TOP)
self._stack_frame = tk.LabelFrame(self._bottom_bottom_pane, text='Stack')
self._stack_frame.pack(fill=tk.BOTH, expand=True, side=tk.RIGHT)
self._stack_frame.pack_propagate(0)
self._stack_pane = tk.Frame(self._stack_frame, bg=self.__DARK_FRAME_BG)
self._stack_pane.pack(fill=tk.BOTH, expand=True, side=tk.RIGHT)
self._locals_frame = tk.LabelFrame(self._bottom_bottom_pane, text='Locals')
self._locals_frame.pack(fill=tk.BOTH, expand=True, side=tk.RIGHT)
# stack window
self._stack_window = tk.Text(self._stack_pane, wrap=tk.WORD,
font=self.__BASE_FONT, bg=self.__DARK_BG, fg=self.__DARK_FG)
self._stack_window.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)
self._stack_sb = tk.Scrollbar(self._stack_window)
self._stack_sb.pack(side=tk.RIGHT, fill=tk.BOTH)
self._stack_window.config(yscrollcommand=self._stack_sb)
self._stack_sb.config(command=self._stack_window.yview)
# CLI input window
self._prompt = tk.Label(self._cli_frame, text="> ")
self._prompt.pack(side=tk.LEFT)
self._input = tk.StringVar()
self._cli = tk.Entry(self._cli_frame, text=self._input)
self._cli.pack(side=tk.LEFT, fill=tk.BOTH, padx=2, expand=True)
self._cli.bind('<Return>', self._on_cli_enter)
self._cli.bind('<Up>', self._on_cli_up)
self._cli.bind('<Down>', self._on_cli_down)
self._input.set(self.__DEBUGGER_NOT_CONNECTED_MSG)
self._cli.configure(state=tk.DISABLED)
# other internals
self._cli_history = []
self._cli_history_pos = 0
self._asm_formatter = iced_x86.Formatter(iced_x86.FormatterSyntax.NASM)
self._target_memory_request_queue = []
self._trace_log_file = open('traces.log', 'a')
self._trace_log_file.write(f'\nstart trace @ {datetime.now()}\n')
self._kernel_info = None
# CLI commands and handlers
self._commands = {'h': ('help', self._cli_cmd_help)}
self._commands['?'] = self._commands['h']
self._commands['g'] = ('go', self._cli_cmd_go)
self._commands['d'] = ('dump memory', self._cli_cmd_d)
self._commands['r'] = ('dump registers', self._cli_cmd_r)
self._commands['u'] = ('unassemble', self._cli_cmd_u)
self._commands['t'] = ('trace step', self._cli_cmd_t)
self._commands['p'] = ('single step and step over', self._cli_cmd_p)
self._commands['.pt'] = ('page table traverse', self._cli_cmd_pt)
self._commands['rdmsr'] = ('read MSR', self._cli_cmd_rdmsr)
self._commands['bp'] = ('set breakpoint @ address or symbol name', self._cli_cmd_bp)
self._commands['bl'] = ('list breakpoints', self._cli_cmd_bl)
self._commands['be'] = ('enable breakpoint', self._cli_cmd_be)
self._commands['bd'] = ('disable breakpoint', self._cli_cmd_bd)
self._commands['bc'] = ('clear breakpoint', self._cli_cmd_bc)
self._commands['ds'] = ('dump structure', self._cli_cmd_ds)
self._commands['s'] = ('stack dump N levels', self._cli_cmd_s)
self._commands['cpuid'] = ('cpuid leaf X', self._cli_cmd_cpuid)
self._commands['di'] = ('dump kernel info', self._cli_cmd_di)
self._commands['mm'] = ('dump a memory map', self._cli_cmd_mm)
def _print_output(self, text, tag=None):
self._output_window.insert(tk.END, text, tag)
self._output_window.see(tk.END)
def _print_trace(self, text, tag=None):
self._trace_window.configure(state=tk.NORMAL)
self._trace_window.insert(tk.END, text, tag)
self._trace_window.configure(state=tk.DISABLED)
self._trace_window.see(tk.END)
def _on_print_callstack_entry(self, text):
self._stack_window.configure(state=tk.NORMAL)
self._stack_window.insert(tk.END, text)
self._stack_window.configure(state=tk.DISABLED)
self._stack_window.see(tk.END)
def _clear_stack(self):
self._stack_window.configure(state=tk.NORMAL)
self._stack_window.delete("1.0", "end")
self._stack_window.configure(state=tk.DISABLED)
def _cli_disable(self):
self._cli.configure(state=tk.DISABLED)
def _cli_enable(self):
self._cli.configure(state=tk.NORMAL)
self._input.set('')
def _on_target_memory_read(self, packet):
at, handler, extradata = self._target_memory_request_queue.pop()
handler(packet, at, extradata)
def _on_assert(self, json_data):
import json
assert_obj = json_data['assert']
cond = assert_obj['cond']
file = assert_obj['file']
line = assert_obj['line']
self._print_output(f'\nASSERT:\n\t{cond}\n\tin {file} @ line {line}\n'
f'EXECUTION WILL NOT CONTINUE', 'assert')
def _on_cpuid(self, packet):
regs_type = ctypes.c_uint32 * 6
regs = regs_type.from_buffer_copy(packet)
self._print_output(f'\nCPUID : {hex(regs[0])} (ecx={hex(regs[1])})\n'
f'\teax {hex(regs[2])}\tebx {hex(regs[3])}'
f'\tecx {hex(regs[4])}\tedx {hex(regs[5])}\n')
def _on_get_pagetable_info(self, table_info):
self._print_output(f'\npagetable info for {hex(table_info.address)}:\n')
pml4e = table_info.entries[0]
pdpte = table_info.entries[1]
if (pdpte & 1) == 0:
self._print_output(f'\tNOT PRESENT\n')
else:
if pdpte & (1 << 7):
# 1GB pages
phys_base = pdpte & ~0x1ff
flags = pdpte & 0xfff
self._print_output(f'\t(1GB page): phys @ '
f'{hex(phys_base + (table_info.address & 0x3fffffff))}'
f' flags {hex(flags)}\n')
else:
pde = table_info.entries[2]
if (pde & 1) == 0:
self._print_output(f'\tNOT PRESENT\n')
else:
if pde & (1 << 7):
# 2MB pages
phys_base = pde & ~0x1ff
flags = pde & 0xfff
self._print_output(f'\t(2MB page): phys @ '
f'{hex(phys_base + (table_info.address & 0x1fffff))}'
f' flags {hex(flags)}\n')
else:
# 4KB pages
pte = table_info.entries[3]
if (pte & 1) == 0:
self._print_output(f'\tNOT PRESENT\n')
else:
phys_base = pte & ~0x1ff
flags = pte & 0xfff
self._print_output(f'\t(4KB page): phys @ '
f'{hex(phys_base + (table_info.address & 0xfff))}'
f' flags {hex(flags)}\n')
def _on_read_msr(self, msr_packet):
self._print_output(f'\nMSR info for {hex(msr_packet.msr)}, '
f'lo: {hex(msr_packet.lo)} hi: {hex(msr_packet.hi)}\n')
def _cli_cmd_help(self, _):
self._print_output(f'\nCommands:\n')
for command, info in self._commands.items():
self._print_output(f'\t{command}\t\t\t{info[0]}\n')
def _cli_cmd_di(self, _):
self._print_output(json.dumps(self._kernel_info))
def _cli_cmd_go(self, _):
self._input.set(self.__DEBUGGER_NOT_CONNECTED_MSG)
self._cli_disable()
self.synchronize_kernel()
self.continue_execution()
def _cli_cmd_cpuid(self, cmd_parts):
if len(cmd_parts) > 1:
try:
leaf = _convert_input_number(cmd_parts[1])
subleaf = 0
if len(cmd_parts) > 2:
subleaf = _convert_input_number(cmd_parts[2])
self.cpuid(leaf, subleaf)
except ValueError:
pass
def _cli_cmd_r(self, _):
self._dump_registers(self._last_bp_packet)
def _cli_cmd_mm(self, _):
self.memory_map()
def _cli_cmd_u(self, cmd_parts):
target = self._last_bp_packet.stack.rip
if len(cmd_parts) > 1:
target = _convert_input_number(cmd_parts[1])
self._target_memory_request_queue.append((target, self._disassemble_bytes_impl, None))
self.read_target_memory(target, 52)
def _cli_cmd_d(self, cmd_parts):
if len(cmd_parts) > 1:
target = _convert_input_number(cmd_parts[1])
else:
target = self._last_bp_packet.stack.rip
self._target_memory_request_queue.append((target, self._dump_memory_bytes,
None if len(cmd_parts) < 3 else cmd_parts))
self.read_target_memory(target, 8 * 16)
def _cli_cmd_t(self, _):
self.synchronize_kernel()
self.trace_step()
def _cli_cmd_p(self, _):
self.synchronize_kernel()
self.single_step()
def _cli_cmd_pt(self, cmd_parts):
if len(cmd_parts) > 1:
target = _convert_input_number(cmd_parts[1])
else:
target = self._last_bp_packet.stack.rip
self.traverse_pagetable(target)
def _cli_cmd_rdmsr(self, cmd_parts):
if len(cmd_parts) > 1:
self.read_msr(_convert_input_number(cmd_parts[1]))
def _cli_cmd_ds(self, cmd_parts):
if len(cmd_parts) > 1:
# TODO for now the argument has to be a variable name
try:
var_info = self._pdb.get_variable_declaration(cmd_parts[1])
if var_info is None:
self._print_output(f'{cmd_parts[1]} is not found in PDB\n')
return
if var_info[1] != 'LF_STRUCTURE':
self._print_output(f'{cmd_parts[1]} is not a structure\n')
return
struct_info = self._pdb.get_structure_info(var_info[0])
packed_size = 0
for field in struct_info:
packed_size = packed_size + field[2]
target = self.rva_to_phys(var_info[2].offset, var_info[2].segment - 1)
self._target_memory_request_queue.append((target, self._dump_structure,
(cmd_parts[1], var_info, struct_info)))
self.read_target_memory(target, packed_size)
except Exception as e:
self._print_output(str(e))
def _cli_cmd_bp(self, cmd_parts):
if len(cmd_parts) == 1:
return
target = None
try:
target = _convert_input_number(cmd_parts[1])
except ValueError:
symbol_info = self._pdb.lookup_by_symbol(cmd_parts[1])
if symbol_info is not None:
target = symbol_info[2]
finally:
if target is not None and self.set_breakpoint(target):
self._print_output(f'breakpoint {self._breakpoints[target][1]} set @ {hex(target)}\n')
def _cli_cmd_bl(self, _):
self._print_output(f'\n#\taddr\tcall site\n')
for target, bp in self._breakpoints.items():
if DebugCore.breakpoint_marked_for_clear(bp):
continue
lookup = self._pdb.lookup_symbol_at_address(target)
self._print_output("".join([f'{bp[1]}\t{hex(target)}\t{lookup}\t',
'(enabled)' if bp[0] else '(disabled)', '\n']))
def _cli_cmd_be(self, cmd_parts):
if len(cmd_parts) == 1:
return
try:
index = _convert_input_number(cmd_parts[1])
self.enable_breakpoint(index)
except KeyError:
# not found
pass
except ValueError:
# invalid argument
pass
def _cli_cmd_bd(self, cmd_parts):
if len(cmd_parts) == 1:
return
try:
index = _convert_input_number(cmd_parts[1])
self.disable_breakpoint(index)
except KeyError or IndexError:
# not found
pass
except ValueError:
# invalid argument
pass
def _cli_cmd_bc(self, cmd_parts):
if len(cmd_parts) == 1:
return
try:
index = _convert_input_number(cmd_parts[1])
self.clear_breakpoint(index)
except KeyError:
# not found
pass
except ValueError:
# invalid argument
pass
def _cli_exec_cmd(self, cmd):
if len(cmd) == 0:
return
cmd_parts = cmd.split()
try:
command = self._commands[cmd_parts[0]]
# execute the command handler
command[1](cmd_parts)
self._cli_history.append(cmd)
except KeyError:
self._print_output(f'\nunknown command\n', 'warning')
finally:
# always clear the command
self._input.set('')
def _on_cli_enter(self, _):
cmd = self._input.get().lstrip()
self._cli_exec_cmd(cmd)
def _on_cli_up(self, _):
if len(self._cli_history) == 0:
return
self._cli_history_pos = (self._cli_history_pos - 1) % len(self._cli_history)
cmd = self._cli_history[self._cli_history_pos]
self._input.set(cmd)
def _on_cli_down(self, _):
if len(self._cli_history) == 0:
return
self._cli_history_pos = (self._cli_history_pos + 1) % len(self._cli_history)
cmd = self._cli_history[self._cli_history_pos]
self._input.set(cmd)
def _cli_cmd_s(self, cmd):
depth = 4
if len(cmd) > 1:
try:
depth = max(depth, _convert_input_number(cmd[1]))
except ValueError:
self._print_output(f'\ninvalid argument\n', 'warning')
self._target_memory_request_queue.append((self._last_bp_packet.stack.rsp, self._dump_stack, depth))
self.read_target_memory(self._last_bp_packet.stack.rsp, depth * 8)
def run(self, pe_path, pdb_path):
self.set_paths(pe_path, pdb_path)
try:
self.pipe_connect(r'\\.\pipe\josxDbg')
while True:
self.update()
self._root.update_idletasks()
self._root.update()
except Exception as e:
print(f'disconnecting : {str(e)}')
finally:
self._trace_log_file.close()
def _dump_stack(self, raw_bytes, at, depth):
self._print_output(f'\nstack dump from {hex(at)}, {depth} levels:\n')
offset = 0
for d in range(depth):
self._print_output(f'{hex(at + offset)}\t'
f'{hex(ctypes.c_uint64.from_buffer_copy(raw_bytes[offset:offset + 8]).value)}\n')
offset = offset + 8
def _dump_structure(self, raw_bytes, at, extra_data):
var_name = extra_data[0]
var_info = extra_data[1]
struct_info = extra_data[2]
phys = self.rva_to_phys(var_info[2].offset, var_info[2].segment - 1)
self._print_output(f'\n{var_info[0]} {var_name} @ {hex(phys)} (rva {hex(var_info[2].offset)})\n')
offset = 0
for field in struct_info:
# TODO for now...
if field[2] == 8:
val = ctypes.c_uint64.from_buffer_copy(raw_bytes[offset:offset + 8])
self._print_output(f'\t{field[0]}\t{hex(val.value)};\n')
elif field[2] == 4:
val = ctypes.c_uint32.from_buffer_copy(raw_bytes[offset:offset + 4])
self._print_output(f'\t{field[0]}\t{hex(val.value)};\n')
elif field[2] == 2:
val = ctypes.c_uint16.from_buffer_copy(raw_bytes[offset:offset + 2])
self._print_output(f'\t{field[0]}\t{hex(val.value)};\n')
elif field[2] == 1:
val = ctypes.c_uint8.from_buffer_copy(raw_bytes[offset:offset + 1])
self._print_output(f'\t{field[0]}\t{hex(val.value)};\n')
else:
self._print_output(f'\t{field[0]}\tUNKNOWN TYPE;\n')
return
offset = offset + field[2]
def _dump_memory_bytes(self, raw_bytes, at, args):
self._print_output('\n')
runs = len(raw_bytes) // 16
i = 0
width = 1
steps = 1
if args is not None:
width = _convert_input_number(args[2])
if width > 16:
raise Exception("dump command unit size > 16 not supported")
steps = 16//width
for j in range(runs):
run = raw_bytes[i:i + 16]
literal = "".join([chr(b) if 31 < b < 128 else '.' for b in run])
if width == 1:
bytes_str = run.hex(' ').lower()
else:
# TODO: optimise...
chunks = []
for s in range(steps):
chunks.append(run[s*width:(s+1)*width].hex().lower())
bytes_str = " ".join(chunks)
self._print_output(f'{at:016x} {bytes_str} {literal}\n')
i = i + 16
rem = len(raw_bytes) % 16
if rem:
run = raw_bytes[i:i + rem]
bytes_str = run.hex(' ').lower()
literal = "".join([chr(b) if 31 < b < 128 else '.' for b in run])
bytes_str = bytes_str.ljust(3 * 16 - 1, ' ')
self._print_output(f'{at:016x} {bytes_str} {literal}\n')
def _disassemble_output_instruction(self, instr: iced_x86.Instruction, bytes_str: str, disasm: str,
lookup_calls: bool):
if lookup_calls:
cflow = instr.flow_control
if cflow == iced_x86.FlowControl.CALL or cflow == iced_x86.FlowControl.INDIRECT_CALL:
call_target = 0
if instr.op0_kind == iced_x86.OpKind.REGISTER:
# TODO: TESTING ONLY
if instr.op0_register == iced_x86.Register.RAX:
call_target = self._last_bp_packet.stack.rax
elif instr.op0_register == iced_x86.Register.RCX:
call_target = self._last_bp_packet.stack.rcx
elif instr.op0_register == iced_x86.Register.RDX:
call_target = self._last_bp_packet.stack.rdx
elif instr.op0_register == iced_x86.Register.R8:
call_target = self._last_bp_packet.stack.r8
elif instr.op0_register == iced_x86.Register.R9:
call_target = self._last_bp_packet.stack.r9
elif instr.op0_register == iced_x86.Register.R10:
call_target = self._last_bp_packet.stack.r10
elif instr.op0_register == iced_x86.Register.R11:
call_target = self._last_bp_packet.stack.r11
elif instr.op0_register == iced_x86.Register.R12:
call_target = self._last_bp_packet.stack.r12
elif instr.op0_register == iced_x86.Register.R13:
call_target = self._last_bp_packet.stack.r13
elif instr.op0_register == iced_x86.Register.R14:
call_target = self._last_bp_packet.stack.r14
elif instr.op0_register == iced_x86.Register.R15:
call_target = self._last_bp_packet.stack.r15
elif instr.op0_register == iced_x86.Register.RSI:
call_target = self._last_bp_packet.stack.rsi
elif instr.op0_register == iced_x86.Register.RDI:
call_target = self._last_bp_packet.stack.rdi
if call_target != 0:
lookup = self._pdb.lookup_symbol_at_address(call_target)
disasm = disasm + ' ==> ' + lookup
self._print_output(f'{instr.ip:016x} {bytes_str:30} {disasm}\n')
def _disassemble_bytes_impl(self, raw_bytes, at, _):
lookup = self._pdb.lookup_symbol_at_address(at)
self._print_output(f'\n{lookup}:\n')
decoder = iced_x86.Decoder(64, raw_bytes, ip=at)
line = 0
for instr in decoder:
if instr.code == iced_x86.Code.INVALID or line == 5:
break
disasm = self._asm_formatter.format(instr)
start_index = instr.ip - at
bytes_str = raw_bytes[start_index:start_index + instr.len].hex().lower()
self._disassemble_output_instruction(instr, bytes_str, disasm, False)
line = line + 1
def _on_connect_impl(self, kernel_info_json):
self._kernel_info = kernel_info_json
image_info = kernel_info_json['image_info']
entry_point = image_info['entry_point']
version_str = str(kernel_info_json['version']['major']) + \
'.' + \
str(kernel_info_json['version']['minor']) + \
'.' + \
str(kernel_info_json['version']['patch'])
self._print_output(f'\nconnected to kernel {version_str}, base is @ {hex(self._image_base)}, '
f'entry point @ {hex(entry_point)}\n')
self._print_output('kernel reports available RAM ' +
str(kernel_info_json['system_info']['memory'])
+ ', and '
+ str(kernel_info_json['system_info']['processors']) + ' processors\n')
self._print_output('CPU vendor string: ' + kernel_info_json['system_info']['vendor'] + '\n')
try:
hypervisor = kernel_info_json['hypervisor']
self._print_output('Hypervisor vendor string: ' + hypervisor['id'] + '\n')
except KeyError:
pass
self._print_output('\n')
def _dump_registers(self, bp_packet):
self._print_output(
f'\nrax {bp_packet.stack.rax:016x} rbx {bp_packet.stack.rbx:016x} rcx '
f'{bp_packet.stack.rcx:016x} rdx {bp_packet.stack.rdx:016x}')
self._print_output(
f'\nrsi {bp_packet.stack.rsi:016x} rdi {bp_packet.stack.rdi:016x} rsp '
f'{bp_packet.stack.rsp:016x} rbp {bp_packet.stack.rbp:016x}')
self._print_output(
f'\nr8 {bp_packet.stack.r8:016x} r9 {bp_packet.stack.r9:016x} r10 '
f'{bp_packet.stack.r10:016x} r11 {bp_packet.stack.r11:016x}')
self._print_output(
f'\nr12 {bp_packet.stack.r12:016x} r13 {bp_packet.stack.r13:016x} r14 '
f'{bp_packet.stack.r14:016x} r15 {bp_packet.stack.r15:016x}')
self._print_output(
f'\nrflags {bp_packet.stack.rflags:08x} cs {bp_packet.stack.cs:02x} ss '
f'{bp_packet.stack.ss:02x}\n')
if bp_packet.stack.rflags & (1 << 0) != 0:
self._print_output('CF ')
if bp_packet.stack.rflags & (1 << 6) != 0:
self._print_output('ZF ')
if bp_packet.stack.rflags & (1 << 7) != 0:
self._print_output('SF ')
if bp_packet.stack.rflags & (1 << 8) != 0:
self._print_output('TF ')
if bp_packet.stack.rflags & (1 << 9) != 0:
self._print_output('IF ')
if bp_packet.stack.rflags & (1 << 10) != 0:
self._print_output('DF ')
if bp_packet.stack.rflags & (1 << 11) != 0:
self._print_output('OF ')
self._print_output(
f'\ncr0 {bp_packet.cr0:08x} cr3 {bp_packet.cr3:08x} cr4 '
f'{bp_packet.cr4:08x}\n')
self._print_output('\n')
def _on_breakpoint(self):
try:
lookup = self._pdb.lookup_symbol_at_address(self._last_bp_packet.stack.rip)
self._print_output(f'\n>break - code @ {lookup}\n')
self._clear_stack()
if self._last_bp_packet.call_stack_size > 0:
callstack = (ctypes.c_uint64 * self._last_bp_packet.call_stack_size) \
.from_buffer_copy(self._last_bp_callstack)
self._on_print_callstack_entry(f'{hex(self._last_bp_packet.stack.rip)}\t{lookup}\n')
self.process_callstack(callstack)
raw_bytes = bytearray(self._last_bp_packet.instruction)
instr = iced_x86.Decoder(64, raw_bytes, ip=self._last_bp_packet.stack.rip).decode()
disasm = self._asm_formatter.format(instr)
bytes_str = raw_bytes[:instr.len].hex().lower()
self._disassemble_output_instruction(instr, bytes_str, disasm, True)
# allow input
self._cli_enable()
except Exception as e:
print(str(e))
def _on_pf(self):
lookup = self._pdb.lookup_symbol_at_address(self._last_bp_packet.stack.rip)
error_code = self._last_bp_packet.stack.error_code
cr2 = self._last_bp_packet.cr2
p = 'page level protection' if (error_code & (1 << 0)) else 'page not-present'
wr = 'write' if (error_code & (1 << 1)) else 'read'
us = 'user mode' if (error_code & (1 << 2)) else 'kernel mode'
rsvd = 'reserved bit violation' if (error_code & (1 << 3)) else ''
instr = 'instruction fetch' if (error_code & (1 << 4)) else ''
flags = ' '.join([p, wr, us, rsvd, instr])
self._print_output(f'\n>PAGE FAULT - code @ {lookup}\n\t'
f'@ {hex(cr2)}: {flags}', 'error')
# allow input
self._cli_enable()
def _on_gpf(self):
"""
Could be any of:
* Executing a privileged instruction while CPL > 0.
* Writing a 1 into any register field that is reserved, must be zero (MBZ).
* Attempting to execute an SSE instruction specifying an unaligned memory operand.
* Loading a non-canonical base address into the GDTR or IDTR.
* Using WRMSR to write a read-only MSR.
* Any long-mode consistency-check violation.
"""
lookup = self._pdb.lookup_symbol_at_address(self._last_bp_packet.stack.rip)
error_code = self._last_bp_packet.stack.error_code
self._print_output(f'\n>GENERAL PROTECTION FAULT - code @ {hex(self._last_bp_packet.stack.rip)} : {lookup}, '
f'error code {hex(error_code)}', 'error')
self._on_breakpoint()
def _process_trace_queue_impl(self, trace_queue: queue.Queue):
lines = []
while not trace_queue.empty():
line = trace_queue.get_nowait()
self._print_trace(f'\n{line}')
lines.append(line)
trace_queue.task_done()
if len(lines) > 0:
self._trace_log_file.writelines("\n".join(lines))
self._trace_log_file.write('\n')
if __name__ == '__main__':
app = DebuggerApp()
app.run(f'e:/dev/osdev/josx64/build/bootx64.efi', f'e:/dev/osdev/josx64/build/bootx64.pdb')
app._root.mainloop()