Click here to Skip to main content
14,392,366 members

C / C++ / MFC

 
GeneralRe: recv no blocking Pin
Albert Holguin30-Jun-15 14:26
professionalAlbert Holguin30-Jun-15 14:26 
GeneralRe: recv no blocking Pin
Daniel Pfeffer30-Jun-15 18:53
professionalDaniel Pfeffer30-Jun-15 18:53 
GeneralRe: recv no blocking Pin
Albert Holguin1-Jul-15 5:02
professionalAlbert Holguin1-Jul-15 5:02 
GeneralRe: recv no blocking Pin
Albert Holguin30-Jun-15 14:27
professionalAlbert Holguin30-Jun-15 14:27 
QuestionCan a modeless Dialog Box be a main window "m_pMainWnd" Pin
ForNow29-Jun-15 6:07
MemberForNow29-Jun-15 6:07 
AnswerRe: Can a modeless Dialog Box be a main window "m_pMainWnd" Pin
Richard MacCutchan29-Jun-15 6:12
mveRichard MacCutchan29-Jun-15 6:12 
GeneralRe: Can a modeless Dialog Box be a main window "m_pMainWnd" Pin
ForNow29-Jun-15 6:22
MemberForNow29-Jun-15 6:22 
QuestionCan anybody help me with converting this example for x64? Pin
Member 802436529-Jun-15 3:42
MemberMember 802436529-Jun-15 3: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.
#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 4:36
mveRichard MacCutchan29-Jun-15 4:36 
QuestionQ: CTreeCtrl: How to over-ride default selection of item Pin
ShivaPrasadGadapa29-Jun-15 0:43
MemberShivaPrasadGadapa29-Jun-15 0:43 
QuestionDraw in front of bitmap Pin
lor7527-Jun-15 9:11
Memberlor7527-Jun-15 9:11 
AnswerRe: Draw in front of bitmap Pin
Richard MacCutchan27-Jun-15 22:11
mveRichard MacCutchan27-Jun-15 22:11 
GeneralRe: Draw in front of bitmap Pin
lor7527-Jun-15 23:22
Memberlor7527-Jun-15 23:22 
QuestionClass or object? Pin
Vaclav_26-Jun-15 6:32
MemberVaclav_26-Jun-15 6:32 
SuggestionRe: Class or object? Pin
David Crow26-Jun-15 6:54
MemberDavid Crow26-Jun-15 6:54 
AnswerRe: Class or object? Pin
Maximilien26-Jun-15 8:24
MemberMaximilien26-Jun-15 8:24 
AnswerRe: Class or object? Pin
CPallini26-Jun-15 9:05
mveCPallini26-Jun-15 9:05 
GeneralRe: Class or object? Pin
Albert Holguin26-Jun-15 11:26
professionalAlbert Holguin26-Jun-15 11:26 
AnswerRe: Class or object? Pin
Albert Holguin26-Jun-15 11:30
professionalAlbert Holguin26-Jun-15 11:30 
AnswerRe: Class or object? Pin
Stefan_Lang1-Jul-15 0:50
MemberStefan_Lang1-Jul-15 0:50 
GeneralRe: Class or object? Pin
_Flaviu1-Jul-15 23:17
Member_Flaviu1-Jul-15 23:17 
GeneralRe: Class or object? Pin
Stefan_Lang1-Jul-15 23:36
MemberStefan_Lang1-Jul-15 23:36 
GeneralRe: Class or object? Pin
_Flaviu2-Jul-15 2:21
Member_Flaviu2-Jul-15 2:21 
AnswerRe: Class or object? Pin
Kevin McFarlane7-Jul-15 0:16
MemberKevin McFarlane7-Jul-15 0:16 
QuestionVery simple binary file compressor in C Pin
stonemanhero26-Jun-15 2:52
Memberstonemanhero26-Jun-15 2: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.