home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / CHECKEXE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  141 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** CHECKEXE.C - checksum protection for executable files
  5. **
  6. ** by: Bob Jarvis
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <stdlib.h>
  13. #include "crc.h"
  14.  
  15. static struct {
  16.       unsigned char marker[16];
  17.       unsigned long checksum;
  18. } marker_struct = {"CHECKEXE MARKER",0L};
  19.  
  20. void checkexe(char *fname)
  21. {
  22.       FILE *fptr;
  23.       unsigned int c;
  24.       int first_time = 0, i;
  25.       char buffer[14];
  26.       unsigned long chksum = 0L;
  27.       unsigned long l;
  28.       unsigned long marker_offset;
  29.       unsigned char *charptr;
  30.       time_t tm;
  31.  
  32.       fptr = fopen(fname,"r+b");
  33.       if(fptr == NULL)
  34.       {
  35.             fprintf(stderr,"checkexe : unable to open input file '%s'\n",
  36.                   fname);
  37.             exit(99);
  38.       }
  39.  
  40.       setvbuf(fptr, NULL, _IOFBF, 32767);    /* try to get a 32K buffer */
  41.  
  42.    /* 
  43.     * If this is the first time the check has been run, scan the entire file
  44.     * to find the marker.  Otherwise proceed.
  45.     */
  46.  
  47.       if(marker_struct.checksum == 0L)
  48.       {
  49.             first_time = 1;
  50.  
  51.             c = fgetc(fptr);
  52.             while(!feof(fptr))
  53.             {
  54.                   if(c == (unsigned int)marker_struct.marker[0])
  55.                   {
  56.                         fread(buffer,sizeof(buffer),1,fptr);
  57.                         if(memcmp(buffer,&marker_struct.marker[1],
  58.                               sizeof(buffer)) == 0)
  59.                         {
  60.                               marker_offset = ftell(fptr) + 1L;
  61.                               break;
  62.                         }
  63.                         fseek(fptr,-13L,SEEK_CUR);
  64.                   }
  65.  
  66.                   c = fgetc(fptr);
  67.             }
  68.  
  69.             if(feof(fptr))
  70.             {
  71.                   fprintf(stderr,"checkexe : unable to locate marker\n");
  72.                   exit(99);
  73.             }
  74.  
  75.       /* Change the marker field to random values */
  76.  
  77.             tm = time(NULL);
  78.  
  79.             srand((unsigned int)tm);
  80.  
  81.             for(i = 0 ; i < sizeof(marker_struct.marker) ; ++i)
  82.                   marker_struct.marker[i] = (unsigned char) rand();
  83.  
  84.             fseek(fptr,marker_offset - sizeof(marker_struct.marker),SEEK_SET);
  85.  
  86.             fwrite(marker_struct.marker,sizeof(marker_struct.marker),1,fptr);
  87.       }
  88.  
  89.    /* Calculate the checksum for the entire file */
  90.  
  91.       rewind(fptr);
  92.  
  93.       c = fgetc(fptr);
  94.       for(l = 0 ; !feof(fptr) ; ++l)
  95.       {
  96.             chksum += (unsigned long)c;
  97.             c = fgetc(fptr);
  98.       }
  99.  
  100.       if(first_time)
  101.       {
  102.             marker_struct.checksum = chksum;
  103.             fseek(fptr,marker_offset,SEEK_SET);
  104.             fwrite(&marker_struct.checksum,sizeof(unsigned long),1,fptr);
  105.       }
  106.       else
  107.       {
  108.             charptr = (unsigned char*) &marker_struct.checksum;
  109.  
  110.             for(i = 0 ; i < sizeof(marker_struct.checksum) ; ++i)
  111.                   chksum -= (unsigned long)(charptr[i]);
  112.  
  113.             if(chksum != marker_struct.checksum)
  114.             {
  115.                   fprintf(stderr, "\acheckexe : %s has been altered, "
  116.                         "possibly by a virus\n", fname);
  117.                   exit(99);
  118.             }
  119.       }
  120.  
  121.       fclose(fptr);
  122.       return;
  123. }
  124.  
  125. #ifdef TEST
  126.  
  127. #ifdef __WATCOMC__
  128.  #pragma off (unreferenced);
  129. #endif
  130. #ifdef __TURBOC__
  131.  #pragma argsused
  132. #endif
  133.  
  134. main(int argc, char *argv[])
  135. {
  136.       checkexe(argv[0]);
  137.       return EXIT_SUCCESS;
  138. }
  139.  
  140. #endif /* TEST */
  141.