code of ptrace

来源:互联网 发布:网络拓扑是指 编辑:程序博客网 时间:2024/05/28 23:20

/*a.c */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <signal.h>




void first(void)
{
        printf("This is in Function First\n");
        return ;
}


void second(void)
{
//      __asm__{"nop"};
        printf("This is in Function Second\n");
        return;
}


int main(int argc, char* argv[])
{
        signal(SIGTRAP, SIG_IGN);
        if( 0 != mprotect((void*)0x8048000, 4096, PROT_READ|PROT_EXEC) )
        {
                perror("mprotect error\n");
                exit(1);
        }
        while(1)
        {
                printf("This Process's ID == %d\n", getpid());
                printf("Function First's address == %p\n", (void*)first);
                printf("Function Second's address == %p\n", (void*)second);
                first();
                printf("\n\n\n\n");
                sleep(1);
        }
        return 0;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*b.c*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <linux/user.h>


int pid = 0;
#define NUM 1
void show_message(int *num)
{
        int i = 0;
        for(i = 0; i < NUM;i ++)
        {
                printf("%x ", num[i]);
        }
        printf("\n");
        return;
}
int read_data(int length, int addr, int* src)
{
        int i = 0;
        if(0 != ptrace(PTRACE_ATTACH, pid, NULL, NULL))
        {
                perror("Attach Error and Exiting\n");
                exit(5);
        }
        for(i = 0; i < length; i++)
        {
                src[i] = ptrace(PTRACE_PEEKTEXT, pid, (void*)(addr+i*4), NULL);
        }


        show_message(src);
        src[0] = 0x0486ca24;
//      src[0] = 0;
        show_message(src);


        for(i = 0; i < length; i++)
        {
                ptrace(PTRACE_POKETEXT, pid, (void*)(addr+i*4), (void*)src[i]);
        }


        for(i = 0;i < length; i++)
        {
                src[i] = ptrace(PTRACE_PEEKTEXT, pid, (void*)(addr+i*4), NULL);
        }
        show_message(src);


        if(0 != ptrace(PTRACE_DETACH, pid, NULL, NULL))
        {
               perror("Detach Error and Exiting\n");
               exit(6);
        }


        return 0;
}


int main(int argc, char* argv[])
{
//      int pid = 16167;
        int length = 1;
        int string[1] = {0};
        struct user_regs_struct src = {0};


        pid = atoi(argv[1]);
        read_data(1, 0x80484f4+8, string);


        if(0 != ptrace(PTRACE_ATTACH, pid, NULL, NULL))
        {
                perror("Attach Error and Exiting\n");
                exit(1);
        }
        if(0 != ptrace(PTRACE_GETREGS, pid, NULL, &src))
        {
                perror("Geting Data Error and Exiting...\n");
                exit(2);
        }


//      src.eip += 0x14;


        if(0 != ptrace(PTRACE_SETREGS, pid, NULL, &src))
        {
                perror("Setting Data Error and Exiting\n");
                exit(3);
        }


        if(0 != ptrace(PTRACE_DETACH, pid, NULL, NULL))
        {
                perror("Detach Error and Exit...\n");
                exit(4);
        }
        printf("Done\n");
        return 0;
}

================================================================================================================================================================

>> ./target

This Process's ID == 4125
Function First's address == 0x80484f4
Function Second's address == 0x8048508
This is in Function Second


>>./trace 4125


原创粉丝点击