http://www.csdn123.com/html/mycsdn20140110/65/659154f3fcd53390cb5cc65945fc3cb7.html
DWORD getCryptValue(DWORD ptr)
{
return *(PDWORD)ptr;
}
int FirstMemScan(HANDLE hProcess, DWORD start, DWORD end, DWORD value, DWORD *result, int size)
{
MEMORY_BASIC_INFORMATION mbi;
DWORD localStart;
DWORD localEnd;
DWORD totalsize;
int count;
VirtualQueryEx(hProcess, (PVOID)(start), &mbi, sizeof(MEMORY_BASIC_INFORMATION));
localStart = (DWORD)mbi.BaseAddress;
count = 0;
while(localStart < MEMORY_BOUNDARY && localStart <= end)
{
if( !IsValidMem(mbi) )
{
localStart += mbi.RegionSize;
VirtualQueryEx(hProcess, (PVOID)(localStart), &mbi, sizeof(MEMORY_BASIC_INFORMATION));
continue;
}
totalsize = 0;
while (localStart + totalsize < MEMORY_BOUNDARY)
{
VirtualQueryEx(hProcess, (PVOID)(localStart + totalsize), &mbi, sizeof(MEMORY_BASIC_INFORMATION));
if( !IsValidMem(mbi) )
break;
totalsize += mbi.RegionSize;
}
localEnd = min(localStart + totalsize, end);
for (DWORD curr = max(localStart , start); curr < localEnd; curr += 4)
{
if (getCryptValue(curr) == value)
{
result[count++] = curr;
debug("[%4d] %08X\n", count - 1, result[count - 1]);
if (count >= size)
{
debug("result arrive max size\n");
break;
}
}
}
if (count >= size)
break;
localStart += totalsize;
}
return count;
}
int MemScan(HANDLE hProcess, DWORD value, DWORD *oldResult, DWORD *newResult, int size)
{
MEMORY_BASIC_INFORMATION mbi;
int count = 0;
if (oldResult == nullptr || newResult == nullptr)
return -1;
for (int index = 0; index < size; index++)
{
if (IsValidPtr((PVOID)(oldResult[index])) && getCryptValue(oldResult[index]) == value)
{
newResult[count++] = oldResult[index];
debug("new[%4d] %08X\n", count - 1, newResult[count - 1]);
}
}
return count;
}