Ptrace application 3 code injection (codeinjection)

Source: Internet
Author: User

I saw an article about ptrace code injection in linuxjoural, which is very well written, so I have the idea of translating it. The basic content is as follows: if we want to track and debug a running process, we can use ptrace (ptrace_attach .......), its functions are basically the same as ptrace (ptrace_traceme .....). After tracking, we can use ptrace (ptrace_detach...) to continue running the sub-process. The following is an instance program:

# Include
Int main ()
{
Int I;
For (I = 0; I {
Printf ("My counter: % d/N", I );
Sleep (2 );
}
Return 0;
}

Compile the program and run it as a background process: gcc-O Counter. c

Then enter the command./counetr &

The following program can use ptrace (ptrace_attach ....) attach to the counter daemon, pause the running counter process, print the Hello, world string, and then execute the original counter program.

      1 #include
 
        2 #include
  
         3 #include
   
          4 #include
    
           5 #include      6 #include
     
            7 const int long_size=sizeof(long);//global variable is declarated      8 void getdata(pid_t child,long addr,char*  str,int len)      9 {     10         int i,j;     11         union u     12         {     13           long val;     14           char  chars[long_size];     15         }data;     16         i=0;     17         j=len/long_size;     18         while(i
      
       if(j!=0) 27 { 28 data.val=ptrace(PTRACE_PEEKDATA,child,addr+i*4,NULL); 29 memcpy(str,data.chars,j); 30 31 } 32 str[len]='/0';
      
     
    
   
  
 
     33 }     34 void putdata(pid_t child,long addr,char* str,int len)     35 {     36         int i,j;     37         union u     38         {     39           long val;     40           char chars[long_size];     41         }data;     42         i=0;     43         j=len/long_size;     44         while(i
 
  if(j!=0)     53         {     54           memcpy(data.chars,str,j);     55           ptrace(PTRACE_POKEDATA,child,addr+i*4,data.val);     56         }     57 }     58 int main(int argc ,char* argv[])     59 {     60         pid_t  traced_process;     61         struct user_regs_struct regs;     62         long insys;
 
     63         int  len=41;     64         char code[]=     65 "/xeb/x15/x5e/xb8/x04/x00/x00/x00"     66 "/xbb/x02/x00/x00/x00/x89/xf1/xba"     67 "/x0c/x00/x00/x00/xcd/x80/xcc/xe8"     68 "/xe6/xff/xff/xff/x48/x65/x6c/x6c"     69 "/x6f/x20/x57/x6f/x72/x6c/x64/x0a/x00";     70         char backup[len];     71         long addr;     72         if(argc!=2)     73         {     74           printf("command input error:/n");     75           exit(1);     76         }     77         traced_process=atoi(argv[1]);//change char to int     78         ptrace(PTRACE_ATTACH,traced_process,NULL,NULL);     79         wait(NULL);     80         ptrace(PTRACE_GETREGS,traced_process,NULL,&regs);     81         getdata(traced_process,regs.eip,backup,len);     82         putdata(traced_process,regs.eip,code,len);     83         ptrace(PTRACE_SETREGS,traced_process,NULL,&regs);     84         ptrace(PTRACE_CONT,traced_process,NULL,NULL); //begin to execute the new process with code injection     85         wait(NULL);//wait to receive a return sigal from the process with code injection     86         printf("the process stopped,continue to execute the orginal process/n");     87         printf("press the enter key/n");     88         getchar();     89         putdata(traced_process,regs.eip,backup,len);     90         ptrace(PTRACE_SETREGS,traced_process,NULL,&regs);//set eip back up to the orginal value);     91         ptrace(PTRACE_DETACH,traced_process,NULL,NULL);//detach the traceing process from the traced process     92         return 0;
     93 }
The basic process of this program is: first save the content in the EIP (Program pointer) of the counter process in backup, and then save the content in the Code array to EIP, at this time, the program starts execution from the code, and the content in the Code is the machine code that prints Hello, world. After the execution is completed, return to the original program counter to continue the execution.
Compile the program gcc-O codeinjection. c. enter it in a terminal first. /counter &, followed by another terminal. /codeinjection PID (PID is counter's PID). Press the Enter key to see the result as follows:

[Root @ localhost ptrace] #./counter &

[1] 21054

[Root @ localhost ptrace] # My counter: 0

My counter: 1

My counter: 2

My counter: 3

My counter: 4

Hello World

My counter: 5

My counter: 6

My counter: 7

My counter: 8

My counter: 9

[1] + done./Counter

[Root @ localhost ptrace] #./codeinjection 21054

The process stopped, continue to execute the orginal Process

Press the Enter key

We can see that Hello world is injected into the counter process. After execution, it returns to the original eip and continues to execute the original program. Where. /codeinjection is used as the first parameter of main function argv [0], and PID is used as the second parameter of main function, because this parameter is character type (char ), the form parameter in the ptrace function is the PID of the sub-process. It is an integer (INT), so a conversion is required. In the first row, the atoi function completes this function. Line 3 attaches the process codeinjection to the counter process, and traced_process is the counter PID. The getdata function of Row 3 enables you to store 41 bytes of data at the start address of regs. EIP in the backup array for backup. It is 41 bytes because the machine code for printing hello and world is 41 bytes. If the length of the backup array Len is smaller than 41 bytes, regs. EIP + Len to regs. data between eip and 41 will be overwritten by the program that prints helloworld. After printing hello and world, the original regs will be returned. an error occurs. Therefore, the length of the backup array should be 41 bytes equal to the length of the inserted code. The code for getdata and putdata is mostly the same. After executing the Hello world code, you need to restore the value of regs. EIP so that the counter of the original program can continue to execute. This function is available in rows 89th and 90. The Hello world machine code in the Code array is obtained by the following program.
#include
 
  void main(){__asm__(        "jmp forward/n/t"        "backward:popl %esi/n/t"        "movl $4,%eax/n/t"        "movl $2,%ebx/n/t"        "movl %esi,%ecx/n/t"        "movl $12,%edx/n/t"        "int $0x80/n/t"        "int3/n/t"        "forward:call  backward/n/t"        ".string  /"Hello World//n/""        );//      return 0;}
 

Compile gcc-g-o hello. c

Then debug the program with GDB hello as follows:

(gdb) disassemble mainDump of assembler code for function main:0x08048334 
 
  :    push   %ebp0x08048335 
  
   :    mov    %esp,%ebp0x08048337 
   
    :    sub    $0x8,%esp0x0804833a 
    
     :    and    $0xfffffff0,%esp0x0804833d 
     
      :    mov    $0x0,%eax0x08048342 
      
       : add $0xf,%eax0x08048345 
       
        : add $0xf,%eax0x08048348 
        
         : shr $0x4,%eax0x0804834b 
         
          : shl $0x4,%eax0x0804834e 
          
           : sub %eax,%esp0x08048350 
           
            : jmp 0x8048367 
            
             0x08048352 
             
              : pop %esi0x08048353 
              
               : mov $0x4,%eax0x08048358 
               
                : mov $0x2,%ebx0x0804835d 
                
                 : mov %esi,%ecx0x0804835f 
                 
                  : mov $0xc,%edx0x08048364 
                  
                   : int $0x800x08048366 
                   
                    : int30x08048367 
                    
                     : call 0x8048352 
                     
                      0x0804836c 
                      
                       : dec %eax0x0804836d 
                       
                        : gs0x0804836e 
                        
                         : insb (%dx),%es:(%edi)0x0804836f 
                         
                          : insb (%dx),%es:(%edi)0x08048370 
                          
                           : outsl %ds:(%esi),(%dx)0x08048371 
                           
                            : and %dl,0x6f(%edi)0x08048374 
                            
                             : jb 0x80483e2 <__libc_csu_fini>0x08048376 
                             
                              : or %fs:(%eax),%al0x08048379 
                              
                               : leave0x0804837a 
                               
                                : retEnd of assembler dump.(gdb)
                               
                              
                             
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

Hello. C is a C Language Program for Embedded Assembly. __asm _ indicates the start of embedding the assembly language. Disassemble the C language program in GDB and find that the embedded assembly command JMP forward is executed from main + 28, from Main + 0 to main + 28, the operating system saves some actions performed on the site.

(gdb) x /40bx main+280x8048350 
 
  :    0xeb    0x15    0x5e    0xb8    0x04    0x00    0x00    0x000x8048358 
  
   : 0xbb    0x02    0x00    0x00    0x00    0x89    0xf1    0xba0x8048360 
   
    :        0x0c    0x00    0x00    0x00    0xcd    0x80    0xcc    0xe80x8048368 
    
     :  0xe6    0xff    0xff    0xff    0x48    0x65    0x6c    0x6c0x8048370 
     
      :  0x6f    0x20    0x57    0x6f    0x72    0x6c    0x64    0x0a
     
    
   
  
 

The machine code above is Hello world, which is inserted into the counter process as the content of the Code array to complete code injection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.