Cleanup unused code inside launcher

This commit is contained in:
AeonLucid
2021-10-30 20:25:23 +02:00
parent 86cd7332f6
commit 341c8de5ba
6 changed files with 1 additions and 205 deletions
-11
View File
@@ -23,14 +23,6 @@ namespace Prospect.Launcher.Invoke
out ProcessInformation lpProcessInformation) => CreateProcess(lpApplicationName, lpCommandLine, IntPtr.Zero,
IntPtr.Zero, false, dwCreationFlags, IntPtr.Zero, null, ref lpStartupInfo, out lpProcessInformation);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
byte[] lpBuffer,
Int32 nSize,
out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern unsafe bool WriteProcessMemory(
IntPtr hProcess,
@@ -40,9 +32,6 @@ namespace Prospect.Launcher.Invoke
out IntPtr lpNumberOfBytesWritten
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, uint flNewProtect, out uint lpflOldProtect);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
-19
View File
@@ -1,19 +0,0 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using Prospect.Launcher.Invoke.Structs;
namespace Prospect.Launcher.Invoke
{
public class PSAPI
{
[DllImport("psapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern int EnumProcessModules(IntPtr hProcess, [Out] IntPtr lphModule, uint cb, out uint lpcbNeeded);
[DllImport("psapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern bool GetModuleInformation(IntPtr hProcess, IntPtr hModule, out ModuleInfo lpModInfo, int cb);
[DllImport("psapi.dll")]
public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize);
}
}
@@ -1,13 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Prospect.Launcher.Invoke.Structs
{
[StructLayout(LayoutKind.Sequential)]
public struct ModuleInfo
{
public IntPtr lpBaseOfDll;
public uint SizeOfImage;
public IntPtr EntryPoint;
}
}
-133
View File
@@ -1,133 +0,0 @@
using System;
using System.Buffers.Binary;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Prospect.Launcher.Invoke;
using Prospect.Launcher.Invoke.Structs;
namespace Prospect.Launcher
{
public class MemoryHelper
{
private readonly IntPtr _handle;
private readonly IntPtr _baseAddress;
private MemoryHelper(IntPtr handle, IntPtr baseAddress)
{
_handle = handle;
_baseAddress = baseAddress;
}
public void WriteU32(int offset, uint value)
{
Span<byte> data = stackalloc byte[sizeof(int)];
BinaryPrimitives.WriteUInt32LittleEndian(data, value);
Write(offset, data);
}
public void WriteASCII(int offset, string value)
{
// + 1 for NULL character
var dataCount = Encoding.ASCII.GetByteCount(value);
Span<byte> data = stackalloc byte[dataCount + 1];
Encoding.ASCII.GetBytes(value, data);
Write(offset, data);
}
public void WriteUnicode(int offset, string value)
{
// + 2 for NULL character
var dataCount = Encoding.Unicode.GetByteCount(value);
Span<byte> data = stackalloc byte[dataCount + 2];
Encoding.Unicode.GetBytes(value, data);
Write(offset, data);
}
public unsafe void Write(int offset, ReadOnlySpan<byte> data)
{
Write(_baseAddress + offset, data);
}
public unsafe void Write(IntPtr addr, ReadOnlySpan<byte> data)
{
// Modify protection.
if (!Kernel32.VirtualProtectEx(_handle, addr, data.Length, 0x40, out var oldProtect))
{
throw new Exception($"Unable to change protection {Marshal.GetLastWin32Error()}.");
}
fixed (byte* pData = data)
{
var write = Kernel32.WriteProcessMemory(_handle, addr, pData, data.Length, out _);
// Restore protection.
Kernel32.VirtualProtectEx(_handle, addr, data.Length, oldProtect, out _);
if (!write)
{
throw new Exception($"Unable to write process memory {Marshal.GetLastWin32Error()}.");
}
}
}
public byte[] Read(int offset, int count)
{
var buffer = new byte[count];
if (!Kernel32.ReadProcessMemory(_handle, _baseAddress + offset, buffer, count, out _))
{
throw new Exception("Unable to read process memory.");
}
return buffer;
}
public static MemoryHelper CreateForHandle(IntPtr handle, string gameBinary)
{
var gameBinaryName = Path.GetFileName(gameBinary);
var hMods = new IntPtr[512];
var hModsHandle = GCHandle.Alloc(hMods, GCHandleType.Pinned);
try
{
var hModsPtr = hModsHandle.AddrOfPinnedObject();
var hModsSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length));
if (PSAPI.EnumProcessModules(handle, hModsPtr, hModsSize, out var cbNeeded) == 1)
{
var moduleCount = (int)(cbNeeded / Marshal.SizeOf(typeof(IntPtr)));
for (var i = 0; i < moduleCount; i++)
{
var stringBuilder = new StringBuilder(1024);
if (PSAPI.GetModuleFileNameEx(handle, hMods[i], stringBuilder, stringBuilder.Capacity) == 0)
{
continue;
}
if (!gameBinaryName.Equals(Path.GetFileName(stringBuilder.ToString())))
{
continue;
}
if (!PSAPI.GetModuleInformation(handle, hMods[i], out var modInfo, Marshal.SizeOf<ModuleInfo>()))
{
continue;
}
return new MemoryHelper(handle, modInfo.lpBaseOfDll);
}
}
}
finally
{
hModsHandle.Free();
}
return null;
}
}
}
-28
View File
@@ -1,28 +0,0 @@
namespace Prospect.Launcher
{
public static class Patches
{
private const string DefaultPlayFabUrl = ".playfabapi.com";
public static bool ChangePlayFabUrl(MemoryHelper memory, string value)
{
if (value.Length > DefaultPlayFabUrl.Length)
{
return false;
}
var lengthWithNull = (uint)(value.Length + 1);
// Modify wide string.
memory.WriteU32(0xCA45A9 + 2, lengthWithNull * 2); // mov r8d, 20h
memory.WriteUnicode(0x4431860, value); // text "UTF-16LE", '.playfabapi.com',0
// Modify normal string.
memory.WriteU32(0x605F44 + 1, lengthWithNull); // mov edx, 10h
memory.WriteU32(0x605F86 + 2, lengthWithNull); // mov r9d, 10h
memory.WriteASCII(0x4431588, value); // '.playfabapi.com',0
return true;
}
}
}
@@ -23,7 +23,7 @@
<Content Include="Prospect.Agent.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>false</ExcludeFromSingleFile>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>