Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 7ecf79a

Browse files
author
Sharkbyteprojects
committed
Add example + detours
Need vcpkg now
1 parent d76dfc4 commit 7ecf79a

File tree

12 files changed

+307
-13
lines changed

12 files changed

+307
-13
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## files generated by popular Visual Studio add-ons.
33
##
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5-
5+
vcpkg_installed
66
# User-specific files
77
*.rsuser
88
*.suo

attachToMe/attachToMe.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
#include <Windows.h>
3+
#include <cstdio>
4+
5+
int main()
6+
{
7+
std::cout << "Hello World!\nPress any Key...";
8+
getchar();
9+
MessageBoxA(NULL, "Hello World", "Hi", MB_OK | MB_ICONINFORMATION);
10+
}

attachToMe/attachToMe.vcxproj

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{e8cc63da-9b28-4460-85ec-f8aee801353e}</ProjectGuid>
25+
<RootNamespace>attachToMe</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v143</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v143</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v143</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v143</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<ClCompile>
75+
<WarningLevel>Level3</WarningLevel>
76+
<SDLCheck>true</SDLCheck>
77+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
78+
<ConformanceMode>true</ConformanceMode>
79+
</ClCompile>
80+
<Link>
81+
<SubSystem>Console</SubSystem>
82+
<GenerateDebugInformation>true</GenerateDebugInformation>
83+
</Link>
84+
</ItemDefinitionGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
86+
<ClCompile>
87+
<WarningLevel>Level3</WarningLevel>
88+
<FunctionLevelLinking>true</FunctionLevelLinking>
89+
<IntrinsicFunctions>true</IntrinsicFunctions>
90+
<SDLCheck>true</SDLCheck>
91+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
92+
<ConformanceMode>true</ConformanceMode>
93+
</ClCompile>
94+
<Link>
95+
<SubSystem>Console</SubSystem>
96+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
97+
<OptimizeReferences>true</OptimizeReferences>
98+
<GenerateDebugInformation>false</GenerateDebugInformation>
99+
</Link>
100+
</ItemDefinitionGroup>
101+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
102+
<ClCompile>
103+
<WarningLevel>Level3</WarningLevel>
104+
<SDLCheck>true</SDLCheck>
105+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106+
<ConformanceMode>true</ConformanceMode>
107+
</ClCompile>
108+
<Link>
109+
<SubSystem>Console</SubSystem>
110+
<GenerateDebugInformation>true</GenerateDebugInformation>
111+
</Link>
112+
</ItemDefinitionGroup>
113+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
114+
<ClCompile>
115+
<WarningLevel>Level3</WarningLevel>
116+
<FunctionLevelLinking>true</FunctionLevelLinking>
117+
<IntrinsicFunctions>true</IntrinsicFunctions>
118+
<SDLCheck>true</SDLCheck>
119+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
120+
<ConformanceMode>true</ConformanceMode>
121+
</ClCompile>
122+
<Link>
123+
<SubSystem>Console</SubSystem>
124+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
125+
<OptimizeReferences>true</OptimizeReferences>
126+
<GenerateDebugInformation>false</GenerateDebugInformation>
127+
</Link>
128+
</ItemDefinitionGroup>
129+
<ItemGroup>
130+
<ClCompile Include="attachToMe.cpp" />
131+
</ItemGroup>
132+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
133+
<ImportGroup Label="ExtensionTargets">
134+
</ImportGroup>
135+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Quelldateien">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Headerdateien">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Ressourcendateien">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="attachToMe.cpp">
19+
<Filter>Quelldateien</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

inject/detoursInternal.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <Windows.h>
2+
#include <detours/detours.h>
3+
#include <tlhelp32.h>
4+
#include <cstdio>
5+
#include "detoursInternal.h"
6+
7+
namespace det {
8+
void dUt() {
9+
DetourTransactionBegin();
10+
DetourUpdateThread(GetCurrentThread());
11+
12+
auto i = GetCurrentProcessId();
13+
auto id = GetCurrentThreadId();
14+
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, i);
15+
if (h != INVALID_HANDLE_VALUE) {
16+
THREADENTRY32 te;
17+
te.dwSize = sizeof(te);
18+
if (Thread32First(h, &te)) {
19+
do {
20+
if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
21+
sizeof(te.th32OwnerProcessID)) {
22+
if (te.th32OwnerProcessID != i || te.th32ThreadID == id) continue;
23+
auto h = OpenThread(THREAD_ALL_ACCESS & (~(WRITE_OWNER | DELETE)), NULL, te.th32ThreadID);
24+
if (h == NULL) continue;
25+
if (DetourUpdateThread(h) == NO_ERROR) {
26+
printf("Changed at Process 0x%04x Thread 0x%04x\n",
27+
te.th32OwnerProcessID, te.th32ThreadID);
28+
}
29+
else printf("ErrOnThread\n");
30+
}
31+
te.dwSize = sizeof(te);
32+
} while (Thread32Next(h, &te));
33+
}
34+
else {
35+
fputs("Invalid Handle", stderr);
36+
}
37+
CloseHandle(h);
38+
}
39+
}
40+
41+
void loadDetours(PVOID* real, PVOID detoured) {
42+
DetourRestoreAfterWith();
43+
44+
printf("simple" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
45+
" Starting.\n");
46+
fflush(stdout);
47+
48+
dUt();
49+
DetourAttach(real, detoured);
50+
LONG error = DetourTransactionCommit();
51+
52+
if (error == NO_ERROR)
53+
printf("simple" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
54+
" Detoured MsgB().\n");
55+
else
56+
printf("simple" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
57+
" Error detouring MsgB(): %ld\n", error);
58+
}
59+
60+
void disMountDe(PVOID* real, PVOID detoured) {
61+
dUt();
62+
DetourDetach(real, detoured);
63+
LONG error = DetourTransactionCommit();
64+
65+
printf("simple" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
66+
" Removed MsgB() (result=%ld)\n", error);
67+
fflush(stdout);
68+
}
69+
}

inject/detoursInternal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
namespace det {
3+
void dUt();
4+
void disMountDe(PVOID* real, PVOID detoured);
5+
void loadDetours(PVOID* real, PVOID detoured);
6+
}

inject/inject.vcxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
7171
</ImportGroup>
7272
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Label="Vcpkg">
74+
<VcpkgEnableManifest>true</VcpkgEnableManifest>
75+
</PropertyGroup>
7376
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
7477
<ClCompile>
7578
<WarningLevel>Level3</WarningLevel>
@@ -135,12 +138,17 @@
135138
</Link>
136139
</ItemDefinitionGroup>
137140
<ItemGroup>
141+
<ClCompile Include="detoursInternal.cpp" />
138142
<ClCompile Include="mtx.cpp" />
139143
<ClCompile Include="needle.cpp" />
140144
</ItemGroup>
141145
<ItemGroup>
146+
<ClInclude Include="detoursInternal.h" />
142147
<ClInclude Include="mtx.hxx" />
143148
</ItemGroup>
149+
<ItemGroup>
150+
<None Include="vcpkg.json" />
151+
</ItemGroup>
144152
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
145153
<ImportGroup Label="ExtensionTargets">
146154
</ImportGroup>

inject/inject.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@
2121
<ClCompile Include="mtx.cpp">
2222
<Filter>Quelldateien</Filter>
2323
</ClCompile>
24+
<ClCompile Include="detoursInternal.cpp">
25+
<Filter>Quelldateien</Filter>
26+
</ClCompile>
2427
</ItemGroup>
2528
<ItemGroup>
2629
<ClInclude Include="mtx.hxx">
2730
<Filter>Headerdateien</Filter>
2831
</ClInclude>
32+
<ClInclude Include="detoursInternal.h">
33+
<Filter>Headerdateien</Filter>
34+
</ClInclude>
35+
</ItemGroup>
36+
<ItemGroup>
37+
<None Include="vcpkg.json" />
2938
</ItemGroup>
3039
</Project>

inject/needle.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <Windows.h>
22
#include "mtx.hxx"
33
#include <filesystem>
4+
#include <detours/detours.h>
5+
#include "detoursInternal.h"
6+
#include <string>
47

58
DWORD WINAPI mthread(LPVOID p) {
69
wchar_t fname[MAX_PATH]{ 0 };
@@ -27,18 +30,32 @@ DWORD WINAPI mthread(LPVOID p) {
2730
return 0;
2831
}
2932

30-
BOOL WINAPI DllMain(
31-
HINSTANCE hinstDLL, // handle to DLL module
32-
DWORD fdwReason, // reason for calling function
33-
LPVOID lpvReserved) // reserved
33+
static int(WINAPI* TrueMsgA)(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) = MessageBoxA;
34+
35+
int WINAPI FakeMsgA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
36+
{
37+
std::string capt = "Detoured ";
38+
capt += lpCaption;
39+
return TrueMsgA(hWnd, lpText, capt.c_str(), uType | MB_TOPMOST);
40+
}
41+
42+
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved) // reserved
3443
{
44+
(void)hinst;
45+
(void)reserved;
46+
47+
if (DetourIsHelperProcess()) {
48+
return TRUE;
49+
}
50+
3551
// Perform actions based on the reason for calling.
36-
switch (fdwReason)
52+
switch (dwReason)
3753
{
3854
case DLL_PROCESS_ATTACH:
3955
// Initialize once for each new process.
4056
// Return FALSE to fail DLL load.
41-
CreateThread(NULL, NULL, mthread, hinstDLL, NULL, NULL);
57+
det::loadDetours(&(PVOID&)TrueMsgA, FakeMsgA);
58+
CreateThread(NULL, NULL, mthread, hinst, NULL, NULL);
4259
break;
4360

4461
case DLL_THREAD_ATTACH:
@@ -50,8 +67,8 @@ BOOL WINAPI DllMain(
5067
break;
5168

5269
case DLL_PROCESS_DETACH:
53-
54-
if (lpvReserved != nullptr)
70+
det::disMountDe(&(PVOID&)TrueMsgA, FakeMsgA);
71+
if (reserved != nullptr)
5572
{
5673
break; // do not do cleanup if process termination scenario
5774
}

inject/vcpkg.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
3+
"name": "my-application",
4+
"version": "0.15.2",
5+
"dependencies": [
6+
"detours"
7+
]
8+
}

0 commit comments

Comments
 (0)