├── .DotSettings ├── .gitattributes ├── .gitignore ├── ILject.Core ├── CustomAssemblyLoadContext.cs ├── IInjector.cs ├── ILject.Core.csproj ├── IPatchContext.cs ├── MemberDefinitionResolver.cs ├── MemberInjectAttribute.cs └── PatchContext.cs ├── ILject.SampleFrameworkPatchTarget ├── ILject.SampleFrameworkPatchTarget.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── ILject.SamplePatch ├── ILject.SamplePatch.csproj ├── Program.cs ├── Properties │ └── launchSettings.json └── SampleInjectors.cs ├── ILject.SamplePatchTarget ├── ILject.SamplePatchTarget.csproj └── Program.cs ├── ILject.sln ├── ILject.sln.DotSettings ├── LICENSE └── README.MD /.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | False 3 | HINT 4 | HINT 5 | SUGGESTION 6 | WARNING 7 | WARNING 8 | WARNING 9 | WARNING 10 | WARNING 11 | WARNING 12 | WARNING 13 | WARNING 14 | SUGGESTION 15 | SUGGESTION 16 | WARNING 17 | WARNING 18 | WARNING 19 | WARNING 20 | WARNING 21 | WARNING 22 | WARNING 23 | WARNING 24 | Required 25 | Required 26 | Required 27 | Required 28 | False 29 | Join 30 | 2044 31 | 32 | True 33 | 34 | True 35 | 36 | 37 | True 38 | True 39 | 0 40 | 0 41 | 0 42 | TOGETHER_SAME_LINE 43 | 1 44 | 1 45 | False 46 | True 47 | LINE_BREAK 48 | False 49 | CHOP_IF_LONG 50 | 150 51 | 52 | 53 | 54 | 55 | CHOP_ALWAYS 56 | 57 | 58 | 59 | False 60 | True 61 | True 62 | True -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /ILject.Core/CustomAssemblyLoadContext.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.Loader; 3 | 4 | namespace ILject.Core 5 | { 6 | internal class CustomAssemblyLoadContext : AssemblyLoadContext 7 | { 8 | protected override Assembly Load(AssemblyName assemblyName) => Assembly.Load(assemblyName); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ILject.Core/IInjector.cs: -------------------------------------------------------------------------------- 1 | namespace ILject.Core 2 | { 3 | public interface IInjector 4 | { 5 | // This number determines when this injector gets run 6 | // The higher the execution priority, the earlier it runs. 7 | int ExecutionPriority { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ILject.Core/ILject.Core.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard1.5 5 | 6 | 7 | 8 | AnyCPU 9 | TRACE;DEBUG;NETSTANDARD1_5 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /ILject.Core/IPatchContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using Mono.Cecil; 5 | 6 | namespace ILject.Core 7 | { 8 | public interface IPatchContext 9 | { 10 | AssemblyDefinition AssemblyDefinition { get; } 11 | void LoadInjectors(IEnumerable injectors); 12 | void RunInjectors(); 13 | void Run(Func getEntryPoint, object[] arguments); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ILject.Core/MemberDefinitionResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using Mono.Cecil; 4 | using Mono.Collections.Generic; 5 | 6 | namespace ILject.Core 7 | { 8 | internal class MemberDefinitionResolver where T : IMemberDefinition 9 | { 10 | public Func> GetCollectionFunc; 11 | 12 | public MemberDefinitionResolver(string fullName, Func> getCollFunc = null) 13 | { 14 | FullName = fullName; 15 | GetCollectionFunc = getCollFunc; 16 | } 17 | 18 | private string FullName { get; } 19 | private string TypeName 20 | { 21 | get 22 | { 23 | var fullNameSplit = FullName.Split(' '); 24 | return fullNameSplit.Length > 1 ? fullNameSplit[1].Split(new[] {"::"}, StringSplitOptions.None)[0] : FullName; 25 | } 26 | } 27 | 28 | // Don't smite me for doing this in a generic class! 29 | public T GetMember(AssemblyDefinition assembly) 30 | { 31 | return typeof(T) == typeof(TypeDefinition) ? (T) (object) GetType(assembly) : GetMemberInternal(assembly); 32 | } 33 | 34 | private TypeDefinition GetType(AssemblyDefinition assembly) 35 | { 36 | return assembly.Modules.Select(m => m.GetType(TypeName)).Single(t => t != null); 37 | } 38 | 39 | private T GetMemberInternal(AssemblyDefinition assembly) 40 | { 41 | return GetCollectionFunc(GetType(assembly)).Single(f => f.FullName == FullName); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ILject.Core/MemberInjectAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Mono.Cecil; 3 | 4 | namespace ILject.Core 5 | { 6 | public enum MemberType 7 | { 8 | Type, 9 | Field, 10 | Event, 11 | Property, 12 | Method 13 | } 14 | 15 | [AttributeUsage(AttributeTargets.Method, Inherited = false)] 16 | public class MemberInjectAttribute : Attribute 17 | { 18 | public Func GetMember; 19 | 20 | public MemberInjectAttribute(string fullName, MemberType memberType) 21 | { 22 | switch (memberType) 23 | { 24 | case MemberType.Type: 25 | GetMember = a => new MemberDefinitionResolver(fullName).GetMember(a); 26 | break; 27 | case MemberType.Field: 28 | GetMember = a => new MemberDefinitionResolver(fullName, t => t.Fields).GetMember(a); 29 | break; 30 | case MemberType.Event: 31 | GetMember = a => new MemberDefinitionResolver(fullName, t => t.Events).GetMember(a); 32 | break; 33 | case MemberType.Property: 34 | GetMember = a => new MemberDefinitionResolver(fullName, t => t.Properties).GetMember(a); 35 | break; 36 | case MemberType.Method: 37 | GetMember = a => new MemberDefinitionResolver(fullName, t => t.Methods).GetMember(a); 38 | break; 39 | default: 40 | throw new ArgumentOutOfRangeException(nameof(memberType), memberType, null); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ILject.Core/PatchContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Runtime.Loader; 7 | using Mono.Cecil; 8 | 9 | namespace ILject.Core 10 | { 11 | public class PatchContext : IPatchContext 12 | { 13 | #region Initialising the context 14 | 15 | public AssemblyDefinition AssemblyDefinition { get; } 16 | public AssemblyLoadContext LoadContext { get; } 17 | 18 | public PatchContext(string executableName) : this(AssemblyDefinition.ReadAssembly(executableName)) {} 19 | 20 | public PatchContext(AssemblyDefinition assemblyDefinition) 21 | { 22 | AssemblyDefinition = assemblyDefinition; 23 | LoadContext = new CustomAssemblyLoadContext(); 24 | } 25 | 26 | #endregion 27 | 28 | #region Loading the injectors 29 | 30 | private List Injectors { get; } = new List(); 31 | 32 | public void LoadInjectors(IEnumerable injectors) 33 | { 34 | Injectors.AddRange(injectors); 35 | } 36 | 37 | #endregion 38 | 39 | #region Running the injectors 40 | 41 | public void RunInjectors() 42 | { 43 | Injectors.OrderByDescending(i => i.ExecutionPriority).ToList().ForEach(RunInjector); 44 | } 45 | 46 | private void RunInjector(IInjector injector) 47 | { 48 | injector.GetType() 49 | .GetRuntimeMethods() 50 | .Select(m => new {Method = m, Attributes = m.GetCustomAttributes().OfType()}) 51 | .Where(m => m.Attributes.Count() == 1) 52 | .ToList() 53 | .ForEach(m => RunInjectionMethod(injector, m.Method, m.Attributes.Single())); 54 | } 55 | 56 | private void RunInjectionMethod(IInjector injector, MethodBase injectionMethod, MemberInjectAttribute attribute) 57 | { 58 | injectionMethod.Invoke(injector, new object[] {attribute.GetMember(AssemblyDefinition)}); 59 | } 60 | 61 | #endregion 62 | 63 | #region Running the assembly 64 | 65 | public void Run(Func getEntryPoint = null, object[] arguments = null) 66 | { 67 | using (var assemblyStream = new MemoryStream()) 68 | { 69 | AssemblyDefinition.MainModule.Write(assemblyStream); 70 | assemblyStream.Seek(0, SeekOrigin.Begin); 71 | var assembly = LoadContext.LoadFromStream(assemblyStream); 72 | GetEntryPoint(assembly, getEntryPoint).Invoke(null, arguments ?? new object[] {new string[0]}); 73 | } 74 | } 75 | 76 | private static MethodInfo GetEntryPoint(Assembly assembly, Func getEntryPoint) 77 | { 78 | if (getEntryPoint != null) 79 | { 80 | return getEntryPoint(assembly); 81 | } 82 | var entryPointField = assembly.GetType().GetRuntimeField("EntryPoint"); 83 | if (entryPointField != null) 84 | { 85 | return (MethodInfo) entryPointField.GetValue(assembly); 86 | } 87 | var mainMethods = assembly.DefinedTypes.SelectMany(t => t.DeclaredMethods).Where(m => m.Name == "Main").ToList(); 88 | if (mainMethods.Count != 1) 89 | { 90 | throw new Exception("Unable to find single Main method."); 91 | } 92 | return mainMethods.Single(); 93 | } 94 | 95 | #endregion 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /ILject.SampleFrameworkPatchTarget/ILject.SampleFrameworkPatchTarget.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {5F5923CF-4CEA-4589-9A89-0F98CB05E6B3} 8 | Exe 9 | ILject.SampleFrameworkPatchTarget 10 | ILject.SampleFrameworkPatchTarget 11 | v4.0 12 | 512 13 | 14 | 15 | AnyCPU 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | AnyCPU 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /ILject.SampleFrameworkPatchTarget/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ILject.SampleFrameworkPatchTarget 4 | { 5 | internal class Program 6 | { 7 | // ReSharper disable once UnusedMember.Local 8 | // ReSharper disable once UnusedParameter.Local 9 | private static void Main(string[] args) => Console.WriteLine($"1 + 1 = {Add(1, 1)}"); 10 | 11 | private static int Add(int a, int b) => a + b; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ILject.SampleFrameworkPatchTarget/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | 8 | [assembly: AssemblyTitle("ILject.SampleFrameworkPatchTarget")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ILject.SampleFrameworkPatchTarget")] 13 | [assembly: AssemblyCopyright("Copyright © 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | 21 | [assembly: ComVisible(false)] 22 | 23 | // The following GUID is for the ID of the typelib if this project is exposed to COM 24 | 25 | [assembly: Guid("5f5923cf-4cea-4589-9a89-0f98cb05e6b3")] 26 | 27 | // Version information for an assembly consists of the following four values: 28 | // 29 | // Major Version 30 | // Minor Version 31 | // Build Number 32 | // Revision 33 | // 34 | // You can specify all the values or you can default the Build and Revision Numbers 35 | // by using the '*' as shown below: 36 | // [assembly: AssemblyVersion("1.0.*")] 37 | 38 | [assembly: AssemblyVersion("1.0.0.0")] 39 | [assembly: AssemblyFileVersion("1.0.0.0")] 40 | -------------------------------------------------------------------------------- /ILject.SamplePatch/ILject.SamplePatch.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp1.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ILject.SamplePatch/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using ILject.Core; 4 | 5 | namespace ILject.SamplePatch 6 | { 7 | internal class Program 8 | { 9 | // ReSharper disable once UnusedMember.Local 10 | // ReSharper disable once SuggestBaseTypeForParameter 11 | private static void Main(string[] args) 12 | { 13 | var arguments = ValidateArguments(args); 14 | if (arguments.IsValid) 15 | { 16 | var context = new PatchContext(arguments.CoreExecutableName); 17 | context.LoadInjectors(new[] {new SampleCoreInjector()}); 18 | context.RunInjectors(); 19 | context.Run(); 20 | 21 | Console.WriteLine($"Finished running .NET Core injection sample.{Environment.NewLine}Press Any key to continue"); 22 | Console.ReadKey(true); 23 | 24 | context = new PatchContext(arguments.FrameworkExecutableName); 25 | context.LoadInjectors(new[] {new SampleFrameworkInjector()}); 26 | context.RunInjectors(); 27 | context.Run(); 28 | Console.WriteLine("Finished running .NET Framework injection sample."); 29 | } 30 | Console.WriteLine("Press any key to quit"); 31 | Console.ReadKey(true); 32 | } 33 | 34 | private static Arguments ValidateArguments(IReadOnlyList args) 35 | { 36 | var arguments = new Arguments {IsValid = args.Count == 2}; 37 | if (arguments.IsValid) 38 | { 39 | arguments.CoreExecutableName = args[0]; 40 | arguments.FrameworkExecutableName = args[1]; 41 | } 42 | else 43 | { 44 | Console.Error.WriteLine("Invalid usage. Correct usage is \"ILject core-filename framework-filename\""); 45 | } 46 | return arguments; 47 | } 48 | 49 | internal class Arguments 50 | { 51 | public bool IsValid { get; set; } 52 | public string CoreExecutableName { get; set; } 53 | public string FrameworkExecutableName { get; set; } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ILject.SamplePatch/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "ILject.SamplePatch": { 4 | "commandName": "Project", 5 | "commandLineArgs": 6 | "$(SolutionDir)ILject.SamplePatchTarget\\bin\\Debug\\netcoreapp1.1\\ILject.SamplePatchTarget.dll $(SolutionDir)ILject.SampleFrameworkPatchTarget\\bin\\Debug\\ILject.SampleFrameworkPatchTarget.exe" 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /ILject.SamplePatch/SampleInjectors.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using ILject.Core; 3 | using Mono.Cecil; 4 | using Mono.Cecil.Cil; 5 | 6 | namespace ILject.SamplePatch 7 | { 8 | public class SampleCoreInjector : IInjector 9 | { 10 | public int ExecutionPriority { get; set; } = 0; 11 | 12 | [MemberInject("System.Int32 ILject.SamplePatchTarget.Program::Add(System.Int32,System.Int32)", MemberType.Method)] 13 | public void InjectProgramAdd(MethodDefinition addMethod) 14 | { 15 | var processor = addMethod.Body.GetILProcessor(); 16 | var addInsn = addMethod.Body.Instructions.Single(i => i.OpCode == OpCodes.Add); 17 | var loadOne = processor.Create(OpCodes.Ldc_I4_1); 18 | var add = processor.Create(OpCodes.Add); 19 | processor.InsertAfter(addInsn, loadOne); 20 | processor.InsertAfter(loadOne, add); 21 | } 22 | } 23 | 24 | public class SampleFrameworkInjector : IInjector 25 | { 26 | public int ExecutionPriority { get; set; } = 0; 27 | 28 | [MemberInject("System.Int32 ILject.SampleFrameworkPatchTarget.Program::Add(System.Int32,System.Int32)", MemberType.Method)] 29 | public void InjectProgramAdd(MethodDefinition addMethod) 30 | { 31 | var processor = addMethod.Body.GetILProcessor(); 32 | var addInsn = addMethod.Body.Instructions.Single(i => i.OpCode == OpCodes.Add); 33 | var loadOne = processor.Create(OpCodes.Ldc_I4_1); 34 | var add = processor.Create(OpCodes.Add); 35 | processor.InsertAfter(addInsn, loadOne); 36 | processor.InsertAfter(loadOne, add); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ILject.SamplePatchTarget/ILject.SamplePatchTarget.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp1.1 6 | 7 | 8 | 9 | pdbonly 10 | True 11 | 12 | -------------------------------------------------------------------------------- /ILject.SamplePatchTarget/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ILject.SamplePatchTarget 4 | { 5 | internal class Program 6 | { 7 | // ReSharper disable once UnusedMember.Local 8 | // ReSharper disable once UnusedParameter.Local 9 | private static void Main(string[] args) => Console.WriteLine($"1 + 1 = {Add(1, 1)}"); 10 | 11 | private static int Add(int a, int b) => a + b; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ILject.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26228.9 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILject.Core", "ILject.Core\ILject.Core.csproj", "{C95076C0-D988-458B-AD6C-B34653434076}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILject.SamplePatch", "ILject.SamplePatch\ILject.SamplePatch.csproj", "{698E8B7D-2228-48D4-B61D-A359D520273E}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILject.SamplePatchTarget", "ILject.SamplePatchTarget\ILject.SamplePatchTarget.csproj", "{0695DA26-118B-42FB-8C6B-B88172BBD012}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILject.SampleFrameworkPatchTarget", "ILject.SampleFrameworkPatchTarget\ILject.SampleFrameworkPatchTarget.csproj", "{5F5923CF-4CEA-4589-9A89-0F98CB05E6B3}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {C95076C0-D988-458B-AD6C-B34653434076}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {C95076C0-D988-458B-AD6C-B34653434076}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {C95076C0-D988-458B-AD6C-B34653434076}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {C95076C0-D988-458B-AD6C-B34653434076}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {698E8B7D-2228-48D4-B61D-A359D520273E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {698E8B7D-2228-48D4-B61D-A359D520273E}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {698E8B7D-2228-48D4-B61D-A359D520273E}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {698E8B7D-2228-48D4-B61D-A359D520273E}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {0695DA26-118B-42FB-8C6B-B88172BBD012}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {0695DA26-118B-42FB-8C6B-B88172BBD012}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {0695DA26-118B-42FB-8C6B-B88172BBD012}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {0695DA26-118B-42FB-8C6B-B88172BBD012}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {5F5923CF-4CEA-4589-9A89-0F98CB05E6B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {5F5923CF-4CEA-4589-9A89-0F98CB05E6B3}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {5F5923CF-4CEA-4589-9A89-0F98CB05E6B3}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {5F5923CF-4CEA-4589-9A89-0F98CB05E6B3}.Release|Any CPU.Build.0 = Release|Any CPU 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | EndGlobal 41 | -------------------------------------------------------------------------------- /ILject.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | D:\Projects\ILject\ILject\.DotSettings 3 | ..\.DotSettings 4 | True 5 | True 6 | ILject 7 | 1 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Cameron Aavik 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # ILject 2 | 3 | > Provides a way which you can load a .NET dll/exe from disk, modify/inject IL, and then run the assembly all in memory without modifying the file. 4 | 5 | ## Projects in this repository 6 | 7 | - ILject.Core: A .NET Standard 1.4 Library that is the main component of this repository 8 | - ILject.SamplePatch: A sample application which changes the IL of a method in ILject.SamplePatchTarget and ILject.SampleFrameworkPatchTarget and then runs them 9 | - ILject.SampleFrameworkPatchTarget: A .NET Framework application which gets targeted by ILject.SamplePatch 10 | - ILject.SamplePatchTarget: A .NET Core application which gets targeted by ILject.SamplePatch 11 | 12 | ## How to use 13 | 14 | I don't have any proper documentation at the moment. But you should be able to make out how to use this by looking at 15 | [this method](https://github.com/CameronAavik/ILject/blob/master/ILject.SamplePatch/Program.cs#L16-L19) and 16 | [a sample injector](https://github.com/CameronAavik/ILject/blob/master/ILject.SamplePatch/SampleInjectors.cs#L8-L22) 17 | 18 | ## Contribute 19 | 20 | PRs accepted. --------------------------------------------------------------------------------