-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawdbg.gdb
409 lines (364 loc) · 12.1 KB
/
rawdbg.gdb
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
# -*- init -*-
set pagination off
set confirm off
set disassembly-flavor intel
define-prefix rawdbg
# platform {
# "default": 0,
# "linux" : 1,
# "bsd" : 2,
# }
set $rawdbg_platform=2
set $rawdbg_arch=0
set $rawdbg_align=4
# debug mode
set $rawdbg_debug=0
# logging
set logging overwrite on
set logging redirect
set $RAWDBGPATH = "/root/rawdbg"
eval "set $rawdbg_log_file = \"%s/.gdblog\"",$RAWDBGPATH
eval "set $rawdbg_run_file = \"%s/.gdbrun\"",$RAWDBGPATH
eval "set logging file %s",$rawdbg_log_file
# get width
set logging on
show width
set logging off
eval "shell echo \"set \\$rawdbg_console_width=$(cat %s | sed 's@.*a line is @@g' | sed 's@\\.@@g' | xargs echo -n)\" > %s",$rawdbg_log_file,$rawdbg_run_file
eval "source %s",$rawdbg_run_file
# default value
set $rawdbg_loop_cnt = 4
set $rawdbg_tmp = 0
# @function: rawdbg strcmp
# @file: lib/rawdbg_strcmp.sh
# @return: $rawdbg_strcmp_result
define rawdbg strcmp
if $argc != 2
shell echo "read rawdbg strcmp document"
else
eval "shell %s/lib/rawdbg_strcmp.sh %s %s %s",(char *)$RAWDBGPATH,$rawdbg_run_file,$arg0,$arg1
eval "source %s",$rawdbg_run_file
if $rawdbg_debug == 1
eval "shell echo \"[D] [rawdbg strcmp] \\$rawdbg_strcmp_result = %d\"",$rawdbg_strcmp_result
end
end
end
# @function: rawdbg arch
# @file: lib/rawdbg_arch.sh
# @return: $rawdbg_arch
define rawdbg arch
if $argc == 0
set logging on
show architecture
set logging off
eval "shell %s/lib/rawdbg_arch.sh %s %s",(char *)$RAWDBGPATH,$rawdbg_log_file,$rawdbg_run_file
eval "source %s",$rawdbg_run_file
if $rawdbg_debug == 1
eval "shell echo \"[D] [rawdbg arch] \\$rawdbg_arch = %d\"",$rawdbg_arch
end
else
shell echo "[-] [rawdbg arch] argument error"
end
end
# @function: vmmap
# @file: lib/vmmap.sh
define vmmap
rawdbg arch
if $argc == 0
set logging on
info proc mappings
set logging off
eval "shell %s/lib/vmmap.sh %d %s",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file
else
if $argc == 1
set logging on
info proc mappings
set logging off
if $rawdbg_align == 4
eval "shell %s/lib/vmmap.sh %d %s %#x",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file,$arg0
else
eval "shell %s/lib/vmmap.sh %d %s %#llx",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file,$arg0
end
else
shell echo "read vmmap document"
end
end
end
# @function: rawdbg symbol
# @file: lib/rawdbg_symbol.sh
# @return: $rawdbg_symbol_address
define rawdbg symbol
if $argc != 1
eval "shell echo \"read rawdbg_symbol document\""
else
set $rawdbg_symbol_tmp = $arg0
set logging on
output $rawdbg_symbol_tmp
set logging off
eval "shell %s/lib/rawdbg_unavailable.sh %s %s",(char *)$RAWDBGPATH,$rawdbg_log_file,$rawdbg_run_file
eval "source %s",$rawdbg_run_file
if $rawdbg_tmp == 2
set $rawdbg_symbol_addr=""
else
rawdbg arch
# We need to check whether this address is valid
set logging on
info proc mappings
set logging off
if $rawdbg_align == 4
eval "shell %s/lib/rawdbg_symbol.sh %d %s %s %#x",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file,$rawdbg_run_file,$rawdbg_symbol_tmp
else
eval "shell %s/lib/rawdbg_symbol.sh %d %s %s %#llx",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file,$rawdbg_run_file,$rawdbg_symbol_tmp
end
eval "source %s",$rawdbg_run_file
if $rawdbg_debug == 1
if $rawdbg_align == 4
eval "shell echo \"[D] [rawdbg symbol] \\$rawdbg_symbol_tmp = %#x\"",$rawdbg_symbol_tmp
else
eval "shell echo \"[D] [rawdbg symbol] \\$rawdbg_symbol_tmp = %#llx\"",$rawdbg_symbol_tmp
end
eval "shell echo \"[D] [rawdbg symbol] \\$rawdbg_tmp[1] = %d\"",$rawdbg_tmp
end
# The address is legal
if $rawdbg_tmp == 1
set logging on
if $rawdbg_align == 4
eval "disassemble/r %#x,+1",$rawdbg_symbol_tmp
else
eval "disassemble/r %#llx,+1",$rawdbg_symbol_tmp
end
set logging off
eval "shell %s/lib/rawdbg_symbol.sh %d %s %s",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file,$rawdbg_run_file
eval "source %s",$rawdbg_run_file
if $rawdbg_debug == 1
eval "shell echo \"[D] [rawdbg symbol] \\$rawdbg_tmp[2] = %d\"",$rawdbg_tmp
end
# The address is illegal
else
set $rawdbg_symbol_addr=""
end
end
end
end
# @function: rawdbg loop
# @file: lib/rawdbg_loop.sh
# @sample: $rawdbg_loop_cnt default is 4
define rawdbg loop
while 1
if $argc == 3
set $rawdbg_loop_prev = $arg1
set $rawdbg_loop_i = $arg2
else
if $argc == 2
set $rawdbg_loop_prev = $arg1
else
if $argc != 1
shell echo "[-] read rawdbg loop document"
loop_break
end
set $rawdbg_loop_prev = 0
end
set $rawdbg_loop_i = 0
end
rawdbg arch
if $rawdbg_debug == 1
eval "shell echo \"\n[D] [rawdbg loop] \\$rawdbg_loop_i = %d\"",$rawdbg_loop_i
eval "shell echo \"[D] [rawdbg loop] \\$rawdbg_loop_cnt = %d\"",$rawdbg_loop_cnt
end
if $rawdbg_loop_i > $rawdbg_loop_cnt
loop_break
end
set $rawdbg_loop_address = $arg0
if $rawdbg_debug == 1
if $rawdbg_align == 4
eval "shell echo \"[D] [rawdbg loop] \\$rawdbg_loop_address = %#x\"",$rawdbg_loop_address
else
eval "shell echo \"[D] [rawdbg loop] \\$rawdbg_loop_address = %#llx\"",$rawdbg_loop_address
end
end
# [rawdbg symbol] function return $rawdbg_tmp value
rawdbg symbol $rawdbg_loop_address
# The value is a legal address
if $rawdbg_tmp == 1
eval "shell echo -n ' → %s'",$rawdbg_symbol_addr
set $rawdbg_loop_i = $rawdbg_loop_i + 1
set $rawdbg_loop_prev = $rawdbg_loop_address
if $rawdbg_align == 4
set $rawdbg_tmp = *(int *)$rawdbg_loop_address
else
set $rawdbg_tmp = *(long long *)$rawdbg_loop_address
end
rawdbg loop $rawdbg_tmp $rawdbg_loop_prev $rawdbg_loop_i
# The value is not an address, determine whether it is a string, and end the loop
else
if $rawdbg_debug == 1
if $rawdbg_align == 4
eval "shell echo \"[D] [rawdbg loop] \\$rawdbg_loop_prev = %#x\"",$rawdbg_loop_prev
else
eval "shell echo \"[D] [rawdbg loop] \\$rawdbg_loop_prev = %#llx\"",$rawdbg_loop_prev
end
end
if $rawdbg_loop_prev == 0
else
set logging on
if $rawdbg_align == 4
eval "x/s %#x",$rawdbg_loop_prev
eval "x/wx %#x",$rawdbg_loop_prev
else
eval "x/s %#llx",$rawdbg_loop_prev
eval "x/gx %#llx",$rawdbg_loop_prev
end
set logging off
shell echo -n " → "
eval "shell %s/lib/rawdbg_loop.sh %d %s %s",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file,$rawdbg_run_file
end
end
loop_break
end
end
# print title
define printcenter
eval "shell export semiwidth=$(((%d - $(echo %s | wc | awk '{print $3}')) / 2)) && echo \"$(printf '%%*s' $semiwidth | sed 's/ /─/g')%s$(printf '%%*s' $semiwidth | sed 's/ /─/g')\"",$rawdbg_console_width,$arg0,$arg0
end
# print registers info
define regs
printcenter "[ REGISTERS ]"
rawdbg arch
set logging on
info registers
set logging off
eval "shell %s/lib/rawdbg_regs.sh %s %s %d",(char *)$RAWDBGPATH,$rawdbg_log_file,$rawdbg_run_file,$rawdbg_arch
eval "source %s",$rawdbg_run_file
end
# print pc info
define disasm
rawdbg arch
eval "printcenter \"[ DISASM / %s ]\"",$rawdbg_arch_name
if $rawdbg_arch == 0
else
if $rawdbg_arch == 1
disassemble $eip,+30
else
if $rawdbg_arch == 2
disassemble $rip,+30
end
end
end
end
define rawdbg telescope
if $rawdbg_arch == 0
eval "shell echo '[-] unsopported architecture: %s'",$rawdbg_arch_name
loop_break
else
if $rawdbg_arch == 1
set $rawdbg_telescope_addr=$esp
else
if $rawdbg_arch == 2
set $rawdbg_telescope_addr=$rsp
end
end
end
end
# print addrs info
define telescope
rawdbg arch
while 1
rawdbg telescope
set $rawdbg_o = 20
if $argc == 1
set $rawdbg_telescope_addr=$arg0
else
if $argc == 2
set $rawdbg_telescope_addr=$arg0
set $rawdbg_o = $arg1
else
if $argc != 0
shell echo '[-] read telescope document'
loop_break
end
end
end
if $rawdbg_debug == 1
if $rawdbg_align == 4
eval "shell echo \"[D] [telescope] \\$rawdbg_telescope_addr = %#x\"",$rawdbg_telescope_addr
else
eval "shell echo \"[D] [telescope] \\$rawdbg_telescope_addr = %#llx\"",$rawdbg_telescope_addr
end
end
set $rawdbg_i = 0
while ($rawdbg_i < $rawdbg_o)
rawdbg symbol $rawdbg_telescope_addr
# The value is an illegal address
if $rawdbg_tmp == 0
if $rawdbg_align == 4
eval "shell echo ' %#x'",$rawdbg_telescope_addr
else
eval "shell echo ' %#llx'",$rawdbg_telescope_addr
end
# The value is a legal address
else
eval "shell echo -n ' %s'",$rawdbg_symbol_addr
# check whether pointer of this addr is null
if $rawdbg_align == 4
set $rawdbg_telescope_tmp = *(int *)$rawdbg_telescope_addr
else
set $rawdbg_telescope_tmp = *(long long *)$rawdbg_telescope_addr
end
rawdbg symbol $rawdbg_telescope_tmp
# The value is an illegal address
if $rawdbg_tmp == 0
set logging on
if $rawdbg_align == 4
eval "x/s %#x",$rawdbg_telescope_addr
eval "x/wx %#x",$rawdbg_telescope_addr
else
eval "x/s %#llx",$rawdbg_telescope_addr
eval "x/gx %#llx",$rawdbg_telescope_addr
end
set logging off
shell echo -n " → "
eval "shell %s/lib/rawdbg_loop.sh %d %s %s",(char *)$RAWDBGPATH,$rawdbg_platform,$rawdbg_log_file,$rawdbg_run_file
else
rawdbg loop $rawdbg_telescope_tmp
end
shell echo ''
end
set $rawdbg_telescope_addr = $rawdbg_telescope_addr + $rawdbg_align
set $rawdbg_i = $rawdbg_i + 1
end
loop_break
end
end
# print stack info
define stack
printcenter "[ STACK ]"
rawdbg telescope
telescope $rawdbg_telescope_addr 8
end
define context
regs
disasm
stack
printcenter "[ BACKTRACE ]"
backtrace
end
# -*- overwrite execute function
define si
stepi
context
end
define s
step
context
end
define ni
nexti
context
end
define n
next
context
end
# -*- alias -*-
alias ctx = context
alias tel = telescope