Skip to content

Commit

Permalink
Changed the module spoofer to only change file name
Browse files Browse the repository at this point in the history
  • Loading branch information
slxdy committed Jun 2, 2024
1 parent d4761e2 commit 368186d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion TweaksLauncher.API/DevTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public bool TryBuild(string outputDir)
{
var buildConfig = Config.DefaultBuildConfig ?? "Debug";

if (!Dotnet(false, "build", CsprojPath, "-v", "q", "-c", buildConfig, "-o", outputDir))
if (!Dotnet(false, "build", CsprojPath, "-v", "q", "-c", buildConfig, $"-p:OutDir=\"{outputDir}\""))
{
logger.Log($"Failed to build project at '{CsprojPath}'. Ignoring.", Color.Yellow);
return false;
Expand Down
9 changes: 9 additions & 0 deletions TweaksLauncher.API/Imports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Runtime.InteropServices;

namespace TweaksLauncher;

internal static unsafe partial class Imports
{
[LibraryImport("ntdll")]
internal static partial void RtlCopyUnicodeString(Windows.Win32.Foundation.UNICODE_STRING* destination, Windows.Win32.Foundation.UNICODE_STRING* source);
}
7 changes: 0 additions & 7 deletions TweaksLauncher.API/Launcher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using MonoMod.RuntimeDetour;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Runtime.InteropServices;

namespace TweaksLauncher;

Expand Down Expand Up @@ -74,12 +73,6 @@ private static int StartGame(string[] args)
logger.Log($"Game Exe: '{Context.GameExePath}'");
logger.Log($"Unity Version: '{Context.UnityVersion}'");

if (!NativeLibrary.TryLoad(Context.GameExePath, out _))
{
logger.Log("Failed to load the original game exe module. Make sure you're running TweaksLauncher for the right architecture (x64 or x86).", Color.Red);
return -1;
}

ProxyGenerator.Generate();

DevTools.BuildProjectsForCurrentGame();
Expand Down
30 changes: 25 additions & 5 deletions TweaksLauncher.API/ModuleSpoofer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
using Windows.Win32.Foundation;

namespace TweaksLauncher;

internal unsafe static class ModuleSpoofer
{
[NotNull] private static Dobby.Patch<LdrGetDllFullNameSig>? ldrGetDllFullName = null;

private static nint fakeExeHandle;
private static UNICODE_STRING* fakeExeName;
private static nint ourHandle;
private static bool inited;

Expand All @@ -19,18 +21,36 @@ public static void Spoof(string fakeExePath)
inited = true;

ourHandle = NativeLibrary.GetMainProgramHandle();
fakeExeHandle = NativeLibrary.Load(fakeExePath);

fakeExePath = Path.GetFullPath(fakeExePath);
var length = (ushort)Encoding.Unicode.GetByteCount(fakeExePath);

fakeExeName = (UNICODE_STRING*)Marshal.AllocHGlobal(sizeof(UNICODE_STRING));
*fakeExeName = new()
{
Length = length,
MaximumLength = length,
Buffer = (char*)Marshal.StringToHGlobalUni(fakeExePath)
};

ldrGetDllFullName = Dobby.CreatePatch<LdrGetDllFullNameSig>("ntdll", "LdrGetDllFullName", OnLdrGetDllFullName);
}

private static nint OnLdrGetDllFullName(nint hModule, nint lpFilename)
private static uint OnLdrGetDllFullName(nint hModule, UNICODE_STRING* lpFilename)
{
if (hModule == 0 || hModule == ourHandle)
return ldrGetDllFullName.Original(fakeExeHandle, lpFilename);
{
Imports.RtlCopyUnicodeString(lpFilename, fakeExeName);


if (lpFilename->MaximumLength < fakeExeName->Length + sizeof(char))
return 0xC0000023; // STATUS_BUFFER_TOO_SMALL

return 0;
}

return ldrGetDllFullName.Original(hModule, lpFilename);
}

internal delegate nint LdrGetDllFullNameSig(nint hModule, nint lpFilename);
internal delegate uint LdrGetDllFullNameSig(nint hModule, UNICODE_STRING* lpFilename);
}
3 changes: 2 additions & 1 deletion TweaksLauncher.API/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Module32Next
Module32FirstW
Module32NextW
GetModuleHandle
SetDllDirectory
SetDllDirectory
RtlCopyUnicodeString

0 comments on commit 368186d

Please sign in to comment.