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
| #include <windows.h> #include <stdio.h>
DWORD Size(FILE* fp){ DWORD size = 0; fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); return size; }
DWORD RVA_TO_FOA(DWORD RVA,char* Buffer){ PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FileHeader = NULL; PIMAGE_OPTIONAL_HEADER OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL;
DosHeader = (PIMAGE_DOS_HEADER)Buffer; FileHeader = (PIMAGE_FILE_HEADER)(Buffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FileHeader->SizeOfOptionalHeader); if (RVA < OptionalHeader->SizeOfHeaders) { return RVA; }
for(int i=0;i<FileHeader->NumberOfSections-1;i++){ if(RVA >= SectionHeader->VirtualAddress && RVA < (SectionHeader+1)->VirtualAddress){ DWORD offset = RVA - SectionHeader->VirtualAddress; return SectionHeader->PointerToRawData + offset; } else{ SectionHeader +=1; } }
SectionHeader +=1; DWORD offset = RVA - SectionHeader->VirtualAddress; return SectionHeader->PointerToRawData + offset; }
void GetFuncAddrByName(char* Buffer,char* name){ PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FILEHeader = NULL; PIMAGE_OPTIONAL_HEADER64 OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL;
DosHeader = (PIMAGE_DOS_HEADER)Buffer; NTHeader = (PIMAGE_NT_HEADERS)(Buffer + DosHeader->e_lfanew); FILEHeader = (PIMAGE_FILE_HEADER)(Buffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER64)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FILEHeader->SizeOfOptionalHeader);
DWORD Export_Table_RVA = OptionalHeader->DataDirectory[0].VirtualAddress;
if(Export_Table_RVA == 0){ printf("This PE File Has Not Export Table!\n"); return ; } DWORD Export_Table_FOA = RVA_TO_FOA(Export_Table_RVA,Buffer); PIMAGE_EXPORT_DIRECTORY _Export_Table = (PIMAGE_EXPORT_DIRECTORY)(Buffer + Export_Table_FOA); DWORD* AddressOfFuncitons = (DWORD*)(Buffer + RVA_TO_FOA(_Export_Table->AddressOfFunctions,Buffer)); WORD* AddressOfOrdinals = (WORD*)(Buffer + RVA_TO_FOA(_Export_Table->AddressOfNameOrdinals,Buffer)); DWORD* AddressOfNames = (DWORD*)(Buffer + RVA_TO_FOA(_Export_Table->AddressOfNames,Buffer));
for(int i = 0; i < _Export_Table->NumberOfNames;i++){ char* nameaddr = Buffer + RVA_TO_FOA(*(AddressOfNames + i),Buffer); if(!strcmp(name,nameaddr)){ int ordinal = *(AddressOfOrdinals + i); printf("%s found: %x\n",name,*(AddressOfFuncitons + ordinal)); return; } }
printf("Func Not Found\n"); }
void GetFuncAddrByOrd(char* Buffer,int Ord){ PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FILEHeader = NULL; PIMAGE_OPTIONAL_HEADER64 OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL;
DosHeader = (PIMAGE_DOS_HEADER)Buffer; NTHeader = (PIMAGE_NT_HEADERS)(Buffer + DosHeader->e_lfanew); FILEHeader = (PIMAGE_FILE_HEADER)(Buffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER64)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FILEHeader->SizeOfOptionalHeader);
DWORD Export_Table_RVA = OptionalHeader->DataDirectory[0].VirtualAddress;
if(Export_Table_RVA == 0){ printf("This PE File Has Not Export Table!\n"); return ; } DWORD Export_Table_FOA = RVA_TO_FOA(Export_Table_RVA,Buffer); PIMAGE_EXPORT_DIRECTORY _Export_Table = (PIMAGE_EXPORT_DIRECTORY)(Buffer + Export_Table_FOA); DWORD* AddressOfFuncitons = (DWORD*)(Buffer + RVA_TO_FOA(_Export_Table->AddressOfFunctions,Buffer)); int base = _Export_Table->Base; printf("base: %d\n",base); printf("%d found: %x\n",Ord,*(AddressOfFuncitons + Ord-base)); }
void Export_Table(char* Buffer){ PIMAGE_DOS_HEADER DosHeader = NULL; PIMAGE_NT_HEADERS NTHeader = NULL; PIMAGE_FILE_HEADER FILEHeader = NULL; PIMAGE_OPTIONAL_HEADER64 OptionalHeader = NULL; PIMAGE_SECTION_HEADER SectionHeader = NULL;
DosHeader = (PIMAGE_DOS_HEADER)Buffer; NTHeader = (PIMAGE_NT_HEADERS)(Buffer + DosHeader->e_lfanew); FILEHeader = (PIMAGE_FILE_HEADER)(Buffer + DosHeader->e_lfanew + 4); OptionalHeader = (PIMAGE_OPTIONAL_HEADER64)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER); SectionHeader = (PIMAGE_SECTION_HEADER)(Buffer + DosHeader->e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER + FILEHeader->SizeOfOptionalHeader);
DWORD Export_Table_RVA = OptionalHeader->DataDirectory[0].VirtualAddress;
if(Export_Table_RVA == 0){ printf("This PE File Has Not Export Table!\n"); return; } DWORD Export_Table_FOA = RVA_TO_FOA(Export_Table_RVA,Buffer); PIMAGE_EXPORT_DIRECTORY _Export_Table = (PIMAGE_EXPORT_DIRECTORY)(Buffer + Export_Table_FOA);
printf("Characteristics: %#x\n", _Export_Table->Characteristics); printf("TimeDateStamp: %#x\n", _Export_Table->TimeDateStamp); printf("MajorVersion: %#x\n", _Export_Table->MajorVersion); printf("MinorVersion: %#x\n", _Export_Table->MinorVersion); printf("Name: %s\n", (Buffer + RVA_TO_FOA(_Export_Table->Name, Buffer))); printf("Base: %#x\n", _Export_Table->Base); printf("NumberOfFunctions: %#x\n", _Export_Table->NumberOfFunctions); printf("NumberOfNames: %#x\n", _Export_Table->NumberOfNames); printf("AddressOfFunctions: %#x\n", _Export_Table->AddressOfFunctions); printf("AddressOfNames: %#x\n", _Export_Table->AddressOfNames); printf("AddressOfNameOrdinals: %#x\n", _Export_Table->AddressOfNameOrdinals);
printf("==========函数地址表 AddressOfFuncitons==========\n"); DWORD* AddressOfFuncitons = (DWORD*)(Buffer + RVA_TO_FOA(_Export_Table->AddressOfFunctions,Buffer)); for(int i = 0; i < _Export_Table->NumberOfFunctions;i++){ printf("下标:%d 函数地址:%x\n", i , *(AddressOfFuncitons + i)); }
printf("==========序号表 AddressOfOrdinals==========\n"); WORD* AddressOfOrdinals = (WORD*)(Buffer + RVA_TO_FOA(_Export_Table->AddressOfNameOrdinals,Buffer)); for (int i = 0; i < _Export_Table->NumberOfNames; i++) { printf("下标:%d 序号:%x\n", i , *(AddressOfOrdinals + i)); }
printf("==========函数名称表 AddressOfNames==========\n"); DWORD* AddressOfNames = (DWORD*)(Buffer + RVA_TO_FOA(_Export_Table->AddressOfNames,Buffer)); for(int i = 0; i < _Export_Table->NumberOfNames;i++){ char* nameaddres = Buffer + RVA_TO_FOA(*(AddressOfNames + i),Buffer); printf("下标:%d 函数名称:%s\n", i ,nameaddres); } }
int main(){ char* buffer;
FILE* fp1 = NULL; errno_t err_1 = fopen_s(&fp1, "C:\\Program Files\\Microsoft SQL Server Compact Edition\\v4.0\\sqlceme40.dll", "rb");
DWORD size = Size(fp1); buffer = (char*)malloc(size); fread(buffer,size,1,fp1);
char* func_name = "DllRelease"; int func_ord = 2; Export_Table(buffer); GetFuncAddrByName(buffer,func_name); GetFuncAddrByOrd(buffer,func_ord); }
|