[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern int VirtualAllocEx(int hProcess, int lpAddress,
int dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, string lpBuffer, int nSize, int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
static extern int CreateRemoteThread(int hProcess,
int lpThreadAttributes, uint dwStackSize, int lpStartAddress, int lpParameter, uint dwCreationFlags, int lpThreadId);
[StructLayout(LayoutKind.Sequential)]
public struct Luid
{
public UInt32 LowPart;
public Int32 HighPart;
}
public const UInt32 SePrivilegeEnabledByDefault = 0x00000001;
public const UInt32 SePrivilegeEnabled = 0x00000002;
public const UInt32 SePrivilegeRemoved = 0x00000004;
public const UInt32 SePrivilegeUsedForAccess = 0x80000000;
[StructLayout(LayoutKind.Sequential)]
public struct TokenPrivileges
{
public UInt32 PrivilegeCount;
public Luid Luid;
public UInt32 Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct LuidAndAttributes
{
public Luid Luid;
public UInt32 Attributes;
}
public static void Inject(int pid)
{
var dllPath = "C:\aa.dll";
var p = Process.GetProcessById(pid);
var hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
int dllPathSize = dllPath.Length + 1;
int lpBaseAddress = VirtualAllocEx(hProcess, 0, dllPathSize, MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(hProcess, lpBaseAddress, dllPath, dllPathSize, 0);
int module = GetModuleHandle("Kernel32.dll");
int LoadLibraryAddress = GetProcAddress(module, "LoadLibraryA");
var rt = CreateRemoteThread(hProcess, 0, 0, LoadLibraryAddress, lpBaseAddress, 0, 0);
}
public static string GetAefHookPath()
{
var fn = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AefHook.bin");
if (!File.Exists(fn))
{
var bytes = AefHookCode.xSplitBySpace().Select(s => byte.Parse(s)).ToArray();
File.WriteAllBytes(fn, bytes);
}
return fn;
}