编写程序打印notepad.exe节表中的信息. 为什么除了第一个节是对的,其他都是错的
本帖最后由 d173220523 于 2020-5-3 15:14 编辑#include "iostream"
#include "stdio.h"
#include <stdlib.h>
#include <windows.h>
LPVOID ReadPEFile(LPSTR lpszFile)
{
FILE *pFile = NULL;
DWORD fileSize = 0;
LPVOID pFileBuffer = NULL;
if ( (pFile = fopen(lpszFile, "rb")) == NULL )
puts("Fail to open file!");
fseek(pFile,0,SEEK_END);
fileSize=ftell(pFile);
pFileBuffer = malloc(fileSize);
fseek(pFile,0,SEEK_SET);
if(pFileBuffer == NULL)
puts("申请失败");
size_t n = fread(pFileBuffer, fileSize, 1, pFile);
if(!n)
{
printf(" 读取数据失败! ");
free(pFileBuffer);
fclose(pFile);
return NULL;
}
fclose(pFile);
return pFileBuffer;
}
VOID PrintNTHeaders()
{
LPVOID pFileBuffer = NULL;
PIMAGE_DOS_HEADER pDosHeader = NULL;
PIMAGE_NT_HEADERS pNTHeader = NULL;
PIMAGE_FILE_HEADER pPEHeader = NULL;
PIMAGE_OPTIONAL_HEADER32 pOptionHeader = NULL;
PIMAGE_SECTION_HEADER pSectionHeader = NULL;
pFileBuffer = ReadPEFile("C:\\Windows\\system32\\notepad.exe");
if(*((PWORD)pFileBuffer) != IMAGE_DOS_SIGNATURE)
{
printf("不是有效的MZ标志\n");
free(pFileBuffer);
return ;
}
pDosHeader = (PIMAGE_DOS_HEADER)pFileBuffer;
printf("********************DOC头********************\n");
printf("MZ标志:%x\n",pDosHeader->e_magic);
printf("PE偏移:%x\n",pDosHeader->e_lfanew);
//if(*((PWORD)pFileBuffer+pDosHeader->e_lfanew) != IMAGE_NT_SIGNATURE)
if(*((PDWORD)((DWORD)pFileBuffer+pDosHeader->e_lfanew)) != IMAGE_NT_SIGNATURE)
{
printf("不是有效的PE标志\n");
free(pFileBuffer);
return ;
}
pNTHeader=(PIMAGE_NT_HEADERS)((DWORD)pFileBuffer+pDosHeader->e_lfanew);
//printf("********************NT头********************\n");
//printf("Signature:%x\n",pNTHeader->Signature);
pPEHeader=(PIMAGE_FILE_HEADER)(((DWORD)pNTHeader)+4);
// printf("********************PE头********************\n");
//printf("Machine:%x\n",pPEHeader->Machine);
// printf("NumberOfSections:%x\n",pPEHeader->NumberOfSections);
// printf("SizeOfOptionalHeader:%x\n",pPEHeader->SizeOfOptionalHeader);
pOptionHeader=(PIMAGE_OPTIONAL_HEADER32)((DWORD)pNTHeader+0x18);
// printf("********************OPTIONAL_PE头********************\n");
// printf("OPTION_PE:%x\n",pOptionHeader->Magic);
// printf("AddressOfEntryPoint:%x\n",pOptionHeader->AddressOfEntryPoint);
// printf("ImageBase:%x\n",pOptionHeader->ImageBase);
// printf("SectionAlignment:%x\n",pOptionHeader->SectionAlignment);
// printf("FileAlignment:%x\n",pOptionHeader->FileAlignment);
pSectionHeader=(PIMAGE_SECTION_HEADER)((DWORD)pOptionHeader+pPEHeader->SizeOfOptionalHeader);
printf("********************节表********************\n");
for(int i=0;i<pPEHeader->NumberOfSections;i++)
{
printf("Name:%s\n",pSectionHeader->Name);
printf("Misc:%x\n",pSectionHeader->Misc);
printf("VirtualAddress:%x\n",pSectionHeader->VirtualAddress);
printf("SizeOfRawData:%x\n",pSectionHeader->SizeOfRawData);
printf("PointerToRawData:%x\n",pSectionHeader->PointerToRawData);
printf("Characteristics:%x\n",pSectionHeader->Characteristics);
printf("\n");
pSectionHeader+=0x28;//这里应该加什么?
}
free(pFileBuffer);
}
int main()
{
PrintNTHeaders();
return 0;
}
兩個建議,可以嘗試看看:
1.把 notepad.exe 複製到 D:\ 之類的地方
(系統路徑可能存在權限問題,不特別處理的話,不一定可以正確讀取)
2.或乾脆隨便找一個綠色軟件來實驗,不要拿系統隨附軟件實驗
(一樣盡可能放在沒有權限問題的路徑)
補充一點:
建議先驗證讀取與寫入,比如完整讀取一個檔案並寫成新檔
然後使用 HashMyFiles 之類的工具,先驗證兩個檔案一致,沒問題再進行下一步 pSectionHeader+=0x28;
它是结构体类型,访问下一个,不应该是加1?+28是?
愚见,不知对吗?说得不当见谅… 我验证了,是指针偏移问题……
pSectionHeader+=0x28
这里+1就正确了,指针偏移+1指向下一个节表……
无闻无问 发表于 2020-5-3 14:32
pSectionHeader+=0x28;
它是结构体类型,访问下一个,不应该是加1?+28是?
愚见,不知对吗?说得不当见 ...
对的对的
页:
[1]