Click here to Skip to main content
15,887,083 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: recv no blocking Pin
Albert Holguin30-Jun-15 13:26
professionalAlbert Holguin30-Jun-15 13:26 
GeneralRe: recv no blocking Pin
Daniel Pfeffer30-Jun-15 17:53
professionalDaniel Pfeffer30-Jun-15 17:53 
GeneralRe: recv no blocking Pin
Albert Holguin1-Jul-15 4:02
professionalAlbert Holguin1-Jul-15 4:02 
GeneralRe: recv no blocking Pin
Albert Holguin30-Jun-15 13:27
professionalAlbert Holguin30-Jun-15 13:27 
QuestionCan a modeless Dialog Box be a main window "m_pMainWnd" Pin
ForNow29-Jun-15 5:07
ForNow29-Jun-15 5:07 
AnswerRe: Can a modeless Dialog Box be a main window "m_pMainWnd" Pin
Richard MacCutchan29-Jun-15 5:12
mveRichard MacCutchan29-Jun-15 5:12 
GeneralRe: Can a modeless Dialog Box be a main window "m_pMainWnd" Pin
ForNow29-Jun-15 5:22
ForNow29-Jun-15 5:22 
QuestionCan anybody help me with converting this example for x64? Pin
Member 802436529-Jun-15 2:42
Member 802436529-Jun-15 2:42 
Hello.

I am trying to hook winapi for 64 bit apps. I found this example: API Hooking with MS Detours[^]

And I tried to modify BeginRedirect to work with 64 bit programs but every time I inject, my target crashes.

Here's my new code.
C++
#undef UNICODE
#include <windows.h>
#include <cstdio>

#define SIZE 10 //Number of bytes needed to redirect

typedef int (WINAPI *pMessageBoxW)(HWND, LPCWSTR, LPCWSTR, UINT);
int WINAPI MyMessageBoxW(HWND, LPCWSTR, LPCWSTR, UINT);

void BeginRedirect(LPVOID);

pMessageBoxW pOrigMBAddress = NULL;
BYTE oldBytes[SIZE] = {0}; //This will hold the overwritten bytes
BYTE JMP[SIZE] = {0};	//This holds the JMP to our code
DWORD oldProtect, myProtect = PAGE_EXECUTE_READWRITE; //Protection settings on memory
char debugBuffer[128]; //Used for DbgView

INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
	switch(Reason)
	{
	case DLL_PROCESS_ATTACH:
		MessageBoxW(NULL, L"Attacheds", L"Hooked MBW", MB_ICONEXCLAMATION);
		pOrigMBAddress = (pMessageBoxW) //Get MessageBoxW pointer
			GetProcAddress(GetModuleHandle("user32.dll"), "MessageBoxW");
		if(pOrigMBAddress != NULL)
			BeginRedirect(MyMessageBoxW);	//Valid? Redirect
		break;
	case DLL_PROCESS_DETACH:
		memcpy(pOrigMBAddress, oldBytes, SIZE);
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
		break;
	}
	return TRUE;
}

void BeginRedirect(LPVOID newFunction)
{
	sprintf_s(debugBuffer, 128, "pOrigMBAddress: %x", pOrigMBAddress);
	OutputDebugString(debugBuffer);
	BYTE tempJMP[SIZE] = {0xE9, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xC3}; //JMP <NOP> RET for now
	memcpy(JMP, tempJMP, SIZE); //Copy into global for convenience later
	DWORD JMPSize = ((DWORD)newFunction - (DWORD)pOrigMBAddress - 9); //Get address difference
	VirtualProtect((LPVOID)pOrigMBAddress, SIZE, PAGE_EXECUTE_READWRITE, &oldProtect);
			//Change memory settings to make sure we can write the JMP in
	memcpy(oldBytes, pOrigMBAddress, SIZE); //Copy old bytes before writing JMP
	sprintf_s(debugBuffer, 128, "Old bytes: %x%x%x%x%x", oldBytes[0], oldBytes[1],
		oldBytes[2], oldBytes[3], oldBytes[4], oldBytes[5]);
	OutputDebugString(debugBuffer);
	memcpy(&JMP[1], &JMPSize, 8); //Write the address to JMP to
	sprintf_s(debugBuffer, 128, "JMP: %x%x%x%x%x", JMP[0], JMP[1],
		JMP[2], JMP[3], JMP[4], JMP[5]);
	OutputDebugString(debugBuffer);
	memcpy(pOrigMBAddress, JMP, SIZE); //Write it in process memory
	VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); //Change setts back
}

int  WINAPI MyMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uiType)
{
	VirtualProtect((LPVOID)pOrigMBAddress, SIZE, myProtect, NULL); //ReadWrite again
	memcpy(pOrigMBAddress, oldBytes, SIZE); //Unhook API
	MessageBoxW(NULL, L"This should pop up", L"Hooked MBW", MB_ICONEXCLAMATION);
	int retValue = MessageBoxW(hWnd, lpText, lpCaption, uiType); //Get ret value
	memcpy(pOrigMBAddress, JMP, SIZE); //Rehook API
	VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); //Normal setts
	return retValue; //Return what should be returned
}


Modified lines are 5, 42, 44, 51. Changes I made are underlined.

Could anyone tell me how to fix it?
SuggestionRe: Can anybody help me with converting this example for x64? Pin
Richard MacCutchan29-Jun-15 3:36
mveRichard MacCutchan29-Jun-15 3:36 
QuestionQ: CTreeCtrl: How to over-ride default selection of item Pin
ShivaPrasadGadapa28-Jun-15 23:43
ShivaPrasadGadapa28-Jun-15 23:43 
QuestionDraw in front of bitmap Pin
lor7527-Jun-15 8:11
lor7527-Jun-15 8:11 
AnswerRe: Draw in front of bitmap Pin
Richard MacCutchan27-Jun-15 21:11
mveRichard MacCutchan27-Jun-15 21:11 
GeneralRe: Draw in front of bitmap Pin
lor7527-Jun-15 22:22
lor7527-Jun-15 22:22 
QuestionClass or object? Pin
Vaclav_26-Jun-15 5:32
Vaclav_26-Jun-15 5:32 
SuggestionRe: Class or object? Pin
David Crow26-Jun-15 5:54
David Crow26-Jun-15 5:54 
AnswerRe: Class or object? Pin
Maximilien26-Jun-15 7:24
Maximilien26-Jun-15 7:24 
AnswerRe: Class or object? Pin
CPallini26-Jun-15 8:05
mveCPallini26-Jun-15 8:05 
GeneralRe: Class or object? Pin
Albert Holguin26-Jun-15 10:26
professionalAlbert Holguin26-Jun-15 10:26 
AnswerRe: Class or object? Pin
Albert Holguin26-Jun-15 10:30
professionalAlbert Holguin26-Jun-15 10:30 
AnswerRe: Class or object? Pin
Stefan_Lang30-Jun-15 23:50
Stefan_Lang30-Jun-15 23:50 
GeneralRe: Class or object? Pin
_Flaviu1-Jul-15 22:17
_Flaviu1-Jul-15 22:17 
GeneralRe: Class or object? Pin
Stefan_Lang1-Jul-15 22:36
Stefan_Lang1-Jul-15 22:36 
GeneralRe: Class or object? Pin
_Flaviu2-Jul-15 1:21
_Flaviu2-Jul-15 1:21 
AnswerRe: Class or object? Pin
Kevin McFarlane6-Jul-15 23:16
Kevin McFarlane6-Jul-15 23:16 
QuestionVery simple binary file compressor in C Pin
stonemanhero26-Jun-15 1:52
stonemanhero26-Jun-15 1:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.