This commit is contained in:
Mino 2020-04-04 14:38:03 +09:00
parent ac80895f43
commit b5f1084f73
6 changed files with 109 additions and 46 deletions

View file

@ -58,7 +58,13 @@ namespace Dalamud.Bootstrap
var exePath = process.GetImageFilePath(); var exePath = process.GetImageFilePath();
var argument = ReadArgumentFromProcess(process); var argument = process.GetGameArguments();
var encryptedArgument = EncryptArgument(argument.ToString()); var encryptedArgument = EncryptArgument(argument.ToString());

View file

@ -5,19 +5,9 @@ using Microsoft.Win32.SafeHandles;
namespace Dalamud.Bootstrap namespace Dalamud.Bootstrap
{ {
internal sealed class GameProcess : IDisposable internal sealed class GameProcess : Process
{ {
private Process m_process; public GameProcess(SafeProcessHandle handle) : base(handle) { }
public GameProcess(Process process)
{
m_process = process;
}
public void Dispose()
{
m_process?.Dispose();
m_process = null!;
}
public static GameProcess Open(uint pid) public static GameProcess Open(uint pid)
{ {
@ -29,11 +19,11 @@ namespace Dalamud.Bootstrap
| PROCESS_ACCESS_RIGHT.PROCESS_CREATE_THREAD | PROCESS_ACCESS_RIGHT.PROCESS_CREATE_THREAD
| PROCESS_ACCESS_RIGHT.PROCESS_TERMINATE; | PROCESS_ACCESS_RIGHT.PROCESS_TERMINATE;
// TODO: unfuck VM_WRITE // TODO: unfuck VM_WRITE?
var process = Process.Open(pid, access); var handle = OpenHandle(pid, access);
return new GameProcess(process); return new GameProcess(handle);
} }
/// <summary> /// <summary>
@ -46,7 +36,7 @@ namespace Dalamud.Bootstrap
/// </remarks> /// </remarks>
private uint GetArgumentEncryptionKey() private uint GetArgumentEncryptionKey()
{ {
var createdTime = m_process.GetCreationTime(); var createdTime = GetCreationTime();
// Get current tick // Get current tick
var currentDt = DateTime.Now; var currentDt = DateTime.Now;
@ -67,14 +57,14 @@ namespace Dalamud.Bootstrap
/// Command-line arguments that looks like this: /// Command-line arguments that looks like this:
/// /DEV.TestSID =ABCD /UserPath =C:\Examples /// /DEV.TestSID =ABCD /UserPath =C:\Examples
/// </returns> /// </returns>
public string ReadArguments() public string GetGameArguments()
{ {
var processArguments = m_process.GetArguments(); var processArguments = GetProcessArguments();
// arg[0] is a path to exe(normally), arg[1] is actual stuff. // arg[0] is a path to exe(normally), arg[1] is actual stuff.
if (processArguments.Length < 2) if (processArguments.Length < 2)
{ {
throw new BootstrapException($"Process id {m_process.GetPid()} have no arguments to parse."); throw new ProcessException($"There are no process arguments to parse.", GetPid());
} }
// We're interested in argument that contains session id // We're interested in argument that contains session id

View file

@ -10,9 +10,9 @@ namespace Dalamud.Bootstrap
/// <summary> /// <summary>
/// A class that provides a wrapper over operations on Win32 process. /// A class that provides a wrapper over operations on Win32 process.
/// </summary> /// </summary>
internal sealed class Process : IDisposable internal class Process : IDisposable
{ {
private SafeProcessHandle m_handle; protected SafeProcessHandle Handle { get; set; }
/// <summary> /// <summary>
/// Creates a process object that can be used to manipulate process's internal state. /// Creates a process object that can be used to manipulate process's internal state.
@ -20,16 +20,40 @@ namespace Dalamud.Bootstrap
/// <param name="handle">A process handle. Note that this functinon will take the ownership of the handle.</param> /// <param name="handle">A process handle. Note that this functinon will take the ownership of the handle.</param>
public Process(SafeProcessHandle handle) public Process(SafeProcessHandle handle)
{ {
m_handle = handle; Handle = handle;
}
~Process()
{
Dispose(false);
} }
public void Dispose() public void Dispose()
{ {
m_handle?.Dispose(); Dispose(true);
m_handle = null!; GC.SuppressFinalize(this);
} }
private static SafeProcessHandle OpenHandle(uint pid, PROCESS_ACCESS_RIGHT access) protected virtual void Dispose(bool disposing)
{
Handle?.Dispose();
Handle = null!;
}
public static Process Create(ProcessCreationOptions options)
{
//
if (!Win32.CreateProcessW())
{
ProcessException.ThrowLastOsError("Failed to create a new process.");
}
//
//
}
protected static SafeProcessHandle OpenHandle(uint pid, PROCESS_ACCESS_RIGHT access)
{ {
var handle = Win32.OpenProcess((uint)access, false, pid); var handle = Win32.OpenProcess((uint)access, false, pid);
@ -48,11 +72,11 @@ namespace Dalamud.Bootstrap
return new Process(handle); return new Process(handle);
} }
public uint GetPid() => Win32.GetProcessId(m_handle); public uint GetPid() => Win32.GetProcessId(Handle);
public void Terminate(uint exitCode = 0) public void Terminate(uint exitCode = 0)
{ {
if (!Win32.TerminateProcess(m_handle, exitCode)) if (!Win32.TerminateProcess(Handle, exitCode))
{ {
ProcessException.ThrowLastOsError(GetPid()); ProcessException.ThrowLastOsError(GetPid());
} }
@ -64,15 +88,13 @@ namespace Dalamud.Bootstrap
/// <returns> /// <returns>
/// The number of bytes that is actually read. /// The number of bytes that is actually read.
/// </returns> /// </returns>
private int ReadMemory(IntPtr address, Span<byte> destination) protected int ReadMemory(IntPtr address, Span<byte> destination)
{ {
unsafe unsafe
{ {
fixed (byte* pDest = destination) fixed (byte* pDest = destination)
{ {
IntPtr bytesRead; if (!Win32.ReadProcessMemory(Handle, (void*)address, pDest, (IntPtr)destination.Length, out var bytesRead))
if (!Win32.ReadProcessMemory(m_handle, (void*)address, pDest, (IntPtr)destination.Length, &bytesRead))
{ {
ProcessException.ThrowLastOsError(GetPid()); ProcessException.ThrowLastOsError(GetPid());
} }
@ -83,7 +105,7 @@ namespace Dalamud.Bootstrap
} }
} }
private void ReadMemoryExact(IntPtr address, Span<byte> destination) protected void ReadMemoryExact(IntPtr address, Span<byte> destination)
{ {
var totalBytesRead = 0; var totalBytesRead = 0;
while (totalBytesRead < destination.Length) while (totalBytesRead < destination.Length)
@ -126,7 +148,7 @@ namespace Dalamud.Bootstrap
unsafe unsafe
{ {
var info = new PROCESS_BASIC_INFORMATION(); var info = new PROCESS_BASIC_INFORMATION();
var status = Win32.NtQueryInformationProcess(m_handle, PROCESSINFOCLASS.ProcessBasicInformation, &info, sizeof(PROCESS_BASIC_INFORMATION), (IntPtr*)IntPtr.Zero); var status = Win32.NtQueryInformationProcess(Handle, PROCESSINFOCLASS.ProcessBasicInformation, &info, sizeof(PROCESS_BASIC_INFORMATION), (IntPtr*)IntPtr.Zero);
if (!status.Success) if (!status.Success)
{ {
@ -143,7 +165,7 @@ namespace Dalamud.Bootstrap
/// <summary> /// <summary>
/// Reads command-line arguments from the process. /// Reads command-line arguments from the process.
/// </summary> /// </summary>
public string[] GetArguments() public string[] GetProcessArguments()
{ {
unsafe unsafe
{ {
@ -168,7 +190,7 @@ namespace Dalamud.Bootstrap
{ {
FileTime creationTime, exitTime, kernelTime, userTime; FileTime creationTime, exitTime, kernelTime, userTime;
if (!Win32.GetProcessTimes(m_handle, &creationTime, &exitTime, &kernelTime, &userTime)) if (!Win32.GetProcessTimes(Handle, &creationTime, &exitTime, &kernelTime, &userTime))
{ {
ProcessException.ThrowLastOsError(GetPid()); ProcessException.ThrowLastOsError(GetPid());
} }
@ -186,9 +208,7 @@ namespace Dalamud.Bootstrap
fixed (byte* pCommandLine = commandLine) fixed (byte* pCommandLine = commandLine)
{ {
// TODO: maybe explain why we can't just call .Split(' ') argv = Win32.CommandLineToArgvW(pCommandLine, out argc);
// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw
argv = Win32.CommandLineToArgvW(pCommandLine, &argc);
} }
if (argv == null) if (argv == null)
@ -223,7 +243,7 @@ namespace Dalamud.Bootstrap
// On success, receives the number of characters written to the buffer, not including the null-terminating character. // On success, receives the number of characters written to the buffer, not including the null-terminating character.
var size = buffer.Capacity; var size = buffer.Capacity;
if (!Win32.QueryFullProcessImageNameW(m_handle, 0, buffer, ref size)) if (!Win32.QueryFullProcessImageNameW(Handle, 0, buffer, ref size))
{ {
ProcessException.ThrowLastOsError(GetPid()); ProcessException.ThrowLastOsError(GetPid());
} }

View file

@ -23,9 +23,13 @@ namespace Dalamud.Bootstrap.Windows
internal static void ThrowLastOsError(uint pid) internal static void ThrowLastOsError(uint pid)
{ {
var inner = new Win32Exception(); var inner = new Win32Exception();
throw new ProcessException(inner.ToString(), pid, inner);
}
const string message = ""; internal static void ThrowLastOsError(string message)
throw new ProcessException(message, pid, inner); {
var inner = new Win32Exception();
throw new ProcessException(message, inner);
} }
} }
} }

View file

@ -15,21 +15,21 @@ namespace Dalamud.Bootstrap.Windows
public static extern bool TerminateProcess(SafeProcessHandle hProcess, uint uExitCode); public static extern bool TerminateProcess(SafeProcessHandle hProcess, uint uExitCode);
[DllImport("ntdll", CallingConvention = CallingConvention.Winapi, SetLastError = true)] [DllImport("ntdll", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
public static extern NtStatus NtQueryInformationProcess(SafeProcessHandle processHandle, PROCESSINFOCLASS processInfoClass, void* processInformation, int processInformationLength, IntPtr* returnLength); public static extern NtStatus NtQueryInformationProcess(SafeProcessHandle processHandle, PROCESSINFOCLASS processInfoClass, void* processInformation, int processInformationLength, out IntPtr returnLength);
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)] [DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadProcessMemory(SafeProcessHandle hProcess, void* lpBaseAddress, void* lpBuffer, IntPtr nSize, IntPtr* lpNumberOfBytesRead); public static extern bool ReadProcessMemory(SafeProcessHandle hProcess, void* lpBaseAddress, void* lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)] [DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WriteProcessMemory(SafeProcessHandle hProcess, void* lpBaseAddress, void* lpBuffer, IntPtr nSize, IntPtr* lpNumberOfBytesWritten); public static extern bool WriteProcessMemory(SafeProcessHandle hProcess, void* lpBaseAddress, void* lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesWritten);
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)] [DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
public static extern void* LocalFree(void* hMem); public static extern void* LocalFree(void* hMem);
[DllImport("shell32", CallingConvention = CallingConvention.Winapi, SetLastError = true, ExactSpelling = true)] [DllImport("shell32", CallingConvention = CallingConvention.Winapi, SetLastError = true, ExactSpelling = true)]
public static extern char** CommandLineToArgvW(void* lpCmdLine, int* pNumArgs); public static extern char** CommandLineToArgvW(void* lpCmdLine, out int pNumArgs);
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi)] [DllImport("kernel32", CallingConvention = CallingConvention.Winapi)]
public static extern uint GetProcessId(SafeProcessHandle hProcess); public static extern uint GetProcessId(SafeProcessHandle hProcess);
@ -41,6 +41,10 @@ namespace Dalamud.Bootstrap.Windows
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] [DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool QueryFullProcessImageNameW(SafeProcessHandle hProcess, uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpExeName, ref int lpdwSize); public static extern bool QueryFullProcessImageNameW(SafeProcessHandle hProcess, uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpExeName, ref int lpdwSize);
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CreateProcessW(string lpApplicationName, StringBuilder lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandles, PROCESS_CREATION_FLAG dwCreationFlags, lpEnvironment, string lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
@ -174,4 +178,43 @@ namespace Dalamud.Bootstrap.Windows
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000, PROCESS_QUERY_LIMITED_INFORMATION = 0x1000,
SYNCHRONIZE = 0x100000, SYNCHRONIZE = 0x100000,
} }
[Flags]
internal enum PROCESS_CREATION_FLAG : uint
{
CREATE_BREAKAWAY_FROM_JOB = 0x01000000,
CREATE_DEFAULT_ERROR_MODE = 0x04000000,
CREATE_NEW_CONSOLE = 0x00000010,
CREATE_NEW_PROCESS_GROUP = 0x00000200,
CREATE_NO_WINDOW = 0x08000000,
CREATE_PROTECTED_PROCESS = 0x00040000,
CREATE_PRESERVE_CODE_AUTHZ_LEVEL = 0x02000000,
CREATE_SECURE_PROCESS = 0x00400000,
CREATE_SEPARATE_WOW_VDM = 0x00000800,
CREATE_SHARED_WOW_VDM = 0x00001000,
CREATE_SUSPENDED = 0x00000004,
CREATE_UNICODE_ENVIRONMENT = 0x00000400,
DEBUG_ONLY_THIS_PROCESS = 0x00000002,
DEBUG_PROCESS = 0x00000001,
DETACHED_PROCESS = 0x00000008,
EXTENDED_STARTUPINFO_PRESENT = 0x00080000,
INHERIT_PARENT_AFFINITY = 0x00010000,
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct SECURITY_ATTRIBUTES
{
public uint Length;
public SECURITY_DESCRIPTOR* SecurityDescriptor;
[MarshalAs(UnmanagedType.Bool)]
public bool InheritHandle;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SECURITY_DESCRIPTOR
{
}
} }