y0Mike

FindString

Jul 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. std::vector< memory::memory_pointer_tr > CMemoryManager::FindString( const char * szModule, const char * szString )
  2. {
  3.     static constexpr uintptr_t mask = ( PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY );
  4.    
  5.     std::string std( szString );
  6.     std::vector< uint8_t > strBytes( std.begin(), std.end() );
  7.     std::vector< memory::memory_pointer_tr > vecFound;   
  8.  
  9.     uint8_t *puMemoryAddr = reinterpret_cast< uint8_t* >( GetModuleHandle( szModule ) );
  10.  
  11.     PIMAGE_DOS_HEADER pDosHeader = reinterpret_cast< PIMAGE_DOS_HEADER >( puMemoryAddr );
  12.     PIMAGE_NT_HEADERS pNtHeaders = reinterpret_cast< PIMAGE_NT_HEADERS >( pDosHeader + pDosHeader->e_lfanew );
  13.  
  14.     while ( reinterpret_cast< uintptr_t >( puMemoryAddr ) < ( reinterpret_cast< uintptr_t >( puMemoryAddr ) + pNtHeaders->OptionalHeader.SizeOfImage ) )
  15.     {  
  16.         MEMORY_BASIC_INFORMATION mbi;    
  17.         // did VirtualQuery fail?
  18.         if ( VirtualQuery( puMemoryAddr, &mbi, sizeof( MEMORY_BASIC_INFORMATION ) ) == 0 )
  19.         {
  20.             puMemoryAddr += 4096;
  21.             continue;
  22.         }
  23.  
  24.         // Bad region size
  25.         if ( mbi.RegionSize == 0 )
  26.             break;
  27.  
  28.         // bad mem state
  29.         if ( mbi.State != MEM_COMMIT || mbi.Protect & PAGE_GUARD || !( mbi.Protect & mask ) )
  30.             goto next;
  31.        
  32.         // search for bytes
  33.         {
  34.             uint8_t* pBegin = static_cast< uint8_t* >( mbi.BaseAddress );
  35.             uint8_t* pEnd = pBegin + mbi.RegionSize;
  36.             uint8_t* pSearch = std::search( pBegin, pEnd, strBytes.begin(), strBytes.end() );
  37.  
  38.             // if search != end, it means we found something
  39.             while ( pSearch != nullptr && pSearch != pEnd )
  40.             {
  41.                 // we found an address
  42.                 vecFound.push_back( pSearch );
  43.                 // advance the search to the next area
  44.                 pSearch = std::search( pSearch + 1, pEnd, strBytes.begin(), strBytes.end() );
  45.             }
  46.         }
  47.        
  48.     // skip to the next region
  49.     next:
  50.         puMemoryAddr += mbi.RegionSize;
  51.     }      
  52.     return vecFound;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment