@ -18,6 +18,8 @@ import sys
import gdb
import gdb
from six . moves import reload_module
from six . moves import reload_module
import pwndbg . abi
import pwndbg . arch
import pwndbg . auxv
import pwndbg . auxv
import pwndbg . elftypes
import pwndbg . elftypes
import pwndbg . events
import pwndbg . events
@ -120,28 +122,60 @@ def reset_ehdr_type_loaded():
global ehdr_type_loaded
global ehdr_type_loaded
ehdr_type_loaded = 0
ehdr_type_loaded = 0
def get_ehdr ( pointer ) :
@pwndbg.abi.LinuxOnly ( )
""" Returns an ehdr object for the ELF pointer points into.
def find_elf_magic ( pointer , max_pages = 1024 , search_down = False ) :
""" Search the nearest page which contains the ELF headers
by comparing the ELF magic with first 4 bytes .
Parameter :
search_down : change the search direction
to search over the lower address .
That is , decreasing the page pointer instead of increasing .
( default : False )
Returns :
An integer address of ELF page base
None if not found within the page limit
"""
"""
# Align down to a page boundary, and scan until we find
# the ELF header.
base = pwndbg . memory . page_align ( pointer )
base = pwndbg . memory . page_align ( pointer )
step = pwndbg . memory . PAGE_SIZE
if search_down :
step = - step
max_addr = pwndbg . arch . ptrmask
for i in range ( max_pages ) :
# Make sure address within valid range or gdb will raise Overflow exception
if base < 0 or base > max_addr :
return None
try :
try :
data = pwndbg . memory . read ( base , 4 )
data = pwndbg . memory . read ( base , 4 )
except gdb . MemoryError :
return None
# Do not search more than 4MB of memory
# Return the address if found ELF header
for i in range ( 1024 ) :
if data == b ' \x7F ELF ' :
if data == b ' \x7F ELF ' :
break
return base
base - = pwndbg . memory . PAGE_SIZE
base + = step
data = pwndbg . memory . read ( base , 4 )
else :
return None
print ( " ERROR: Could not find ELF base! " )
def get_ehdr ( pointer ) :
""" Returns an ehdr object for the ELF pointer points into.
"""
# Align down to a page boundary, and scan until we find
# the ELF header.
base = pwndbg . memory . page_align ( pointer )
# XXX: for non linux ABI, the ELF header may not be found in memory.
# This will hang the gdb when using the remote gdbserver to scan 1024 pages
if not pwndbg . abi . linux :
return None , None
return None , None
except gdb . MemoryError :
base = find_elf_magic ( pointer , search_down = True )
if base is None :
print ( " ERROR: Could not find ELF base! " )
return None , None
return None , None
# Determine whether it's 32- or 64-bit
# Determine whether it's 32- or 64-bit