etc~ scripts and random things |
| git clone https://github.com/djnnvx/etc.git |
| log | files | refs | archive |
dump_proc (1254B)
1 #!/usr/bin/env python3 2 3 import ctypes, re, sys 4 5 # Load libc.so.6 dynamically 6 libc = ctypes.CDLL("libc.so.6") 7 c_ptrace = libc.ptrace 8 c_pid_t = ctypes.c_int32 9 c_ptrace.argtypes = [ctypes.c_int, c_pid_t, ctypes.c_void_p, ctypes.c_void_p] 10 11 def ptrace(attach, pid): 12 op = ctypes.c_int(16 if attach else 17) # PTRACE_ATTACH or PTRACE_DETACH 13 c_pid = c_pid_t(pid) 14 null = ctypes.c_void_p() 15 err = c_ptrace(op, c_pid, null, null) 16 if err != 0: 17 raise OSError('ptrace') 18 19 def maps_line_range(line): 20 m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r])', line) 21 if not m: 22 return [] 23 return [int(m.group(1), 16), int(m.group(2), 16), m.group(3)] 24 25 def cat_proc_mem(pid): 26 ptrace(True, int(pid)) 27 maps_file_path = f"/proc/{pid}/maps" 28 with open(maps_file_path, 'r') as maps_file: 29 ranges = map(maps_line_range, maps_file.readlines()) 30 31 mem_file_path = f"/proc/{pid}/mem" 32 with open(mem_file_path, 'rb') as mem_file: 33 for r in ranges: 34 if r[2] == 'r': 35 mem_file.seek(r[0]) 36 chunk = mem_file.read(r[1] - r[0]) 37 print(chunk, end='') 38 39 ptrace(False, int(pid)) 40 41 if __name__ == "__main__": 42 for pid in sys.argv[1:]: 43 cat_proc_mem(pid) 44