Skip to content

Commit

Permalink
AssemblyInjector: Look up kernel32.dll exports in the current process…
Browse files Browse the repository at this point in the history
… as a fallback.
  • Loading branch information
alexrp committed Jan 21, 2024
1 parent 0e3a863 commit c43e3eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/injection/AssemblyInjector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Vezel.Ruptura.Injection.IO;
using Vezel.Ruptura.Injection.Threading;
using static Iced.Intel.AssemblerRegisters;
using static Windows.Win32.WindowsPInvoke;

namespace Vezel.Ruptura.Injection;

Expand Down Expand Up @@ -135,9 +136,17 @@ private unsafe void RetrieveKernel32Exports()

nuint GetExport(string name)
{
return exports?.SingleOrDefault(f => f.Name == name)?.Address is uint offset
? (nuint)k32.Address + offset
: throw new InjectionException($"Could not locate '{name}' in the target process.");
using var handle = new SafeFileHandle(k32.Handle, ownsHandle: false);

// Try to resolve the export in the remote process first. If that fails (as it does under e.g. Wine), fall
// back to the old-fashioned approach of resolving it in the current process and relying on the
// implementation detail that kernel32.dll is mapped at the same address in all processes.
fixed (char* p = name)
return exports?.SingleOrDefault(f => f.Name == name)?.Address is uint offset
? (nuint)k32.Address + offset
: GetProcAddress(handle, name) is var proc and { IsNull: false }
? (nuint)(nint)proc
: throw new InjectionException($"Could not locate '{name}' in the target process.");
}

_loadLibraryW = GetExport("LoadLibraryW");
Expand Down
1 change: 1 addition & 0 deletions src/injection/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CreateProcessW
CreateRemoteThreadEx
GetProcAddress

WIN32_ERROR

0 comments on commit c43e3eb

Please sign in to comment.