I am trying to improve in Linux process injection and I have some things that i not fully understand.
the injection method that I am using is the basic ptrace injection in current rip value. the flow is this : ATTACH to the running process using ptrace, get regs and modifying the memory placed in current rip with shellcode contains Linux/x86-x64 /bin/sh using PTRACE_POKETDATA , then DETACH the procss.
the injection file and the target file was taken from https://github.com/0x00pf/0x00sec_code/tree/master/mem_inject
first I ran the target process and saw its pstree hierarchy: running process screenshot process pstree hierarchy
second, I took a look at the memory mapping of the current process (as shown in the picture): process memory mapping - before injection
then I ran the injection (as shown in the picture): injection part
so, the injection occurs at the address 0x7f049990bb96 as we can see in the screenshot. meaning it is in this page in memory (from process memory mapping - before injection picture): injected page in memory
then after the injection I ran again the pstree command to see the process hierarchy and saw memory map: process pstree hierarchy - after injection process memory mapping - after injection
I have two major questions to improve my understanding:
- when the injection occurs, it injects into the libc-2.33.so page. the libc-2.33.so file is shared object so it supposed to be loaded one time in memory and every process will reference to the same shared object file. so now, because of COW(Copy-on-write) protection as we can see in the memory mapping of the process (r-xp permissions [p mean private - has COW protection]), we have two versions of libc-2.33.so loaded in memory, the original libc shared object, and the written(injected) libc loaded into the process because of the COW.
does it true or I misunderstand something in the process ?
- why all the memory mapping of the process after the injection changed and the pstree hierarchy not contain the /bin/sh as a child process of “target” process but it discards the “target” process ?