diff --git a/Dalamud.Bootstrap/Bootstrapper.cs b/Dalamud.Bootstrap/Bootstrapper.cs index bf84f26b9..040168667 100644 --- a/Dalamud.Bootstrap/Bootstrapper.cs +++ b/Dalamud.Bootstrap/Bootstrapper.cs @@ -58,7 +58,13 @@ namespace Dalamud.Bootstrap var exePath = process.GetImageFilePath(); - var argument = ReadArgumentFromProcess(process); + var argument = process.GetGameArguments(); + + + + + + var encryptedArgument = EncryptArgument(argument.ToString()); diff --git a/Dalamud.Bootstrap/GameProcess.cs b/Dalamud.Bootstrap/Windows/GameProcess.cs similarity index 78% rename from Dalamud.Bootstrap/GameProcess.cs rename to Dalamud.Bootstrap/Windows/GameProcess.cs index 8db1ec43f..b95af385f 100644 --- a/Dalamud.Bootstrap/GameProcess.cs +++ b/Dalamud.Bootstrap/Windows/GameProcess.cs @@ -5,19 +5,9 @@ using Microsoft.Win32.SafeHandles; namespace Dalamud.Bootstrap { - internal sealed class GameProcess : IDisposable + internal sealed class GameProcess : Process { - private Process m_process; - public GameProcess(Process process) - { - m_process = process; - } - - public void Dispose() - { - m_process?.Dispose(); - m_process = null!; - } + public GameProcess(SafeProcessHandle handle) : base(handle) { } public static GameProcess Open(uint pid) { @@ -29,11 +19,11 @@ namespace Dalamud.Bootstrap | PROCESS_ACCESS_RIGHT.PROCESS_CREATE_THREAD | 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); } /// @@ -46,7 +36,7 @@ namespace Dalamud.Bootstrap /// private uint GetArgumentEncryptionKey() { - var createdTime = m_process.GetCreationTime(); + var createdTime = GetCreationTime(); // Get current tick var currentDt = DateTime.Now; @@ -67,14 +57,14 @@ namespace Dalamud.Bootstrap /// Command-line arguments that looks like this: /// /DEV.TestSID =ABCD /UserPath =C:\Examples /// - 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. 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 diff --git a/Dalamud.Bootstrap/Windows/Process.cs b/Dalamud.Bootstrap/Windows/Process.cs index 906d49098..6479a3b30 100644 --- a/Dalamud.Bootstrap/Windows/Process.cs +++ b/Dalamud.Bootstrap/Windows/Process.cs @@ -10,9 +10,9 @@ namespace Dalamud.Bootstrap /// /// A class that provides a wrapper over operations on Win32 process. /// - internal sealed class Process : IDisposable + internal class Process : IDisposable { - private SafeProcessHandle m_handle; + protected SafeProcessHandle Handle { get; set; } /// /// Creates a process object that can be used to manipulate process's internal state. @@ -20,16 +20,40 @@ namespace Dalamud.Bootstrap /// A process handle. Note that this functinon will take the ownership of the handle. public Process(SafeProcessHandle handle) { - m_handle = handle; + Handle = handle; + } + + ~Process() + { + Dispose(false); } public void Dispose() { - m_handle?.Dispose(); - m_handle = null!; + Dispose(true); + 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); @@ -48,11 +72,11 @@ namespace Dalamud.Bootstrap return new Process(handle); } - public uint GetPid() => Win32.GetProcessId(m_handle); + public uint GetPid() => Win32.GetProcessId(Handle); public void Terminate(uint exitCode = 0) { - if (!Win32.TerminateProcess(m_handle, exitCode)) + if (!Win32.TerminateProcess(Handle, exitCode)) { ProcessException.ThrowLastOsError(GetPid()); } @@ -64,15 +88,13 @@ namespace Dalamud.Bootstrap /// /// The number of bytes that is actually read. /// - private int ReadMemory(IntPtr address, Span destination) + protected int ReadMemory(IntPtr address, Span destination) { unsafe { fixed (byte* pDest = destination) { - IntPtr bytesRead; - - if (!Win32.ReadProcessMemory(m_handle, (void*)address, pDest, (IntPtr)destination.Length, &bytesRead)) + if (!Win32.ReadProcessMemory(Handle, (void*)address, pDest, (IntPtr)destination.Length, out var bytesRead)) { ProcessException.ThrowLastOsError(GetPid()); } @@ -83,7 +105,7 @@ namespace Dalamud.Bootstrap } } - private void ReadMemoryExact(IntPtr address, Span destination) + protected void ReadMemoryExact(IntPtr address, Span destination) { var totalBytesRead = 0; while (totalBytesRead < destination.Length) @@ -126,7 +148,7 @@ namespace Dalamud.Bootstrap unsafe { 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) { @@ -143,7 +165,7 @@ namespace Dalamud.Bootstrap /// /// Reads command-line arguments from the process. /// - public string[] GetArguments() + public string[] GetProcessArguments() { unsafe { @@ -168,7 +190,7 @@ namespace Dalamud.Bootstrap { 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()); } @@ -186,9 +208,7 @@ namespace Dalamud.Bootstrap fixed (byte* pCommandLine = commandLine) { - // TODO: maybe explain why we can't just call .Split(' ') - // https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw - argv = Win32.CommandLineToArgvW(pCommandLine, &argc); + argv = Win32.CommandLineToArgvW(pCommandLine, out argc); } 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. var size = buffer.Capacity; - if (!Win32.QueryFullProcessImageNameW(m_handle, 0, buffer, ref size)) + if (!Win32.QueryFullProcessImageNameW(Handle, 0, buffer, ref size)) { ProcessException.ThrowLastOsError(GetPid()); } diff --git a/Dalamud.Bootstrap/Windows/ProcessException.cs b/Dalamud.Bootstrap/Windows/ProcessException.cs index f935b3ec2..b7baccdc9 100644 --- a/Dalamud.Bootstrap/Windows/ProcessException.cs +++ b/Dalamud.Bootstrap/Windows/ProcessException.cs @@ -23,9 +23,13 @@ namespace Dalamud.Bootstrap.Windows internal static void ThrowLastOsError(uint pid) { var inner = new Win32Exception(); + throw new ProcessException(inner.ToString(), pid, inner); + } - const string message = ""; - throw new ProcessException(message, pid, inner); + internal static void ThrowLastOsError(string message) + { + var inner = new Win32Exception(); + throw new ProcessException(message, inner); } } } diff --git a/Dalamud.Bootstrap/Windows/Win32.cs b/Dalamud.Bootstrap/Windows/Win32.cs index 22275d052..bb207bba7 100644 --- a/Dalamud.Bootstrap/Windows/Win32.cs +++ b/Dalamud.Bootstrap/Windows/Win32.cs @@ -15,21 +15,21 @@ namespace Dalamud.Bootstrap.Windows public static extern bool TerminateProcess(SafeProcessHandle hProcess, uint uExitCode); [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)] [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)] [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)] public static extern void* LocalFree(void* hMem); [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)] 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)] [return: MarshalAs(UnmanagedType.Bool)] 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)] @@ -174,4 +178,43 @@ namespace Dalamud.Bootstrap.Windows PROCESS_QUERY_LIMITED_INFORMATION = 0x1000, 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 + { + + } } diff --git a/Dalamud.Testing/UnitTest1.cs b/Dalamud.Testing/BootstrapTests.cs similarity index 100% rename from Dalamud.Testing/UnitTest1.cs rename to Dalamud.Testing/BootstrapTests.cs