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
| #include <stdio.h> #include <stdlib.h> #include <windows.h>
DWORD RvaToOffset(DWORD dwRva, char* buffer); void ImportTable(char* buffer); void ExportTable(char* buffer); void TlsTable(char* buffer);
int main() { int nFileLength = 0; FILE* pFile = NULL; char path[100]; char *buffer;
printf("Please input the path of the file you want to look by my parser of PE:\n"); gets(path); fopen_s(&pFile,path, "rb"); if(pFile == NULL) { printf("Open file failed,maybe it's doesn't exist?\n"); return 0; } fseek(pFile, 0, SEEK_END); nFileLength = ftell(pFile); if(nFileLength == 0) { printf("The file is empty!\n"); return 0; } rewind(pFile); int ImageBase = nFileLength * sizeof(char) + 1; buffer = (char*)malloc(ImageBase); memset(buffer, 0, nFileLength * sizeof(char) + 1); fread(buffer, 1, ImageBase, pFile);
PIMAGE_DOS_HEADER P_DosHeader; P_DosHeader = (PIMAGE_DOS_HEADER)buffer; printf("Basic Info is as follow:\n"); printf("The Flag bit of this file is %X\n", P_DosHeader->e_magic); printf("The offset of the DOS header is:%X\n", P_DosHeader->e_lfanew); printf("------------------------Thats's basic info of DosHeaderStruct----------------------------\n"); printf("\n"); if(P_DosHeader->e_magic != IMAGE_DOS_SIGNATURE) { printf("This file is not a PE file!\n"); return 0; } PIMAGE_NT_HEADERS32 P_NtHeader; P_NtHeader = (PIMAGE_NT_HEADERS32)(buffer + P_DosHeader->e_lfanew); printf("The follow is the info of PE file header:\n"); printf("The Flag bit of this file is %X\n", P_NtHeader->Signature); printf("Machine: %X\n", P_NtHeader->FileHeader.Machine); printf("Number of Sections: %d\n", P_NtHeader->FileHeader.NumberOfSections); printf("Characteristics: %X\n", P_NtHeader->FileHeader.Characteristics); printf("------------------------Thats's basic info of FILE_HEADER----------------------------\n"); printf("\n"); printf("The follow is the info of PE file optional header:\n"); printf("Magic: %X\n", P_NtHeader->OptionalHeader.Magic); printf("AddressOfEntryPoint: %X\n", P_NtHeader->OptionalHeader.AddressOfEntryPoint); printf("ImageBase: %X\n", P_NtHeader->OptionalHeader.ImageBase); printf("SectionAlignment: %X\n", P_NtHeader->OptionalHeader.SectionAlignment); printf("FileAlignment: %X\n", P_NtHeader->OptionalHeader.FileAlignment); printf("SizeOfImage: %X\n", P_NtHeader->OptionalHeader.SizeOfImage); printf("SizeOfHeaders: %X\n", P_NtHeader->OptionalHeader.SizeOfHeaders); printf("CheckSum: %X\n", P_NtHeader->OptionalHeader.CheckSum); printf("Subsystem: %X\n", P_NtHeader->OptionalHeader.Subsystem); printf("NumberOfRvaAndSizes: %X\n", P_NtHeader->OptionalHeader.NumberOfRvaAndSizes); printf("Number Of Data Directories: %X\n", P_NtHeader->OptionalHeader.NumberOfRvaAndSizes); printf("------------------------Thats's basic info of OPTIONAL_HEADER----------------------------\n"); printf("\n"); printf("------------------------Next are some tables----------------------------\n"); printf("\n"); printf("The follow is the info of PE file data directory:\n"); printf("Address of Export Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[0].VirtualAddress); printf("Size of Export Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[0].Size); printf("Address of Import Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[1].VirtualAddress); printf("Size of Import Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[1].Size); printf("Address of Resource Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[2].VirtualAddress); printf("Size of Resource Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[2].Size); printf("Address of TLS Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[9].VirtualAddress); printf("Size of TLS Table: %X\n", P_NtHeader->OptionalHeader.DataDirectory[9].Size); printf("Address of IAT: %X\n", P_NtHeader->OptionalHeader.DataDirectory[12].VirtualAddress); printf("Size of IAT: %X\n", P_NtHeader->OptionalHeader.DataDirectory[12].Size); printf("------------------------Thats's basic info of DATA_DIRECTORY----------------------------\n"); printf("\n"); printf("The follow is the info of PE file section header:\n"); PIMAGE_SECTION_HEADER P_SectionHeader; P_SectionHeader = (PIMAGE_SECTION_HEADER)(buffer + P_DosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS32)); for(int i = 0; i < P_NtHeader->FileHeader.NumberOfSections; i++) { printf("Section Name: %s\n", P_SectionHeader->Name); printf("Virtual Address: %X\n", P_SectionHeader->VirtualAddress); printf("Size of Raw Data: %X\n", P_SectionHeader->SizeOfRawData); printf("Pointer to Raw Data: %X\n", P_SectionHeader->PointerToRawData); printf("Pointer to Relocations: %X\n", P_SectionHeader->PointerToRelocations); printf("Pointer to Linenumbers: %X\n", P_SectionHeader->PointerToLinenumbers); printf("Number of Relocations: %X\n", P_SectionHeader->NumberOfRelocations); printf("Number of Linenumbers: %X\n", P_SectionHeader->NumberOfLinenumbers); printf("Characteristics: %X\n", P_SectionHeader->Characteristics); printf("------------------------Thats's basic info of SECTION----------------------------\n"); printf("\n"); P_SectionHeader++; } printf("Please input which information you want to look :\n"); printf("1.Export Table\n"); printf("2.Import Table\n"); printf("3.TLS Table\n"); printf("4.exit\n"); int choice; while(1) { printf("Input a number: "); scanf("%d", &choice); if (choice == 1) ExportTable(buffer); else if (choice == 2) ImportTable(buffer); else if (choice == 3) TlsTable(buffer); else if (choice == 4) return 0; else printf("Please input again and make sur that's valid\n"); } system("pause"); 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)(pDos->e_lfanew + buffer); 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; } } } void ImportTable(char* buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer; PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)(pDos->e_lfanew + buffer); PIMAGE_DATA_DIRECTORY pDataDirectory = (PIMAGE_DATA_DIRECTORY)(pNt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT); PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(RvaToOffset(pDataDirectory->VirtualAddress, buffer) + buffer); while(pImportDescriptor->Name) { char * szDllname = (char*)(RvaToOffset(pImportDescriptor->Name, buffer) + buffer); printf("DLL Name: %s\n", szDllname); PIMAGE_THUNK_DATA32 PIAT = (PIMAGE_THUNK_DATA32)(RvaToOffset(pImportDescriptor->FirstThunk, buffer) + buffer); PIMAGE_THUNK_DATA32 PINT = (PIMAGE_THUNK_DATA32)(RvaToOffset(pImportDescriptor->OriginalFirstThunk, buffer) + buffer); while(PIAT->u1.Ordinal) { if(PIAT->u1.Ordinal & 0x80000000) { printf("Ordinal: %2d\n", PIAT->u1.Ordinal & 0xFFFF); printf("\n"); } else { PIMAGE_IMPORT_BY_NAME szFuncName = (PIMAGE_IMPORT_BY_NAME)(RvaToOffset(PIAT->u1.AddressOfData, buffer) + buffer); printf("Function Name: %s\n", szFuncName->Name); printf("Hint: %X\n", szFuncName->Hint); printf("\n"); } PIAT++; } pImportDescriptor++; } } void ExportTable(char* buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer; PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)(pDos->e_lfanew + buffer); 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("Without EXPORT_TABLE!\n"); return; } printf("Name:%s\n", szName); printf("Number of Functions:%08X\n", pExport->NumberOfFunctions); printf("Number of Names:%08X\n", pExport->NumberOfNames); printf("Func_addr:%08X\n", pExport->AddressOfFunctions); printf("\n"); DWORD dwNumOfFUN = pExport->NumberOfFunctions; DWORD dwNumOfNames = pExport->NumberOfNames; 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("Name:%X Address:0x%08X Name[NuLL]\n", i + dwBase, pEat32[i]); printf("\n"); } else { char* szFunName = (char*)(RvaToOffset(pEnt32[Id], buffer) + buffer); printf("Name:%X Address:0x%08X Name[%s]\n", i + dwBase, pEat32[i],szFunName); printf("\n"); } printf("-------------------------------------\n"); } printf("----------------------------------------------------------\n"); } void TlsTable(char* buffer) { PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)buffer; PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)(pDos->e_lfanew + buffer); PIMAGE_DATA_DIRECTORY pTLSDir = (pNt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_TLS); PIMAGE_TLS_DIRECTORY pTLS = (PIMAGE_TLS_DIRECTORY)(RvaToOffset(pTLSDir->VirtualAddress, buffer) + buffer); printf("StartAddressOfRawData: %08X\n", pTLS->StartAddressOfRawData); printf("EndAddressOfRawData: %08X\n", pTLS->EndAddressOfRawData); printf("TLS_Callback: %08X\n", pTLS->AddressOfCallBacks); printf("TLS_Size: %08X\n", pTLS->SizeOfZeroFill); printf("TLS_Characteristics: %08X\n", pTLS->Characteristics); }
|