Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector< memory::memory_pointer_tr > CMemoryManager::FindString( const char * szModule, const char * szString )
- {
- static constexpr uintptr_t mask = ( PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY );
- std::string std( szString );
- std::vector< uint8_t > strBytes( std.begin(), std.end() );
- std::vector< memory::memory_pointer_tr > vecFound;
- uint8_t *puMemoryAddr = reinterpret_cast< uint8_t* >( GetModuleHandle( szModule ) );
- PIMAGE_DOS_HEADER pDosHeader = reinterpret_cast< PIMAGE_DOS_HEADER >( puMemoryAddr );
- PIMAGE_NT_HEADERS pNtHeaders = reinterpret_cast< PIMAGE_NT_HEADERS >( pDosHeader + pDosHeader->e_lfanew );
- while ( reinterpret_cast< uintptr_t >( puMemoryAddr ) < ( reinterpret_cast< uintptr_t >( puMemoryAddr ) + pNtHeaders->OptionalHeader.SizeOfImage ) )
- {
- MEMORY_BASIC_INFORMATION mbi;
- // did VirtualQuery fail?
- if ( VirtualQuery( puMemoryAddr, &mbi, sizeof( MEMORY_BASIC_INFORMATION ) ) == 0 )
- {
- puMemoryAddr += 4096;
- continue;
- }
- // Bad region size
- if ( mbi.RegionSize == 0 )
- break;
- // bad mem state
- if ( mbi.State != MEM_COMMIT || mbi.Protect & PAGE_GUARD || !( mbi.Protect & mask ) )
- goto next;
- // search for bytes
- {
- uint8_t* pBegin = static_cast< uint8_t* >( mbi.BaseAddress );
- uint8_t* pEnd = pBegin + mbi.RegionSize;
- uint8_t* pSearch = std::search( pBegin, pEnd, strBytes.begin(), strBytes.end() );
- // if search != end, it means we found something
- while ( pSearch != nullptr && pSearch != pEnd )
- {
- // we found an address
- vecFound.push_back( pSearch );
- // advance the search to the next area
- pSearch = std::search( pSearch + 1, pEnd, strBytes.begin(), strBytes.end() );
- }
- }
- // skip to the next region
- next:
- puMemoryAddr += mbi.RegionSize;
- }
- return vecFound;
- }
Advertisement
Add Comment
Please, Sign In to add comment