test.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdio.h> // printf
  2. #include <string.h> // strerror
  3. #include <errno.h> // errno
  4. #include <unistd.h> // execl
  5. #include <sys/ptrace.h> // ptrace
  6. #include <sys/user.h> // user_regs_struct
  7. #include <sys/personality.h> // personality
  8. #include <sys/wait.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. int
  13. main(int argc, char *argv[], char *envp[]) {
  14. // create a child process
  15. int pid = fork();
  16. // if error occurs
  17. if (0 > pid) {
  18. printf("Error during forking: %s\n", strerror(errno));
  19. return 1;
  20. }
  21. // child process
  22. if (0 == pid) {
  23. ptrace(PTRACE_TRACEME, 0, 0, 0);
  24. personality(ADDR_NO_RANDOMIZE);
  25. execve(argv[1], &(argv[1]), envp);
  26. }
  27. // parent process
  28. int status;
  29. struct user_regs_struct regs;
  30. int n;
  31. wait(&status);
  32. if(1407 == status) {
  33. char infilename[128], line[256], out[256];
  34. char* outfilename = "/tmp/pal_range";
  35. int mapfd, ret, outfd;
  36. unsigned long vas;
  37. unsigned long vae;
  38. sprintf(infilename, "/proc/%d/maps",pid);
  39. while((mapfd = open(infilename,O_RDONLY)) == -1);
  40. read(mapfd, &line, 256);
  41. /*scan for the virtual addresses*/
  42. n = sscanf(line, "%lX-%lX r-xp", &vas, &vae);
  43. if(n == 2)
  44. {
  45. outfd = open(outfilename, O_WRONLY|O_CREAT, S_IRUSR);
  46. memset(out,0,256);
  47. sprintf(out,"%lX,%lX\n", vas, vae);
  48. write(outfd, out, 256);
  49. close(outfd);
  50. }
  51. close(mapfd);
  52. }
  53. kill(pid,SIGKILL);
  54. return 0;
  55. }