mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-01-03 14:23:40 +01:00
shit codes
This commit is contained in:
parent
d663252ed1
commit
2a82a16301
10 changed files with 322 additions and 113 deletions
|
|
@ -4,6 +4,7 @@
|
|||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<nullable>enable</nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.7.82" />
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ namespace Dalamud.Injector
|
|||
|
||||
public void Launch(string exePath)
|
||||
{
|
||||
//
|
||||
|
||||
}
|
||||
|
||||
public void Relaunch(uint pid)
|
||||
|
|
|
|||
|
|
@ -1,65 +0,0 @@
|
|||
using Microsoft.Win32.SafeHandles;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Dalamud.Injector.FFI
|
||||
{
|
||||
internal static class Win32
|
||||
{
|
||||
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
|
||||
public static extern SafeProcessHandle OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);
|
||||
|
||||
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
|
||||
public static extern NtStatus NtQueryInformationProcess(/* TODO */);
|
||||
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal partial struct NtStatus
|
||||
{
|
||||
public uint Value { get; }
|
||||
}
|
||||
|
||||
internal partial struct NtStatus
|
||||
{
|
||||
public NtStatus(uint value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_SUCCESS
|
||||
/// </summary>
|
||||
public bool Success => Value <= 0x7FFFFFFF;
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_INFORMATION
|
||||
/// </summary>
|
||||
public bool Information => 0x40000000 <= Value && Value <= 0x7FFFFFFF;
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_WARNING
|
||||
/// </summary>
|
||||
public bool Warning => 0x80000000 <= Value && Value <= 0xBFFFFFFF;
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_ERROR
|
||||
/// </summary>
|
||||
public bool Error => 0xC0000000 <= Value;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct PROCESS_BASIC_INFORMATION
|
||||
{
|
||||
// https://github.com/processhacker/processhacker/blob/e43d7c0513ec5368c3309a58c9f2c2a3ca5de367/phnt/include/ntpsapi.h#L272
|
||||
public NtStatus ExitStatus;
|
||||
public IntPtr PebBaseAddress;
|
||||
public IntPtr AffinityMask;
|
||||
public IntPtr BasePriority;
|
||||
public IntPtr UniqueProcessId;
|
||||
public IntPtr InheritedFromUniqueProcessId;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
using Microsoft.Win32.SafeHandles;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Dalamud.Injector.OS
|
||||
{
|
||||
internal sealed partial class Process : IDisposable
|
||||
{
|
||||
private SafeProcessHandle m_handle;
|
||||
}
|
||||
|
||||
internal sealed partial class Process {
|
||||
internal Process(SafeProcessHandle handle)
|
||||
{
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
public static Process Open(uint pid/* and perms? maybe? */)
|
||||
{
|
||||
//
|
||||
throw new NotImplementedException("TODO");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_handle?.Dispose();
|
||||
m_handle = null!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using CommandLine;
|
||||
|
||||
|
||||
namespace Dalamud.Injector
|
||||
{
|
||||
internal static class Program
|
||||
|
|
|
|||
14
Dalamud.Injector/Windows/NtException.cs
Normal file
14
Dalamud.Injector/Windows/NtException.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
|
||||
namespace Dalamud.Injector.Windows
|
||||
{
|
||||
internal sealed class NtException : Exception
|
||||
{
|
||||
public NtStatus Status { get; }
|
||||
|
||||
public NtException(NtStatus status)
|
||||
{
|
||||
Status = status;
|
||||
}
|
||||
}
|
||||
}
|
||||
160
Dalamud.Injector/Windows/Process.cs
Normal file
160
Dalamud.Injector/Windows/Process.cs
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
using Microsoft.Win32.SafeHandles;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Dalamud.Injector.Windows
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///
|
||||
/// </remarks>
|
||||
internal sealed partial class Process : IDisposable
|
||||
{
|
||||
private SafeProcessHandle m_handle;
|
||||
}
|
||||
|
||||
internal sealed partial class Process {
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="handle">A process handle. Note that this functinon will take the ownership of the handle.</param>
|
||||
public Process(SafeProcessHandle handle)
|
||||
{
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_handle?.Dispose();
|
||||
m_handle = null!;
|
||||
}
|
||||
|
||||
public static Process Open(uint pid)
|
||||
{
|
||||
const PROCESS_ACCESS_RIGHT access = PROCESS_ACCESS_RIGHT.PROCESS_VM_OPERATION |
|
||||
PROCESS_ACCESS_RIGHT.PROCESS_VM_READ |
|
||||
PROCESS_ACCESS_RIGHT.PROCESS_VM_WRITE |
|
||||
PROCESS_ACCESS_RIGHT.PROCESS_QUERY_LIMITED_INFORMATION |
|
||||
PROCESS_ACCESS_RIGHT.PROCESS_QUERY_INFORMATION |
|
||||
PROCESS_ACCESS_RIGHT.PROCESS_CREATE_THREAD |
|
||||
PROCESS_ACCESS_RIGHT.PROCESS_TERMINATE;
|
||||
|
||||
var handle = Win32.OpenProcess((uint) access, false, pid);
|
||||
if (!handle.IsInvalid)
|
||||
{
|
||||
throw new Win32Exception();
|
||||
}
|
||||
|
||||
return new Process(handle);
|
||||
}
|
||||
|
||||
public int ReadMemory(IntPtr address, Span<byte> destination)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* pDest = destination)
|
||||
{
|
||||
IntPtr bytesRead;
|
||||
|
||||
if (!Win32.ReadProcessMemory(m_handle, (void*)address, pDest, (IntPtr)destination.Length, &bytesRead))
|
||||
{
|
||||
throw new Win32Exception();
|
||||
}
|
||||
|
||||
return (uint)bytesRead;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadMemoryExact(IntPtr address, Span<byte> destination)
|
||||
{
|
||||
var totalBytesRead = 0;
|
||||
while (totalBytesRead < destination.Length)
|
||||
{
|
||||
var bytesRead = ReadMemory(address + totalBytesRead, destination[totalBytesRead..]);
|
||||
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
throw new NotImplementedException("TODO: unexpected EOF");
|
||||
}
|
||||
|
||||
totalBytesRead += bytesRead;
|
||||
}
|
||||
}
|
||||
|
||||
public IntPtr WriteMemory(IntPtr address, ReadOnlySpan<byte> source)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* pSrc = source)
|
||||
{
|
||||
IntPtr bytesWritten;
|
||||
|
||||
if (!Win32.WriteProcessMemory(m_handle, (void*)address, pSrc, (IntPtr)source.Length, &bytesWritten))
|
||||
{
|
||||
throw new Win32Exception();
|
||||
}
|
||||
|
||||
return bytesWritten;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Terminate(uint exitCode = 0)
|
||||
{
|
||||
if (!Win32.TerminateProcess(m_handle, exitCode))
|
||||
{
|
||||
throw new Win32Exception();
|
||||
}
|
||||
}
|
||||
|
||||
private IntPtr ReadPebAddress()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var info = new PROCESS_BASIC_INFORMATION();
|
||||
IntPtr _retLen;
|
||||
|
||||
var status = Win32.NtQueryInformationProcess(m_handle, PROCESSINFOCLASS.ProcessBasicInformation, &info, sizeof(PROCESS_BASIC_INFORMATION), &_retLen);
|
||||
if (!status.Success)
|
||||
{
|
||||
// TODO
|
||||
throw new InvalidOperationException("TODO");
|
||||
}
|
||||
|
||||
return info.PebBaseAddress;
|
||||
}
|
||||
}
|
||||
|
||||
private ReadOnlySpan<char> ReadMemoryAsUtf16(IntPtr address, int length)
|
||||
{
|
||||
var buffer = new byte[length];
|
||||
ReadMemoryExact(address, buffer);
|
||||
|
||||
// TODO..
|
||||
|
||||
return MemoryMarshal.Cast<byte, char>(buffer);
|
||||
}
|
||||
|
||||
public string ReadCommandLine()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var pPeb = ReadPebAddress();
|
||||
var pPebLdr = pPeb + (int)Marshal.OffsetOf<PEB>("ProcessParameters");
|
||||
|
||||
Span<byte> procParamBuf = stackalloc byte[sizeof(RTL_USER_PROCESS_PARAMETERS)];
|
||||
ReadMemoryExact(pPebLdr, procParamBuf);
|
||||
ref var procParam = ref MemoryMarshal.AsRef<RTL_USER_PROCESS_PARAMETERS>(procParamBuf);
|
||||
|
||||
var commandLine = ReadMemoryAsUtf16(procParam.CommandLine.Buffer, procParam.CommandLine.Length);
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
145
Dalamud.Injector/Windows/Win32.cs
Normal file
145
Dalamud.Injector/Windows/Win32.cs
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
using Microsoft.Win32.SafeHandles;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Dalamud.Injector.Windows
|
||||
{
|
||||
internal static unsafe class Win32
|
||||
{
|
||||
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
|
||||
public static extern SafeProcessHandle OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);
|
||||
|
||||
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
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);
|
||||
|
||||
[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);
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal partial struct NtStatus
|
||||
{
|
||||
public uint Value { get; }
|
||||
}
|
||||
|
||||
internal partial struct NtStatus
|
||||
{
|
||||
public NtStatus(uint value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_SUCCESS
|
||||
/// </summary>
|
||||
public bool Success => Value <= 0x7FFFFFFF;
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_INFORMATION
|
||||
/// </summary>
|
||||
public bool Information => 0x40000000 <= Value && Value <= 0x7FFFFFFF;
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_WARNING
|
||||
/// </summary>
|
||||
public bool Warning => 0x80000000 <= Value && Value <= 0xBFFFFFFF;
|
||||
|
||||
/// <summary>
|
||||
/// Equivalent to NT_ERROR
|
||||
/// </summary>
|
||||
public bool Error => 0xC0000000 <= Value;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct UNICODE_STRING
|
||||
{
|
||||
public ushort Length;
|
||||
public ushort MaximumLength;
|
||||
public IntPtr Buffer;
|
||||
}
|
||||
|
||||
internal enum PROCESSINFOCLASS : uint
|
||||
{
|
||||
ProcessBasicInformation = 0,
|
||||
ProcessDebugPort = 7,
|
||||
ProcessWow64Information = 26,
|
||||
ProcessImageFileName = 27,
|
||||
ProcessBreakOnTermination = 29,
|
||||
ProcessSubsystemInformation = 75,
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct PROCESS_BASIC_INFORMATION
|
||||
{
|
||||
// https://github.com/processhacker/processhacker/blob/e43d7c0513ec5368c3309a58c9f2c2a3ca5de367/phnt/include/ntpsapi.h#L272
|
||||
public NtStatus ExitStatus;
|
||||
public IntPtr PebBaseAddress;
|
||||
public IntPtr AffinityMask;
|
||||
public IntPtr BasePriority;
|
||||
public IntPtr UniqueProcessId;
|
||||
public IntPtr InheritedFromUniqueProcessId;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct PEB
|
||||
{
|
||||
// https://github.com/processhacker/processhacker/blob/238287786b80abad647b988e60f69090cab4c8fe/phnt/include/ntpebteb.h#L57-L218
|
||||
public byte InheritedAddressSpace;
|
||||
public byte ReadImageFileExecOptions;
|
||||
public byte BeingDebugged;
|
||||
public byte BitField;
|
||||
public IntPtr Mutant;
|
||||
public IntPtr ImageBaseAddress;
|
||||
public IntPtr Ldr;
|
||||
public IntPtr ProcessParameters;
|
||||
// ..snip..
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct RTL_USER_PROCESS_PARAMETERS
|
||||
{
|
||||
|
||||
public uint MaximumLength;
|
||||
public uint LengthInitialized;
|
||||
public uint Flags;
|
||||
public uint DebugFlags;
|
||||
public IntPtr ConsoleHandle;
|
||||
public uint ConsoleFlags;
|
||||
public IntPtr StandardInput;
|
||||
public IntPtr StandardOutput;
|
||||
public IntPtr StandardError;
|
||||
public UNICODE_STRING CurrentDirectory_DosPath;
|
||||
public IntPtr CurrentDirectory_Handle;
|
||||
public UNICODE_STRING DllPath;
|
||||
public UNICODE_STRING ImagePathName;
|
||||
public UNICODE_STRING CommandLine;
|
||||
// ..snip..
|
||||
}
|
||||
|
||||
[Flags]
|
||||
internal enum PROCESS_ACCESS_RIGHT : uint
|
||||
{
|
||||
PROCESS_TERMINATE = 0x1,
|
||||
PROCESS_CREATE_THREAD = 0x2,
|
||||
PROCESS_VM_OPERATION = 0x8,
|
||||
PROCESS_VM_READ = 0x10,
|
||||
PROCESS_VM_WRITE = 0x20,
|
||||
PROCESS_DUP_HANDLE = 0x40,
|
||||
PROCESS_CREATE_PROCESS = 0x80,
|
||||
PROCESS_SET_QUOTA = 0x100,
|
||||
PROCESS_SET_INFORMATION = 0x200,
|
||||
PROCESS_QUERY_INFORMATION = 0x400,
|
||||
PROCESS_SUSPEND_RESUME = 0x800,
|
||||
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000,
|
||||
SYNCHRONIZE = 0x100000,
|
||||
}
|
||||
}
|
||||
|
|
@ -6,21 +6,12 @@
|
|||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<nullable>enable</nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="Resources\eye.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Resources\eye.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Lumina" Version="1.0.0-preview7" />
|
||||
<PackageReference Include="Serilog" Version="2.6.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Async" Version="1.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Configuration\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\lib\CoreHook\src\CoreHook.CoreLoad\CoreHook.CoreLoad.csproj" />
|
||||
<ProjectReference Include="..\lib\CoreHook\src\CoreHook\CoreHook.csproj" />
|
||||
|
|
|
|||
|
|
@ -1,9 +1,4 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Dalamud.Interface;
|
||||
using Serilog;
|
||||
using Serilog.Core;
|
||||
using CoreHook;
|
||||
|
||||
namespace Dalamud
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue