home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / VIRUS / DEFORM.ZIP / DEFORM.C next >
Encoding:
C/C++ Source or Header  |  1991-05-15  |  4.8 KB  |  185 lines

  1. /*  Remove the Form virus from floppy diskettes  */
  2. /*  P. Jarvis.                       10/05/1991  */
  3.  
  4. /*  This program should only be used to remove   */
  5. /*  the Form virus from floppy disks. The SYS    */
  6. /*  command should work with hard disks, but     */
  7. /*  make a backup before trying!                 */
  8.  
  9. /*  Disinfection method:                         */
  10. /*                                               */
  11. /*  1)  Read boot sector and check for virus     */
  12. /*  2)  Locate copy of original boot sector      */
  13. /*  3)  Clear bad cluster flag from FAT          */
  14. /*  4)  Copy original boot sector back           */
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <dos.h>
  20.  
  21. void main(int, char **);
  22. void exit(int);
  23. void patch(unsigned char *fat, int s, int t, int h);
  24.  
  25. int bytes_sector;
  26. int sectors_track;
  27. int sectors_cluster;
  28. int sectors_fat;
  29. int tracks_side;
  30. int directory_size;
  31. int heads;
  32. int reserved;
  33.  
  34. void main(int argc, char *argv[])
  35. {
  36.   int drive;                       /*  Drive code (0 - 3)  */
  37.   static unsigned char boot[512];
  38.   static unsigned char orig[512];
  39.   unsigned char *fat;
  40.   union REGS regs;
  41.   struct SREGS sregs;
  42.  
  43.   if (argc != 2) {
  44.     fprintf(stderr, "Usage:  %s drive\n", argv[0]);
  45.     exit(1);
  46.   }
  47.  
  48.   drive = toupper(argv[1][0]) - 'A';       /*  Get drive number  */
  49.   if ((drive < 0) || (drive > 3)) {
  50.     fprintf(stderr, "Drive must be in range A - D\n");
  51.     exit(2);
  52.   }
  53.  
  54.   regs.h.ah = 0x00;                        /*  Reset disk  */
  55.   regs.h.dl = (unsigned char) drive;
  56.   int86(0x13, ®s, ®s);
  57.   
  58.   regs.h.ah = 0x08;                        /*  Now status it  */
  59.   regs.h.dl = (unsigned char) drive;
  60.   int86x(0x13, ®s, ®s, &sregs);
  61.   if (regs.x.cflag) {
  62.     fprintf(stderr, "Unable to status disk\n");
  63.     exit(3);
  64.   }
  65.  
  66. /*  Read boot sector  */
  67.  
  68.   regs.h.al = (unsigned char) drive;
  69.   regs.x.bx = (unsigned int) boot;
  70.   regs.x.cx = 1;
  71.   regs.x.dx = 0;
  72.   int86(0x25, ®s, ®s);
  73.   if (regs.x.cflag) {
  74.     fprintf(stderr, "Failed to read boot sector\n");
  75.     exit(4);
  76.   }
  77.  
  78. /*  Check if disk is infected already  */
  79.  
  80.   if ((boot[0x3F] != 0x01) || (boot[0x40] != 0xFE)) {
  81.     fprintf(stderr, "This disk does not have the Form virus\n");
  82.     exit(0);
  83.   }
  84.  
  85. /*  Get parameters  */
  86.  
  87.   heads = boot[0x1B] * 256 + boot[0x1A];
  88.   bytes_sector = boot[0x0C] * 256 + boot[0x0b];
  89.   sectors_track = boot[0x19] * 256 + boot[0x18];
  90.   sectors_cluster = (int) boot[0x0D];
  91.   sectors_fat = boot[0x17] * 256 + boot[0x16];
  92.   tracks_side = (boot[0x14] * 256 + boot[0x13]) / sectors_track / heads;
  93.   reserved = boot[0x0f] * 256 + boot[0x0E] + boot[0x10] *
  94.              (boot[0x17] * 256 + boot[0x16]);
  95.   directory_size = boot[0x12] * 256 + boot[0x11];
  96.  
  97. /*  Allocate space for FAT  */
  98.  
  99.   fat = (unsigned char *) malloc(bytes_sector * sectors_fat);
  100.   if (fat == NULL) {
  101.     fprintf(stderr, "Unable to assign space for FAT\n");
  102.     exit(6);
  103.   }
  104.  
  105. /*  Now read the FAT  */
  106.  
  107.   regs.h.al = (unsigned char) drive;
  108.   regs.x.bx = (unsigned int) fat;
  109.   regs.x.cx = sectors_fat;
  110.   regs.x.dx = 1;
  111.   int86(0x25, ®s, ®s);
  112.   if (regs.x.cflag) {
  113.     fprintf(stderr, "Failed to read FAT\n");
  114.     exit(4);
  115.   }
  116.  
  117.   patch(fat, boot[0x49], boot[0x4C], boot[0x4A]);
  118.   patch(fat, boot[0x4D], boot[0x50], boot[0x4E]);
  119.  
  120. /*  Now re-write the FAT  */
  121.  
  122.   regs.h.al = (unsigned char) drive;
  123.   regs.x.bx = (unsigned int) fat;
  124.   regs.x.cx = sectors_fat;
  125.   regs.x.dx = 1;
  126.   int86(0x26, ®s, ®s);
  127.   if (regs.x.cflag) {
  128.     fprintf(stderr, "Failed to re-write FAT sector\n");
  129.     exit(4);
  130.   }
  131.  
  132. /*  Now try to read original boot sector  */
  133.  
  134.   regs.x.ax = 0x0201;
  135.   regs.x.bx = (unsigned int) orig;
  136.   regs.x.cx = boot[0x4A] * 256 + boot[0x49];
  137.   regs.x.dx = boot[0x4C] * 256 + boot[0x4B];
  138.   regs.h.dl = (unsigned char) drive;
  139.   int86(0x13, ®s, ®s);
  140.   if (regs.x.cflag) {
  141.     fprintf(stderr, "Unable to read original boot sector\n");
  142.     exit(5);
  143.   }
  144.  
  145. /*  Now write the original boot sector  */
  146.  
  147.   regs.h.al = (unsigned char) drive;
  148.   regs.x.bx = (unsigned int) orig;
  149.   regs.x.cx = 1;
  150.   regs.x.dx = 0;
  151.   int86(0x26, ®s, ®s);
  152.   if (regs.x.cflag) {
  153.     fprintf(stderr, "Failed to re-write boot sector\n");
  154.     exit(4);
  155.   }
  156.  
  157.   printf("The Form virus has been removed.\n");
  158.   exit(0);
  159. }
  160.  
  161.  
  162. void patch(unsigned char *fat, int s, int t, int h)
  163. {
  164.   int sector;
  165.   int cluster;
  166.   int offset;
  167.   sector = s -1 +
  168.            t * sectors_track +
  169.            h * sectors_track * heads;
  170.   cluster = (sector - reserved - (directory_size * 32 + bytes_sector -1) /
  171.              bytes_sector) / sectors_cluster + 2;
  172.   offset = cluster * 3 / 2;
  173.   if (cluster & 1) {
  174.     fat[offset] &= 0x0F;
  175.     fat[offset+1] = 0;
  176.   }
  177.   else {
  178.     fat[offset] = 0;
  179.     fat[offset+1] &= 0xF0;
  180.   }
  181. }
  182.  
  183.  
  184.  
  185.