Advertisement
Guest User

whitequark

a guest
Jan 21st, 2010
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <grp.h>
  3. #include <sys/types.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. int main(int argc, char** argv) {
  8.     uid_t olduid;
  9.     gid_t target, *list;
  10.     struct group* grent;
  11.     int i, index = -1;
  12.    
  13.     if(argc != 2) {
  14.         printf("Usage: %s <group to remove>\n", argv[0]);
  15.         printf("Must be SUID. Launches shell.\n");
  16.         return 1;
  17.     }
  18.    
  19.     olduid = getuid();
  20.  
  21.     if(setuid(0) == -1) {
  22.         perror("setuid(0)");
  23.         return 1;
  24.     }
  25.    
  26.     int groups = getgroups(0, NULL);
  27.    
  28.     list = malloc(sizeof(gid_t) * groups);
  29.    
  30.     if(getgroups(groups, list) == -1) {
  31.         perror("getgroups");
  32.         return 1;
  33.     }
  34.    
  35.     while(grent = getgrent()) {
  36.         if(!strcmp(grent->gr_name, argv[1])) {
  37.             target = grent->gr_gid;
  38.             break;
  39.         }
  40.     }
  41.     endgrent();
  42.    
  43.     if(grent == NULL) {
  44.         fprintf(stderr, "group not found\n");
  45.         return 1;
  46.     }
  47.    
  48.     for(i = 0; i < groups; i++) {
  49.         if(target == list[i]) {
  50.             index = i;
  51.             break;
  52.         }
  53.     }
  54.    
  55.     if(index == -1) {
  56.         fprintf(stderr, "gid not found\n");
  57.         return 1;
  58.     }
  59.    
  60.     for(i = index + 1; i < groups; i++)
  61.         list[i - 1] = list[i];
  62.    
  63.     if(setgroups(groups - 1, list) == -1) {
  64.         perror("setgroups");
  65.         return 1;
  66.     }
  67.    
  68.     if(setuid(olduid) == -1) {
  69.         perror("setuid(old)");
  70.         return 1;
  71.     }
  72.    
  73.     execl("/bin/sh", "/bin/sh", NULL);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement