home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / tmp9 / uncrypt.c < prev    next >
C/C++ Source or Header  |  2000-01-05  |  5KB  |  197 lines

  1. #include <windows.h>
  2.  
  3. #define _PROTOTYP( func, parms ) func parms
  4.  
  5. #define FILECOUNT 2
  6.  
  7. CHAR * FileName[FILECOUNT] ;
  8. HANDLE hFile[FILECOUNT] ;
  9. HANDLE hFileMapping[FILECOUNT] ;
  10. CHAR * pView[FILECOUNT] ;
  11. struct ck_registration * RegBlock[FILECOUNT] ;
  12. FILETIME ftLastAccess[FILECOUNT], ftCreation[FILECOUNT], ftLastWrite[FILECOUNT] ;
  13.  
  14. char *zinptr=NULL;
  15. int zincnt=0, zoutcnt=0;
  16. char *zoutptr=NULL;
  17. void zdstuff(char c) {};
  18.  
  19.  
  20. int
  21. ckOpenFile(int i)
  22. {
  23.     hFile[i] = CreateFile(
  24.                    FileName[i],    // address of name of the file 
  25.                    GENERIC_READ|GENERIC_WRITE,
  26.                    0,     // share mode 
  27.                    NULL,// address of security descriptor 
  28.                    OPEN_EXISTING,    // how to create 
  29.                    FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,    // file attributes 
  30.                    NULL     // handle of file with attributes to copy  
  31.    );
  32.  
  33.    GetFileTime( hFile[i], &ftCreation[i], &ftLastAccess[i], &ftLastWrite[i] ) ;
  34.    hFileMapping[i] = CreateFileMapping( hFile[i],
  35.                                             NULL,
  36.                                             PAGE_READWRITE,
  37.                                             0,
  38.                                             0,
  39.                                             NULL );
  40.  
  41.    pView[i] = (CHAR *) MapViewOfFile( hFileMapping[i],
  42.                                  FILE_MAP_ALL_ACCESS,
  43.                                  0,0,0);
  44.  
  45.     if ( !pView[i] ) {
  46.         return 0;
  47.     }
  48.    return 1;
  49. }
  50.  
  51. int 
  52. ckCloseFile( int i ) 
  53. {
  54.    if (pView[i])
  55.       UnmapViewOfFile( pView[i] ) ;
  56.    if (hFileMapping[i])
  57.       CloseHandle( hFileMapping[i] ) ;
  58.    if (hFile[i])
  59.    {
  60.       SetFileTime( hFile[i], &ftCreation[i], &ftLastAccess[i], &ftLastWrite[i] ) ;
  61.       CloseHandle( hFile[i] ) ;
  62.    }
  63.    return 1;
  64. }
  65.  
  66. int 
  67. SearchForRegBlock( int i ) 
  68. {
  69.     CHAR * SearchString1 = NULL, * SearchString2 = NULL;
  70.     int    SearchStringLength1 = 0;
  71.     int    SearchStringLength2 = 0;
  72.     int SearchLength = 0;
  73.     BY_HANDLE_FILE_INFORMATION fileInfo ;
  74.     CHAR * start = NULL ;
  75.     CHAR * p = NULL ;
  76.     int found = 0 ;
  77.     ULONG k = 0 ;
  78.  
  79.     if ( i == 0 ) {
  80.         SearchString1 = "K95CRYPT";
  81.         // SearchString2 = "SSLEAY32";
  82.     }
  83.     else {
  84.         SearchString1 = "K2CRYPT";
  85.         // SearchString2 = "SSLEAY2";
  86.     }
  87.     SearchStringLength1 = SearchLength = strlen(SearchString1);
  88.  
  89.     GetFileInformationByHandle( hFile[i], &fileInfo ) ;
  90.  
  91.     //  We only search for the first null terminated portion of the search string 
  92.     //  because we want to be able to find a pre-registered copy
  93.  
  94.     if ( !pView[i] )
  95.         return 0 ;
  96.  
  97.     if ( SearchString1 ) {
  98.         start = pView[i] ;
  99.         p = pView[i] ;
  100.         found = 0 ;
  101.  
  102.         while ( !found && k < fileInfo.nFileSizeLow )
  103.         {
  104.             LONG j = 0 ;
  105.             if ( p[k] == SearchString1[0] )
  106.             {
  107.                 start = &p[k] ;
  108.                 for ( j = 0 ; j < SearchLength ; j++ )
  109.                 {
  110.                     if ( start[j] != SearchString1[j] )
  111.                         break;
  112.                 }
  113.                 if ( j == SearchLength )
  114.                     found = 1 ;
  115.             }
  116.             k++ ;
  117.         }
  118.  
  119.         if ( found )
  120.         {
  121.             printf("%s replaced by NULs\n",SearchString1);
  122.             memset(start,0,SearchStringLength1);
  123.         }
  124.     }
  125.     
  126.     if ( SearchString2 ) {
  127.         SearchStringLength2 = SearchLength = strlen(SearchString2);
  128.         start = pView[i] ;
  129.         p = pView[i] ;
  130.         found = 0 ;
  131.  
  132.         while ( !found && k < fileInfo.nFileSizeLow )
  133.         {
  134.             LONG j = 0 ;
  135.             if ( p[k] == SearchString2[0] )
  136.             {
  137.                 start = &p[k] ;
  138.                 for ( j = 0 ; j < SearchLength ; j++ )
  139.                 {
  140.                     if ( start[j] != SearchString2[j] )
  141.                         break;
  142.                 }
  143.                 if ( j == SearchLength )
  144.                     found = 1 ;
  145.             }
  146.             k++ ;
  147.         }
  148.  
  149.         if ( found )
  150.         {
  151.             printf("%s replaced by NULs\n",SearchString2);
  152.             memset(start,0,SearchStringLength2);
  153.         }
  154.     }
  155.     
  156.    return found;
  157. }
  158.  
  159.  
  160. int
  161. main( int argc, char ** argv ) 
  162. {
  163.    int dsn = 0 ;
  164.    int i ;
  165.  
  166.    FileName[0] = "k95.exe" ;
  167.    FileName[1] = "k2.exe" ;
  168.  
  169.    for ( i=0 ; i < FILECOUNT ; i++ )
  170.    {
  171.       hFile[i]        = NULL ;
  172.       hFileMapping[i] = NULL ;
  173.       pView[i]        = NULL ;
  174.       RegBlock[i] = NULL ;
  175.  
  176.       if ( !ckOpenFile(i) )
  177.       {
  178.          printf("Unable to open %s\n", FileName[i]) ;
  179.          continue;
  180.       }
  181.  
  182.       printf("\n%s\n++++++++++++\n",FileName[i]);
  183.       if (!SearchForRegBlock(i))
  184.       {
  185.          printf( "Invalid %s file.\n", FileName[i] ) ;
  186.          continue;
  187.       }
  188.    }
  189.  
  190.    /* Parse command line and use it to register the files */
  191.  
  192.  
  193.    for ( i=0; i<FILECOUNT; i++)
  194.       ckCloseFile(i) ;
  195. }
  196.  
  197.