Skip to content

Commit

Permalink
Adde code to send also external files
Browse files Browse the repository at this point in the history
  • Loading branch information
enkomio committed Dec 20, 2018
1 parent 51a2c7f commit c949a7a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
16 changes: 15 additions & 1 deletion Src/ES.ManagedInjector/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ internal class Client
private readonly NamedPipeClientStream _client = new NamedPipeClientStream(".", Constants.NamedPipeCode.ToString("X"), PipeDirection.InOut);
private readonly PipeChanell _pipeChanell = null;
private readonly List<Byte[]> _dependencies = null;
private readonly Dictionary<String, Byte[]> _files = null;
private readonly Byte[] _assemblyContent;
private readonly String _methodName = null;
private InjectionResult _lastError = InjectionResult.Success;

public Client(Byte[] assemblyContent, String methodName, List<Byte[]> dependencies)
public Client(Byte[] assemblyContent, String methodName, List<Byte[]> dependencies, Dictionary<String, Byte[]> files)
{
_assemblyContent = assemblyContent;
_methodName = methodName;
_dependencies = dependencies;
_files = files;
_pipeChanell = new PipeChanell(_client);
}

Expand All @@ -34,6 +36,7 @@ public void ActivateAssembly()
var invocationResult =
_pipeChanell.SendMessage(Constants.Ping) &&
SendDependencies() &&
SendFiles() &&
SendToken() &&
SendAssembly() &&
_pipeChanell.SendMessage(Constants.Run);
Expand Down Expand Up @@ -125,6 +128,17 @@ private Int32 GetMethodToken(Assembly assembly, String methodName)
return methodToken;
}

private Boolean SendFiles()
{
var result = true;
foreach (var kv in _files)
{
var value = String.Format("{0}|{1}{2}", kv.Key.Length, kv.Key, Convert.ToBase64String(kv.Value));
result = result && _pipeChanell.SendMessage(Constants.File, value);
}
return result;
}

private Boolean SendDependencies()
{
var result = true;
Expand Down
20 changes: 10 additions & 10 deletions Src/ES.ManagedInjector/ES.ManagedInjector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
<None Include="paket.references" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ImportGroup Label=".NET DllExport">
<Import Project="$(SolutionDir)packages\DllExport.1.6.1\tools\net.r_eg.DllExport.targets" Condition="Exists('$(SolutionDir)packages\DllExport.1.6.1\tools\net.r_eg.DllExport.targets')" Label="8337224c9ad9e356" />
</ImportGroup>
<Target Name="DllExportRestorePkg" BeforeTargets="PrepareForBuild">
<Warning Condition="!Exists('$(SolutionDir)DllExport.bat')" Text="We can't find 'DllExport.bat' in '$(SolutionDir)' - https://github.com/3F/DllExport" />
<Exec Condition="('$(DllExportModImported)' != 'true' Or !Exists('$(SolutionDir)packages\DllExport.1.6.1\tools\net.r_eg.DllExport.targets')) And Exists('$(SolutionDir)DllExport.bat')" Command="cd &quot;$(SolutionDir)&quot; &amp; DllExport.bat -action Restore" />
</Target>
<Target Name="DllExportRPkgDynamicImport" BeforeTargets="PostBuildEvent" DependsOnTargets="GetFrameworkPaths" Condition="'$(DllExportModImported)' != 'true' And '$(DllExportRPkgDyn)' != 'false'">
<MSBuild BuildInParallel="true" UseResultsCache="true" Projects="$(MSBuildProjectFullPath)" Properties="DllExportRPkgDyn=true" Targets="Build" />
</Target>
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework'">
<ItemGroup>
Expand All @@ -84,14 +94,4 @@
</ItemGroup>
</When>
</Choose>
<ImportGroup Label=".NET DllExport">
<Import Project="$(SolutionDir)packages\DllExport.1.6.1\tools\net.r_eg.DllExport.targets" Condition="Exists('$(SolutionDir)packages\DllExport.1.6.1\tools\net.r_eg.DllExport.targets')" Label="8337224c9ad9e356" />
</ImportGroup>
<Target Name="DllExportRestorePkg" BeforeTargets="PrepareForBuild">
<Warning Condition="!Exists('$(SolutionDir)DllExport.bat')" Text="We can't find 'DllExport.bat' in '$(SolutionDir)' - https://github.com/3F/DllExport" />
<Exec Condition="('$(DllExportModImported)' != 'true' Or !Exists('$(SolutionDir)packages\DllExport.1.6.1\tools\net.r_eg.DllExport.targets')) And Exists('$(SolutionDir)DllExport.bat')" Command="cd &quot;$(SolutionDir)&quot; &amp; DllExport.bat -action Restore" />
</Target>
<Target Name="DllExportRPkgDynamicImport" BeforeTargets="PostBuildEvent" DependsOnTargets="GetFrameworkPaths" Condition="'$(DllExportModImported)' != 'true' And '$(DllExportRPkgDyn)' != 'false'">
<MSBuild BuildInParallel="true" UseResultsCache="true" Projects="$(MSBuildProjectFullPath)" Properties="DllExportRPkgDyn=true" Targets="Build" />
</Target>
</Project>
2 changes: 2 additions & 0 deletions Src/ES.ManagedInjector/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ internal static class Constants
public static readonly String Token = "TOKEN";
public static readonly String Assembly = "ASSEMBLY";
public static readonly String Dependency = "DEPENDENCY";
public static readonly String File = "FILE";
public static readonly String Run = "RUN";
}

Expand All @@ -74,5 +75,6 @@ public enum InjectionResult : Int32
UnableToConnectToNamedPipe = 7,
ErrorDuringInvocation = 8,
InvalidAssemblyDependencyBuffer = 9,
InvalidFileBuffer = 10
}
}
26 changes: 25 additions & 1 deletion Src/ES.ManagedInjector/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Injector
private readonly String _methodName = null;
private readonly Assembly _assembly = null;
private readonly List<Byte[]> _dependency = new List<Byte[]>();
private readonly Dictionary<String, Byte[]> _files = new Dictionary<String, Byte[]>();
private Process _process = null;
private IntPtr _processHandle = IntPtr.Zero;

Expand Down Expand Up @@ -148,6 +149,29 @@ public void AddDependency(Byte[] assemblyContent)
_dependency.Add(assemblyContent);
}

/// <summary>
/// Copy the content of the given filename to the folder of the injected process
/// </summary>
/// <param name="filename"></param>
public void AddFile(String filename, Byte[] content)
{
var basename = Path.GetFileName(filename);
if (!_files.ContainsKey(basename))
{
_files.Add(basename, content);
}
}

/// <summary>
/// Copy the content of the given filename to the folder of the injected process. In this case, the
/// file must exists on the filesystem.
/// </summary>
/// <param name="filename"></param>
public void AddFile(String filename)
{
AddFile(filename, File.ReadAllBytes(filename));
}

private void ResolveDependencies()
{
if (_assembly != null)
Expand All @@ -174,7 +198,7 @@ private void ResolveDependencies()

private InjectionResult ActivateAssembly()
{
var client = new Client(_assemblyContent, _methodName, _dependency);
var client = new Client(_assemblyContent, _methodName, _dependency, _files);
client.ActivateAssembly();
return client.GetLastError();
}
Expand Down
17 changes: 17 additions & 0 deletions Src/ES.ManagedInjector/Server.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -75,6 +76,22 @@ private Boolean ProcessCommand(PipeMessage msg)
_lastError = InjectionResult.InvalidAssemblyDependencyBuffer;
}
}
else if (msgType.Equals(Constants.File, StringComparison.OrdinalIgnoreCase))
{
try
{
var data = msg.GetData();
var indexOfPipe = data.IndexOf("|");
var filenameLength = Int32.Parse(data.Substring(0, indexOfPipe));
var filename = data.Substring(indexOfPipe + 1, filenameLength);
var fileContent = Convert.FromBase64String(data.Substring(indexOfPipe + 1 + filenameLength));
File.WriteAllBytes(filename, fileContent);
}
catch
{
_lastError = InjectionResult.InvalidFileBuffer;
}
}
else if (msgType.Equals(Constants.Run, StringComparison.OrdinalIgnoreCase))
{
if (_assemblyBuffer == null)
Expand Down
8 changes: 8 additions & 0 deletions Src/ManagedInjectorSln.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D1577D93-4AE1-4613-92DB-C33B50D6AC00}"
ProjectSection(SolutionItems) = preProject
..\build.bat = ..\build.bat
build.fsx = build.fsx
DllExport.bat = DllExport.bat
paket.dependencies = paket.dependencies
EndProjectSection
Expand All @@ -24,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleProgram", "Examples\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyWithDefaultMethodName", "Examples\AssemblyWithDefaultMethodName\AssemblyWithDefaultMethodName.csproj", "{533B05F2-50F0-456F-A9D9-699A2C6C0CA0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssemblyWithFileResourceNeeded", "Examples\AssemblyWithFileResourceNeeded\AssemblyWithFileResourceNeeded.csproj", "{9411B58F-2FA9-44EE-A829-F97956D254FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -54,6 +57,10 @@ Global
{533B05F2-50F0-456F-A9D9-699A2C6C0CA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{533B05F2-50F0-456F-A9D9-699A2C6C0CA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{533B05F2-50F0-456F-A9D9-699A2C6C0CA0}.Release|Any CPU.Build.0 = Release|Any CPU
{9411B58F-2FA9-44EE-A829-F97956D254FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9411B58F-2FA9-44EE-A829-F97956D254FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9411B58F-2FA9-44EE-A829-F97956D254FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9411B58F-2FA9-44EE-A829-F97956D254FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -64,6 +71,7 @@ Global
{7FC1759D-6878-421E-AF2E-41094E7C984D} = {4E58D7CE-0C9E-4AB4-A449-D3FBDA16B732}
{1B22270A-A1EE-4488-9AE0-C261B1C5F025} = {4E58D7CE-0C9E-4AB4-A449-D3FBDA16B732}
{533B05F2-50F0-456F-A9D9-699A2C6C0CA0} = {4E58D7CE-0C9E-4AB4-A449-D3FBDA16B732}
{9411B58F-2FA9-44EE-A829-F97956D254FD} = {4E58D7CE-0C9E-4AB4-A449-D3FBDA16B732}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {050BA54E-C98C-4AC4-8E70-A51CBA300720}
Expand Down

0 comments on commit c949a7a

Please sign in to comment.