1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
| #include "Entry.h"
int main() { FILE *pFile = NULL; errno_t err; char *buffer; int nFileLength = 0; err = fopen_s(&pFile, "D:\\desktop\\PEiD.exe", "rb"); fseek(pFile, 0, SEEK_END); nFileLength = ftell(pFile); rewind(pFile); int imageLength = nFileLength * sizeof(char) + 1; buffer = (char *)malloc(imageLength); memset(buffer, 0, nFileLength * sizeof(char) + 1); fread(buffer, 1, imageLength, pFile);
PIMAGE_DOS_HEADER Read_Dos_Header; Read_Dos_Header = (PIMAGE_DOS_HEADER)buffer; printf("--- MS DOS HEADER ---\n"); printf("DOS标志位:%x\n", Read_Dos_Header->e_magic); printf("PE头偏移:%x\n\n", Read_Dos_Header->e_lfanew);
printf("--- PE HEADER ---\n"); PIMAGE_NT_HEADERS Read_NT_Headers; Read_NT_Headers = (PIMAGE_NT_HEADERS)(buffer + Read_Dos_Header->e_lfanew); printf("PE标志位:%x\n", Read_NT_Headers->Signature); printf("运行平台:%x\n", Read_NT_Headers->FileHeader.Machine); printf("程序入口点:%x\n\n", Read_NT_Headers->OptionalHeader.ImageBase);
PIMAGE_SECTION_HEADER Read_Section_Header = IMAGE_FIRST_SECTION(Read_NT_Headers); printf("--- Section HEADER ---\n"); PIMAGE_FILE_HEADER PFileHeder = &(Read_NT_Headers->FileHeader); for (int i = 0; i < PFileHeder->NumberOfSections; ++i) { printf("Name(区段名):%s\n", Read_Section_Header[i].Name); printf("VOffset(起始的相对虚拟地址):%08X\n", Read_Section_Header[i].VirtualAddress); printf("VSize(内存区段大小):%08X\n", Read_Section_Header[i].SizeOfRawData); printf("ROffset(文件偏移):%08X\n", Read_Section_Header[i].PointerToRawData); printf("RSize(文件区段大小):%08X\n", Read_Section_Header[i].Misc.VirtualSize); printf("标记(区段属性):%08X\n\n", Read_Section_Header[i].Characteristics); }
printf("---导入表信息---\n"); ImportTbale(buffer);
printf("---导入表信息---\n"); ExportTable(buffer);
printf("---TLS表信息---\n"); TLSTable(buffer);
printf("---延迟导入表表信息---\n"); DelayImport(buffer);
free(buffer); return 0; }
DWORD RvaToOffset(DWORD dwRva, char *buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer; PIMAGE_NT_HEADERS pNT = (PIMAGE_NT_HEADERS)(buffer + pDos->e_lfanew);
PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pNT);
if (dwRva < pSection[0].VirtualAddress) { return dwRva; } for (int i = 0; i < pNT->FileHeader.NumberOfSections; ++i) { if (dwRva >= pSection[i].VirtualAddress && dwRva <= pSection[i].VirtualAddress + pSection[i].Misc.VirtualSize) { return dwRva - pSection[i].VirtualAddress + pSection[i].PointerToRawData; } } return 0; }
void ImportTbale(char *buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer;
PIMAGE_NT_HEADERS pNT = (PIMAGE_NT_HEADERS)(buffer + pDos->e_lfanew);
PIMAGE_DATA_DIRECTORY pImoprtDir = (PIMAGE_DATA_DIRECTORY)(pNT->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT); PIMAGE_IMPORT_DESCRIPTOR pImport = (PIMAGE_IMPORT_DESCRIPTOR)(RvaToOffset(pImoprtDir->VirtualAddress, buffer) + buffer); while (pImport->Name != NULL) { char *szDllName = (char *)(RvaToOffset(pImport->Name, buffer) + buffer); printf("DLL名称: %s\n", szDllName); printf("日期时间标志: %08x\n", pImport->TimeDateStamp); printf("ForwarderChain: %08x\n", pImport->ForwarderChain); printf("名称OFFSET: %08x\n", pImport->Name); printf("FirstThunk: %08x\n", pImport->FirstThunk); printf("OriginalFirstThunk: %08x\n\n", pImport->OriginalFirstThunk);
PIMAGE_THUNK_DATA pIat = (PIMAGE_THUNK_DATA)(RvaToOffset(pImport->OriginalFirstThunk, buffer) + buffer); DWORD index = 0; DWORD ImportOffset = 0; while (pIat->u1.Ordinal != 0) { printf("ThunkRva: %08x\n", pImport->OriginalFirstThunk + index); ImportOffset = RvaToOffset(pImport->OriginalFirstThunk, buffer); printf("ThunkOffset: %08x\n", ImportOffset + index); index += 4; if ((pIat->u1.Ordinal & 0x80000000) != 1) { PIMAGE_IMPORT_BY_NAME pName = (PIMAGE_IMPORT_BY_NAME)(RvaToOffset(pIat->u1.AddressOfData, buffer) + buffer); printf("API名称:%s\n", pName->Name); printf("Hint(API序号):%04x\n", pName->Hint); printf("ThunkValue:%08x\n\n", pIat->u1.Function); } pIat++; } pImport++; }
}
void ExportTable(char *buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer;
PIMAGE_NT_HEADERS pNT = (PIMAGE_NT_HEADERS)(buffer + pDos->e_lfanew);
PIMAGE_DATA_DIRECTORY pExportDir = pNT->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT;
PIMAGE_EXPORT_DIRECTORY pExport = (PIMAGE_EXPORT_DIRECTORY)(RvaToOffset(pExportDir->VirtualAddress, buffer) + buffer); char *szName = (char *)(RvaToOffset(pExport->Name, buffer) + buffer); if (pExport->AddressOfFunctions == 0) { printf("当前没有导出表!\n\n"); return; } printf("导出表OFFSET:%08x\n", RvaToOffset(pExportDir->VirtualAddress, buffer)); printf("特征值:%08x\n", pExport->Characteristics); printf("基数:%08x\n", pExport->Base); printf("名称OFFSET:%08x\n", pExport->Name); printf("名称:%s\n", szName); printf("函数数量:%08x\n", pExport->NumberOfFunctions); printf("函数名数量:%08x\n", pExport->NumberOfNames); printf("函数地址:%08x\n", pExport->AddressOfFunctions); printf("函数名地址:%08x\n", pExport->AddressOfNames); printf("函数名称序号地址:%08x\n", pExport->AddressOfNameOrdinals); DWORD dwNumOfFun = pExport->NumberOfFunctions; DWORD dwNumOfNames = pExport->AddressOfNames; DWORD dwBase = pExport->Base; PDWORD pEat32 = (PDWORD)(RvaToOffset(pExport->AddressOfFunctions, buffer) + buffer); PDWORD pEnt32 = (PDWORD)(RvaToOffset(pExport->AddressOfNames, buffer) + buffer); PWORD pId = (PWORD)(RvaToOffset(pExport->AddressOfNameOrdinals, buffer) + buffer); for (int i = 0; i < dwNumOfFun; ++i) { if (pEat32[i] == 0) { continue; } DWORD Id = 0; for (; Id < dwNumOfNames; ++Id) { if (pId[Id] == i) { break; } } if (Id == dwNumOfNames) { printf("Id: %x Address: 0x%08x Name[NULL]\n", i + dwBase, pEat32[i]); } else { char *szFunName = (char *)(RvaToOffset(pEnt32[Id], buffer) + buffer); printf("Id: %x Address: 0x%08x Name[%s]\n", i + dwBase, pEat32[i], szFunName); } } }
void RelocTable(char *buffer) { typedef struct _TYPE { WORD Offset : 12; WORD Type : 4; }TYPE, *PTYPE;
PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer;
PIMAGE_NT_HEADERS pNT = (PIMAGE_NT_HEADERS)(buffer + pDos->e_lfanew);
PIMAGE_DATA_DIRECTORY pRelocDir = (pNT->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_BASERELOC);
PIMAGE_BASE_RELOCATION pReloc = (PIMAGE_BASE_RELOCATION)(RvaToOffset(pRelocDir->VirtualAddress, buffer) + buffer); PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pNT);
while (pReloc->SizeOfBlock != 0) { DWORD dwCount = (pReloc->SizeOfBlock - 8) / 2; DWORD dwRva = (pReloc->VirtualAddress); PTYPE pRelocArr = (PTYPE)(pReloc + 1); printf("区段: %s\n", pSection->Name); printf("RVA: %08x\n", dwRva); printf("项目: %X H / %d\n", pReloc->SizeOfBlock, pReloc->SizeOfBlock); pReloc = (PIMAGE_BASE_RELOCATION)((char *)pReloc + pReloc->SizeOfBlock);
for (int i = 0; i < dwCount; ++i) { PDWORD pData = (PDWORD)(RvaToOffset(pRelocArr[i].Offset + dwRva, buffer) + buffer); DWORD pDataOffset = RvaToOffset(pRelocArr[i].Offset + dwRva, buffer); printf("RVA: %08x\n", pRelocArr[i].Offset + dwRva); printf("区段: %08x\n", *pData); printf("偏移: %08x\n\n", pDataOffset); } } }
void TLSTable(char *buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer;
PIMAGE_NT_HEADERS pNT = (PIMAGE_NT_HEADERS)(buffer + pDos->e_lfanew);
PIMAGE_DATA_DIRECTORY pTLSDir = (pNT->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_TLS);
PIMAGE_TLS_DIRECTORY pTLS = (PIMAGE_TLS_DIRECTORY)(RvaToOffset(pTLSDir->VirtualAddress, buffer) + buffer);
printf("数据块开始VA:%08x\n", pTLS->StartAddressOfRawData); printf("数据块结束VA:%08x\n", pTLS->EndAddressOfRawData); printf("索引变量VA:%08x\n", pTLS->AddressOfIndex); printf("回调表VA:%08x\n", pTLS->AddressOfCallBacks); printf("填0大小:%08x\n", pTLS->SizeOfZeroFill); printf("特征值:%08x\n\n", pTLS->Characteristics); }
void DelayImport(char *buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer;
PIMAGE_NT_HEADERS pNT = (PIMAGE_NT_HEADERS)(buffer + pDos->e_lfanew);
PIMAGE_DATA_DIRECTORY pDelayLoadDir = (pNT->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT);
PIMAGE_DELAYLOAD_DESCRIPTOR pDelayLoad = (PIMAGE_DELAYLOAD_DESCRIPTOR)(RvaToOffset(pDelayLoadDir->VirtualAddress, buffer) + buffer);
while (pDelayLoad->DllNameRVA != NULL) { char *szDllName = (char *)(RvaToOffset(pDelayLoad->DllNameRVA, buffer) + buffer); printf("DllName: %s\n", szDllName); printf("Attributes: %08X\n", pDelayLoad->Attributes); printf("ModuleHandleRVA: %08X\n", pDelayLoad->ModuleHandleRVA); printf("ImportAddressTableRVA: %08X\n", pDelayLoad->ImportAddressTableRVA); printf("ImportNameTableRVA: %08X\n", pDelayLoad->ImportNameTableRVA); printf("BoundImportAddressTableRVA: %08X\n", pDelayLoad->BoundImportAddressTableRVA); printf("UnloadInformationTableRVA: %08X\n", pDelayLoad->UnloadInformationTableRVA); printf("TimeDateStamp: %08X\n\n", pDelayLoad->TimeDateStamp); pDelayLoad++; } }
|