Skip to content

Commit

Permalink
WinAPI Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Sep 2, 2023
1 parent e63dcab commit 6d4b3cd
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/BUTR.Site.NexusMods.Server/Utils/MethodReplacer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Mono.Unix.Native;

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -44,8 +46,17 @@ public static void Replace(MethodBase source, MethodBase target)
0xC3 // ret
}).ToArray();

VirtualProtect(sourceAddress, (uint) instruction.Length, Protection.PAGE_EXECUTE_READWRITE, out var old);
var oldWindows = default(Protection);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
_ = Syscall.mprotect(sourceAddress, (ulong) instruction.Length, MmapProts.PROT_WRITE | MmapProts.PROT_READ);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
_ = VirtualProtect(sourceAddress, (uint) instruction.Length, Protection.PAGE_EXECUTE_READWRITE, out oldWindows);

Marshal.Copy(instruction, 0, sourceAddress, instruction.Length);
VirtualProtect(sourceAddress, (uint) instruction.Length, old, out _);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
_ = Syscall.mprotect(sourceAddress, (ulong) instruction.Length, MmapProts.PROT_EXEC | MmapProts.PROT_READ);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
_ = VirtualProtect(sourceAddress, (uint) instruction.Length, oldWindows, out _);
}
}

0 comments on commit 6d4b3cd

Please sign in to comment.