Last active
April 30, 2017 22:16
-
-
Save the-nose-knows/630d387f8fff0cf69e9ee77b14c4426f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Get Dependency List of DLLs | |
* Lists names of DLLs used to build the binary | |
* CAVEAT 1: Will not tell you what DLLs are loaded into the module at runtime | |
* CAVEAT 2: Will not tell you .NET Assembly Information | |
* CAVEAT 3: This app seems to crash on 64-bit applications; not sure why, exactly. Even occurs on itself when compiled as x86_amd64. | |
* | |
* I take no credit for this. It was wholly derived from a forum post of the C Boards. This is my hacked diff to only get DLL names. | |
* | |
* Forum Post Internet Archive Snapshot: http://web.archive.org/web/20090327115720/http://cboard.cprogramming.com:80/windows-programming/82906-problem-while-reading-inport-table.html | |
* Forum Post Original URI: https://cboard.cprogramming.com/windows-programming/82906-problem-while-reading-inport-table.html | |
* Forum Questioner: Brij | |
* Forum Successful Answer by: BobS0327 | |
* Forum Answer Permalink: https://cboard.cprogramming.com/windows-programming/82906-problem-while-reading-inport-table.html#post590967 | |
*/ | |
#include <Windows.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <stdexcept> | |
#include <string> | |
#include "tchar.h" | |
DWORD Rva2Offset(DWORD dwRva, PIMAGE_SECTION_HEADER dwSectionRva, USHORT uNumberOfSections) | |
{ | |
try { | |
for (USHORT i = 0; i<uNumberOfSections; i++) | |
{ | |
if (dwRva >= dwSectionRva->VirtualAddress) | |
{ | |
if (dwRva < dwSectionRva->VirtualAddress + dwSectionRva->Misc.VirtualSize) | |
{ | |
return (DWORD)(dwRva - dwSectionRva->VirtualAddress + dwSectionRva->PointerToRawData); | |
} | |
} | |
dwSectionRva++; | |
} | |
return (DWORD)-1; | |
} | |
catch(std::exception &e) {printf("An exception occurred: %s", e.what()); return -1;} | |
} | |
int GetBinaryImportTable(LPCTSTR szFileName) | |
{ | |
try { | |
USHORT uNumberOfSections; | |
HANDLE hFile, hMapping; | |
DWORD dwFileSize; | |
DWORD dwImportTableVirtualAddress; | |
DWORD dwImportTableVirtualSize; | |
LPVOID lpView; | |
PIMAGE_NT_HEADERS pimage_nt_headers; | |
PIMAGE_DATA_DIRECTORY pimage_data_directory; | |
PIMAGE_OPTIONAL_HEADER pimage_optional_header; | |
PIMAGE_IMPORT_DESCRIPTOR pimage_import_desciptor; | |
PIMAGE_SECTION_HEADER pimage_import_section_header; | |
PIMAGE_THUNK_DATA pimage_thunk_data; | |
PIMAGE_SECTION_HEADER pimage_section_header; | |
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
if (INVALID_HANDLE_VALUE == hFile) | |
{ | |
return -1; | |
} | |
dwFileSize = GetFileSize(hFile, NULL); | |
if (INVALID_FILE_SIZE == dwFileSize) | |
{ | |
CloseHandle(hFile); return -1; | |
} | |
hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); | |
if (NULL == hMapping) | |
{ | |
CloseHandle(hFile); return -1; | |
} | |
lpView = (LPTSTR)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); | |
PIMAGE_DOS_HEADER pimage_dos_header = PIMAGE_DOS_HEADER(lpView); | |
pimage_nt_headers = (PIMAGE_NT_HEADERS) | |
(pimage_dos_header + pimage_dos_header->e_lfanew); | |
if (pimage_dos_header->e_magic == IMAGE_DOS_SIGNATURE) | |
{ | |
pimage_nt_headers = (PIMAGE_NT_HEADERS)((DWORD)lpView + pimage_dos_header->e_lfanew); | |
} | |
else { return NULL; } | |
if (pimage_nt_headers->Signature == IMAGE_NT_SIGNATURE) | |
{ | |
uNumberOfSections = pimage_nt_headers->FileHeader.NumberOfSections; | |
} | |
else { return -1; } | |
pimage_optional_header = &pimage_nt_headers->OptionalHeader; | |
pimage_data_directory = pimage_optional_header->DataDirectory; | |
++pimage_data_directory; | |
dwImportTableVirtualAddress = pimage_data_directory->VirtualAddress; | |
dwImportTableVirtualSize = pimage_data_directory->Size; | |
pimage_section_header = (PIMAGE_SECTION_HEADER)((DWORD)lpView + pimage_dos_header->e_lfanew + sizeof(IMAGE_NT_HEADERS)); | |
pimage_import_section_header = pimage_section_header; | |
if (dwImportTableVirtualSize != 0) | |
{ | |
pimage_import_desciptor = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)lpView + Rva2Offset(dwImportTableVirtualAddress, pimage_import_section_header, uNumberOfSections)); | |
while (pimage_import_desciptor->Name != NULL) | |
{ | |
printf("%s", (char *)((DWORD)lpView + Rva2Offset(pimage_import_desciptor->Name, pimage_import_section_header, uNumberOfSections))); | |
printf("\n"); | |
if (pimage_import_desciptor->OriginalFirstThunk != 0) | |
{ | |
pimage_thunk_data = (PIMAGE_THUNK_DATA)((DWORD)lpView + Rva2Offset(pimage_import_desciptor->OriginalFirstThunk, pimage_import_section_header, uNumberOfSections)); | |
} | |
else | |
{ | |
pimage_thunk_data = (PIMAGE_THUNK_DATA)((DWORD)lpView + Rva2Offset(pimage_import_desciptor->FirstThunk, pimage_import_section_header, uNumberOfSections)); | |
} | |
pimage_import_desciptor++; | |
} | |
} | |
else | |
{ | |
CloseHandle(hMapping); | |
CloseHandle(hFile); | |
printf("Unable to locate the import table. \n"); | |
return -1; | |
} | |
CloseHandle(hMapping); | |
CloseHandle(hFile); | |
return 0; | |
} catch(std::exception &e) {printf("An exception occurred: %s", e.what()); return -1;} | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
try { | |
if (argc < 2) { printf("You forgot to specify a valid file path as your first and only parameter for %s", argv[0]); return -1; } | |
int reader = GetBinaryImportTable(argv[1]); | |
return reader; | |
} | |
catch (std::exception &e) | |
{ | |
printf("Exception was thrown: %s", e.what()); | |
return -1; | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment