Explorar o código

Simple injector that launches a process with a dll injected and returns the launched PID

kaezin %!s(int64=8) %!d(string=hai) anos
pai
achega
d11ff4157c
Modificáronse 5 ficheiros con 358 adicións e 0 borrados
  1. 112 0
      injector.cpp
  2. 30 0
      injector.h
  3. 32 0
      injector.sln
  4. 157 0
      injector.vcxproj
  5. 27 0
      injector.vcxproj.filters

+ 112 - 0
injector.cpp

@@ -0,0 +1,112 @@
1
+#include "injector.h"
2
+
3
+#include <cassert>
4
+#include <windows.h>
5
+
6
+namespace {
7
+
8
+DWORD ExecuteFunctionInProcess(HANDLE process, void const* function, void const* argument, size_t argument_size)
9
+{
10
+    void* argument_buffer{nullptr};
11
+    if (argument != nullptr && argument_size > 0) {
12
+        argument_buffer = VirtualAllocEx(process, nullptr, argument_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
13
+        WriteProcessMemory(process, argument_buffer, argument, argument_size, nullptr);
14
+    }
15
+
16
+    auto thread = CreateRemoteThread(
17
+        process,
18
+        nullptr,
19
+        0,
20
+        (LPTHREAD_START_ROUTINE)function,
21
+        argument_buffer,
22
+        0,
23
+        nullptr);
24
+
25
+    DWORD result{0};
26
+    WaitForSingleObject(thread, INFINITE);
27
+    GetExitCodeThread(thread, &result);
28
+
29
+    if (argument_buffer != nullptr) {
30
+        VirtualFreeEx(process, argument_buffer, 0, MEM_RELEASE);
31
+        argument_buffer = nullptr;
32
+    }
33
+
34
+    return result;
35
+}
36
+
37
+}
38
+
39
+uint32_t LaunchInjected(
40
+    wchar_t const* command_line, 
41
+    wchar_t const* working_directory, 
42
+    wchar_t const* injected_dll, 
43
+    char const* initialize_function)
44
+{
45
+    if (!command_line || !working_directory || !injected_dll || !initialize_function) {
46
+        return 0;
47
+    }
48
+
49
+    PROCESS_INFORMATION process_info{};
50
+    STARTUPINFO startup_info{};
51
+    startup_info.cb = sizeof(startup_info);
52
+
53
+    // or, you know, i could just const_cast and :pray:...
54
+    auto command_length = wcslen(command_line);
55
+    wchar_t* mutable_command_line = new wchar_t[command_length + 1];
56
+    wcsncpy(mutable_command_line, command_line, command_length);
57
+    mutable_command_line[command_length] = L'\0';
58
+
59
+    auto process_launched = CreateProcess(
60
+        nullptr, 
61
+        mutable_command_line, 
62
+        nullptr, 
63
+        nullptr, 
64
+        false, 
65
+        CREATE_SUSPENDED, 
66
+        nullptr, 
67
+        working_directory, 
68
+        &startup_info, 
69
+        &process_info);
70
+    if (!process_launched) {
71
+        return 0;
72
+    }
73
+
74
+    delete mutable_command_line;
75
+    mutable_command_line = nullptr;
76
+
77
+    // technically wrong, but oh-so-useful. the base address of kernel32.dll is
78
+    // identical for all processes in any given session.
79
+    void* load_library = GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW");
80
+    assert(load_library);
81
+
82
+    void* remote_base_address = (void*)ExecuteFunctionInProcess(
83
+        process_info.hProcess, 
84
+        load_library, 
85
+        injected_dll, 
86
+        sizeof(wchar_t) * wcslen(injected_dll)
87
+    );
88
+    if (!remote_base_address) {
89
+        TerminateProcess(process_info.hProcess, 0);
90
+        return 0;
91
+    }
92
+
93
+    void* local_base_address = (void*)LoadLibrary(injected_dll);
94
+    ptrdiff_t offset =
95
+        (uintptr_t)GetProcAddress((HMODULE)local_base_address, initialize_function) - (uintptr_t)local_base_address;
96
+    FreeLibrary((HMODULE)local_base_address);
97
+
98
+    void* initialize_address = (void*)((uintptr_t)remote_base_address + offset);
99
+    ExecuteFunctionInProcess(process_info.hProcess, initialize_address, nullptr, 0);
100
+
101
+    ResumeThread(process_info.hThread);
102
+
103
+    CloseHandle(process_info.hThread);
104
+    CloseHandle(process_info.hProcess);
105
+
106
+    return static_cast<uint32_t>(process_info.dwProcessId);
107
+}
108
+
109
+int WINAPI DllMain(HMODULE, DWORD, LPVOID)
110
+{
111
+    return true;
112
+}

+ 30 - 0
injector.h

@@ -0,0 +1,30 @@
1
+#ifndef INJECTOR_H_
2
+#define INJECTOR_H_
3
+
4
+#include <cstdint>
5
+
6
+#if defined(INJECTOR_EXPORTS)
7
+#define INJECTOR_EXPORT __declspec(dllexport)
8
+#else
9
+#define INJECTOR_EXPORT __declspec(dllimport)
10
+#endif
11
+
12
+#if defined(__cplusplus)
13
+extern "C" {
14
+#endif
15
+
16
+// Launches `command_line` from `working_directory` and injects `injected_dll`,
17
+// optionally calling `initialize_function` in after injection. If everything 
18
+// goes off without a hitch the process ID will be returned, otherwise 0.
19
+INJECTOR_EXPORT uint32_t LaunchInjected(
20
+    wchar_t const* command_line, 
21
+    wchar_t const* working_directory, 
22
+    wchar_t const* injected_dll, 
23
+    char const* initialize_function
24
+);
25
+
26
+#if defined(__cplusplus)
27
+}
28
+#endif
29
+
30
+#endif // INJECTOR_H_

+ 32 - 0
injector.sln

@@ -0,0 +1,32 @@
1
+
2
+Microsoft Visual Studio Solution File, Format Version 12.00
3
+# Visual Studio 14
4
+VisualStudioVersion = 14.0.25420.1
5
+MinimumVisualStudioVersion = 10.0.40219.1
6
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "injector", "injector.vcxproj", "{28694F69-B04C-43AF-9DD1-812085A8E25A}"
7
+EndProject
8
+Global
9
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
10
+		Debug|Any CPU = Debug|Any CPU
11
+		Debug|x64 = Debug|x64
12
+		Debug|x86 = Debug|x86
13
+		Release|Any CPU = Release|Any CPU
14
+		Release|x64 = Release|x64
15
+		Release|x86 = Release|x86
16
+	EndGlobalSection
17
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
18
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Debug|Any CPU.ActiveCfg = Debug|Win32
19
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Debug|x64.ActiveCfg = Debug|x64
20
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Debug|x64.Build.0 = Debug|x64
21
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Debug|x86.ActiveCfg = Debug|Win32
22
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Debug|x86.Build.0 = Debug|Win32
23
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Release|Any CPU.ActiveCfg = Release|Win32
24
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Release|x64.ActiveCfg = Release|x64
25
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Release|x64.Build.0 = Release|x64
26
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Release|x86.ActiveCfg = Release|Win32
27
+		{28694F69-B04C-43AF-9DD1-812085A8E25A}.Release|x86.Build.0 = Release|Win32
28
+	EndGlobalSection
29
+	GlobalSection(SolutionProperties) = preSolution
30
+		HideSolutionNode = FALSE
31
+	EndGlobalSection
32
+EndGlobal

+ 157 - 0
injector.vcxproj

@@ -0,0 +1,157 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<Project DefaultTargets="Build" ToolsVersion="14.0" 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
+    <ProjectGuid>{28694F69-B04C-43AF-9DD1-812085A8E25A}</ProjectGuid>
23
+    <Keyword>Win32Proj</Keyword>
24
+    <RootNamespace>injector</RootNamespace>
25
+    <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
26
+  </PropertyGroup>
27
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
28
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
29
+    <ConfigurationType>DynamicLibrary</ConfigurationType>
30
+    <UseDebugLibraries>true</UseDebugLibraries>
31
+    <PlatformToolset>v140</PlatformToolset>
32
+    <CharacterSet>Unicode</CharacterSet>
33
+  </PropertyGroup>
34
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
35
+    <ConfigurationType>DynamicLibrary</ConfigurationType>
36
+    <UseDebugLibraries>false</UseDebugLibraries>
37
+    <PlatformToolset>v140</PlatformToolset>
38
+    <WholeProgramOptimization>true</WholeProgramOptimization>
39
+    <CharacterSet>Unicode</CharacterSet>
40
+  </PropertyGroup>
41
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
42
+    <ConfigurationType>DynamicLibrary</ConfigurationType>
43
+    <UseDebugLibraries>true</UseDebugLibraries>
44
+    <PlatformToolset>v140</PlatformToolset>
45
+    <CharacterSet>Unicode</CharacterSet>
46
+  </PropertyGroup>
47
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
48
+    <ConfigurationType>DynamicLibrary</ConfigurationType>
49
+    <UseDebugLibraries>false</UseDebugLibraries>
50
+    <PlatformToolset>v140</PlatformToolset>
51
+    <WholeProgramOptimization>true</WholeProgramOptimization>
52
+    <CharacterSet>Unicode</CharacterSet>
53
+  </PropertyGroup>
54
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
55
+  <ImportGroup Label="ExtensionSettings">
56
+  </ImportGroup>
57
+  <ImportGroup Label="Shared">
58
+  </ImportGroup>
59
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
60
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
61
+  </ImportGroup>
62
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
63
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
64
+  </ImportGroup>
65
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
66
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67
+  </ImportGroup>
68
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
69
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
70
+  </ImportGroup>
71
+  <PropertyGroup Label="UserMacros" />
72
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
73
+    <LinkIncremental>true</LinkIncremental>
74
+  </PropertyGroup>
75
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
76
+    <LinkIncremental>true</LinkIncremental>
77
+  </PropertyGroup>
78
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
79
+    <LinkIncremental>false</LinkIncremental>
80
+  </PropertyGroup>
81
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
82
+    <LinkIncremental>false</LinkIncremental>
83
+  </PropertyGroup>
84
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
85
+    <ClCompile>
86
+      <PrecompiledHeader>
87
+      </PrecompiledHeader>
88
+      <WarningLevel>Level3</WarningLevel>
89
+      <Optimization>Disabled</Optimization>
90
+      <PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_WINDOWS;_USRDLL;INJECTOR_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
91
+      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
92
+    </ClCompile>
93
+    <Link>
94
+      <SubSystem>Windows</SubSystem>
95
+      <GenerateDebugInformation>true</GenerateDebugInformation>
96
+    </Link>
97
+  </ItemDefinitionGroup>
98
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
99
+    <ClCompile>
100
+      <PrecompiledHeader>
101
+      </PrecompiledHeader>
102
+      <WarningLevel>Level3</WarningLevel>
103
+      <Optimization>Disabled</Optimization>
104
+      <PreprocessorDefinitions>_DEBUG;_WINDOWS;_USRDLL;INJECTOR_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
105
+      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
106
+    </ClCompile>
107
+    <Link>
108
+      <SubSystem>Windows</SubSystem>
109
+      <GenerateDebugInformation>true</GenerateDebugInformation>
110
+    </Link>
111
+  </ItemDefinitionGroup>
112
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
113
+    <ClCompile>
114
+      <WarningLevel>Level3</WarningLevel>
115
+      <PrecompiledHeader>
116
+      </PrecompiledHeader>
117
+      <Optimization>MaxSpeed</Optimization>
118
+      <FunctionLevelLinking>true</FunctionLevelLinking>
119
+      <IntrinsicFunctions>true</IntrinsicFunctions>
120
+      <PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_WINDOWS;_USRDLL;INJECTOR_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
121
+      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
122
+    </ClCompile>
123
+    <Link>
124
+      <SubSystem>Windows</SubSystem>
125
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
126
+      <OptimizeReferences>true</OptimizeReferences>
127
+      <GenerateDebugInformation>true</GenerateDebugInformation>
128
+    </Link>
129
+  </ItemDefinitionGroup>
130
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
131
+    <ClCompile>
132
+      <WarningLevel>Level3</WarningLevel>
133
+      <PrecompiledHeader>
134
+      </PrecompiledHeader>
135
+      <Optimization>MaxSpeed</Optimization>
136
+      <FunctionLevelLinking>true</FunctionLevelLinking>
137
+      <IntrinsicFunctions>true</IntrinsicFunctions>
138
+      <PreprocessorDefinitions>NDEBUG;_WINDOWS;_USRDLL;INJECTOR_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
139
+      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
140
+    </ClCompile>
141
+    <Link>
142
+      <SubSystem>Windows</SubSystem>
143
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
144
+      <OptimizeReferences>true</OptimizeReferences>
145
+      <GenerateDebugInformation>true</GenerateDebugInformation>
146
+    </Link>
147
+  </ItemDefinitionGroup>
148
+  <ItemGroup>
149
+    <ClCompile Include="injector.cpp" />
150
+  </ItemGroup>
151
+  <ItemGroup>
152
+    <ClInclude Include="injector.h" />
153
+  </ItemGroup>
154
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
155
+  <ImportGroup Label="ExtensionTargets">
156
+  </ImportGroup>
157
+</Project>

+ 27 - 0
injector.vcxproj.filters

@@ -0,0 +1,27 @@
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="Source Files">
5
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7
+    </Filter>
8
+    <Filter Include="Header Files">
9
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10
+      <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11
+    </Filter>
12
+    <Filter Include="Resource Files">
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="injector.cpp">
19
+      <Filter>Source Files</Filter>
20
+    </ClCompile>
21
+  </ItemGroup>
22
+  <ItemGroup>
23
+    <ClInclude Include="injector.h">
24
+      <Filter>Header Files</Filter>
25
+    </ClInclude>
26
+  </ItemGroup>
27
+</Project>