Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- PE32 / PE32+
- PE32 / PE64
- Dumper Import table
- */
- #include <windows.h>
- #include <winnt.h>
- #include <stdio.h>
- int PE32Plus(char *filename)
- {
- HANDLE hFile,hFileMap;
- DWORD dwImportDirectoryVA,dwSection=0,dwRawOffset;
- ULONGLONG dwSectionCount;
- LPVOID lpFile;
- PIMAGE_DOS_HEADER pDosHeader;
- PIMAGE_NT_HEADERS64 pNtHeaders;
- PIMAGE_SECTION_HEADER pSectionHeader;
- PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor;
- PIMAGE_THUNK_DATA pThunkData;
- hFile = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
- if(hFile==INVALID_HANDLE_VALUE)
- ExitProcess(1);
- hFileMap = CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0);
- lpFile = MapViewOfFile(hFileMap,FILE_MAP_READ,0,0,0);
- pDosHeader = (PIMAGE_DOS_HEADER)lpFile;
- printf("e_magic %x \n", pDosHeader->e_magic);
- printf("%x", pDosHeader->e_cblp);
- printf("%x", pDosHeader->e_cp);
- printf("%x", pDosHeader->e_crlc);
- printf("%x", pDosHeader->e_cparhdr);
- printf("%x", pDosHeader->e_maxalloc);
- printf("%x", pDosHeader->e_ss);
- printf("%x", pDosHeader->e_sp);
- printf("%x", pDosHeader->e_ip);
- printf("%x", pDosHeader->e_cs);
- printf("%x", pDosHeader->e_lfarlc);
- printf("%x", pDosHeader->e_ovno);
- printf("%x", pDosHeader->e_oemid);
- printf("%x", pDosHeader->e_oeminfo);
- printf("e_lfanew %x", pDosHeader->e_lfanew);
- pNtHeaders = (PIMAGE_NT_HEADERS64)((DWORD)lpFile+pDosHeader->e_lfanew);
- printf("Signature %x \n", pNtHeaders->Signature);
- dwSectionCount = pNtHeaders->FileHeader.NumberOfSections;
- printf("pNtHeaders->FileHeader.NumberOfSections %x\n", dwSectionCount);
- dwImportDirectoryVA = pNtHeaders->OptionalHeader.DataDirectory[1].VirtualAddress;
- printf("dwImportDirectoryVA %x\n", dwImportDirectoryVA);
- pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pNtHeaders+sizeof(IMAGE_NT_HEADERS64));
- printf("pSectionHeader %x\n", pSectionHeader);
- for(;dwSection < dwSectionCount && pSectionHeader->VirtualAddress <= dwImportDirectoryVA;pSectionHeader++,dwSection++);
- pSectionHeader--;
- dwRawOffset = (DWORD)lpFile+pSectionHeader->PointerToRawData;
- printf("dwRawOffset %x\n", dwRawOffset);
- pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(dwRawOffset+(dwImportDirectoryVA-pSectionHeader->VirtualAddress));
- printf("pImportDescriptor %x\n", pImportDescriptor);
- for(;pImportDescriptor->Name!=0;pImportDescriptor++)
- {
- printf("\nDLL Name : %s \n\n",dwRawOffset+(pImportDescriptor->Name-pSectionHeader->VirtualAddress));
- //pThunkData = (PIMAGE_THUNK_DATA)(dwRawOffset+(pImportDescriptor->FirstThunk-pSectionHeader->VirtualAddress));
- }
- UnmapViewOfFile(lpFile);
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- return 0;
- }
- int PE32(char *filename)
- {
- HANDLE hFile,hFileMap;
- DWORD dwImportDirectoryVA,dwSectionCount,dwSection=0,dwRawOffset;
- LPVOID lpFile;
- PIMAGE_DOS_HEADER pDosHeader;
- PIMAGE_NT_HEADERS pNtHeaders;
- PIMAGE_SECTION_HEADER pSectionHeader;
- PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor;
- PIMAGE_THUNK_DATA pThunkData;
- hFile = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
- if(hFile==INVALID_HANDLE_VALUE)
- ExitProcess(1);
- hFileMap = CreateFileMapping(hFile,0,PAGE_READONLY,0,0,0);
- lpFile = MapViewOfFile(hFileMap,FILE_MAP_READ,0,0,0);
- pDosHeader = (PIMAGE_DOS_HEADER)lpFile;
- pNtHeaders = (PIMAGE_NT_HEADERS)((DWORD)lpFile+pDosHeader->e_lfanew);
- dwSectionCount = pNtHeaders->FileHeader.NumberOfSections;
- dwImportDirectoryVA = pNtHeaders->OptionalHeader.DataDirectory[1].VirtualAddress;
- pSectionHeader = (PIMAGE_SECTION_HEADER)((DWORD)pNtHeaders+sizeof(IMAGE_NT_HEADERS));
- for(;dwSection < dwSectionCount && pSectionHeader->VirtualAddress <= dwImportDirectoryVA;pSectionHeader++,dwSection++);
- pSectionHeader--;
- dwRawOffset = (DWORD)lpFile+pSectionHeader->PointerToRawData;
- pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(dwRawOffset+(dwImportDirectoryVA-pSectionHeader->VirtualAddress));
- for(;pImportDescriptor->Name!=0;pImportDescriptor++)
- {
- printf("\nDLL Name : %s\n\n",dwRawOffset+(pImportDescriptor->Name-pSectionHeader->VirtualAddress));
- // pThunkData = (PIMAGE_THUNK_DATA)(dwRawOffset+(pImportDescriptor->FirstThunk-pSectionHeader->VirtualAddress));
- // for(;pThunkData->u1.AddressOfData != 0;pThunkData++)
- // printf("\tFunction : %s\n",(dwRawOffset+(pThunkData->u1.AddressOfData-pSectionHeader->VirtualAddress+2)));
- }
- UnmapViewOfFile(lpFile);
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- DWORD bintype;
- if(argc<2)
- {
- printf("\nUsage : ImportDirParser.exe TargetExe\n");
- ExitProcess(0);
- }
- if(GetBinaryType(argv[1], &bintype) && bintype == SCS_64BIT_BINARY)
- {
- printf("64Bit file detected... \n");
- PE32Plus(argv[1]);
- }
- else
- {
- printf("32Bit file detected... \n");
- PE32(argv[1]);
- }
- }
Add Comment
Please, Sign In to add comment