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
| #include <iostream> #include <windows.h>
LPVOID ReadPE( IN LPCSTR lpszName ) { FILE* file = nullptr; fopen_s(&file, lpszName, "rb"); if (!file) { printf("打开文件失败!\n"); return nullptr; }
fseek(file, 0, SEEK_END); size_t size = ftell(file); fseek(file, 0, SEEK_SET);
LPVOID fileBuff = malloc(size); if (!fileBuff) { printf("申请内存空间失败!\n"); fclose(file); return nullptr; } fread_s(fileBuff, size, 1, size, file);
WORD mz = *((PWORD)fileBuff); if (mz != 0x5a4d) { printf("该文件不是pe可执行程序!\n"); fclose(file); free(fileBuff); return nullptr; }
return fileBuff; }
void AnlyzePE( IN LPVOID pe, OUT PIMAGE_DOS_HEADER& dos, OUT PIMAGE_FILE_HEADER& file, OUT PIMAGE_OPTIONAL_HEADER32& optional, OUT PIMAGE_SECTION_HEADER*& section ) { dos = (PIMAGE_DOS_HEADER)pe; file = (PIMAGE_FILE_HEADER)((PCHAR)pe + dos->e_lfanew + 4); optional = (PIMAGE_OPTIONAL_HEADER32)((PCHAR)pe + dos->e_lfanew + 4 + 20); section = (PIMAGE_SECTION_HEADER*)malloc(file->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)); if (section != nullptr) { for (int i = 0; i < file->NumberOfSections; i++) { *(section + i) = (PIMAGE_SECTION_HEADER)((PCHAR)pe + dos->e_lfanew + 4 + 20 + file->SizeOfOptionalHeader + (i * sizeof(IMAGE_SECTION_HEADER))); } } }
DWORD RvaToFoa( IN LPVOID pe, IN UINT_PTR rva ) { PIMAGE_DOS_HEADER dos = nullptr; PIMAGE_FILE_HEADER file = nullptr; PIMAGE_OPTIONAL_HEADER32 optional = nullptr; PIMAGE_SECTION_HEADER* section = nullptr; AnlyzePE(pe, dos, file, optional, section); DWORD foa = -1;
for (int i = 0; i < file->NumberOfSections; i++) { UINT_PTR begin = (*section + i)->VirtualAddress; UINT_PTR end = (*section + i)->VirtualAddress + (*section + i)->SizeOfRawData; if (begin <= rva && rva <= end) { foa = rva - begin + (*section + i)->PointerToRawData; break; } } free(section); return foa; }
void PrintExport( IN LPVOID fileBuff ) { PIMAGE_DOS_HEADER dos = nullptr; PIMAGE_FILE_HEADER file = nullptr; PIMAGE_OPTIONAL_HEADER32 optional = nullptr; PIMAGE_SECTION_HEADER* section = nullptr; AnlyzePE(fileBuff, dos, file, optional, section);
DWORD offset = RvaToFoa(fileBuff, optional->DataDirectory[0].VirtualAddress); PIMAGE_EXPORT_DIRECTORY exportTable = (PIMAGE_EXPORT_DIRECTORY)((PCHAR)fileBuff + offset); printf(">>>> 导出表 <<<<\n"); printf("Characteristics =%x\n", exportTable->Characteristics); printf("TimeDateStamp =%x\n", exportTable->TimeDateStamp); printf("MajorVersion =%x\n", exportTable->MajorVersion); printf("MinorVersion =%x\n", exportTable->MinorVersion); printf("Name =%x\n", exportTable->Name); printf("Base =%x\n", exportTable->Base); printf("NumberOfFunctions =%x\n", exportTable->NumberOfFunctions); printf("NumberOfNames =%x\n", exportTable->NumberOfNames); printf("AddressOfFunctions =%x\n", exportTable->AddressOfFunctions); printf("AddressOfNames =%x\n", exportTable->AddressOfNames); printf("AddressOfNameOrdinals =%x\n", exportTable->AddressOfNameOrdinals);
DWORD(*function)[1]; function = (DWORD(*)[1])((PCHAR)fileBuff + RvaToFoa(fileBuff, exportTable->AddressOfFunctions)); printf(">>>> Functions <<<<\n"); for (int i = 0; i < exportTable->NumberOfFunctions; i++) { printf("%d = %x\n", i, *(*(function)+i)); }
WORD(*ordinal)[1]; ordinal = (WORD(*)[1])((PCHAR)fileBuff + RvaToFoa(fileBuff, exportTable->AddressOfNameOrdinals)); printf(">>>> Ordinals <<<<\n"); for (int i = 0; i < exportTable->NumberOfFunctions; i++) { printf("%d = %x\n", i, *(*(ordinal)+i)); }
DWORD(*name)[1]; name = (DWORD(*)[1])((PCHAR)fileBuff + RvaToFoa(fileBuff, exportTable->AddressOfNames)); printf(">>>> Names <<<<\n"); for (int i = 0; i < exportTable->NumberOfFunctions; i++) { printf("%d = %s\n", i, (PCHAR)fileBuff + RvaToFoa(fileBuff, *(*(name)+i))); } free(section); return; }
bool M_strcmp( IN char* s1, IN char* s2 ) { int length = strlen(s1); if (length != strlen(s2)) { return false; } else { for (int i = 0; i < length; i++) { if (s1[i] != s2[i]) { return false; } } } return true; }
LPVOID GetFunctionAddrByName( IN LPVOID pe, IN LPCSTR funcName ) { PIMAGE_DOS_HEADER dos = nullptr; PIMAGE_FILE_HEADER file = nullptr; PIMAGE_OPTIONAL_HEADER32 optional = nullptr; PIMAGE_SECTION_HEADER* section = nullptr; AnlyzePE(pe, dos, file, optional, section);
DWORD offset = RvaToFoa(pe, optional->DataDirectory[0].VirtualAddress); PIMAGE_EXPORT_DIRECTORY exportTable = (PIMAGE_EXPORT_DIRECTORY)((PCHAR)pe + offset);
DWORD(*function)[1]; function = (DWORD(*)[1])((PCHAR)pe + RvaToFoa(pe, exportTable->AddressOfFunctions)); WORD(*ordinal)[1]; ordinal = (WORD(*)[1])((PCHAR)pe + RvaToFoa(pe, exportTable->AddressOfNameOrdinals)); DWORD(*name)[1]; name = (DWORD(*)[1])((PCHAR)pe + RvaToFoa(pe, exportTable->AddressOfNames)); for (int i = 0; i < exportTable->NumberOfFunctions; i++) { LPCSTR tempName = (PCHAR)pe + RvaToFoa(pe, *(*(name)+i)); if (M_strcmp((char*)tempName, (char*)funcName)) { DWORD funcIndex = *(*(ordinal)+i); free(section); return (LPVOID) * (*(function)+funcIndex); } } free(section); return nullptr; }
LPVOID GetFunctionAddrByOrdinal( IN LPVOID pe, IN DWORD exportNumber ) { PIMAGE_DOS_HEADER dos = nullptr; PIMAGE_FILE_HEADER file = nullptr; PIMAGE_OPTIONAL_HEADER32 optional = nullptr; PIMAGE_SECTION_HEADER* section = nullptr; AnlyzePE(pe, dos, file, optional, section);
DWORD offset = RvaToFoa(pe, optional->DataDirectory[0].VirtualAddress); PIMAGE_EXPORT_DIRECTORY exportTable = (PIMAGE_EXPORT_DIRECTORY)((PCHAR)pe + offset);
DWORD(*function)[1]; function = (DWORD(*)[1])((PCHAR)pe + RvaToFoa(pe, exportTable->AddressOfFunctions)); free(section); return (LPVOID) * (*(function)+(exportNumber - exportTable->Base)); }
int main() { LPVOID fileBuff = ReadPE(R"(D:\source\repos\dllmain\Debug\MyDll.dll)"); if (fileBuff) { PrintExport(fileBuff); LPVOID addAddress = GetFunctionAddrByName(fileBuff, "add"); LPVOID maxAddress = GetFunctionAddrByOrdinal(fileBuff, 5); printf("null\n"); }
free(fileBuff); system("pause"); return 0; }
|