aevitas / bluerain

BlueRain is a fully-featured, managed memory manipulation library written in C#
Apache License 2.0
36 stars 13 forks source link

`ExternalProcessMemory` throws `NullReferenceException` when `createInjector = true` #7

Closed psytrx closed 7 years ago

psytrx commented 7 years ago

MCVE:

    internal class Program
    {
        private static void Main(string[] args)
        {
            var dllPath = @"C:\mydll.dll";
            var process = Process.GetProcessesByName("csgo").Single();
            var memory = new ExternalProcessMemory(process, true);
            var module = memory.Injector.Inject(dllPath);
        }
    }

The ExternalProcessMemory constructor throws a NullReferenceException when being called with createInjector = true. This is due to the constructor in question calling its base constructor (in which the Injector gets instantiated) before the ProcessHandle gets instantiated, which is required for injection.

See https://github.com/aevitas/bluerain/blob/v2/src/BlueRain/ExternalProcessMemory.cs#L36

My suggestion is to move ProcessHandle = OpenProcess(access, false, process.Id); up into its base constructor with sane ProcessAccess defaults.

Any thoughts?

psytrx commented 7 years ago

Another approach might be removing the createInjector parameter from the constructors which don't expect ProcessAccess as an argument in order to prevent using shady defaults.

aevitas commented 7 years ago

Fixed in #8 . Moving the ProcessHandle to NativeMemory, as it's internal, which allows us to simply call OpenProcess in the native memory ctor when we're external (local procs don't require the call anyway)