Hi guys !
I want check if the code section in file(test.dll) is the same with which I loaded into process memory. Here is the code.
Firstly I get the crc of code section in PE file
hFile = CreateFile(
szFileName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if( hFile != INVALID_HANDLE_VALUE )
{
FileSize=GetFileSize(hFile,&szTemp);
if (FileSize == 0xFFFFFFFF) return FALSE;
pBuffer = new TCHAR [FileSize];
if(ReadFile(hFile, pBuffer, FileSize, &szTemp, NULL)==NULL) return FALSE;
}
else
{
printf("I can't access file!");
return false;
}
pDosHeader=(PIMAGE_DOS_HEADER)pBuffer;
pNtHeader=(PIMAGE_NT_HEADERS32)((DWORD)pDosHeader+pDosHeader->e_lfanew);
IMAGE_FILE_HEADER *pFileHeader = &pNtHeader->FileHeader;
pSecHeader=IMAGE_FIRST_SECTION(pNtHeader);
for(int i = 0 ; i < pFileHeader->NumberOfSections;i++)
{
if((strcmp((char*)pSecHeader->Name,".text") == 0))
{
break;
}
pSecHeader++;
}
BYTE* pBuffStart = (BYTE*)(pBuffer+pSecHeader->PointerToRawData);
szCRC32=Crc32_ComputeBuf(pBuffStart,pSecHeader->Misc.VirtualSize);
Secondly, I get the crc in memory scapce
PIMAGE_DOS_HEADER pDosHeader = nullptr;
PIMAGE_NT_HEADERS pNTHeader = nullptr;
PIMAGE_SECTION_HEADER pSectionHeader = nullptr;
DWORD ImageBase, OriginalCRC32;
ImageBase = (DWORD)GetModuleHandleA(pModuleName);
pDosHeader = (PIMAGE_DOS_HEADER)ImageBase;
pNTHeader = (PIMAGE_NT_HEADERS32)((DWORD)pDosHeader + pDosHeader->e_lfanew);
OriginalCRC32 = *((DWORD*)((DWORD)pNTHeader - 4));
printf("Original Crc read from file: %08x\n",OriginalCRC32);
pSectionHeader = IMAGE_FIRST_SECTION(pNTHeader);
IMAGE_FILE_HEADER* pFileHeader = &pNTHeader->FileHeader;
for (int i = 0; i < pFileHeader->NumberOfSections;i++)
{
if((strcmp((char*)pSectionHeader->Name,".text") == 0))
{
break;
}
pSectionHeader++;
}
printf("ImageCodeSectionCrc32 first section name:%s\n",pSectionHeader->Name);
BYTE* pBuffStart = (BYTE*)(ImageBase + pSectionHeader->VirtualAddress);
crc32 = Crc32_ComputeBuf(pBuffStart,pSectionHeader->Misc.VirtualSize);
The two crc values are not match, I also can't find the original code buff of pe file in the code section of dll image.
Does I make a mistake here? I sincerely hope someone can help me find out why.