Skip to content

Instantly share code, notes, and snippets.

@ivanvza
Created February 28, 2017 20:36
Show Gist options
  • Save ivanvza/5d6df37c16517fd52e22bcd3916449f1 to your computer and use it in GitHub Desktop.
Save ivanvza/5d6df37c16517fd52e22bcd3916449f1 to your computer and use it in GitHub Desktop.
Python PID Memory Dump
#!/usr/bin/env python
# USAGE memdump.py <pid>
import ctypes, re, sys
## Partial interface to ptrace(2), only for PTRACE_ATTACH and PTRACE_DETACH.
c_ptrace = ctypes.CDLL("libc.so.6").ptrace
c_pid_t = ctypes.c_int32 # This assumes pid_t is int32_t
c_ptrace.argtypes = [ctypes.c_int, c_pid_t, ctypes.c_void_p, ctypes.c_void_p]
def ptrace(attach, pid):
op = ctypes.c_int(16 if attach else 17) #PTRACE_ATTACH or PTRACE_DETACH
c_pid = c_pid_t(pid)
null = ctypes.c_void_p()
err = c_ptrace(op, c_pid, null, null)
if err != 0: raise Exception, 'ptrace', err
if (len(sys.argv) < 2):
print "%s <pid>" % sys.argv[0]
sys.exit(-1)
pid = sys.argv[1]
ptrace(True, int(pid))
maps_file = open("/proc/%s/maps" % pid, 'r')
mem_file = open("/proc/%s/mem" % pid, 'r', 0)
for line in maps_file.readlines(): # for each mapped region
m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r])', line)
if m.group(3) == 'r': # if this is a readable region
try:
start = int(m.group(1), 16)
end = int(m.group(2), 16)
mem_file.seek(start) # seek to region start
chunk = mem_file.read(end - start) # read region contents
print chunk
except:
pass
maps_file.close()
mem_file.close()
ptrace(False, int(pid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment