Skip to content

Instantly share code, notes, and snippets.

@tallpeak
Created October 5, 2023 03:40
Show Gist options
  • Save tallpeak/ba0d28cde1647bc606acbc40a4466e55 to your computer and use it in GitHub Desktop.
Save tallpeak/ba0d28cde1647bc606acbc40a4466e55 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import re
import os
pid = int(os.popen("grep -a ^./asmttpd /proc/[0-9]*/cmdline | cut -d/ -f3") .read())
maps_file = open(f'/proc/{pid}/maps', 'r')
mem_file = open(f'/proc/{pid}/mem', 'rb', 0)
output_file = open("self.dump", 'wb')
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
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
output_file.write(bytes(line, 'utf-8'))
output_file.write(chunk) # dump contents to standard output
maps_file.close()
mem_file.close()
output_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment