Skip to content

eviraznva/MonoNativeInjector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MonoNativeInjector

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.

Features

  • 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.

Requirements

  • 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.

Getting Started

To start using MonoNativeInjector, ensure you have cloned this repository and built MonoNativeInjector.dll according to the instructions provided.

Build

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.

Example Usage

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);

Contributing

Contributions to MonoNativeInjector are always welcome. Please feel free to submit pull requests or create issues for bugs, feature requests, or any other feedback.

Disclaimer

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.

License

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.

Documentation

MonoNativeInjector

IMonoInjector type

Namespace

MonoNativeInjector.Common

Summary

Defines the contract for Mono injectors, including methods for injecting and ejecting assemblies, and disposing resources.

Eject(assemblyPtr,namespaceName,className,methodName) method

Summary

Ejects an injected assembly from a Mono process.

Parameters
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.

Inject(assemblyPath,namespaceName,className,methodName) method

Summary

Injects an assembly into a Mono process.

Returns

A pointer indicating the result of the injection process.

Parameters
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.

Logger type

Namespace

MonoNativeInjector.Misc

Summary

Provides logging functionalities for the application, allowing the logging of information, warnings, and debug messages.

LogDebug(message) method

Summary

Logs a debug message.

Parameters
Name Type Description
message System.String The message to log.

LogInfo(message) method

Summary

Logs an informational message.

Parameters
Name Type Description
message System.String The message to log.

LogWarning(message) method

Summary

Logs a warning message.

Parameters
Name Type Description
message System.String The message to log.

Main type

Namespace

MonoNativeInjector

CloseMonoInjector(instanceOfMonoInjector) method

Summary

Closes and disposes of the MonoInjector instance.

Parameters
Name Type Description
instanceOfMonoInjector System.IntPtr Handle to the instance of MonoInjector.

Eject(instanceOfMonoInjector,assemblyPtr,namespaceName,className,methodName) method

Summary

Ejects an injected assembly from the specified namespace and class method within a Mono process.

Parameters
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.

Inject(instanceOfMonoInjector,assemblyPathPtr,namespacePtr,classNamePtr,methodNamePtr) method

Summary

Injects an assembly into the specified namespace and class method within a Mono process.

Returns

A pointer indicating the result of the injection process.

Parameters
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.

OpenMonoInjector(processNamePtr) method

Summary

Opens an instance of MonoInjector based on the process name provided. It tries to find the process up to 5 times before failing.

Returns

A handle to the instance of MonoInjector.

Parameters
Name Type Description
processNamePtr System.IntPtr Pointer to the process name in memory.
Exceptions
Name Description
System.Exception Thrown when an instance of MonoInjector cannot be created.

SetLogger(logger) method

Summary

Sets the logger function pointer to be used for logging within the application.

Parameters
Name Type Description
logger System.IntPtr A pointer to the managed delegate for logging.

Memory type

Namespace

MonoNativeInjector.Misc

Summary

Manages memory operations (read, write, allocate, free) in a specified process.

#ctor(processId) constructor

Summary

Initializes a new instance of the Memory class for a given process.

Parameters
Name Type Description
processId System.Int32 The ID of the process to operate on.
Exceptions
Name Description
System.AccessViolationException Thrown when access to the process is denied.

ProcessHandle property

Summary

Gets the handle to the process being operated on.

AllocateMemory(size) method

Summary

Allocates memory in the target process.

Returns

The address of the allocated memory.

Parameters
Name Type Description
size System.Int32 The size of the memory to allocate.

FreeMemory(address) method

Summary

Frees previously allocated memory in the target process.

Parameters
Name Type Description
address System.IntPtr The address of the memory to free.

InvokeRemoteThread(address) method

Summary

Invokes a thread at a specified address within the target process.

Parameters
Name Type Description
address System.IntPtr The address of the code to execute in the new thread.

ReadAndCastToStruct``1(address) method

Summary

Reads a structure of type T from a specified memory address.

Returns

The structure read from the address.

Parameters
Name Type Description
address System.IntPtr The memory address to read from.

ReadInt16(address) method

Summary

Reads a 16-bit integer from a specified memory address.

Returns

The 16-bit integer read from the address.

Parameters
Name Type Description
address System.IntPtr The memory address to read from.

ReadInt32(address) method

Summary

Reads a 32-bit integer from a specified memory address.

Returns

The 32-bit integer read from the address.

Parameters
Name Type Description
address System.IntPtr The memory address to read from.

ReadInt64(address) method

Summary

Reads a 64-bit integer from a specified memory address.

Returns

The 64-bit integer read from the address.

Parameters
Name Type Description
address System.IntPtr The memory address to read from.

ReadIntPtr(address,is64) method

Summary

Reads a pointer from a specified memory address, considering the architecture (32/64 bit).

Returns

The pointer read from the address.

Parameters
Name Type Description
address System.IntPtr The memory address to read from.
is64 System.Boolean Indicates whether to read as 64-bit pointer.

ReadString(address,encoding) method

Summary

Reads a string from a specified memory address.

Returns

The string read from the address.

Parameters
Name Type Description
address System.IntPtr The memory address to read from.
encoding System.Text.Encoding The encoding of the string.

WriteBytes(address,buffer) method

Summary

Writes bytes to a specified memory address.

Parameters
Name Type Description
address System.IntPtr The memory address to write to.
buffer System.Collections.Generic.IEnumerable{System.Byte} The bytes to write.

MonoInjector type

Namespace

MonoNativeInjector.Abstractions

CreateInjector() method

Summary

When implemented in a derived class, creates and returns a new instance of a class implementing the IMonoInjector interface.

Returns

An IMonoInjector instance created by the derived class.

Parameters

This method has no parameters.

CreateInstanceOfInjector() method

Summary

Creates and returns an instance of a class implementing the IMonoInjector interface using the abstract CreateInjector method defined in derived classes.

Returns

An instance of a class implementing the IMonoInjector interface.

Parameters

This method has no parameters.

MonoInjector32 type

Namespace

MonoNativeInjector.MonoInjectors

Summary

A Mono injector implementation for 32-bit processes.

#ctor() constructor

Summary

A Mono injector implementation for 32-bit processes.

Parameters

This constructor has no parameters.

Eject(assemblyPtr,namespaceName,className,methodName) method

Summary

Ejects an assembly from the Mono process.

Parameters
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.
Remarks

Currently implements the same logic as injection for demonstration. Actual ejection logic may differ based on the Mono runtime's capabilities.

ExecuteAssemblyMethod(funcAddress,attach,args) method

Summary

Executes a Mono assembly method.

Returns

The result of the method execution.

Parameters
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.

GetMonoAssemblyImage(monoAssembly) method

Summary

Gets the Mono image for an assembly.

Returns

A pointer to the Mono image.

Parameters
Name Type Description
monoAssembly System.IntPtr A pointer to the Mono assembly.

GetMonoClass(monoAssemblyImage,nameSpace,className) method

Summary

Retrieves a Mono class from an assembly image.

Returns

A pointer to the Mono class.

Parameters
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.

GetMonoFuncAddress(funcName) method

Summary

Gets the address of a function in the Mono runtime by name.

Returns

The address of the function, or Zero if not found.

Parameters
Name Type Description
funcName System.String The name of the function.

GetMonoMethod(monoClass,methodName) method

Summary

Retrieves a Mono method from a class.

Returns

A pointer to the Mono method.

Parameters
Name Type Description
monoClass System.IntPtr A pointer to the Mono class.
methodName System.String The name of the method.

GetRootDomain() method

Summary

Retrieves the Mono root domain for the process.

Returns

A pointer to the Mono root domain.

Parameters

This method has no parameters.

Inject(assemblyPath,namespaceName,className,methodName) method

Summary

Injects an assembly into the Mono process.

Returns

A pointer to the injected assembly.

Parameters
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.
Exceptions
Name Description
System.InvalidOperationException Thrown when the root domain or assembly image cannot be obtained, or class or method lookup fails.

InvokeMonoMethod(monoMethod) method

Summary

Invokes a Mono method.

Parameters
Name Type Description
monoMethod System.IntPtr A pointer to the Mono method.

OpenMonoAssembly(assemblyPath,status) method

Summary

Opens a Mono assembly from the specified path.

Returns

A pointer to the opened Mono assembly.

Parameters
Name Type Description
assemblyPath System.String The file path of the assembly.
status System.Int32@ The status code returned by the Mono runtime.

MonoInjector32Creator type

Namespace

MonoNativeInjector.MonoInjectorCreators

Summary

A factory class for creating instances of MonoInjector32, tailored for 32-bit Mono processes.

#ctor() constructor

Summary

A factory class for creating instances of MonoInjector32, tailored for 32-bit Mono processes.

Parameters

This constructor has no parameters.

CreateInjector() method

Summary

Creates an instance of MonoInjector32, providing the necessary implementation of IMonoInjector for 32-bit processes.

Returns

An instance of MonoInjector32 specialized for injecting into 32-bit Mono processes.

Parameters

This method has no parameters.

MonoInjector64 type

Namespace

MonoNativeInjector.MonoInjectors

Summary

A Mono injector implementation for 64-bit processes.

#ctor() constructor

Summary

A Mono injector implementation for 64-bit processes.

Parameters

This constructor has no parameters.

Eject(assemblyPtr,namespaceName,className,methodName) method

Summary

Ejects an assembly from the Mono process, adapted for 64-bit processes.

Parameters
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.
Remarks

Implements the same logic as injection for demonstration. Actual ejection logic may vary.

ExecuteAssemblyMethod(funcAddress,attach,args) method

Summary

Executes a method in the Mono runtime using the specified function address and arguments.

Returns

The result of the method execution.

Parameters
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.

GetMonoAssemblyImage(monoAssembly) method

Summary

Gets the Mono image for an assembly, which contains metadata and code.

Returns

A pointer to the Mono image.

Parameters
Name Type Description
monoAssembly System.IntPtr A pointer to the Mono assembly.

GetMonoClass(monoAssemblyImage,nameSpace,className) method

Summary

Retrieves a Mono class from an assembly image based on the namespace and class name.

Returns

A pointer to the Mono class.

Parameters
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.

GetMonoFuncAddress(funcName) method

Summary

Resolves the address of a function in the Mono runtime by its name.

Returns

The address of the function if found; otherwise, Zero.

Parameters
Name Type Description
funcName System.String The name of the function.

GetMonoMethod(monoClass,methodName) method

Summary

Retrieves a Mono method from a class based on the method name.

Returns

A pointer to the Mono method.

Parameters
Name Type Description
monoClass System.IntPtr A pointer to the Mono class.
methodName System.String The name of the method.

GetRootDomain() method

Summary

Retrieves the root domain of the Mono runtime, necessary for various Mono API calls.

Returns

A pointer to the Mono root domain.

Parameters

This method has no parameters.

Inject(assemblyPath,namespaceName,className,methodName) method

Summary

Injects an assembly into the Mono process, specifically designed for 64-bit architectures.

Returns

A pointer to the injected assembly.

Parameters
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.
Exceptions
Name Description
System.InvalidOperationException Thrown when the root domain, assembly, class, or method cannot be obtained.

InvokeMonoMethod(monoMethod) method

Summary

Invokes a Mono method, executing the specified logic within the Mono runtime.

Parameters
Name Type Description
monoMethod System.IntPtr A pointer to the Mono method.

OpenMonoAssembly(assemblyPath,status) method

Summary

Opens a Mono assembly from the specified path, necessary for the injection process.

Returns

A pointer to the opened Mono assembly.

Parameters
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.

MonoInjector64Creator type

Namespace

MonoNativeInjector.MonoInjectorCreators

Summary

A factory class for creating instances of MonoInjector64, specifically designed for 64-bit Mono processes.

#ctor() constructor

Summary

A factory class for creating instances of MonoInjector64, specifically designed for 64-bit Mono processes.

Parameters

This constructor has no parameters.

CreateInjector() method

Summary

Creates an instance of MonoInjector64, providing a concrete implementation of IMonoInjector optimized for 64-bit processes.

Returns

An instance of MonoInjector64 that is specialized for injecting assemblies into 64-bit Mono processes.

Parameters

This method has no parameters.

MonoInjectorBase type

Namespace

MonoNativeInjector.Abstractions

Summary

Provides a base implementation for Mono injectors, handling common setup and disposal functionalities.

#ctor(gameProcess) constructor

Summary

Initializes a new instance of the MonoInjectorBase class.

Parameters
Name Type Description
gameProcess System.Diagnostics.Process The target game process for injection.

WaitForMonoModule() method

Summary

Waits for the Mono module to be loaded into the game process, essential for the injection process.

Parameters

This method has no parameters.

Exceptions
Name Description
System.InvalidOperationException Thrown if the Mono module cannot be found after several attempts.