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
|
from __future__ import print_function
import gdb
import subprocess
import re
import copy
from os import path
directory, file = path.split(__file__)
directory = path.expanduser(directory)
directory = path.abspath(directory)
#sys.path.append(directory)
# arch
capsize = 0
word = ""
arch = ""
magic_variable = ["__malloc_hook","__free_hook","_IO_2_1_stdin_","_IO_2_1_stdout_","_IO_2_1_stderr_","_IO_list_all","__after_morecore_hook"]
magic_function = ["system","execve","open","read","write","gets","setcontext","__libc_realloc"]
def to_int(val):
"""
Convert a string to int number
from https://github.com/longld/peda
"""
try:
return int(str(val), 0)
except:
return None
def normalize_argv(args, size=0):
"""
Normalize argv to list with predefined length
from https://github.com/longld/peda
"""
args = list(args)
for (idx, val) in enumerate(args):
if to_int(val) is not None:
args[idx] = to_int(val)
if size and idx == size:
return args[:idx]
if size == 0:
return args
for i in range(len(args), size):
args += [None]
return args
class PwnCmd(object):
commands = []
prevbp = []
bpoff = []
def __init__(self):
# list all commands
self.commands = [cmd for cmd in dir(self) if callable(getattr(self, cmd)) ]
def libc(self):
""" Get libc base """
libcbs = libcbase()
print("\033[34m" + "libc : " + "\033[37m" + hex(libcbs))
def heapbase(self):
""" Get heapbase """
heapbase = getheapbase()
if heapbase :
print("\033[34m" + "heapbase : " + "\033[37m" + hex(heapbase))
else :
print("heap not found")
def ld(self):
""" Get ld.so base """
print("\033[34m" + "ld : " + "\033[37m" + hex(ldbase()))
def codebase(self):
""" Get text base """
codebs = codeaddr()[0]
print("\033[34m" + "codebase : " + "\033[37m" + hex(codebs))
def tls(self):
""" Get tls base """
print("\033[34m" + "tls : " + "\033[37m" + hex(gettls()))
def canary(self):
""" Get canary value """
print("\033[34m" + "canary : " + "\033[37m" + hex(getcanary()))
def fmtarg(self,*arg):
(addr,) = normalize_argv(arg,1)
getfmtarg(addr)
def off(self,*arg) :
""" Calculate the offset of libc """
#(sym,)= normalize_argv(arg,1)
(sym,) = normalize_argv(arg,1)
symaddr = getoff(sym)
if symaddr == 0 :
print("Not found the symbol")
else :
if type(sym) is int :
print("\033[34m" + hex(sym) + ":" + "\033[37m" +hex(symaddr))
else :
print("\033[34m" + sym + ":" + "\033[37m" +hex(symaddr))
def offf(self,*arg) :
""" Calculate the offset of libc """
#(sym,)= normalize_argv(arg,1)
(sym,) = normalize_argv(arg,1)
codebs = codeaddr()[0]
print("\033[34m" + hex(sym) + ":" + "\033[37m" +hex(sym - codebs))
def fp(self,*arg):
""" show FILE structure """
(addr,) = normalize_argv(arg,1)
showfp(addr)
def fpchain(self):
""" show FILE chain """
showfpchain()
def orange(self,*arg):
""" test house of orange """
(addr,) = normalize_argv(arg,1)
if addr :
testorange(addr)
else :
print("You need to specifiy an address")
def fsop(self,*arg):
""" test fsop """
(addr,) = normalize_argv(arg,1)
testfsop(addr)
def magic(self):
""" Print usefual variables or function in glibc """
getarch()
try :
print("========== function ==========")
for f in magic_function :
print("\033[34m" + f + ":" + "\033[33m" +hex(getoff(f)))
print("\033[00m========== variables ==========")
for v in magic_variable :
cmd = "x/" + word + "&" +v
content = gdb.execute(cmd,to_string=True).split(":")[1].strip()
offset = hex(getoff("&"+ v))
pad = 36 - len(v) - len(offset) - 2
print("\033[34m%s\033[33m(%s)\033[37m%s: \033[37m%s" % (v, offset, ' ' *pad, content))
except :
print("You need run the program first")
def findsyscall(self):
""" find the syscall gadget"""
arch = getarch()
start,end = codeaddr()
if arch == "x86-64" :
gdb.execute("find 0x050f " + hex(start) + " " + hex(end) )
elif arch == "i386":
gdb.execute("find 0x80cd " + hex(start) + " " + hex(end) )
elif arch == "arm":
gdb.execute("find 0xbc80df00 " + hex(start) + " " + hex(end) )
elif arch == "aarch64":
gdb.execute("find 0xd4000001 " + hex(start) + " " + hex(end) )
else :
print("error")
def got(self):
""" Print the got table """
processname = getprocname()
if processname :
cmd = "objdump -R "
if iscplus :
cmd += "--demangle "
cmd += "\"" + processname + "\""
got = subprocess.check_output(cmd,shell=True)[:-2].decode('utf8')
print(got)
else :
print("No current process or executable file specified." )
def dyn(self):
""" Print dynamic section """
processname = getprocname()
if processname :
dyn = subprocess.check_output("readelf -d \"" + processname + "\"",shell=True).decode('utf8')
print(dyn)
else :
print("No current process or executable file specified." )
def readelf(self):
"""Print the seccons"""
processname = getprocname()
elfinfo = {}
codebs = codeaddr()[0]
out = subprocess.check_output("readelf -W -S \"" + processname + "\"",shell = True).decode('utf-8')
if not out:
return {}
p = re.compile(".*\[.*\] (\.[^ ]*) [^0-9]* ([^ ]*) [^ ]* ([^ ]*)(.*)")
matches = p.findall(out)
if not matches:
return {}
# print(matches)
"""[('.interp', '0000000000000318', '00001c', ' 00 A 0 0 1'), ('.note.gn]"""
for (hname, start, size, attr) in matches:
start,end = int(start,16),int(start,16) + int(size,16)
if start == 0:
continue
if start < codebs:
start += codebs
if end < codebs:
end += codebs
if "X" in attr:
htype = "code"
elif "W" in attr:
htype = "data"
else:
htype = "rodata"
elfinfo[hname.strip()] = (start,end,htype)
result = {}
result = elfinfo
for (k, (start, end, type)) in sorted(result.items(), key=lambda x: x[1]):
print("%s = 0x%x" % (k, start))
def rop(self):
""" ROPgadget """
procname = getprocname()
if procname :
subprocess.call("ROPgadget --binary \"" + procname +"\"",shell=True)
else :
print("No current process or executable file specified." )
def findcall(self,*arg):
""" Find some function call """
(sym,)= normalize_argv(arg,1)
output = searchcall(sym)
print(output)
def at(self,*arg):
""" Attach by processname """
(processname,) = normalize_argv(arg,1)
if not processname :
processname = getprocname(relative=True)
if not processname :
print("Attaching program: ")
print("No executable file specified.")
print("Use the \"file\" or \"exec-file\" command.")
return
try :
print("Attaching to %s ..." % processname)
pidlist = subprocess.check_output("pidof " + processname,shell=True).decode('utf8').split()
gdb.execute("attach " + pidlist[0])
getheapbase()
libcbase()
codeaddr()
ldbase()
except :
print( "No such process" )
def bcall(self,*arg):
""" Set the breakpoint at some function call """
(sym,)= normalize_argv(arg,1)
call = searchcall(sym)
if "not found" in call :
print("symbol not found")
else :
if ispie():
codebaseaddr,codeend = codeaddr()
for callbase in call.split('\n')[:-1]:
addr = int(callbase.split(':')[0],16) + codebaseaddr
cmd = "b*" + hex(addr)
print(gdb.execute(cmd,to_string=True))
else:
for callbase in call.split('\n')[:-1]:
addr = int(callbase.split(':')[0],16)
cmd = "b*" + hex(addr)
print(gdb.execute(cmd,to_string=True))
def boff(self,*arg):
""" Set the breakpoint at some offset from base address """
(sym,) = normalize_argv(arg,1)
codebaseaddr,codeend = codeaddr()
if sym not in self.bpoff:
self.bpoff.append(sym)
cmd = "b*" + hex(codebaseaddr + sym)
x = gdb.execute(cmd,to_string=True)
y = x.rstrip().split("\n")[-1].split()[1]
self.prevbp.append(y)
print(x.rstrip())
def tboff(self,*arg):
""" Set temporary breakpoint at some offset from base address """
(sym,) = normalize_argv(arg,1)
codebaseaddr,codeend = codeaddr()
cmd = "tb*" + hex(codebaseaddr + sym)
print(gdb.execute(cmd,to_string=True))
def atboff(self,*arg):
""" Attach and set breakpoints accordingly """
(sym,) = normalize_argv(arg,1)
cmd = "attach " + str(sym)
print(gdb.execute(cmd,to_string=True))
x = len(self.prevbp)
while x > 0:
i = self.prevbp.pop(0)
cmd = "del " + i
gdb.execute(cmd,to_string=True)
x -= 1
for i in self.bpoff:
self.boff(hex(i))
def doff(self,*arg):
""" Delete the breakpoint using breakpoint number at some offset from base address """
(sym,) = normalize_argv(arg,1)
if str(sym) not in self.prevbp:
return
codebaseaddr,codeend = codeaddr()
cmd = "i b " + str(sym)
x = gdb.execute(cmd,to_string=True)
y = int(x.rstrip().split("\n")[1].split()[4], 16) - codebaseaddr
cmd = "del " + str(sym)
print(gdb.execute(cmd,to_string=True).rstrip())
self.bpoff.remove(y)
self.prevbp.remove(str(sym))
def xo(self,*arg):
""" Examine at offset from base address """
(_,arg1,) = normalize_argv(arg,2)
cmd = "x" + arg[0] + " "
if arg1:
codebaseaddr,_ = codeaddr()
cmd += hex(codebaseaddr + arg1)
print(gdb.execute(cmd,to_string=True)[:-1])
class PwngdbCmd(gdb.Command):
""" Pwngdb command wrapper """
def __init__(self):
super(PwngdbCmd,self).__init__("pwngdb",gdb.COMMAND_USER)
def try_eval(self, expr):
try:
return gdb.parse_and_eval(expr)
except:
#print("Unable to parse expression: {}".format(expr))
return expr
def eval_argv(self, expressions):
""" Leave command alone, let GDB parse and evaluate arguments """
return [expressions[0]] + [ self.try_eval(expr) for expr in expressions[1:] ]
def invoke(self,args,from_tty):
self.dont_repeat()
# Don't eval expression in PwngdbCmd commands
#expressions = gdb.string_to_argv(args)
#arg = self.eval_argv(expressions)
arg = args.split()
if len(arg) > 0 :
cmd = arg[0]
if cmd in pwncmd.commands :
func = getattr(pwncmd,cmd)
func(*arg[1:])
else :
print("Unknown command")
else :
print("Unknown command")
return
class PwngdbAlias(gdb.Command):
""" Pwngdb Alias """
def __init__(self,alias,command):
self.command = command
super(PwngdbAlias,self).__init__(alias,gdb.COMMAND_NONE)
def invoke(self,args,from_tty):
self.dont_repeat()
gdb.execute("%s %s" % (self.command,args))
def getarch():
global capsize
global word
global arch
data = gdb.execute('show arch',to_string = True)
tmp = re.search("currently.*",data)
if tmp :
info = tmp.group()
if "x86-64" in info:
capsize = 8
word = "gx "
arch = "x86-64"
return "x86-64"
elif "aarch64" in info :
capsize = 8
word = "gx "
arch = "aarch64"
return "aarch64"
elif "arm" in info :
capsize = 4
word = "wx "
arch = "arm"
return "arm"
else :
word = "wx "
capsize = 4
arch = "i386"
return "i386"
else :
return "error"
def procmap():
data = gdb.execute('info proc exe',to_string = True)
pid = re.search('process.*',data)
if pid :
pid = pid.group()
pid = pid.split()[1]
maps = open("/proc/" + pid + "/maps","r")
infomap = maps.read()
maps.close()
return infomap
else :
return "error"
def iscplus():
name = getprocname()
data = subprocess.check_output("readelf -s " + name,shell=True).decode('utf8')
if "CXX" in data :
return True
else :
return False
def getprocname(relative=False):
procname = None
try:
data = gdb.execute("info proc exe",to_string=True)
procname = re.search("exe.*",data).group().split("=")[1][2:-1]
except:
data = gdb.execute("info files",to_string=True)
if data:
procname = re.search('Symbols from "(.*)"',data).group(1)
if procname and relative :
return procname.split("/")[-1]
return procname
def libcbase():
infomap = procmap()
data = re.search(".*libc.*\.so",infomap)
if data :
libcaddr = data.group().split("-")[0]
gdb.execute("set $libc=%s" % hex(int(libcaddr,16)))
return int(libcaddr,16)
else :
return 0
def ldbase():
infomap = procmap()
data = re.search(".*ld.*\.so",infomap)
if data :
ldaddr = data.group().split("-")[0]
gdb.execute("set $ld=%s" % hex(int(ldaddr,16)))
return int(ldaddr,16)
else :
return 0
def getheapbase():
infomap = procmap()
data = re.search(".*heap\]",infomap)
if data :
heapbase = data.group().split("-")[0]
gdb.execute("set $heap=%s" % hex(int(heapbase,16)))
return int(heapbase,16)
else :
return 0
def codeaddr(): # ret (start,end)
infomap = procmap()
procname = getprocname()
pat = ".*" + procname
data = re.findall(pat,infomap)
if data :
codebaseaddr = data[0].split("-")[0]
codeend = data[0].split("-")[1].split()[0]
gdb.execute("set $code=%s" % hex(int(codebaseaddr,16)))
return (int(codebaseaddr,16),int(codeend,16))
else :
return (0,0)
def gettls():
arch = getarch()
if arch == "i386" :
vsysaddr = gdb.execute("info functions __kernel_vsyscall",to_string=True).split("\n")[-2].split()[0].strip()
sysinfo= gdb.execute("find " + vsysaddr,to_string=True).split("\n")[2]
match = re.search(r"0x[0-9a-z]{8}",sysinfo)
if match :
tlsaddr = int(match.group(),16) - 0x10
else:
return "error"
return tlsaddr
elif arch == "x86-64" :
gdb.execute("call (int)arch_prctl(0x1003,$rsp-8)",to_string=True)
data = gdb.execute("x/xg $rsp-8",to_string=True)
return int(data.split(":")[1].strip(),16)
else:
return "error"
def getcanary():
arch = getarch()
tlsaddr = gettls()
if arch == "i386" :
offset = 0x14
result = gdb.execute("x/xw " + hex(tlsaddr + offset),to_string=True).split(":")[1].strip()
return int(result ,16)
elif arch == "x86-64" :
offset = 0x28
result = gdb.execute("x/xg " + hex(tlsaddr + offset),to_string=True).split(":")[1].strip()
return int(result,16)
else :
return "error"
def getoff(sym):
libc = libcbase()
if type(sym) is int :
return sym-libc
else :
try :
data = gdb.execute("x/x " + sym ,to_string=True)
if "No symbol" in data:
return 0
else :
data = re.search("0x.*[0-9a-f] ",data)
data = data.group()
symaddr = int(data[:-1] ,16)
return symaddr-libc
except :
return 0
def searchcall(sym):
procname = getprocname()
cmd = "objdump -d -M intel "
if iscplus :
cmd += "--demangle "
cmd += "\"" + procname + "\""
try :
call = subprocess.check_output(cmd
+ "| grep \"call.*" + sym + "@plt>\"" ,shell=True).decode('utf8')
return call
except :
return "symbol not found"
def ispie():
procname = getprocname()
result = subprocess.check_output("readelf -h " + "\"" + procname +"\"",shell=True).decode('utf8')
if re.search("DYN",result):
return True
else:
return False
def get_reg(reg):
cmd = "info register " + reg
result = int(gdb.execute(cmd,to_string=True).split()[1].strip(),16)
return result
def showfp(addr):
if addr :
cmd = "p *(struct _IO_FILE_plus *)" + hex(addr)
try :
result = gdb.execute(cmd)
except :
print("Can't not access 0x%x" % addr)
else :
print("You need to specify an address")
def showfpchain():
getarch()
cmd = "x/" + word + "&_IO_list_all"
head = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
print("\033[32mfpchain:\033[1;37m ",end = "")
chain = head
print("0x%x" % chain,end = "")
try :
while chain != 0 :
print(" --> ",end = "")
cmd = "x/" + word + "&((struct _IO_FILE_plus *)" + hex(chain) +").file._chain"
chain = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
print("0x%x" % chain,end = "")
print("")
except :
print("Chain is corrupted")
def testorange(addr):
getarch()
result = True
cmd = "x/" + word + "&((struct _IO_FILE_plus *)" + hex(addr) + ").file._mode"
mode = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16) & 0xffffffff
cmd = "x/" + word + "&((struct _IO_FILE_plus *)" + hex(addr) + ").file._IO_write_ptr"
write_ptr = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
cmd = "x/" + word + "&((struct _IO_FILE_plus *)" + hex(addr) + ").file._IO_write_base"
write_base = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
if mode < 0x80000000 and mode != 0:
try :
cmd = "x/" + word + "&((struct _IO_FILE_plus *)" + hex(addr) + ").file._wide_data"
wide_data = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
cmd = "x/" + word + "&((struct _IO_wide_data *)" + hex(wide_data) + ")._IO_write_ptr"
w_write_ptr = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
cmd = "x/" + word + "&((struct _IO_wide_data *)" + hex(wide_data) + ")._IO_write_base"
w_write_base = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
if w_write_ptr <= w_write_base :
print("\033[;1;31m_wide_data->_IO_write_ptr(0x%x) < _wide_data->_IO_write_base(0x%x)\033[1;37m" % (w_write_ptr,w_write_base))
result = False
except :
print("\033;1;31mCan't access wide_data\033[1;37m")
result = False
else :
if write_ptr <= write_base :
print("\033[;1;31m_IO_write_ptr(0x%x) < _IO_write_base(0x%x)\033[1;37m" % (write_ptr,write_base))
result = False
if result :
print("Result : \033[34mTrue\033[37m")
cmd = "x/" + word + "&((struct _IO_FILE_plus *)" + hex(addr) + ").vtable.__overflow"
overflow = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
print("Func : \033[33m 0x%x\033[1;37m" % overflow)
else :
print("Result : \033[31mFalse\033[1;37m")
def testfsop(addr=None):
getarch()
if addr :
cmd = "x/" + word + hex(addr)
else :
cmd = "x/" + word + "&_IO_list_all"
head = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
chain = head
print("---------- fp : 0x%x ----------" % chain)
testorange(chain)
try :
while chain != 0 :
cmd = "x/" + word + "&((struct _IO_FILE_plus *)" + hex(chain) +").file._chain"
chain = int(gdb.execute(cmd,to_string=True).split(":")[1].strip(),16)
if chain != 0 :
print("---------- fp : 0x%x ----------" % chain)
testorange(chain)
except :
print("Chain is corrupted")
def getfmtarg(addr):
if capsize == 0 :
getarch()
if arch == "i386" :
start = get_reg("esp")
idx = (addr- start)/4
print("The index of format argument : %d (\"\%%%d$p\")" % (idx,idx - 1))
elif arch == "x86-64" :
start = get_reg("rsp")
idx = (addr - start)/8 + 6
print("The index of format argument : %d (\"\%%%d$p\")" % (idx,idx - 1))
else :
print("Not support the arch")
pwncmd = PwnCmd()
PwngdbCmd()
for cmd in pwncmd.commands :
PwngdbAlias(cmd,"pwngdb %s" % cmd)
gdb.execute("set print asm-demangle on")
|