节表是 IMAGE_SECTION_HEADER 的数组,只要得到节表的位置通过循环就可以进行遍历,代码如下:
void PrintSecTab(PIMAGE_NT_HEADERS pNtHdr)
{
PIMAGE_SECTION_HEADER pSecHdr = IMAGE_FIRST_SECTION(pNtHdr);
PIMAGE_SECTION_HEADER pTmpHdr = pSecHdr;
printf("IMAGE_SECTION_HEADER: \r\n");
for (int i = 0; i < pNtHdr->FileHeader.NumberOfSections; i ++)
{
printf("\t Name:%s \r\n", pTmpHdr->Name);
printf("\t VirtualSize:%08x \r\n", pTmpHdr->Misc.VirtualSize);
printf("\t VirtualAddress:%08x \r\n", pTmpHdr->VirtualAddress);
printf("\t SizeOfRawData:%08x \r\n", pTmpHdr->SizeOfRawData);
printf("\t PointerToRawData:%08x \r\n", pTmpHdr->PointerToRawData);
printf("\t PointerToRelocations:%08x \r\n", pTmpHdr->PointerToRelocations);
printf("\t PointerToLinenumbers:%08x \r\n", pTmpHdr->PointerToLinenumbers);
printf("\t NumberOfRelocations:%08x \r\n", pTmpHdr->NumberOfRelocations);
printf("\t NumberOfLinenumbers:%08x \r\n", pTmpHdr->NumberOfLinenumbers);
printf("\t Characteristics:%08x \r\n", pTmpHdr->Characteristics);
for (int j = 0; j < sizeof(SecCharacteristics) / sizeof(DWORD); j ++)
{
if (SecCharacteristics[j] & pTmpHdr->Characteristics)
{
printf("\t \t: %ws \r\n", SecCharacteristicsStr[j]);
}
}
pTmpHdr ++;
}
}
运行结果如下:
注意:
在前面的代码当中有一些小的 Bug,这次写的时候发现了。
在循环属性的时候,通过 sizeof 求了数组的大小,但是数组的大小并不是循环的次数,这是写的时候的 Bug,代码如下:
sizeof(SecCharacteristics) / sizeof(DWORD)
之前的代码,都没有除以 sizeof(type) 这个运算,因此循环的次数是错误的。前面的代码我都已经修正了,一共有两处,一处是 连载16:输出文件头 解析属性,另外一处是 连载23:代码输出可选头 解析子系统的代码。请大家也进行修改。
微信中搜索 “码农UP2U” 关注我的公众号吧!!!