Hi, I created this application based on the above to remove the dependencies to other tools (dumpbin). It is a test and can be improved.
Regards.
<br />
#include "stdafx.h"<br />
#include "stdafx.h"<br />
#include "windows.h"<br />
#include "winnt.h"<br />
#include <assert.h><br />
#include <stdio.h><br />
#include <stdlib.h><br />
#include "string.h"<br />
<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
char DefFile[255];<br />
char CppFile[255];<br />
<br />
if(argc!=3)<br />
{<br />
printf("\nMissing parameters ex:");<br />
printf("\nExtractDllExports ws2_32.dll ws2_32");<br />
return 2;<br />
}<br />
memset(DefFile,0,sizeof(DefFile));<br />
memset(CppFile,0,sizeof(CppFile));<br />
sprintf(DefFile,"%s.def",argv[2]);<br />
sprintf(CppFile,"%s.cpp",argv[2]);<br />
FILE *fpdef; <br />
FILE *fpcpp;<br />
if((fpdef=fopen(DefFile, "w+"))==NULL) <br />
{<br />
printf("\Error in CreateFile %s",DefFile);<br />
return 1;<br />
}<br />
if((fpcpp=fopen(CppFile, "w+"))==NULL) <br />
{<br />
printf("\Error in CreateFile %s",CppFile);<br />
return 1;<br />
}<br />
<br />
<br />
HMODULE lib = LoadLibraryExA(argv[1], NULL, DONT_RESOLVE_DLL_REFERENCES);<br />
if(lib==NULL)<br />
{<br />
printf("\nError in LoadLibraryExA. Dll:%s",argv[1]);<br />
return 1;<br />
}<br />
assert(((PIMAGE_DOS_HEADER)lib)->e_magic == IMAGE_DOS_SIGNATURE);<br />
PIMAGE_NT_HEADERS header =PIMAGE_NT_HEADERS((BYTE *)lib + ((PIMAGE_DOS_HEADER)lib)->e_lfanew);<br />
assert(header->Signature == IMAGE_NT_SIGNATURE);<br />
assert(header->OptionalHeader.NumberOfRvaAndSizes > 0);<br />
PIMAGE_EXPORT_DIRECTORY exports = PIMAGE_EXPORT_DIRECTORY((BYTE *)lib + header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);<br />
PVOID names = (BYTE *)lib + exports->AddressOfNames;<br />
WORD *Ordinals = (WORD*)((BYTE *)lib + exports->AddressOfNameOrdinals);<br />
fputs("\n//**** remember to add the /def parameter to linker ****",fpcpp);<br />
fputs("\n#include <windows.h>",fpcpp);<br />
fputs("\n\nHINSTANCE hLThis = 0;",fpcpp);<br />
fputs("\nHINSTANCE hL = 0;",fpcpp);<br />
char Farproc[255];<br />
memset(Farproc,0,sizeof(Farproc));<br />
sprintf(Farproc,"\nFARPROC p[%d] = {0};",exports->NumberOfNames);<br />
fputs(Farproc,fpcpp);<br />
fputs("\nBOOL WINAPI DllMain(HINSTANCE hInst,DWORD reason,LPVOID)",fpcpp);<br />
fputs("\n{",fpcpp);<br />
fputs("\n if (reason == DLL_PROCESS_ATTACH)",fpcpp);<br />
fputs("\n {",fpcpp);<br />
fputs("\n hLThis = hInst;",fpcpp);<br />
char DllName[255];<br />
memset(DllName,0,sizeof(DllName));<br />
sprintf(DllName,"\n hL = LoadLibrary(\"%s\");",argv[1]);<br />
fputs(DllName,fpcpp);<br />
fputs("\n if (!hL) return false;",fpcpp);<br />
<br />
fputs("EXPORTS",fpdef);<br />
for (int i = 0; i < exports->NumberOfNames; i++)<br />
{<br />
char txtFunction[255];<br />
memset(txtFunction,0,sizeof(txtFunction));<br />
WORD w=(WORD )(BYTE *)lib + ((WORD *)Ordinals)[i]+1; <br />
sprintf(txtFunction,"\n%s=__E__%d__ @%d", (BYTE *)lib + ((DWORD *)names)[i],i,w);<br />
fputs(txtFunction,fpdef);<br />
char txtPointer[255];<br />
memset(txtPointer,0,sizeof(txtPointer));<br />
sprintf(txtPointer,"\n p[%d] = GetProcAddress(hL,\"%s\");",i,(BYTE *)lib + ((DWORD *)names)[i]);<br />
fputs(txtPointer,fpcpp);<br />
<br />
}<br />
fputs("\n }",fpcpp);<br />
fputs("\n if (reason == DLL_PROCESS_DETACH)",fpcpp);<br />
fputs("\n {",fpcpp);<br />
fputs("\n FreeLibrary(hL);",fpcpp);<br />
fputs("\n }",fpcpp);<br />
fputs("\n return 1;",fpcpp);<br />
fputs("\n}",fpcpp);<br />
fputs("\n// gethostname",fpcpp);<br />
fputs("\n// Example replace functions ",fpcpp);<br />
fputs("\n//extern \"C\" int __stdcall __E__92__(char *name, int namelen)",fpcpp);<br />
fputs("\n// {",fpcpp);<br />
fputs("\n//call original gethostname",fpcpp);<br />
fputs("\n// typedef int (__stdcall *pS)(char*,int);",fpcpp);<br />
fputs("\n// pS pps = (pS)p[92];",fpcpp);<br />
fputs("\n// int rv = pps(name,namelen);",fpcpp);<br />
fputs("\n// if(rv==0)",fpcpp);<br />
fputs("\n// {",fpcpp);<br />
fputs("\n// memset(name,0,namelen);",fpcpp);<br />
fputs("\n//Manipulate result",fpcpp);<br />
fputs("\n// strcpy(name,\"TestName\");",fpcpp);<br />
fputs("\n// }",fpcpp);<br />
fputs("\n// return rv;",fpcpp);<br />
fputs("\n// }",fpcpp);<br />
fputs("\n//end example;",fpcpp);<br />
for (int i = 0; i < exports->NumberOfNames; i++)<br />
{<br />
char Comentario[255];<br />
char Encabezado[255];<br />
char Salto[255];<br />
memset(Comentario,0,sizeof(Comentario));<br />
memset(Encabezado,0,sizeof(Encabezado));<br />
memset(Salto,0,sizeof(Salto));<br />
sprintf(Comentario,"\n\n//%s",(BYTE *)lib + ((DWORD *)names)[i]);<br />
sprintf(Encabezado,"\nextern \"C\" __declspec(naked) void __stdcall __E__%d__()",i);<br />
sprintf(Salto,"\n jmp p[%u*%u];",i,sizeof(void*));<br />
fputs(Comentario,fpcpp);<br />
fputs(Encabezado,fpcpp);<br />
fputs("\n{",fpcpp);<br />
fputs("\n __asm",fpcpp);<br />
fputs("\n {",fpcpp);<br />
fputs(Salto,fpcpp);<br />
fputs("\n }",fpcpp);<br />
fputs("\n}",fpcpp);<br />
<br />
}<br />
fcloseall();<br />
return 0;<br />
}<br />
<br />
<br />
|