diff options
Diffstat (limited to 'samples/einst')
-rw-r--r-- | samples/einst/Makefile | 148 | ||||
-rw-r--r-- | samples/einst/edll1x.cpp | 55 | ||||
-rw-r--r-- | samples/einst/edll2x.cpp | 55 | ||||
-rw-r--r-- | samples/einst/edll3x.cpp | 82 | ||||
-rw-r--r-- | samples/einst/einst.cpp | 98 |
5 files changed, 438 insertions, 0 deletions
diff --git a/samples/einst/Makefile b/samples/einst/Makefile new file mode 100644 index 0000000..ea6fea7 --- /dev/null +++ b/samples/einst/Makefile @@ -0,0 +1,148 @@ +############################################################################## +## +## Makefile for Detours Test Programs. +## +## Microsoft Research Detours Package +## +## Copyright (c) Microsoft Corporation. All rights reserved. +## + +!include ..\common.mak + +# ARM64 does not like base addresses below 4GB.
+# Append two extra zeros for it. +#
+!if "$(DETOURS_TARGET_PROCESSOR)" == "ARM64"
+EDLL1X_BASE=0x710000000
+EDLL2X_BASE=0x720000000
+EDLL3X_BASE=0x730000000
+!else +EDLL1X_BASE=0x7100000
+EDLL2X_BASE=0x7200000
+EDLL3X_BASE=0x7300000
+!endif + +LIBS=$(LIBS) kernel32.lib user32.lib + +all: dirs \ + $(BIND)\edll1x$(DETOURS_BITS).dll \ + $(BIND)\edll2x$(DETOURS_BITS).dll \ + $(BIND)\edll3x$(DETOURS_BITS).dll \ + $(BIND)\einst.exe \ + \ +!IF $(DETOURS_SOURCE_BROWSING)==1 + $(OBJD)\edll1x$(DETOURS_BITS).bsc \ + $(OBJD)\edll2x$(DETOURS_BITS).bsc \ + $(OBJD)\edll3x$(DETOURS_BITS).bsc \ + $(OBJD)\einst.bsc \ +!ENDIF + option + +clean: + -del *~ 2>nul + -del $(BIND)\edll1x*.* 2>nul + -del $(BIND)\edll2x*.* 2>nul + -del $(BIND)\edll3x*.* 2>nul + -del $(BIND)\einst.* 2>nul + -rmdir /q /s $(OBJD) 2>nul + +realclean: clean + -rmdir /q /s $(OBJDS) 2>nul + +dirs: + @if not exist $(BIND) mkdir $(BIND) && echo. Created $(BIND) + @if not exist $(OBJD) mkdir $(OBJD) && echo. Created $(OBJD) + +############################################################################## + +$(OBJD)\einst.obj : einst.cpp + +$(BIND)\einst.exe : $(OBJD)\einst.obj $(DEPS) + cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb $(OBJD)\einst.obj \ + /link $(LINKFLAGS) $(LIBS) \ + $(BIND)\edll1x$(DETOURS_BITS).lib $(BIND)\edll2x$(DETOURS_BITS).lib $(BIND)\edll3x$(DETOURS_BITS).lib \ + /subsystem:console /entry:WinMainCRTStartup + +$(OBJD)\einst.bsc : $(OBJD)\einst.obj + bscmake /v /n /o $@ $(OBJD)\einst.sbr + +$(OBJD)\edll1x.obj : edll1x.cpp + +$(BIND)\edll1x$(DETOURS_BITS).dll : $(OBJD)\edll1x.obj $(DEPS) + cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb \ + $(OBJD)\edll1x.obj /LD \ + /link $(LINKFLAGS) $(LIBS) \ + /subsystem:windows \ + /base:$(EDLL1X_BASE) + +$(OBJD)\edll1x$(DETOURS_BITS).bsc : $(OBJD)\edll1x.obj + bscmake /v /n /o $@ $(OBJD)\edll1x.sbr + +$(OBJD)\edll2x.obj : edll2x.cpp + +$(BIND)\edll2x$(DETOURS_BITS).dll : $(OBJD)\edll2x.obj $(DEPS) + cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb \ + $(OBJD)\edll2x.obj /LD \ + /link $(LINKFLAGS) $(LIBS) \ + /subsystem:console \ + /base:$(EDLL2X_BASE)
+ +$(OBJD)\edll2x$(DETOURS_BITS).bsc : $(OBJD)\edll2x.obj + bscmake /v /n /o $@ $(OBJD)\edll2x.sbr + +$(OBJD)\edll3x.obj : edll3x.cpp + +$(BIND)\edll3x$(DETOURS_BITS).dll : $(OBJD)\edll3x.obj $(DEPS) + cl $(CFLAGS) /Fe$@ /Fd$(@R).pdb \ + $(OBJD)\edll3x.obj /LD \ + /link $(LINKFLAGS) $(LIBS) \ + /subsystem:console \ + /base:$(EDLL3X_BASE)
+ +$(OBJD)\edll3x$(DETOURS_BITS).bsc : $(OBJD)\edll3x.obj + bscmake /v /n /o $@ $(OBJD)\edll3x.sbr + +############################################### Install non-bit-size binaries. + +!IF "$(DETOURS_OPTION_PROCESSOR)" != "" + +$(OPTD)\edll1x$(DETOURS_OPTION_BITS).dll: +$(OPTD)\edll1x$(DETOURS_OPTION_BITS).pdb: +$(OPTD)\edll2x$(DETOURS_OPTION_BITS).dll: +$(OPTD)\edll2x$(DETOURS_OPTION_BITS).pdb: +$(OPTD)\edll3x$(DETOURS_OPTION_BITS).dll: +$(OPTD)\edll3x$(DETOURS_OPTION_BITS).pdb: + +$(BIND)\edll1x$(DETOURS_OPTION_BITS).dll : $(OPTD)\edll1x$(DETOURS_OPTION_BITS).dll + @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). +$(BIND)\edll1x$(DETOURS_OPTION_BITS).pdb : $(OPTD)\edll1x$(DETOURS_OPTION_BITS).pdb + @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). +$(BIND)\edll2x$(DETOURS_OPTION_BITS).dll : $(OPTD)\edll2x$(DETOURS_OPTION_BITS).dll + @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). +$(BIND)\edll2x$(DETOURS_OPTION_BITS).pdb : $(OPTD)\edll2x$(DETOURS_OPTION_BITS).pdb + @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). +$(BIND)\edll3x$(DETOURS_OPTION_BITS).dll : $(OPTD)\edll3x$(DETOURS_OPTION_BITS).dll + @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). +$(BIND)\edll3x$(DETOURS_OPTION_BITS).pdb : $(OPTD)\edll3x$(DETOURS_OPTION_BITS).pdb + @if exist $? copy /y $? $(BIND) >nul && echo $@ copied from $(DETOURS_OPTION_PROCESSOR). + +option: \ + $(BIND)\edll1x$(DETOURS_OPTION_BITS).dll \ + $(BIND)\edll1x$(DETOURS_OPTION_BITS).pdb \ + $(BIND)\edll2x$(DETOURS_OPTION_BITS).dll \ + $(BIND)\edll2x$(DETOURS_OPTION_BITS).pdb \ + $(BIND)\edll3x$(DETOURS_OPTION_BITS).dll \ + $(BIND)\edll3x$(DETOURS_OPTION_BITS).pdb \ + +!ELSE + +option: + +!ENDIF + +############################################################################## + +test: all + $(BIND)\einst.exe + +################################################################# End of File. diff --git a/samples/einst/edll1x.cpp b/samples/einst/edll1x.cpp new file mode 100644 index 0000000..ee0880f --- /dev/null +++ b/samples/einst/edll1x.cpp @@ -0,0 +1,55 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Detours Test Program (edll1x.cpp of edll1x.dll) +// +// Microsoft Research Detours Package +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +#include <stdio.h> +#include <windows.h> +#include <detours.h> + +//////////////////////////////////////////////////////////////////// DLL Stuff +// +struct CPrivateStuff +{ + DETOUR_SECTION_HEADER header; + DETOUR_SECTION_RECORD record; + CHAR szMessage[32]; +}; + +#pragma data_seg(".detour") + +static CPrivateStuff private_stuff = { + DETOUR_SECTION_HEADER_DECLARE(sizeof(CPrivateStuff)), + { + (sizeof(CPrivateStuff) - sizeof(DETOUR_SECTION_HEADER)), + 0, + { /* d9ab8a40-f4cc-11d1-b6d7-006097b010e3 */ + 0xd9ab8a40, + 0xf4cc, + 0x11d1, + {0xb6, 0xd7, 0x00, 0x60, 0x97, 0xb0, 0x10, 0xe3} + } + }, + "The First Dll!" +}; +#pragma data_seg() + +__declspec(dllexport) VOID WINAPI EDll1Function(VOID) +{ + return; +} + +__declspec(dllexport) ULONG WINAPI +DllMain(HINSTANCE hInstance, DWORD dwReason, PVOID lpReserved) +{ + (void)hInstance; + (void)dwReason; + (void)lpReserved; + + return TRUE; +} + +///////////////////////////////////////////////////////////////// End of File. diff --git a/samples/einst/edll2x.cpp b/samples/einst/edll2x.cpp new file mode 100644 index 0000000..62a5abc --- /dev/null +++ b/samples/einst/edll2x.cpp @@ -0,0 +1,55 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Detours Test Program (edll2x.cpp of einst.exe/edll2x.dll) +// +// Microsoft Research Detours Package +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +#include <stdio.h> +#include <windows.h> +#include <detours.h> + +//////////////////////////////////////////////////////////////////// DLL Stuff +// +struct CPrivateStuff +{ + DETOUR_SECTION_HEADER header; + DETOUR_SECTION_RECORD record; + CHAR szMessage[32]; +}; + +#pragma data_seg(".detour") + +static CPrivateStuff private_stuff = { + DETOUR_SECTION_HEADER_DECLARE(sizeof(CPrivateStuff)), + { + (sizeof(CPrivateStuff) - sizeof(DETOUR_SECTION_HEADER)), + 0, + { /* d9ab8a40-f4cc-11d1-b6d7-006097b010e3 */ + 0xd9ab8a40, + 0xf4cc, + 0x11d1, + {0xb6, 0xd7, 0x00, 0x60, 0x97, 0xb0, 0x10, 0xe3} + } + }, + "The Second Dll!" +}; +#pragma data_seg() + +__declspec(dllexport) VOID WINAPI EDll2Function(VOID) +{ + return; +} + +__declspec(dllexport) ULONG WINAPI +DllMain(HINSTANCE hInstance, DWORD dwReason, PVOID lpReserved) +{ + (void)hInstance; + (void)dwReason; + (void)lpReserved; + + return TRUE; +} + +///////////////////////////////////////////////////////////////// End of File. diff --git a/samples/einst/edll3x.cpp b/samples/einst/edll3x.cpp new file mode 100644 index 0000000..2693d00 --- /dev/null +++ b/samples/einst/edll3x.cpp @@ -0,0 +1,82 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Detours Test Program (edll3x.cpp of einst.exe/edll3x.dll) +// +// Microsoft Research Detours Package +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +#include <stdio.h> +#include <windows.h> +#include <detours.h> + +//////////////////////////////////////////////////////////////////// DLL Stuff +// +struct CPrivateStuffPart1 +{ + DETOUR_SECTION_RECORD header; + CHAR szMessage[48]; +}; + +struct CPrivateStuffPart2 +{ + DETOUR_SECTION_RECORD header; + CHAR szMessage[64]; +}; + +struct CPrivateStuff +{ + DETOUR_SECTION_HEADER header; + CPrivateStuffPart1 record1; + CPrivateStuffPart2 record2; +}; + +#pragma data_seg(".detour") + +static CPrivateStuff private_stuff = { + DETOUR_SECTION_HEADER_DECLARE(sizeof(CPrivateStuff)), + { + { + sizeof(CPrivateStuffPart1), + 0, + { /* d9ab8a41-f4cc-11d1-b6d7-006097b010e3 */ + 0xd9ab8a41, + 0xf4cc, + 0x11d1, + {0xb6, 0xd7, 0x00, 0x60, 0x97, 0xb0, 0x10, 0xe3} + } + }, + "The Third DLL Part One!" + }, + { + { + sizeof(CPrivateStuffPart2), + 0, + { /* d9ab8a40-f4cc-11d1-b6d7-006097b010e3 */ + 0xd9ab8a40, + 0xf4cc, + 0x11d1, + {0xb6, 0xd7, 0x00, 0x60, 0x97, 0xb0, 0x10, 0xe3} + } + }, + "The Third DLL Part Two!" + } +}; +#pragma data_seg() + +__declspec(dllexport) VOID WINAPI EDll3Function(VOID) +{ + return; +} + +__declspec(dllexport) ULONG WINAPI +DllMain(HINSTANCE hInstance, DWORD dwReason, PVOID lpReserved) +{ + (void)hInstance; + (void)dwReason; + (void)lpReserved; + + return TRUE; +} + +///////////////////////////////////////////////////////////////// End of File. diff --git a/samples/einst/einst.cpp b/samples/einst/einst.cpp new file mode 100644 index 0000000..e732a1f --- /dev/null +++ b/samples/einst/einst.cpp @@ -0,0 +1,98 @@ +////////////////////////////////////////////////////////////////////////////// +// +// Detours Test Program (einst.cpp of einst.exe) +// +// Microsoft Research Detours Package +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +#include <stdio.h> +#include <windows.h> +#include <detours.h> + +struct CPrivateStuff +{ + DETOUR_SECTION_HEADER header; + DETOUR_SECTION_RECORD record; + CHAR szMessage[32]; +}; + +#ifdef INCLUDE_THIS +#pragma data_seg(".detour") + +static CPrivateStuff private_stuff = { + DETOUR_SECTION_HEADER_DECLARE(sizeof(CPrivateStuff)), + { + (sizeof(CPrivateStuff) - sizeof(DETOUR_SECTION_HEADER)), + 0, + { /* d9ab8a40-f4cc-11d1-b6d7-006097b010e3 */ + 0xd9ab8a40, + 0xf4cc, + 0x11d1, + {0xb6, 0xd7, 0x00, 0x60, 0x97, 0xb0, 0x10, 0xe3} + } + }, + "The Application!" +}; +#pragma data_seg() +#endif + +GUID my_guid = +{ /* d9ab8a40-f4cc-11d1-b6d7-006097b010e3 */ + 0xd9ab8a40, + 0xf4cc, + 0x11d1, + {0xb6, 0xd7, 0x00, 0x60, 0x97, 0xb0, 0x10, 0xe3} +}; + +__declspec(dllimport) VOID WINAPI EDll1Function(VOID); +__declspec(dllimport) VOID WINAPI EDll2Function(VOID); +__declspec(dllimport) VOID WINAPI EDll3Function(VOID); + +void FindPayload(HINSTANCE hinst) +{ + CHAR szModuleName[256]; + GetModuleFileNameA(hinst, szModuleName, ARRAYSIZE(szModuleName)); + printf(" %p : %s\n", hinst, szModuleName); + + ULONG cbData = 0; + PBYTE pbData = (PBYTE)DetourFindPayload(hinst, my_guid, &cbData); + + if (pbData) { + printf(" %08p..%08p : %50.50s\n", + pbData, + pbData + cbData, + pbData); + } +} + +int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR lpszCmdLine, int nCmdShow) +{ + (void)hinst; + (void)hprev; + (void)lpszCmdLine; + (void)nCmdShow; + + printf("Source .EXE:\n"); + FindPayload(NULL); + printf("\n"); + + printf("DLL and EXE binaries loaded:\n"); + + EDll1Function(); + EDll2Function(); + EDll3Function(); + + for (HINSTANCE hiter = NULL; (hiter = DetourEnumerateModules(hiter)) != NULL;) { + FindPayload(hiter); + } + + if ((PVOID)hinst == (PVOID)lpszCmdLine) { + DispatchMessage(NULL); // Force load of gdi32.dll + } + + return 0; +} + +// +///////////////////////////////////////////////////////////////// End of File. |