home *** CD-ROM | disk | FTP | other *** search
/ BUG 1 / BUGCD1996_0708.ISO / pc / util / pc64 / virus895.cpp < prev    next >
C/C++ Source or Header  |  1995-08-19  |  3KB  |  122 lines

  1. // VIRUS895.CPP (Visual C++ 16 bit) -- Scans for virus found in 8'95
  2. // When running an infected program, the virus infects the first
  3. // non-infected EXE file in the current directory.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <conio.h>
  9. #include <ctype.h>
  10. #include <dos.h>
  11. #include <fcntl.h>
  12. #include <io.h>
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15.  
  16. typedef unsigned char byte;
  17. typedef unsigned short word;
  18. typedef unsigned int uint;
  19. typedef unsigned long dword;
  20. typedef int handle;
  21. typedef int flag;
  22. const FALSE = 0;
  23. const TRUE = 1;
  24.  
  25. word gwScanned = 0;
  26. word gwInfected = 0;
  27.  
  28. void ScanFile(char* pcName) {
  29.   gwScanned++;
  30.   handle hFile = _open(pcName, _O_BINARY | _O_RDONLY);
  31.   if (hFile == -1) {
  32.     perror(pcName);
  33.     return;
  34.   }
  35.   byte abBuffer[256];
  36.   if (_read(hFile, abBuffer, 24) < 24) {
  37.     perror(pcName);
  38.     _close(hFile);
  39.     return;
  40.   }
  41.   word wHeaderSize = *(word*)(abBuffer + 8);
  42.   word wIP = *(word*)(abBuffer + 20);
  43.   word wCS = *(word*)(abBuffer + 22);
  44.   long lEntryPoint = wHeaderSize * 16L + ((wCS * 16L + wIP) & 0x000FFFFF);
  45.   _lseek(hFile, lEntryPoint, SEEK_SET);
  46.   if (_read(hFile, abBuffer, 5) < 5) {
  47.     perror(pcName);
  48.     _close(hFile);
  49.     return;
  50.   }
  51.   if (abBuffer[0] == 0xE8) {
  52.     long lPos = lEntryPoint + 3 + *(int*)(abBuffer + 1);
  53.     _lseek(hFile, lPos, SEEK_SET);
  54.     if (_read(hFile, abBuffer, 12) < 12) {
  55.       perror(pcName);
  56.       _close(hFile);
  57.       return;
  58.     }
  59.     const byte abVirusEntry[12] = {
  60.       0x5D, 0x1E, 0x06, 0x8C, 0xD0, 0x80, 0xC4, 0x10, 0x8B, 0x1E, 0x02, 0x00
  61.     };
  62.     if (memcmp(abBuffer, abVirusEntry, 12) == 0) {
  63.       printf("%s is infected\n", pcName);
  64.       gwInfected++;
  65.     }
  66.   } else if (abBuffer[0] == 0xEA) {
  67.     printf("%s is suspicious\n", pcName);
  68.   }
  69.   if (_close(hFile) == -1) {
  70.     perror(pcName);
  71.     return;
  72.   }
  73. }
  74.  
  75. void ScanDir(char* pcDir) {
  76.   printf("%-79s\r", pcDir);
  77.   char acName[80];
  78.   strcpy(acName, pcDir);
  79.   char* pcName = acName + strlen(acName);
  80.   if (pcName[-1] != '\\') {
  81.     *pcName++ = '\\';
  82.   }
  83.   strcpy(pcName, "*.*");
  84.   _find_t find;
  85.   uint uFind = _dos_findfirst(acName, _A_NORMAL | _A_SUBDIR, &find);
  86.   while (!uFind) {
  87.     strcpy(pcName, find.name);
  88.     if (find.attrib & _A_SUBDIR) {
  89.       if (find.name[0] != '.') {
  90.         ScanDir(acName);
  91.       }
  92.     } else {
  93.       char* pcExt = strchr(pcName, '.');
  94.       if (pcExt != NULL && stricmp(pcExt, ".EXE") == 0) {
  95.         ScanFile(acName);
  96.       }
  97.     }
  98.     uFind = _dos_findnext(&find);
  99.   }
  100. }
  101.  
  102. int main(int argc, char** argv) {
  103.   _bdos(0x0D, 0, 0);
  104.   char acStartDir[80];
  105.   if (argc < 2) {
  106.     _fullpath(acStartDir, ".", 80);
  107.   } else {
  108.     _fullpath(acStartDir, argv[1], 80);
  109.     _strupr(acStartDir);
  110.   }
  111.   printf("scanning for virus found in 8'95\n");
  112.   printf("start directory is %s\n", acStartDir);
  113.   ScanDir(acStartDir);
  114.   printf("\r%80c%u files scanned, %u files infected\n", '\r', gwScanned, gwInfected);
  115.   #ifdef DEBUG
  116.     printf("\nPress any key to continue...");
  117.     _bdos(0x0C, 0, 0x07);
  118.     printf("\r%80c", '\r');
  119.   #endif
  120.   return 0;
  121. }
  122.