Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions clrinject-cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ int main(int argc, char** argv) {
printf("Enumeration:\n");
int index = 1;
for (int i = 0; i < result.numRuntimes; i++) {
const Runtime& runtime = result.runtimes[i];
const RuntimeInfo& runtime = result.runtimes[i];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed Runtime and AppDomain to avoid conflicts with dotnet namespace

printf("#\tRuntime Version: '%ls', %s\n", runtime.version, runtime.started ? "is started" : "is not started");
for (int j = 0; j < runtime.numAppDomains; j++) {
const AppDomain& appDomain = runtime.appDomains[j];
const AppDomainInfo& appDomain = runtime.appDomains[j];
printf("#%d\t\tAppDomain Name: '%ls'\n", index++, appDomain.friendlyName);
}
}
Expand All @@ -148,19 +148,19 @@ int main(int argc, char** argv) {
else {
printf("Injection succeeded for following Runtimes and AppDomains:\n");
for (int i = 0; i < result.numRuntimes; i++) {
const Runtime& runtime = result.runtimes[i];
const RuntimeInfo& runtime = result.runtimes[i];
for (int j = 0; j < runtime.numAppDomains; j++) {
const AppDomain& appDomain = runtime.appDomains[j];
const AppDomainInfo& appDomain = runtime.appDomains[j];
if(appDomain.injected)
printf("\tRuntime '%ls', AppDomain '%ls'\n", runtime.version, appDomain.friendlyName);
}
}
printf("Injection failed for following Runtimes and AppDomains:\n");
int index = 1;
for (int i = 0; i < result.numRuntimes; i++) {
const Runtime& runtime = result.runtimes[i];
const RuntimeInfo& runtime = result.runtimes[i];
for (int j = 0; j < runtime.numAppDomains; j++) {
const AppDomain& appDomain = runtime.appDomains[j];
const AppDomainInfo& appDomain = runtime.appDomains[j];
if (!appDomain.injected && (!options.appDomainIndex || options.appDomainIndex == index))
printf("\tRuntime '%ls', AppDomain '%ls'\n", runtime.version, appDomain.friendlyName);
index++;
Expand Down
10 changes: 5 additions & 5 deletions clrinject-cli/clrinject-cli.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand Down Expand Up @@ -30,26 +30,26 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
Expand Down
6 changes: 6 additions & 0 deletions clrinject-lib.test/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
111 changes: 111 additions & 0 deletions clrinject-lib.test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;

namespace InjectorApp
{
class Program
{
static void Main(string[] args)
{
Process victimProcess = null;

try
{
victimProcess = Process.Start(PathOfVictimApp);
if (!victimProcess.IsRunning())
return;

System.Threading.Thread.Sleep(TimeSpan.FromSeconds(6));

Injector injector = new Injector();

//Test enumeration
var result = injector.EnumerateAppDomains("victim.exe");

if (result.Any())
{
result.ForEach(runtime =>
{
var runtimeStatus = runtime.IsRuntimeStarted ? "started" : "stopped";
Console.WriteLine($"Runtime Version : {runtime.runtimeVersion} is {runtimeStatus}, and have following app domains:");
int index = 0;
runtime.AppDomains.ForEach(appd => Console.WriteLine($"{++index}. {appd}"));
});
}
else
{
Console.WriteLine("Enumeration yielded no result");
}

Console.WriteLine();

//Test injection in all app domains
//injector.InjectIntoProcess("victim.exe", PathOfProcessToInject);

//Test injection in the specified app domain
injector.InjectIntoProcess("victim.exe", PathOfProcessToInject, 3);

}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
}

Console.WriteLine("Press any key to exit.");
Console.ReadLine();

victimProcess.Kill();

}

public static string PathOfProcessToInject
{
get
{
return GetPath("invader");
}
}

public static string PathOfVictimApp
{
get
{
return GetPath("victim");
}
}

private static string GetPath(string appName)
{
var assembly = Assembly.GetExecutingAssembly();
string codeBase = assembly.CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
var projectRootDirectory = Directory.GetParent(Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path))).Parent.Parent.Parent;
string buildMode = codeBase.Contains("Debug") ? "Debug" : "Release";
var assemblyName = AssemblyName.GetAssemblyName(assembly.FullName.Split(',')[0] + ".exe");
var assemblyArchitecture = assemblyName.ProcessorArchitecture == ProcessorArchitecture.Amd64 ? "x64" : "x86";
return $"{projectRootDirectory.FullName}\\{appName}\\bin\\{assemblyArchitecture}\\{buildMode}\\{appName}.exe";
}
}

public static class ProcessExtensions
{
public static bool IsRunning(this Process process)
{
if (process == null)
throw new ArgumentNullException("process");
try
{
Process.GetProcessById(process.Id);
}
catch (ArgumentException)
{
return false;
}
return true;
}
}
}
36 changes: 36 additions & 0 deletions clrinject-lib.test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("InjectorApp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("InjectorApp")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ccc57338-7ddc-4106-bf33-2538188d17ea")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
76 changes: 76 additions & 0 deletions clrinject-lib.test/clrinject-lib.test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CCC57338-7DDC-4106-BF33-2538188D17EA}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>InjectorApp</RootNamespace>
<AssemblyName>InjectorApp</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Release\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Release\</OutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\clrinject-lib\clrinject-lib.vcxproj">
<Project>{9dc43a8d-8596-4704-9982-c2bad7087de9}</Project>
<Name>clrinject-lib</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
21 changes: 21 additions & 0 deletions clrinject-lib/AssemblyInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;

[assembly:AssemblyTitleAttribute(L"clrinjectlib")];
[assembly:AssemblyDescriptionAttribute(L"")];
[assembly:AssemblyConfigurationAttribute(L"")];
[assembly:AssemblyCompanyAttribute(L"")];
[assembly:AssemblyProductAttribute(L"clrinjectlib")];
[assembly:AssemblyCopyrightAttribute(L"Copyright (c) 2019")];
[assembly:AssemblyTrademarkAttribute(L"")];
[assembly:AssemblyCultureAttribute(L"")];

[assembly:AssemblyVersionAttribute("1.0.*")];

[assembly:ComVisible(false)];

[assembly:CLSCompliantAttribute(true)];
Loading