 |
|
 |
Dear Michael,
First of all, Thanks a lot for sharing good solution!
I am trying to use this but proxy DLL is encountering access violation error whenever it was called.
I didn't change anything after run wrapit program. Would you please check the root cause of access violoation error? Here goes my source code.
Thanks in advance!
=======================================================================================
#include
#pragma pack(1)
HINSTANCE hLThis = 0;
HINSTANCE hL = 0;
FARPROC p[6] = {0};
BOOL WINAPI DllMain(HINSTANCE hInst,DWORD reason,LPVOID)
{
if (reason == DLL_PROCESS_ATTACH)
{
hLThis = hInst;
hL = LoadLibrary(".\\Rasco_original.dll");
if (!hL) return false;
p[0] = GetProcAddress(hL,"Hnd_Bin");
p[1] = GetProcAddress(hL,"Hnd_Init");
p[2] = GetProcAddress(hL,"Hnd_Poll");
p[3] = GetProcAddress(hL,"Hnd_Query");
p[4] = GetProcAddress(hL,"Hnd_Set");
p[5] = GetProcAddress(hL,"SndEntryPoint");
}
if (reason == DLL_PROCESS_DETACH)
{
FreeLibrary(hL);
}
return 1;
}
// Hnd_Bin
extern "C" __declspec(naked) void __stdcall __E__0__()
{
__asm
{
jmp p[0*4];
}
}
// Hnd_Init
extern "C" __declspec(naked) void __stdcall __E__1__()
{
__asm
{
jmp p[1*4];
}
}
// Hnd_Poll
extern "C" __declspec(naked) void __stdcall __E__2__()
{
__asm
{
jmp p[2*4];
}
}
// Hnd_Query
extern "C" __declspec(naked) void __stdcall __E__3__()
{
__asm
{
jmp p[3*4];
}
}
// Hnd_Set
extern "C" __declspec(naked) void __stdcall __E__4__()
{
__asm
{
jmp p[4*4];
}
}
// SndEntryPoint
extern "C" __declspec(naked) void __stdcall __E__5__()
{
__asm
{
jmp p[5*4];
}
}
|
|
|
|
 |
|
 |
I downloaded wrappit2 and it generates mydll.1.def and mydll.2.def. I tried to put them both in the /DEF: space in the VS project properties but it will only allow me to include one. If I use just one def or the other, I get symbol errors. Any ideas?
The syntax is different for wrappit2 so maybe I didn't generate it right. It does have the correct exported functions in the def files.
|
|
|
|
 |
|
 |
Use the 2nd one (the one with forwards) and declare any functions that you may override without the forward, but like those in the 1st def.
|
|
|
|
 |
|
|
 |
|
 |
Saved me a lot of time when I had to make proxy for odbc32.
When the app in question looses DB connection the proxy-dll automatically reconnects and the end user experience is improved immensely.
Thanks a lot!!
|
|
|
|
 |
|
 |
There is an error in your code. Why not just compute i*sizeof(void*) in wrappit, instead of leaving it in the source code. On my system, it ignored the p[n*4], so I modified wrappit to precalculate n*4 so it would work. Still blue screening on reboot however...weird. At least when its used as a DLL by another program it works.
|
|
|
|
 |
|
 |
The temp url doesen't work anymore. Could you reupload wrappit2 somewhere please? Thanks.
|
|
|
|
 |
|
 |
The temp link is again valid - thanks.
|
|
|
|
 |
|
 |
http://www.turboirc.com/temp/wrappit2.cpp
|
|
|
|
 |
|
 |
Dear Michael,
I have successfully hooked CreateFile() and ReadFile() with your utility and insights of this wonderful article. The problem is that I cannot replace the output parameters of ReadFile() i.e lpOverlapped, lpNumberOfBytesRead
and lpBuffer.
In this code snippet, when I try this it all works fine:
extern "C" __stdcall __E__675__(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
{
char* myBuffer = "This is not the text file you opened :D The hook worked!"
ReadFile(hFile,lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead,lpOverlapped);
return 1;
}
By the way, you can notice how I didn't use the __asm function to achieve this... Now when I add this:
lpBuffer = myBuffer;
return 1;
lpBuffer (which contains the information of the file in memory) doesn't change, the file is still loaded in the application whose IAT I altered beforehand.
Any idea? Thanks!
|
|
|
|
 |
|
 |
lpBuffer is a pointer, you probably want to use memcpy(lpBuffer,myBuffer,size); (instead of getting data from ReadFile).
|
|
|
|
 |
|
 |
Thank you very much! it worked flawlessly!
Gosh! I even tried to use __asm() to pop values off the stack, comparing them and trying to find them to replace them. Can't believe the solution was so simple
Thanks again!
|
|
|
|
 |
|
 |
On My System i got en error when i use it like
typedef int (__stdcall *pS)(SOCKET,char*,int,int);
pS pps = (pS)p[63*4];
int rv = pps(x,b,l,pr);
i have to remove the *4, but only in the none asm code!
if i don't do it i got an exception!
But my a other software is not working with this dll, it crashes!
Can it be that a programm uses fixed addresses of functions in dlls and not a function to get the adresses?
So can i also create a dll wich has the same adresses for the functions?
|
|
|
|
 |
|
 |
also, Wrappit genertaes the following code:
#pragma pack(j)
this should be:
#pragma pack(1)
|
|
|
|
 |
|
 |
"Note that the naked attribute is only valid on x86, and is not available on x64 or Itanium"
(source: Microsoft, Visual C++ Language Reference naked (C++) )
can your Wrappit be enhanced to check the HW platform?
Thank you
|
|
|
|
 |
|
 |
Hi
I have an DLL, when I dump it, all fuctions seems like what you did on your example but there are some functions like this :
5598 15DD 00106288 ?ye@RW@@QBEIXZ
5599 15DE 000DE7E0 @VLDll@16
5606 15E1 000CB368 KingReport
5601 15E2 0004BDF1 _Em@20
do you know what that KingReport is ? and how to create a proxy dll for this kind of dll ?
|
|
|
|
 |
|
 |
First, big thx for your tool.
But you wrote:
typedef int (__stdcall *pS)(SOCKET,char*,int,int);
pS pps = (pS)p[63*4];
int rv = pps(x,b,l,pr);
I need to do this:
typedef int (__stdcall *pS)(SOCKET,char*,int,int);
pS pps = (pS)p[63];
int rv = pps(x,b,l,pr);
Then everything works fine.
|
|
|
|
 |
|
 |
First: Great tool/code, really helped me out!
Line 204:
fprintf(fcpp,"\t\tp[%u] = GetProcAddress(hL,(LPCSTR)%u);\r\n",i,v[i].o);
Should be changed (remove quotes around ordinal number), otherwise GetProcAddress will return 0;
Also, in Visual Studio Express 2005 some warnings occur when the dll has one of the following functions exported:
DllCanUnloadNow,DllGetClassObject,DllInstall,DllRegisterServer,DllUnregisterServer
Those functions should always be private (why?) and are never loaded by ordinal value.
In the DEF file replace
DllCanUnloadNow=__E__4__ @163
by
DllCanUnloadNow=__E__4__ PRIVATE
|
|
|
|
 |
|
 |
When i try to make a proxy for ws2_32.dll with your tool, i get the following error:
Entry Point Not Found - The procedure entry point wsagetlasterror could not be located in the dynamic link library wsock32.dll
So i tried to proxy wsock32.dll too but then the program says it could not be initialized.
Any idea about what happening ?
|
|
|
|
 |
|
 |
Check the parameters of the linker (/def)
|
|
|
|
 |
|
 |
"/def" ??
Is that a command i did not see and missed or are you talking about the generated def file? I work with visual C++ 8.
|
|
|
|
 |
|
 |
http://msdn.microsoft.com/en-us/library/28d6s79h.aspx[^]
The /DEF option passes a module-definition file (.def) to the linker. Only one .def file can be specified to LINK. For details about .def files, see Module-Definition Files.
To set this linker option in the Visual Studio development environment
Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the Linker folder.
Click the Input property page.
Modify the Module Definition File property.
To specify a .def file from within the development environment, you should add it to the project along with other files and then specify the file to the /DEF option.
|
|
|
|
 |
|
 |
Any idea about this means ?
http://img228.imageshack.us/img228/7624/again.jpg
When the program loads the proxy DLL this error occures.
modified on Wednesday, July 29, 2009 11:15 AM
|
|
|
|
 |
|
 |
Is this the same machine where you installed Visual Studio?
Send me the Project and revised it.
|
|
|
|
 |
|
 |
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.
//email: arielmendoza@hotmail.com
#include "stdafx.h"
#include "stdafx.h"
#include "windows.h"
#include "winnt.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
int main(int argc, char* argv[])
{
char DefFile[255];
char CppFile[255];
if(argc!=3)
{
printf("\nMissing parameters ex:");
printf("\nExtractDllExports ws2_32.dll ws2_32");
return 2;
}
memset(DefFile,0,sizeof(DefFile));
memset(CppFile,0,sizeof(CppFile));
sprintf(DefFile,"%s.def",argv[2]);
sprintf(CppFile,"%s.cpp",argv[2]);
FILE *fpdef;
FILE *fpcpp;
if((fpdef=fopen(DefFile, "w+"))==NULL)
{
printf("\Error in CreateFile %s",DefFile);
return 1;
}
if((fpcpp=fopen(CppFile, "w+"))==NULL)
{
printf("\Error in CreateFile %s",CppFile);
return 1;
}
HMODULE lib = LoadLibraryExA(argv[1], NULL, DONT_RESOLVE_DLL_REFERENCES);
if(lib==NULL)
{
//Error cargando la DLL
printf("\nError in LoadLibraryExA. Dll:%s",argv[1]);
return 1;
}
assert(((PIMAGE_DOS_HEADER)lib)->e_magic == IMAGE_DOS_SIGNATURE);
PIMAGE_NT_HEADERS header =PIMAGE_NT_HEADERS((BYTE *)lib + ((PIMAGE_DOS_HEADER)lib)->e_lfanew);
assert(header->Signature == IMAGE_NT_SIGNATURE);
assert(header->OptionalHeader.NumberOfRvaAndSizes > 0);
PIMAGE_EXPORT_DIRECTORY exports = PIMAGE_EXPORT_DIRECTORY((BYTE *)lib + header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
PVOID names = (BYTE *)lib + exports->AddressOfNames;
WORD *Ordinals = (WORD*)((BYTE *)lib + exports->AddressOfNameOrdinals);
//inicializar el .cpp
fputs("\n//**** remember to add the /def parameter to linker ****",fpcpp);
fputs("\n#include <windows.h>",fpcpp);
fputs("\n\nHINSTANCE hLThis = 0;",fpcpp);
fputs("\nHINSTANCE hL = 0;",fpcpp);
char Farproc[255];
memset(Farproc,0,sizeof(Farproc));
sprintf(Farproc,"\nFARPROC p[%d] = {0};",exports->NumberOfNames);
fputs(Farproc,fpcpp);
fputs("\nBOOL WINAPI DllMain(HINSTANCE hInst,DWORD reason,LPVOID)",fpcpp);
fputs("\n{",fpcpp);
fputs("\n if (reason == DLL_PROCESS_ATTACH)",fpcpp);
fputs("\n {",fpcpp);
fputs("\n hLThis = hInst;",fpcpp);
char DllName[255];
memset(DllName,0,sizeof(DllName));
sprintf(DllName,"\n hL = LoadLibrary(\"%s\");",argv[1]);
fputs(DllName,fpcpp);
fputs("\n if (!hL) return false;",fpcpp);
//inicializar el ,def
fputs("EXPORTS",fpdef);
for (int i = 0; i < exports->NumberOfNames; i++)
{
//Escribir el .def
char txtFunction[255];
memset(txtFunction,0,sizeof(txtFunction));
WORD w=(WORD )(BYTE *)lib + ((WORD *)Ordinals)[i]+1;
sprintf(txtFunction,"\n%s=__E__%d__ @%d", (BYTE *)lib + ((DWORD *)names)[i],i,w);
fputs(txtFunction,fpdef);
//Cpp
char txtPointer[255];
memset(txtPointer,0,sizeof(txtPointer));
sprintf(txtPointer,"\n p[%d] = GetProcAddress(hL,\"%s\");",i,(BYTE *)lib + ((DWORD *)names)[i]);
fputs(txtPointer,fpcpp);
}
fputs("\n }",fpcpp);
fputs("\n if (reason == DLL_PROCESS_DETACH)",fpcpp);
fputs("\n {",fpcpp);
fputs("\n FreeLibrary(hL);",fpcpp);
fputs("\n }",fpcpp);
fputs("\n return 1;",fpcpp);
fputs("\n}",fpcpp);
//Comentario de como modificar la funcion
fputs("\n// gethostname",fpcpp);
fputs("\n// Example replace functions ",fpcpp);
fputs("\n//extern \"C\" int __stdcall __E__92__(char *name, int namelen)",fpcpp);
fputs("\n// {",fpcpp);
fputs("\n//call original gethostname",fpcpp);
fputs("\n// typedef int (__stdcall *pS)(char*,int);",fpcpp);
fputs("\n// pS pps = (pS)p[92];",fpcpp);
fputs("\n// int rv = pps(name,namelen);",fpcpp);
fputs("\n// if(rv==0)",fpcpp);
fputs("\n// {",fpcpp);
fputs("\n// memset(name,0,namelen);",fpcpp);
fputs("\n//Manipulate result",fpcpp);
fputs("\n// strcpy(name,\"TestName\");",fpcpp);
fputs("\n// }",fpcpp);
fputs("\n// return rv;",fpcpp);
fputs("\n// }",fpcpp);
fputs("\n//end example;",fpcpp);
//
for (int i = 0; i < exports->NumberOfNames; i++)
{
char Comentario[255];
char Encabezado[255];
char Salto[255];
memset(Comentario,0,sizeof(Comentario));
memset(Encabezado,0,sizeof(Encabezado));
memset(Salto,0,sizeof(Salto));
sprintf(Comentario,"\n\n//%s",(BYTE *)lib + ((DWORD *)names)[i]);
sprintf(Encabezado,"\nextern \"C\" __declspec(naked) void __stdcall __E__%d__()",i);
sprintf(Salto,"\n jmp p[%u*%u];",i,sizeof(void*));
fputs(Comentario,fpcpp);
fputs(Encabezado,fpcpp);
fputs("\n{",fpcpp);
fputs("\n __asm",fpcpp);
fputs("\n {",fpcpp);
fputs(Salto,fpcpp);
fputs("\n }",fpcpp);
fputs("\n}",fpcpp);
}
//system("pause");
fcloseall();
return 0;
}
|
|
|
|
 |
|