Guest User

Untitled

a guest
Nov 13th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <string.h>
  6. #include <crypt.h>
  7.  
  8. /**
  9. * Illustrative and probably very buggy example of what su essentially does
  10. *
  11. * To compile run: gcc switcharoo.c -o switcharoo -l crypt
  12. * Set permissions: sudo chown root:root switcharoo
  13. * Set suid bit: sudo chmod 4755 switcharoo
  14. */
  15.  
  16. int main(int argc, char** argv) {
  17. char *password = getpass("Password:");
  18. FILE *fh = fopen("/etc/shadow", "r");
  19. char line[200];
  20. fgets(line, 200, fh); // read first line, usually corresponds to root user
  21. char *username = strtok(line, ":"); // extract first column
  22. char *hash = strtok(NULL, ":"); // extract second column
  23. printf("Hash from /etc/shadow is: %s\n", hash);
  24. char *result = crypt(password, hash); // calculate hash with salt from /etc/shadow
  25. printf("User supplied password results in hash: %s\n", result);
  26. int ok = strcmp (result, hash) == 0; // compare hashes
  27.  
  28. puts(ok ? "Access granted." : "Access denied.");
  29.  
  30. if (ok) {
  31. printf("UID before setuid: %d\n", getuid());
  32. printf("Effective UID before setuid: %d\n", geteuid());
  33. setuid(0); // set actual UID to 0
  34. printf("UID after setuid: %d\n", getuid());
  35. printf("Effective after setuid: %d\n", geteuid());
  36. system("bash"); // execute new shell with root permissions
  37. return 0;
  38. } else {
  39. return 255;
  40. }
  41. }
Add Comment
Please, Sign In to add comment