Skip to content

Instantly share code, notes, and snippets.

@bannsec
Created February 5, 2018 17:38
Show Gist options
  • Save bannsec/64a3ad4ec4513b3e66ecbe86164b5059 to your computer and use it in GitHub Desktop.
Save bannsec/64a3ad4ec4513b3e66ecbe86164b5059 to your computer and use it in GitHub Desktop.
GDB PIE Breakpoint Script
#
# Add this to your .gdbinit script to create a pie breakpoint helper. Once added, you can create a PIE breakpoint by the following
# breakpoint_pie file_name offset
# file_name == string name of the file. This can be the main binary, a library, etc.
# offset == Offset to set breakpoint at. This could be an integer or a symbol name if that symbol is resolvable. For instance "main".
#
python
import re
import os
def breakpoint_pie(file_name, offset):
"""Sets breakpoint at offset in file based on currently loaded address."""
map = gdb.execute("info proc map",True,True)
map = map.split("\n")
assert type(file_name) is str, "Unknown type for file_name of {}".format(type(file_name))
if type(offset) is str:
try:
offset = int(gdb.execute("p/x &{}".format(offset),True,True).split(" = ")[1],16)
except:
print("[-] Couldn't resolve offset symbol '{}'".format(offset))
return
for line in map:
try:
lower, upper, size, obj_offset, obj_name = re.findall("\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$",line)[0]
lower = int(lower,16)
upper = int(upper,16)
size = int(size,16)
obj_offset = int(obj_offset,16)
if os.path.basename(obj_name) == file_name:
breakpoint = lower + offset
print("[+] Setting breakpoint: " + hex(breakpoint))
_ = gdb.execute("break *" + hex(breakpoint),True,True)
break
except:
pass
else:
print("[-] Couldn't find file...")
end
define breakpoint_pie
python breakpoint_pie($arg0, $arg1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment