Advertisement
Guest User

Untitled

a guest
Dec 9th, 2011
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. int PatchFile(LPCSTR FileName,char* sP_,char* sM_,char* rP_,char* rM_,int pSize)
  2. {
  3.     int retvalue,filesize;
  4.     HANDLE hFile,hFileMapping,hViewOfFile;
  5.     retvalue=0;
  6.     hFile = CreateFile(FileName,GENERIC_READ+GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL+FILE_ATTRIBUTE_HIDDEN,0);
  7.     if(hFile!=INVALID_HANDLE_VALUE)
  8.     {
  9.         hFileMapping = CreateFileMapping(hFile,0,PAGE_READWRITE,0,0,0);
  10.         if(hFileMapping)
  11.         {
  12.             hViewOfFile = MapViewOfFile(hFileMapping,FILE_MAP_WRITE,0,0,0);
  13.             if(hViewOfFile)
  14.             {
  15.                 filesize = GetFileSize(hFile,0);
  16.  
  17.                 retvalue = Search((PBYTE)hViewOfFile,(char*)sP_,(char*)sM_,(char*)rP_,(char*)rM_,pSize,filesize,1);
  18.  
  19.                 UnmapViewOfFile(hViewOfFile);
  20.             }
  21.             CloseHandle(hFileMapping);
  22.         }
  23.         CloseHandle(hFile);
  24.     }
  25.     return retvalue;
  26. }
  27.  
  28. int Search( PBYTE lpTargetAddress,
  29.              char* lpSearchPattern,
  30.              char* lpSearchMask,
  31.              char* lpReplacePattern,
  32.              char* lpReplacehMask,
  33.              UINT cbPatternSize,
  34.              UINT cbSearchSize,
  35.              BOOL bOffset )
  36. {
  37.     ULONG ReturnValue = 0;                                       // Set return value to 0
  38.     int RetV;
  39.     UINT i = 0;                                                  // Set counter 'i' to 0
  40.     UINT j = 0;   // Set counter 'j' to 0
  41.  
  42.     RetV = 0;
  43.  
  44.     do                                                           // Do {STEPS} while {CONDITION == TRUE}
  45.     {
  46.         j = 0;                                                   // In loop, set counter 'j' to 0;
  47.  
  48.         do                                                       // Do {STEPS} while {CONDITION == TRUE}
  49.         {
  50.             if ( lpSearchMask[j] == 0 )                          // If byte is 0x00, check byte, else skip
  51.             {
  52.                 if ( lpTargetAddress[i+j] != lpSearchPattern[j] )// If current byte isn't a match, break loop
  53.                 {
  54.                     break;                                       // Break loop
  55.                 }
  56.             }
  57.  
  58.             j++;                                                 // Else, check next mask byte in pattern
  59.  
  60.         } while ( j < cbPatternSize );                           // CONDITION: While counter 'j' is less than pattern size
  61.  
  62.         if (j ==cbPatternSize )
  63.         {
  64.             ReturnValue = i;                                     // If it makes it here, we've got a match
  65.  
  66.             ReturnValue += ( ( UINT ) lpTargetAddress );        // Add lpTargetAddress to return value
  67.  
  68.             __asm {
  69.                 mov esi,lpReplacePattern
  70.                 mov edi,ReturnValue
  71.                 mov ecx,cbPatternSize
  72.                 rep movsb
  73.                 }
  74.             RetV = 1;
  75.             break;                                               // Break loop          
  76.         }
  77.                
  78.         i++;                                                     // Next byte in search buffer
  79.  
  80.     } while ( i <= cbSearchSize - cbPatternSize );               // CONDITION: While we havent searched the entire search range
  81.  
  82.     return RetV;                                          // Return result
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement