Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok. Its working now, even with DataExecutionPrevention enabled. In case someone has a similar problem in future, here is the working code:</p> <p>dllmain.cpp:</p> <pre><code>#include "dll.h" #include &lt;windows.h&gt; #include &lt;winsock2.h&gt; #include &lt;iostream&gt; #include &lt;fstream&gt; #pragma comment(lib, "ws2_32.lib") using namespace std; DllClass::DllClass() { } DllClass::~DllClass () { } BYTE hook[6]; BYTE hook2[6]; BYTE jmp[6] = { 0xe9,0x00, 0x00, 0x00, 0x00 ,0xc3 }; ofstream myfile; ofstream myfile2; DWORD pPrevious; DWORD HookFunction(LPCSTR lpModule, LPCSTR lpFuncName, LPVOID lpFunction, unsigned char *lpBackup) { DWORD dwAddr = (DWORD)GetProcAddress(GetModuleHandle(lpModule), lpFuncName); ReadProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, lpBackup, 6, 0); DWORD dwCalc = ((DWORD)lpFunction - dwAddr - 5); VirtualProtect((void*) dwAddr, 6, PAGE_EXECUTE_READWRITE, &amp;pPrevious); memcpy(&amp;jmp[1], &amp;dwCalc, 4); WriteProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, jmp, 6, 0); VirtualProtect((void*) dwAddr, 6, pPrevious, &amp;pPrevious); FlushInstructionCache(GetCurrentProcess(),0,0); return dwAddr; } BOOL UnHookFunction(LPCSTR lpModule, LPCSTR lpFuncName, unsigned char *lpBackup) { DWORD dwAddr = (DWORD)GetProcAddress(GetModuleHandle(lpModule), lpFuncName); if (WriteProcessMemory(GetCurrentProcess(), (LPVOID)dwAddr, lpBackup, 6, 0)) return TRUE; FlushInstructionCache(GetCurrentProcess(),0,0); return FALSE; } int __stdcall nSend(SOCKET s, const char *buf, int len,int flags){ UnHookFunction("ws2_32.dll", "send", hook); int result = send(s,buf,len,flags); myfile.open ("C:\\tmp\\log.txt",ios::app | ios::binary); myfile &lt;&lt; buf; myfile.close(); HookFunction("ws2_32.dll", "send", (LPVOID*) nSend, hook); return result; } int __stdcall nRecv(SOCKET s, char* buf, int len, int flags) { UnHookFunction("ws2_32.dll", "recv", hook2); DWORD tmp; len = recv(s, buf, len, flags); if (len &gt; 0) { myfile2.open ("C:\\tmp\\log.txt",ios::app | ios::binary); myfile2 &lt;&lt; buf; myfile2.close(); } HookFunction("ws2_32.dll", "recv", (LPVOID*) nRecv, hook2); return len; } void fun(){ HookFunction("ws2_32.dll", "send", (LPVOID*) nSend, hook); HookFunction("ws2_32.dll", "recv", (LPVOID*) nRecv, hook2); } BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: HookFunction("ws2_32.dll", "send", (LPVOID*) nSend, hook); HookFunction("ws2_32.dll", "recv", (LPVOID*) nRecv, hook2); case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } </code></pre> <p>dll.h</p> <pre><code>#ifndef _DLL_H_ #define _DLL_H_ #if BUILDING_DLL # define DLLIMPORT __declspec (dllexport) #else /* Not BUILDING_DLL */ # define DLLIMPORT __declspec (dllimport) #endif /* Not BUILDING_DLL */ class DLLIMPORT DllClass { public: DllClass(); virtual ~DllClass(void); private: }; extern "C" __declspec(dllexport) void fun(); #endif /* _DLL_H_ */ </code></pre> <p>Tested and working with nearly all programs on Win XP 32bit and some programs on Win 7 x64</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload