-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathwindbg.py
473 lines (385 loc) · 13.8 KB
/
windbg.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
__AUTHOR__ = "hugsy"
__VERSION__ = 0.3
from typing import TYPE_CHECKING, Callable, List
if TYPE_CHECKING:
from . import *
from . import gdb
import subprocess
import gdb
GEF_DOCS_URL = "https://hugsy.github.io/gef/"
SEARCH_URL = f"https://www.google.com/search?q=site:{GEF_DOCS_URL}"
class BreakOnLoadSharedLibrary(gdb.Breakpoint):
def __init__(self, module_name):
super().__init__("dlopen", type=gdb.BP_BREAKPOINT, internal=False)
self.module_name = module_name
self.silent = True
self.enabled = True
return
def stop(self):
if len(gef.arch.function_parameters) == 0:
return False
reg = gef.arch.function_parameters[0]
addr = lookup_address(gef.arch.register(reg))
if addr.value == 0:
return False
path = gef.memory.read_cstring(addr.value)
if path.endswith(self.module_name):
return True
return False
@register
class WindbgSxeCommand(GenericCommand):
"""WinDBG compatibility layer: sxe (set-exception-enable): break on loading libraries."""
_cmdline_ = "sxe"
_syntax_ = f"{_cmdline_} [ld,ud]:module"
_example_ = f"{_cmdline_} ld:mylib.so"
def __init__(self):
super().__init__(complete=gdb.COMPLETE_NONE)
self.breakpoints = []
return
def do_invoke(self, argv):
if len(argv) < 1:
self.usage()
return
action, module = argv[0].split(":", 1)
if action == "ld":
self.breakpoints.append(BreakOnLoadSharedLibrary(module))
elif action == "ud":
bkps = [bp for bp in self.breakpoints if bp.module_name == module]
if len(bkps):
bkp = bkps[0]
bkp.enabled = False
bkp.delete()
bkps.remove(bkp)
else:
self.usage()
return
def windbg_execute_until(
cnt: int, cmd: str, stop_condition: Callable[[Instruction], bool]
):
while cnt:
cnt -= 1
gef.config["context.enable"] = False
gdb.execute(cmd)
insn = gef_current_instruction(gef.arch.pc)
if stop_condition(insn):
break
gef.config["context.enable"] = True
return
@register
class WindbgTcCommand(GenericCommand):
"""WinDBG compatibility layer: tc - trace until next call."""
_cmdline_ = "tc"
_syntax_ = f"{_cmdline_} [COUNT]"
@only_if_gdb_running
def do_invoke(self, argv):
cnt = int(argv[0]) if len(argv) else 0xFFFFFFFFFFFFFFFF
windbg_execute_until(cnt, "stepi", gef.arch.is_call)
gdb.execute("context")
return
@register
class WindbgPcCommand(GenericCommand):
"""WinDBG compatibility layer: pc - run until next call."""
_cmdline_ = "pc"
_syntax_ = f"{_cmdline_} [COUNT]"
@only_if_gdb_running
def do_invoke(self, argv):
cnt = int(argv[0]) if len(argv) else 0xFFFFFFFFFFFFFFFF
windbg_execute_until(cnt, "nexti", gef.arch.is_call)
gdb.execute("context")
return
@register
class WindbgTtCommand(GenericCommand):
"""WinDBG compatibility layer: tt - trace until next return."""
_cmdline_ = "tt"
_syntax_ = f"{_cmdline_} [COUNT]"
@only_if_gdb_running
def do_invoke(self, argv):
cnt = int(argv[0]) if len(argv) else 0xFFFFFFFFFFFFFFFF
windbg_execute_until(cnt, "stepi", gef.arch.is_ret)
gdb.execute("context")
return
@register
class WindbgPtCommand(GenericCommand):
"""WinDBG compatibility layer: pt - run until next return."""
_cmdline_ = "pt"
_syntax_ = f"{_cmdline_} [COUNT]"
@only_if_gdb_running
def do_invoke(self, argv):
cnt = int(argv[0]) if len(argv) else 0xFFFFFFFFFFFFFFFF
windbg_execute_until(cnt, "nexti", gef.arch.is_ret)
gdb.execute("context")
return
@register
class WindbgPtcCommand(GenericCommand):
"""WinDBG compatibility layer: ptc - run until next call or return."""
_cmdline_ = "ptc"
_syntax_ = f"{_cmdline_} [COUNT]"
@only_if_gdb_running
def do_invoke(self, argv):
cnt = int(argv[0]) if len(argv) else 0xFFFFFFFFFFFFFFFF
def fn(x) -> bool:
return gef.arch.is_ret(x) or gef.arch.is_call(x)
windbg_execute_until(cnt, "nexti", fn)
gdb.execute("context")
return
@register
class WindbgHhCommand(GenericCommand):
"""WinDBG compatibility layer: hh - open help in web browser."""
_cmdline_ = "hh"
_syntax_ = f"{_cmdline_}"
def do_invoke(self, argv):
url = GEF_DOCS_URL
if len(argv):
url += "search.html?q={}".format(argv[0])
subprocess.Popen(
["xdg-open", url], cwd="/", stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
return
@register
class WindbgGoCommand(GenericCommand):
"""WinDBG compatibility layer: g - go."""
_cmdline_ = "g"
_syntax_ = _cmdline_
def do_invoke(self, argv):
if is_alive():
gdb.execute("continue")
else:
gdb.execute(f"run {' '.join(argv)}")
return
@register
class WindbgUCommand(GenericCommand):
"""WinDBG compatibility layer: u - disassemble."""
_cmdline_ = "u"
_syntax_ = _cmdline_
def __init__(self):
super().__init__(complete=gdb.COMPLETE_LOCATION)
return
@only_if_gdb_running
def do_invoke(self, argv: List[str]):
length = 16
location = gef.arch.pc
for arg in argv:
if arg[0] in ("l", "L"):
length = int(arg[1:])
else:
loc = safe_parse_and_eval(arg)
if loc:
if hasattr(loc, "address") and loc.address:
location = int(loc.address, 0) # type: ignore
else:
location = int(loc, 0) # type: ignore
for insn in gef_disassemble(location, length):
gef_print(str(insn))
return
@register
class WindbgXCommand(GenericCommand):
"""WinDBG compatibility layer: x - search symbol."""
_cmdline_ = "xs"
_syntax_ = "{:s} REGEX".format(_cmdline_)
def __init__(self):
super().__init__(complete=gdb.COMPLETE_LOCATION)
return
def do_invoke(self, argv):
if len(argv) < 1:
err("Missing REGEX")
return
sym = argv[0]
try:
gdb.execute("info function {}".format(sym))
gdb.execute("info address {}".format(sym))
except gdb.error:
pass
return
@register
class WindbgRCommand(GenericCommand):
"""WinDBG compatibility layer: r - register info"""
_cmdline_ = "r"
_syntax_ = f"{_cmdline_} [REGISTER[=VALUE]]"
def print_regs(self, reg_list, width=16):
n = self.arch_reg_width()
max_reg_len = max(map(len, reg_list))
def chunks(l, n):
for ii in range(0, len(l), n):
yield l[ii : ii + n]
def print_reg(reg, width=16):
fmt = f"%s=%0{width}x".format()
regval = gef.arch.register(f"${reg}") & ((1 << (gef.arch.ptrsize * 8)) - 1)
gef_print(fmt % (reg.rjust(max_reg_len), regval), end="")
for regs in chunks(reg_list, n):
for ii in range(0, len(regs)):
reg = regs[ii]
if reg is not None:
print_reg(reg, width)
if ii + 1 != len(regs):
gef_print(" ", end="")
else:
gef_print()
def arch_reg_width(self):
if gef.arch.arch.startswith("i386:x86-64"):
return 3
if gef.arch.arch.startswith("i386"):
return 6
if gef.arch.arch.startswith("aarch64"):
return 4
raise NotImplementedError
def print_gprs(self):
gprs = None
if gef.arch.arch == "i386":
# eax=00473dc8 ebx=00622000 ecx=00000000 edx=a0b086dc esi=00a40620 edi=00a44890
# eip=00461003 esp=005bf8c8 ebp=005bf8c8
gprs = [
"eax",
"ebx",
"ecx",
"edx",
"esi",
"edi",
"eip",
"esp",
"ebp",
]
self.print_regs(gprs, 8)
# cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=0000020
segregs = ["cs", "ss", "ds", "es", "fs", "gs"]
self.print_regs(segregs, 4)
return
elif gef.arch.arch.startswith("i386:x86-64"):
# rax=0000000000000000 rbx=000000e62e50b000 rcx=00007ffb4763c564
# rdx=0000000000000000 rsi=00007ffb476cd4c0 rdi=0000000000000010
# rip=00007ffb4767121c rsp=000000e62e28f140 rbp=0000000000000000
# r8=000000e62e28f138 r9=0000000000000000 r10=0000000000000000
# r11=0000000000000246 r12=0000000000000001 r13=0000000000000000
# r14=00007ffb476ccd90 r15=0000027685520000
gprs = [
"rax",
"rbx",
"rcx",
"rdx",
"rsi",
"rdi",
"rip",
"rsp",
"rbp",
"r8",
"r9",
"r10",
"r11",
"r12",
"r13",
"r14",
"r15",
]
self.print_regs(gprs)
# cs=0033 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00000202
segregs = ["cs", "ss", "ds", "es", "fs", "gs"]
self.print_regs(segregs, 4)
return
elif gef.arch.arch.startswith("aarch64"):
# x0=0000000000000078 x1=000002037b0069e0 x2=0000000000000010 x3=000000293a8ff9f0
# x4=000000293a8ffb90 x5=000000293a8ff9eb x6=0000000000000000 x7=0000000000000000
# x8=0000000000000072 x9=0000000000000000 x10=0000000000000000 x11=ffffffffff817aa9
# x12=00007ffea822efe0 x13=0000000000000967 x14=00007ffea8241b1e x15=00007ffea822f008
# x16=0000000000000000 x17=0000000000000000 x18=0000000000000000 x19=000002037b006d60
# x20=000002037b0069e0 x21=000002037b004c10 x22=0000000000000010 x23=000000007ffe03c0
# x24=0000000000000001 x25=0000000000000000 x26=000000293a8ff9e0 x27=0000000000000010
# x28=0000000000000000 fp=000000293a8ffc10 lr=00007ffea80f7388 sp=000000293a8ff9e0
# pc=00007ffea80e30f4 psr=60000000 -ZC- EL0
gprs = [
"x0",
"x1",
"x2",
"x3",
"x4",
"x5",
"x6",
"x7",
"x8",
"x9",
"x10",
"x11",
"x12",
"x13",
"x14",
"x15",
"x16",
"x17",
"x18",
"x19",
"x20",
"x21",
"x22",
"x23",
"x24",
"x25",
"x26",
"x27",
"x28",
"fp",
"lr",
"sp",
"pc",
]
self.print_regs(gprs)
return
raise NotImplementedError
@only_if_gdb_running
def do_invoke(self, argv):
if len(argv) < 1:
self.print_gprs()
else:
combined = "".join(argv).replace(" ", "").replace("@", "")
if "=" in combined:
(regstr, valstr) = combined.split("=")
reg = f"${regstr}"
val = int(valstr, 16)
gdb.execute("set {:s} = {:#x}".format(reg, val))
else:
regs = combined.split(",")
self.print_regs(regs)
return
def __windbg_prompt__(current_prompt: Callable) -> str:
"""WinDBG prompt function."""
p = "0:000 ➤ "
if gef.config["gef.disable_color"] is True:
return p
if is_alive():
return Color.colorify(p, attrs="bold green")
else:
return Color.colorify(p, attrs="bold red")
def __default_prompt__(x):
if (
gef.config["gef.use-windbg-prompt"] is False
or gef.config["gef.readline_compat"] is True
):
return __gef_prompt__(x)
return __windbg_prompt__(x)
# Prompt
gef.config["gef.use-windbg-prompt"] = GefSetting(
False, description="Use WinDBG like prompt"
)
gdb.prompt_hook = __default_prompt__
# Aliases
GefAlias("da", "display/s", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("dt", "pcustom")
GefAlias("dq", "hexdump qword", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("dd", "hexdump dword", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("dw", "hexdump word", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("db", "hexdump byte", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("eq", "patch qword", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("ed", "patch dword", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("ew", "patch word", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("eb", "patch byte", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("ea", "patch string", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("dps", "dereference", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("bp", "break", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("bl", "info breakpoints")
GefAlias("bd", "disable breakpoints")
GefAlias("bc", "delete breakpoints")
GefAlias("be", "enable breakpoints")
GefAlias("tbp", "tbreak", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("s", "grep", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("pa", "advance", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("t", "stepi", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("p", "nexti", completer_class=gdb.COMPLETE_LOCATION)
GefAlias("kp", "info stack")
GefAlias("uf", "disassemble")