#include "stdafx.h"
#include
int _tmain(int argc, _TCHAR* argv[])
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
if(argc < 2){
fprintf(stderr, "C:\\>%s \n", argv[0]);
return 1;
}
memset(&pi, 0, sizeof(pi));
memset(&si, 0, sizeof(si));
si.cb = sizeof(STARTUPINFO);
//1
BOOL r = CreateProcess(
NULL, argv[1], NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_SUSPENDED | DEBUG_PROCESS,
NULL, NULL, &si, &pi);
if(!r)
return -1;
//2
ResumeThread(pi.hThread);
while(1) {
DEBUG_EVENT de;
if(!WaitForDebugEvent(&de, INFINITE))
break;
DWORD dwContinueStatus = DBG_CONTINUE;
switch(de.dwDebugEventCode)
{
case CREATE_PROCESS_DEBUG_EVENT:
printf("CREATE_PROCESS_DEBUG_EVENT\n");
break;
case CREATE_THREAD_DEBUG_EVENT:
printf("CREATE_THREAD_DEBUG_EVENT\n");
break;
case EXIT_THREAD_DEBUG_EVENT:
printf("EXIT_THREAD_DEBUG_EVENT\n");
break;
case EXIT_PROCESS_DEBUG_EVENT:
printf("EXIT_PROCESS_DEBUG_EVENT\n");
break;
case EXCEPTION_DEBUG_EVENT:
if(de.u.Exception.ExceptionRecord.ExceptionCode !=
EXCEPTION_BREAKPOINT)
{
dwContinueStatus = DBG_EXCEPTION_NOT_HANDLED;
}
printf("EXCEPTION_DEBUG_EVENT\n");
break;
case OUTPUT_DEBUG_STRING_EVENT:
printf("OUTPUT_DEBUG_STRING_EVENT\n");
break;
case RIP_EVENT:
printf("RIP_EVENT\n");
break;
case LOAD_DLL_DEBUG_EVENT:
printf("LOAD_DLL_DEBUG_EVENT\n");
break;
case UNLOAD_DLL_DEBUG_EVENT:
printf("UNLOAD_DLL_DEBUG_EVENT\n");
break;
}
if(de.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
break;
ContinueDebugEvent(
de.dwProcessId, de.dwThreadId, dwContinueStatus);
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
DEBUG_PROCESS标识:调试对象所产生的子进程,以及子进程的子进程都作为调试对象
DEBUG_ONLY_THIS_PROCESS:启动的那一个进程作为调试对象
typedef struct _DEBUG_EVENT {
DWORD dwDebugEventCode;
DWORD dwProcessId;
DWORD dwThreadId;
union {
EXCEPTION_DEBUG_INFO Exception;
CREATE_THREAD_DEBUG_INFO CreateThread;
CREATE_PROCESS_DEBUG_INFO CreateProcessInfo;
EXIT_THREAD_DEBUG_INFO ExitThread;
EXIT_PROCESS_DEBUG_INFO ExitProcess;
LOAD_DLL_DEBUG_INFO LoadDll;
UNLOAD_DLL_DEBUG_INFO UnloadDll;
OUTPUT_DEBUG_STRING_INFO DebugString;
RIP_INFO RipInfo; } u;
} DEBUG_EVENT, *LPDEBUG_EVENT;
C:\>wdbg01a.exe "C:\Program Files\Internet Explorer\iexplore.exe"
#include "stdafx.h"
#include
#include "udis86.h"
#pragma comment(lib, "libudis86.lib")
int disas(unsigned char *buff, char *out, int size)
{
ud_t ud_obj;
ud_init(&ud_obj);
ud_set_input_buffer(&ud_obj, buff, 32);
ud_set_mode(&ud_obj, 32);
ud_set_syntax(&ud_obj, UD_SYN_INTEL);
if(ud_disassemble(&ud_obj)){
sprintf_s(out, size, "%14s %s",
ud_insn_hex(&ud_obj), ud_insn_asm(&ud_obj));
}else{
return -1;
}
return (int)ud_insn_len(&ud_obj);
}
int exception_debug_event(DEBUG_EVENT *pde)
{
DWORD dwReadBytes;
HANDLE ph = OpenProcess(
PROCESS_VM_WRITE | PROCESS_VM_READ | PROCESS_VM_OPERATION,
FALSE, pde->dwProcessId);
if(!ph)
return -1;
HANDLE th = OpenThread(THREAD_GET_CONTEXT | THREAD_SET_CONTEXT,
FALSE, pde->dwThreadId);
if(!th)
return -1;
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_ALL;
GetThreadContext(th, &ctx);
char asm_string[256];
unsigned char asm_code[32];
ReadProcessMemory(ph, (VOID *)ctx.Eip, asm_code, 32, &dwReadBytes);
if(disas(asm_code, asm_string, sizeof(asm_string)) == -1)
asm_string[0] = '\0';
printf("Exception: %08x (PID:%d, TID:%d)\n",
pde->u.Exception.ExceptionRecord.ExceptionAddress,
pde->dwProcessId, pde->dwThreadId);
printf(" %08x: %s\n", ctx.Eip, asm_string);
printf(" Reg: EAX=%08x ECX=%08x EDX=%08x EBX=%08x\n",
ctx.Eax, ctx.Ecx, ctx.Edx, ctx.Ebx);
printf(" ESI=%08x EDI=%08x ESP=%08x EBP=%08x\n",
ctx.Esi, ctx.Edi, ctx.Esp, ctx.Ebp);
SetThreadContext(th, &ctx);
CloseHandle(th);
CloseHandle(ph);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
if(argc < 2){
fprintf(stderr, "C:\\>%s \n", argv[0]);
return 1;
}
memset(&pi, 0, sizeof(pi));
memset(&si, 0, sizeof(si));
si.cb = sizeof(STARTUPINFO);
BOOL r = CreateProcess(
NULL, argv[1], NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_SUSPENDED | DEBUG_PROCESS,
NULL, NULL, &si, &pi);
if(!r)
return -1;
ResumeThread(pi.hThread);
int process_counter = 0;
do{
DEBUG_EVENT de;
if(!WaitForDebugEvent(&de, INFINITE))
break;
DWORD dwContinueStatus = DBG_CONTINUE;
switch(de.dwDebugEventCode)
{
case CREATE_PROCESS_DEBUG_EVENT:
process_counter++;
break;
case EXIT_PROCESS_DEBUG_EVENT:
process_counter--;
break;
case EXCEPTION_DEBUG_EVENT:
if(de.u.Exception.ExceptionRecord.ExceptionCode !=
EXCEPTION_BREAKPOINT)
{
dwContinueStatus = DBG_EXCEPTION_NOT_HANDLED;
}
exception_debug_event(&de);
break;
}
ContinueDebugEvent(
de.dwProcessId, de.dwThreadId, dwContinueStatus);
}while(process_counter > 0);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
OpenProcess
ReadProceeMemory
OpenThread
GetThreadContext
SetThreadContext
HANDLE OpenProcess(DWORD dwDesiredAcess,BOOL bInheritHandle,DWORD dwProcessId)
//params:访问标志,句柄继承选项,进程ID
BOOL ReadProcessMemory(HANDLE hProcess,LPCVOID lpBaseAddress,LPVOID lpBUffer,DWORD nSize,LPDWORD lpNumberOFBytesRead)
//pamars:进程句柄,读取起始地址,用于存放数据的缓冲区,要读取的字节数,实际读取的字节数
BOOL WriteProcessMemory(HANDLE hProcess,LPCVOID lpBaseAddress,LPVOID lpBUffer,DWORD nSize,LPDWORD lpNumberOFBytesRead)
//params:进程句柄,写入起始地址,数据缓冲区,要写入的字节数,实际写入的字节数
HANDLE OpenThread(DWORD dwDesiredAccess,BOOL bInheritHandle,DWORD dwThreadId);
//params:访问标志,句柄继承选项,线程ID
BOOL GetThreadContext(HANDLE hThread,LPCONTEXT lpContext);
//params:拥有上下文的线程句柄,接受上下文的结构体地址
BOOL SetThreadContext(HANDLE hThread,CONST CONTEXT *lpContext);
//params:拥有上下文的线程句柄,存放上下文的结构体地址
int main(int argc, char *argv[])
{
char *s = NULL;
*s = 0xFF; return 0;
}
SetWindowsHookEx
HHOOK SetWindowsHookEx(int idHook,HOOKPORC lpfn,HINSTANCE hMod,DWORD dwThreadId);
//params:钩子类型,钩子过程,应用程序示例的句柄,线程ID
CallNextHookEx
LRESULT CallNextHookEx(HHOOK hhk,int nCode,WPARAM wParam,LPARAM lParam);
//当前钩子的句柄,传递给钩子过程的代码,传递给钩子过程的值,传递给钩子过程的值
UnhookWindowsHookEx
BOOL UnhookWindowsHookEx(HHOOK hhk);
//要解除的对象的钩子过程句柄
loging.h
#ifdef LOGING_EXPORTS
#define LOGING_API extern "C" __declspec(dllexport)
#else
#define LOGING_API extern "C" __declspec(dllimport)
#endif
LOGING_API int CallSetWindowsHookEx(VOID);
LOGING_API int CallUnhookWindowsHookEx(VOID);
loging.cpp
#include "stdafx.h"
#include "loging.h"
HHOOK g_hhook = NULL;
//系统消息再传递给目标线程原有的窗口过程之前,先由GetMsgProc来进行处理
static LRESULT WINAPI GetMsgProc(int code, WPARAM wParam, LPARAM lParam)
{
return(CallNextHookEx(NULL, code, wParam, lParam));
//调用了CallNextHookEx函数,这时消息会继续传递给下一个钩子过程
}
LOGING_API int CallSetWindowsHookEx(VOID)
{
if(g_hhook != NULL)
return -1;
MEMORY_BASIC_INFORMATION mbi;
if(VirtualQuery(CallSetWindowsHookEx, &mbi, sizeof(mbi)) == 0)
return -1;
HMODULE hModule = (HMODULE) mbi.AllocationBase;
g_hhook = SetWindowsHookEx(
WH_GETMESSAGE, GetMsgProc, hModule, 0);
//SetWindowsHookEx的功能是将原来传递给窗口过程的消息劫持下来,交给的第2个参数指定的函数来进行处理
if(g_hhook == NULL)
return -1;
return 0;
}
LOGING_API int CallUnhookWindowsHookEx(VOID)
{
if(g_hhook == NULL)
return -1;
UnhookWindowsHookEx(g_hhook);
g_hhook = NULL;
return 0;
}
#include "stdafx.h"
int WriteLog(TCHAR *szData)
{
TCHAR szTempPath[1024];
GetTempPath(sizeof(szTempPath), szTempPath);
lstrcat(szTempPath, "loging.log");
TCHAR szModuleName[1024];
GetModuleFileName(GetModuleHandle(NULL),
szModuleName, sizeof(szModuleName));
TCHAR szHead[1024];
wsprintf(szHead, "[PID:%d][Module:%s] ",
GetCurrentProcessId(), szModuleName);
HANDLE hFile = CreateFile(
szTempPath, GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return -1;
SetFilePointer(hFile, 0, NULL, FILE_END);
DWORD dwWriteSize;
WriteFile(hFile, szHead, lstrlen(szHead), &dwWriteSize, NULL);
WriteFile(hFile, szData, lstrlen(szData), &dwWriteSize, NULL);
CloseHandle(hFile);
return 0;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
WriteLog("DLL_PROCESS_ATTACH\n");
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
WriteLog("DLL_PROCESS_DETACH\n");
break;
}
return TRUE;
}
#include "stdafx.h"
#include
int _tmain(int argc, _TCHAR* argv[])
{
if(argc < 2){
fprintf(stderr, "%s \n", argv[0]);
return 1;
}
//从命令行传入要载入的DLL
HMODULE h = LoadLibrary(argv[1]);
if(h == NULL)
return -1;
int (__stdcall *fcall) (VOID);
fcall = (int (WINAPI *)(VOID))
GetProcAddress(h, "CallSetWindowsHookEx");
if(fcall == NULL){
fprintf(stderr, "ERROR: GetProcAddress\n");
goto _Exit;
}
int (__stdcall *ffree) (VOID);
ffree = (int (WINAPI *)(VOID))
GetProcAddress(h, "CallUnhookWindowsHookEx");
if(ffree == NULL){
fprintf(stderr, "ERROR: GetProcAddress\n");
goto _Exit;
}
if(fcall()){
fprintf(stderr, "ERROR: CallSetWindowsHookEx\n");
goto _Exit;
}
printf("Call SetWindowsHookEx\n");
getchar();
if(ffree()){
fprintf(stderr, "ERROR: CallUnhookWindowsHookEx\n");
goto _Exit;
}
printf("Call UnhookWindowsHookEx\n");
_Exit:
FreeLibrary(h);
return 0;
}
#include "stdafx.h"
#include
int _tmain(int argc, _TCHAR* argv[])
{
if(argc < 2){
fprintf(stderr, "%s \n", argv[0]);
return 1;
}
HKEY hKey;
LSTATUS lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows",
NULL, KEY_ALL_ACCESS, &hKey);
if(lResult != ERROR_SUCCESS){
printf("Error: RegOpenKeyEx failed.\n");
return -1;
}
DWORD dwSize, dwType;
TCHAR szDllName[256];
RegQueryValueEx(hKey, "AppInit_DLLs", NULL, &dwType, NULL, &dwSize);
RegQueryValueEx(hKey, "AppInit_DLLs", NULL, &dwType, (LPBYTE)szDllName, &dwSize);
printf("AppInit_DLLs: %s -> ", szDllName);
lstrcpy(szDllName, argv[1]);
lResult = RegSetValueEx(hKey, "AppInit_DLLs",
0, REG_SZ, (PBYTE)szDllName, lstrlen(szDllName) + 1);
if(lResult != ERROR_SUCCESS){
printf("Error: RegSetValueEx failed.\n");
}
RegQueryValueEx(hKey, "AppInit_DLLs", NULL, &dwType, NULL, &dwSize);
RegQueryValueEx(hKey, "AppInit_DLLs", NULL, &dwType, (LPBYTE)szDllName, &dwSize);
printf("%s\n", szDllName);
RegCloseKey(hKey);
return 0;
}
HANDLE CreateRemoteThread(
HANDLE hProcess, //进程句柄
LPSECURITY_ATTRIBUTES lpThreadAttributes,
DWORD dwStackSize, // 栈初始长度
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter, // 新线程的参数指针
DWORD dwCreationFlags, //创建标志
LPDWORD lpThreadId // 分配的线程ID指针
);
int InjectDLLtoProcessFromName(TCHAR *szTarget, TCHAR *szDllPath);
//按照可执行文件名找到相应的进程并注入DLL
int InjectDLLtoProcessFromPid(DWORD dwPid, TCHAR *szDllPath);
//按照进程ID找到相应的进程并注入DLL
int InjectDLLtoNewProcess(TCHAR *szCommandLine, TCHAR *szDllPath);
//创建新的进程并注入DLL
#include "stdafx.h"
#include
#include "injectcode.h"
DWORD GetProcessIdFromName(TCHAR *szTargetProcessName)
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hSnap == INVALID_HANDLE_VALUE)
return 0;
PROCESSENTRY32 pe;
pe.dwSize = sizeof(pe);
DWORD dwProcessId = 0;
BOOL bResult = Process32First(hSnap, &pe);
while(bResult){
if(!lstrcmp(pe.szExeFile, szTargetProcessName)){
dwProcessId = pe.th32ProcessID;
break;
}
bResult = Process32Next(hSnap, &pe);
}
CloseHandle(hSnap);
return dwProcessId;
}
int InjectDLL(HANDLE hProcess, TCHAR *szDllPath)
{
int szDllPathLen = lstrlen(szDllPath) + 1;
PWSTR RemoteProcessMemory = (PWSTR)VirtualAllocEx(hProcess,
NULL, szDllPathLen, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
if(RemoteProcessMemory == NULL)
return -1;
BOOL bRet = WriteProcessMemory(hProcess,
RemoteProcessMemory, (PVOID)szDllPath, szDllPathLen, NULL);
if(bRet == FALSE)
return -1;
PTHREAD_START_ROUTINE pfnThreadRtn;
pfnThreadRtn = (PTHREAD_START_ROUTINE)GetProcAddress(
GetModuleHandle("kernel32"), "LoadLibraryA");
if(pfnThreadRtn == NULL)
return -1;
//创建一个新的线程
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0,
pfnThreadRtn, RemoteProcessMemory, 0, NULL);
if(hThread == NULL)
return -1;
WaitForSingleObject(hThread, INFINITE);
VirtualFreeEx(hProcess,
RemoteProcessMemory, szDllPathLen, MEM_RELEASE);
CloseHandle(hThread);
return 0;
}
int InjectDLLtoExistedProcess(DWORD dwPid, TCHAR *szDllPath)
{
HANDLE hProcess = OpenProcess(
PROCESS_CREATE_THREAD | PROCESS_VM_READ | PROCESS_VM_WRITE |
PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION , FALSE, dwPid);
if(hProcess == NULL)
return -1;
/*
BOOL bJudgeWow64;
IsWow64Process(hProcess, &bJudgeWow64);
if(bJudgeWow64 == FALSE){
CloseHandle(hProcess);
return -1;
}
*/
if(InjectDLL(hProcess, szDllPath))
return -1;
CloseHandle(hProcess);
return 0;
}
int InjectDLLtoProcessFromName(TCHAR *szTarget, TCHAR *szDllPath)
{
DWORD dwPid = GetProcessIdFromName(szTarget);
if(dwPid == 0)
return -1;
if(InjectDLLtoExistedProcess(dwPid, szDllPath))
return -1;
return 0;
}
int InjectDLLtoProcessFromPid(DWORD dwPid, TCHAR *szDllPath)
{
if(InjectDLLtoExistedProcess(dwPid, szDllPath))
return -1;
return 0;
}
int InjectDLLtoNewProcess(TCHAR *szCommandLine, TCHAR *szDllPath)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
BOOL bResult = CreateProcess(NULL, szCommandLine, NULL, NULL,
FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
if(bResult == FALSE)
return -1;
int nRet = -1;
/*
BOOL bJudgeWow64;
IsWow64Process(pi.hProcess, &bJudgeWow64);
if(bJudgeWow64 == FALSE)
goto _Exit;
*/
if(InjectDLL(pi.hProcess, szDllPath))
goto _Exit;
nRet = 0;
_Exit:
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return nRet;
}
C:\>dllinjection.exe Name iexplore.exe "C:\\sampledll.dll"
#include "stdafx.h"
#include
typedef HWND (WINAPI *GETFORGROUNDWINDOW)(void);
typedef int (WINAPI *MSGBOX)(HWND, PCTSTR, PCTSTR, UINT);
typedef struct _injectdata {
TCHAR szTitle[32];
TCHAR szMessage[32];
HANDLE hProcess;
PDWORD pdwCodeRemote;
PDWORD pdwDataRemote;
MSGBOX fnMessageBox;
GETFORGROUNDWINDOW fnGetForegroundWindow;
} INJECTDATA, *PINJECTDATA;
static DWORD WINAPI func(PINJECTDATA myAPI)
{
myAPI->fnMessageBox((HWND)myAPI->fnGetForegroundWindow(),
myAPI->szMessage, myAPI->szTitle, MB_OK);
/*
if(myAPI->pCodeRemote != NULL)
VirtualFreeEx(myAPI->hProcess,
myAPI->pCodeRemote, 0, MEM_RELEASE);
if(myAPI->pDataRemote != NULL)
VirtualFreeEx(myAPI->hProcess,
myAPI->pDataRemote, 0, MEM_RELEASE);
*/
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE h = LoadLibrary("user32.dll");
if(h == NULL){
printf("ERR: LoadLibrary\n");
return -1;
}
INJECTDATA id;
id.fnGetForegroundWindow = (GETFORGROUNDWINDOW)
GetProcAddress(
GetModuleHandle("user32"), "GetForegroundWindow");
id.fnMessageBox = (MSGBOX)
GetProcAddress(
GetModuleHandle("user32"), "MessageBoxA");
lstrcpy(id.szTitle, "Message");
lstrcpy(id.szMessage, "Hello World!");
HWND hTarget = FindWindow("IEFrame", NULL);
if(hTarget == NULL){
printf("ERR: FindWindow\n");
goto _END1;
}
DWORD dwPID; // PID of iexplore.exe
GetWindowThreadProcessId(hTarget, (DWORD *)&dwPID);
id.hProcess = OpenProcess(PROCESS_CREATE_THREAD |
PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION |
PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, dwPID);
if(id.hProcess == NULL){
printf("ERR: OpenProcess\n");
goto _END1;
}
DWORD dwLen;
if((id.pdwCodeRemote = (PDWORD)VirtualAllocEx(id.hProcess,
0, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == NULL)
{
printf("ERR: VirtualAllocEx(pdwCodeRemote)\n");
goto _END2;
}
if((id.pdwDataRemote = (PDWORD)VirtualAllocEx(id.hProcess,
0, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == NULL)
{
printf("ERR: VirtualAllocEx(pdwDataRemote)\n");
goto _END3;
}
WriteProcessMemory(id.hProcess,
id.pdwCodeRemote, &func, 4096, &dwLen);
WriteProcessMemory(id.hProcess,
id.pdwDataRemote, &id, sizeof(INJECTDATA), &dwLen);
HANDLE hThread = CreateRemoteThread(id.hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)id.pdwCodeRemote, id.pdwDataRemote,
0, &dwLen);
if(hThread == NULL){
printf("ERR: CreateRemoteThread\n");
goto _END4;
}
WaitForSingleObject(hThread, INFINITE);
GetExitCodeThread(hThread, (PDWORD)&dwPID);
CloseHandle(hThread);
_END4:
VirtualFreeEx(id.hProcess, id.pdwDataRemote, 0, MEM_RELEASE);
_END3:
VirtualFreeEx(id.hProcess, id.pdwCodeRemote, 0, MEM_RELEASE);
_END2:
CloseHandle(id.hProcess);
_END1:
FreeLibrary(h);
return 0;
}
#ifdef DETOURSHOOK_EXPORTS
#define DETOURSHOOK_API __declspec(dllexport)
#else
#define DETOURSHOOK_API __declspec(dllimport)
#endif
DETOURSHOOK_API int WINAPI HookedMessageBoxA(HWND hWnd,
LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
#include "stdafx.h"
#include "detours.h"
#include "detourshook.h"
static int (WINAPI * TrueMessageBoxA)(HWND hWnd, LPCTSTR lpText,
LPCTSTR lpCaption, UINT uType) = MessageBoxA;
DETOURSHOOK_API int WINAPI HookedMessageBoxA(HWND hWnd,
LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
int nRet = TrueMessageBoxA(hWnd, lpText, "Hooked Message", uType);
return nRet;
}
int DllProcessAttach(VOID)
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)TrueMessageBoxA, HookedMessageBoxA);
if(DetourTransactionCommit() == NO_ERROR)
return -1;
return 0;
}
int DllProcessDetach(VOID)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)TrueMessageBoxA, HookedMessageBoxA);
DetourTransactionCommit();
return 0;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
DllProcessAttach();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
DllProcessDetach();
break;
}
return TRUE;
}
detours.cpp
detours.h
disasm.cpp
modules.cpp
detver.h
#include "stdafx.h"
#include
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE h = LoadLibrary("detourshook.dll");
MessageBoxA(GetForegroundWindow(),
"Hello World! using MessageBoxA", "Message", MB_OK);
FreeLibrary(h);
return 0;
}