MonoNativeInjector is a powerful tool designed to inject assemblies into Mono processes. It's particularly useful for developers working on modding, reverse engineering, or enhancing Mono-based applications. This document provides an overview of how to use MonoNativeInjector in your projects.
- Inject assemblies into 32-bit and 64-bit Mono processes.
- Custom logger support for monitoring injection process.
- Easy to use API for injecting, ejecting, and managing Mono assemblies.
- This library is compiled natively and is intended to be used on Windows platforms only.
- Compatible with Windows 64-bit (win64) and Windows 32-bit (win86) systems.
- A target Mono-based application process.
To start using MonoNativeInjector, ensure you have cloned this repository and built MonoNativeInjector.dll
according to the instructions provided.
To build the MonoNativeInjector for specific platforms, use the following commands:
For Windows 64-bit:
dotnet publish -r win-x64
For Windows 32-bit:
dotnet publish -r win-x86
These commands will compile the project for the respective Windows architecture, ensuring compatibility and optimizing performance for the target platform.
Below is an example demonstrating how to use MonoNativeInjector to inject and eject an assembly into a Mono process. This example includes setting up a custom logger, opening a Mono process, injecting an assembly, and then cleaning up.
using System;
using System.Runtime.InteropServices;
unsafe
{
delegate* managed<IntPtr, int, void> delegateDefinition1 = &DelegateDefinition;
// Set a custom logger.
SetLogger((IntPtr)delegateDefinition1);
// Open a Mono process by name.
var monoInjector = OpenMonoInjector(Marshal.StringToHGlobalAnsi("ProcessName"));
try
{
// Inject an assembly into the Mono process.
var injectResult = Inject(monoInjector, Marshal.StringToHGlobalAnsi(@"Path\To\Your\Assembly.dll"),
Marshal.StringToHGlobalAnsi("Namespace"),
Marshal.StringToHGlobalAnsi("ClassName"),
Marshal.StringToHGlobalAnsi("MethodName"));
// Eject the previously injected assembly.
Eject(monoInjector, injectResult, Marshal.StringToHGlobalAnsi("Namespace"),
Marshal.StringToHGlobalAnsi("ClassName"),
Marshal.StringToHGlobalAnsi("MethodName"));
}
finally
{
// Ensure the Mono injector is properly closed.
CloseMonoInjector(monoInjector);
}
return;
// Custom logger definition.
static void DelegateDefinition(IntPtr nint, int arg2) => Console.WriteLine($"{arg2}: {Marshal.PtrToStringAnsi(nint)}");
}
// P/Invoke declarations for interacting with MonoNativeInjector.dll.
[DllImport(@"Path\To\MonoNativeInjector.dll", EntryPoint = "SetLogger")]
static extern void SetLogger(IntPtr logger);
[DllImport(@"Path\To\MonoNativeInjector.dll", EntryPoint = "OpenMonoInjector")]
static extern IntPtr OpenMonoInjector(IntPtr processName);
[DllImport(@"Path\To\MonoNativeInjector.dll", EntryPoint = "Inject")]
static extern IntPtr Inject(IntPtr instanceOfMonoInjector, IntPtr assemblyPathPtr, IntPtr namespacePtr,
IntPtr classNamePtr, IntPtr methodNamePtr);
[DllImport(@"Path\To\MonoNativeInjector.dll", EntryPoint = "Eject")]
static extern void Eject(IntPtr instanceOfMonoInjector, IntPtr assemblyPtr, IntPtr namespaceName, IntPtr className, IntPtr methodName);
[DllImport(@"Path\To\MonoNativeInjector.dll", EntryPoint = "CloseMonoInjector")]
static extern void CloseMonoInjector(IntPtr instanceOfMonoInjector);
Contributions to MonoNativeInjector are always welcome. Please feel free to submit pull requests or create issues for bugs, feature requests, or any other feedback.
This project is created for educational purposes only. It is not intended to be used for creating cheats in multiplayer games or any other activities that could violate terms of service or fair play guidelines of any game or software. Use of MonoNativeInjector in such a manner is strictly against the intent of the project and the developers do not condone or support misuse of this software.
MIT License
Copyright (c) 2024 Eviraznva
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
- IMonoInjector
- Logger
- Main
- Memory
- MonoInjector
- MonoInjector32
- #ctor()
- Eject(assemblyPtr,namespaceName,className,methodName)
- ExecuteAssemblyMethod(funcAddress,attach,args)
- GetMonoAssemblyImage(monoAssembly)
- GetMonoClass(monoAssemblyImage,nameSpace,className)
- GetMonoFuncAddress(funcName)
- GetMonoMethod(monoClass,methodName)
- GetRootDomain()
- Inject(assemblyPath,namespaceName,className,methodName)
- InvokeMonoMethod(monoMethod)
- OpenMonoAssembly(assemblyPath,status)
- MonoInjector32Creator
- MonoInjector64
- #ctor()
- Eject(assemblyPtr,namespaceName,className,methodName)
- ExecuteAssemblyMethod(funcAddress,attach,args)
- GetMonoAssemblyImage(monoAssembly)
- GetMonoClass(monoAssemblyImage,nameSpace,className)
- GetMonoFuncAddress(funcName)
- GetMonoMethod(monoClass,methodName)
- GetRootDomain()
- Inject(assemblyPath,namespaceName,className,methodName)
- InvokeMonoMethod(monoMethod)
- OpenMonoAssembly(assemblyPath,status)
- MonoInjector64Creator
- MonoInjectorBase
MonoNativeInjector.Common
Defines the contract for Mono injectors, including methods for injecting and ejecting assemblies, and disposing resources.
Ejects an injected assembly from a Mono process.
Name | Type | Description |
---|---|---|
assemblyPtr | System.IntPtr | A pointer to the injected assembly. |
namespaceName | System.String | The namespace from which the assembly will be ejected. |
className | System.String | The class from which the assembly will be ejected. |
methodName | System.String | The method from which the assembly will be ejected. |
Injects an assembly into a Mono process.
A pointer indicating the result of the injection process.
Name | Type | Description |
---|---|---|
assemblyPath | System.String | The path to the assembly to be injected. |
namespaceName | System.String | The target namespace within the process. |
className | System.String | The target class within the namespace. |
methodName | System.String | The target method within the class to inject the assembly into. |
MonoNativeInjector.Misc
Provides logging functionalities for the application, allowing the logging of information, warnings, and debug messages.
Logs a debug message.
Name | Type | Description |
---|---|---|
message | System.String | The message to log. |
Logs an informational message.
Name | Type | Description |
---|---|---|
message | System.String | The message to log. |
Logs a warning message.
Name | Type | Description |
---|---|---|
message | System.String | The message to log. |
MonoNativeInjector
Closes and disposes of the MonoInjector instance.
Name | Type | Description |
---|---|---|
instanceOfMonoInjector | System.IntPtr | Handle to the instance of MonoInjector. |
Ejects an injected assembly from the specified namespace and class method within a Mono process.
Name | Type | Description |
---|---|---|
instanceOfMonoInjector | System.IntPtr | Handle to the instance of MonoInjector. |
assemblyPtr | System.IntPtr | Pointer to the assembly in memory. |
namespaceName | System.IntPtr | Pointer to the namespace in memory. |
className | System.IntPtr | Pointer to the class name in memory. |
methodName | System.IntPtr | Pointer to the method name in memory. |
Injects an assembly into the specified namespace and class method within a Mono process.
A pointer indicating the result of the injection process.
Name | Type | Description |
---|---|---|
instanceOfMonoInjector | System.IntPtr | Handle to the instance of MonoInjector. |
assemblyPathPtr | System.IntPtr | Pointer to the assembly path in memory. |
namespacePtr | System.IntPtr | Pointer to the namespace in memory. |
classNamePtr | System.IntPtr | Pointer to the class name in memory. |
methodNamePtr | System.IntPtr | Pointer to the method name in memory. |
Opens an instance of MonoInjector based on the process name provided. It tries to find the process up to 5 times before failing.
A handle to the instance of MonoInjector.
Name | Type | Description |
---|---|---|
processNamePtr | System.IntPtr | Pointer to the process name in memory. |
Name | Description |
---|---|
System.Exception | Thrown when an instance of MonoInjector cannot be created. |
Sets the logger function pointer to be used for logging within the application.
Name | Type | Description |
---|---|---|
logger | System.IntPtr | A pointer to the managed delegate for logging. |
MonoNativeInjector.Misc
Manages memory operations (read, write, allocate, free) in a specified process.
Initializes a new instance of the Memory class for a given process.
Name | Type | Description |
---|---|---|
processId | System.Int32 | The ID of the process to operate on. |
Name | Description |
---|---|
System.AccessViolationException | Thrown when access to the process is denied. |
Gets the handle to the process being operated on.
Allocates memory in the target process.
The address of the allocated memory.
Name | Type | Description |
---|---|---|
size | System.Int32 | The size of the memory to allocate. |
Frees previously allocated memory in the target process.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The address of the memory to free. |
Invokes a thread at a specified address within the target process.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The address of the code to execute in the new thread. |
Reads a structure of type T from a specified memory address.
The structure read from the address.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The memory address to read from. |
Reads a 16-bit integer from a specified memory address.
The 16-bit integer read from the address.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The memory address to read from. |
Reads a 32-bit integer from a specified memory address.
The 32-bit integer read from the address.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The memory address to read from. |
Reads a 64-bit integer from a specified memory address.
The 64-bit integer read from the address.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The memory address to read from. |
Reads a pointer from a specified memory address, considering the architecture (32/64 bit).
The pointer read from the address.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The memory address to read from. |
is64 | System.Boolean | Indicates whether to read as 64-bit pointer. |
Reads a string from a specified memory address.
The string read from the address.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The memory address to read from. |
encoding | System.Text.Encoding | The encoding of the string. |
Writes bytes to a specified memory address.
Name | Type | Description |
---|---|---|
address | System.IntPtr | The memory address to write to. |
buffer | System.Collections.Generic.IEnumerable{System.Byte} | The bytes to write. |
MonoNativeInjector.Abstractions
When implemented in a derived class, creates and returns a new instance of a class implementing the IMonoInjector interface.
An IMonoInjector instance created by the derived class.
This method has no parameters.
Creates and returns an instance of a class implementing the IMonoInjector interface using the abstract CreateInjector method defined in derived classes.
An instance of a class implementing the IMonoInjector interface.
This method has no parameters.
MonoNativeInjector.MonoInjectors
A Mono injector implementation for 32-bit processes.
A Mono injector implementation for 32-bit processes.
This constructor has no parameters.
Ejects an assembly from the Mono process.
Name | Type | Description |
---|---|---|
assemblyPtr | System.IntPtr | A pointer to the previously injected assembly. |
namespaceName | System.String | The namespace of the class. |
className | System.String | The class of the method. |
methodName | System.String | The method to invoke for ejection. |
Currently implements the same logic as injection for demonstration. Actual ejection logic may differ based on the Mono runtime's capabilities.
Executes a Mono assembly method.
The result of the method execution.
Name | Type | Description |
---|---|---|
funcAddress | System.IntPtr | The function address in the Mono runtime. |
attach | System.Boolean | Whether to attach the executing thread to the Mono runtime. |
args | System.IntPtr[] | Arguments for the function call. |
Gets the Mono image for an assembly.
A pointer to the Mono image.
Name | Type | Description |
---|---|---|
monoAssembly | System.IntPtr | A pointer to the Mono assembly. |
Retrieves a Mono class from an assembly image.
A pointer to the Mono class.
Name | Type | Description |
---|---|---|
monoAssemblyImage | System.IntPtr | A pointer to the Mono image. |
nameSpace | System.String | The namespace of the class. |
className | System.String | The name of the class. |
Gets the address of a function in the Mono runtime by name.
The address of the function, or Zero if not found.
Name | Type | Description |
---|---|---|
funcName | System.String | The name of the function. |
Retrieves a Mono method from a class.
A pointer to the Mono method.
Name | Type | Description |
---|---|---|
monoClass | System.IntPtr | A pointer to the Mono class. |
methodName | System.String | The name of the method. |
Retrieves the Mono root domain for the process.
A pointer to the Mono root domain.
This method has no parameters.
Injects an assembly into the Mono process.
A pointer to the injected assembly.
Name | Type | Description |
---|---|---|
assemblyPath | System.String | The file path of the assembly to inject. |
namespaceName | System.String | The namespace containing the class. |
className | System.String | The class containing the method to invoke. |
methodName | System.String | The method to invoke upon injection. |
Name | Description |
---|---|
System.InvalidOperationException | Thrown when the root domain or assembly image cannot be obtained, or class or method lookup fails. |
Invokes a Mono method.
Name | Type | Description |
---|---|---|
monoMethod | System.IntPtr | A pointer to the Mono method. |
Opens a Mono assembly from the specified path.
A pointer to the opened Mono assembly.
Name | Type | Description |
---|---|---|
assemblyPath | System.String | The file path of the assembly. |
status | System.Int32@ | The status code returned by the Mono runtime. |
MonoNativeInjector.MonoInjectorCreators
A factory class for creating instances of MonoInjector32, tailored for 32-bit Mono processes.
A factory class for creating instances of MonoInjector32, tailored for 32-bit Mono processes.
This constructor has no parameters.
Creates an instance of MonoInjector32, providing the necessary implementation of IMonoInjector for 32-bit processes.
An instance of MonoInjector32 specialized for injecting into 32-bit Mono processes.
This method has no parameters.
MonoNativeInjector.MonoInjectors
A Mono injector implementation for 64-bit processes.
A Mono injector implementation for 64-bit processes.
This constructor has no parameters.
Ejects an assembly from the Mono process, adapted for 64-bit processes.
Name | Type | Description |
---|---|---|
assemblyPtr | System.IntPtr | A pointer to the previously injected assembly. |
namespaceName | System.String | The namespace of the class. |
className | System.String | The class of the method. |
methodName | System.String | The method to invoke for ejection. |
Implements the same logic as injection for demonstration. Actual ejection logic may vary.
Executes a method in the Mono runtime using the specified function address and arguments.
The result of the method execution.
Name | Type | Description |
---|---|---|
funcAddress | System.IntPtr | The function address in the Mono runtime. |
attach | System.Boolean | Whether to attach the executing thread to the Mono runtime. |
args | System.IntPtr[] | Arguments for the function call. |
Gets the Mono image for an assembly, which contains metadata and code.
A pointer to the Mono image.
Name | Type | Description |
---|---|---|
monoAssembly | System.IntPtr | A pointer to the Mono assembly. |
Retrieves a Mono class from an assembly image based on the namespace and class name.
A pointer to the Mono class.
Name | Type | Description |
---|---|---|
monoAssemblyImage | System.IntPtr | A pointer to the Mono image. |
nameSpace | System.String | The namespace of the class. |
className | System.String | The name of the class. |
Resolves the address of a function in the Mono runtime by its name.
The address of the function if found; otherwise, Zero.
Name | Type | Description |
---|---|---|
funcName | System.String | The name of the function. |
Retrieves a Mono method from a class based on the method name.
A pointer to the Mono method.
Name | Type | Description |
---|---|---|
monoClass | System.IntPtr | A pointer to the Mono class. |
methodName | System.String | The name of the method. |
Retrieves the root domain of the Mono runtime, necessary for various Mono API calls.
A pointer to the Mono root domain.
This method has no parameters.
Injects an assembly into the Mono process, specifically designed for 64-bit architectures.
A pointer to the injected assembly.
Name | Type | Description |
---|---|---|
assemblyPath | System.String | The file path of the assembly to inject. |
namespaceName | System.String | The namespace containing the class. |
className | System.String | The class containing the method to invoke. |
methodName | System.String | The method to invoke upon injection. |
Name | Description |
---|---|
System.InvalidOperationException | Thrown when the root domain, assembly, class, or method cannot be obtained. |
Invokes a Mono method, executing the specified logic within the Mono runtime.
Name | Type | Description |
---|---|---|
monoMethod | System.IntPtr | A pointer to the Mono method. |
Opens a Mono assembly from the specified path, necessary for the injection process.
A pointer to the opened Mono assembly.
Name | Type | Description |
---|---|---|
assemblyPath | System.String | The file path of the assembly. |
status | System.Int32@ | The status code returned by the Mono runtime after attempting to open the assembly. |
MonoNativeInjector.MonoInjectorCreators
A factory class for creating instances of MonoInjector64, specifically designed for 64-bit Mono processes.
A factory class for creating instances of MonoInjector64, specifically designed for 64-bit Mono processes.
This constructor has no parameters.
Creates an instance of MonoInjector64, providing a concrete implementation of IMonoInjector optimized for 64-bit processes.
An instance of MonoInjector64 that is specialized for injecting assemblies into 64-bit Mono processes.
This method has no parameters.
MonoNativeInjector.Abstractions
Provides a base implementation for Mono injectors, handling common setup and disposal functionalities.
Initializes a new instance of the MonoInjectorBase class.
Name | Type | Description |
---|---|---|
gameProcess | System.Diagnostics.Process | The target game process for injection. |
Waits for the Mono module to be loaded into the game process, essential for the injection process.
This method has no parameters.
Name | Description |
---|---|
System.InvalidOperationException | Thrown if the Mono module cannot be found after several attempts. |