I got to print which DLL's an EXE loads, and which APIS from each DLL.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <sys/stat.h>
FILE *f=NULL;
struct stat myStat;
int fileSize=0;
int main(int argc, char *argv[])
{
printf("PE Analysis\n");
if(argc<2) error("Uso: analysis programa.exe\n");
f=fopen(argv[1], "rb");
if(f==NULL) error("Archivo no encontrado.\n");
stat(argv[1], &myStat);
fileSize=myStat.st_size;
char *fileMap = (char *)malloc(fileSize);
fread(fileMap, fileSize, 1, f);
fclose(f);
printf("Tamaño: %i\n", fileSize);
PIMAGE_DOS_HEADER IDH = (PIMAGE_DOS_HEADER)fileMap;
PIMAGE_NT_HEADERS INT = (PIMAGE_NT_HEADERS)(fileMap + IDH->e_lfanew);
printf("Type of machine -> %.4X\nNumber of sections -> %.2X\nEP -> %.4X\n\n", INT->FileHeader.Machine, INT->FileHeader.NumberOfSections, INT->OptionalHeader.AddressOfEntryPoint);
IMAGE_DATA_DIRECTORY imports = (IMAGE_DATA_DIRECTORY)(INT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]); // IMAGE_DIRECTORY_ENTRY_IMPORT
int i=0;
PIMAGE_SECTION_HEADER SH, iSH;
for(i=0; i<INT->FileHeader.NumberOfSections; i++)
{
SH=(PIMAGE_SECTION_HEADER)(fileMap + IDH->e_lfanew + sizeof(IMAGE_NT_HEADERS) + sizeof(IMAGE_SECTION_HEADER)*i);
if(imports.VirtualAddress >= SH->VirtualAddress) iSH=SH;
printf("%s \t- %.4X\n", SH->Name, SH->VirtualAddress);
}
//printf("%.4X - %.4X\n", iSH->VirtualAddress, iSH->PointerToRawData);
printf("\nRVA IAT: %.4X\nFisica: %.4X\nSize: %.4X\n\n", imports.VirtualAddress, imports.VirtualAddress - iSH->VirtualAddress + iSH->PointerToRawData, imports.Size);
PIMAGE_IMPORT_DESCRIPTOR IID = (PIMAGE_IMPORT_DESCRIPTOR)(fileMap + (imports.VirtualAddress - iSH->VirtualAddress + iSH->PointerToRawData));
PIMAGE_THUNK_DATA TD;
PIMAGE_IMPORT_BY_NAME IBN;
for(; IID->Name; IID++)
{
printf("%s\n", fileMap + IID->Name - iSH->VirtualAddress + iSH->PointerToRawData);
TD = (PIMAGE_THUNK_DATA)((DWORD)fileMap + IID->FirstThunk - iSH->VirtualAddress + iSH->PointerToRawData);
for(; TD->u1.Ordinal; TD++)
{
//printf("%.4X\n", fileMap + TD->u1.Function - iSH->VirtualAddress + iSH->PointerToRawData);
IBN = (PIMAGE_IMPORT_BY_NAME)(fileMap + TD->u1.Function - iSH->VirtualAddress + iSH->PointerToRawData);
//printf("%.4X\n", IBN->Hint);
printf("\t%s\n", IBN->Name);
}
}
return 0;
}
void error(char *error)
{
printf("%s", error);
exit(0);
}