using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace ManagedClient;
public static class DllInjector
{
// Importing required Windows API functions via PInvoke
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out IntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern uint GetLastError();
// Constants for access rights and memory allocation
private const uint PROCESS_ALL_ACCESS = 0x1F0FFF;
private const uint MEM_COMMIT = 0x1000;
private const uint MEM_RESERVE = 0x2000;
private const uint PAGE_EXECUTE_READWRITE = 0x40;
public static bool InjectDll(uint targetProcessId, string dllPath)
{
if (!File.Exists(dllPath))
{
Console.WriteLine("DLL file not found: " + dllPath);
return false;
}
IntPtr hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, targetProcessId);
if (hProcess == IntPtr.Zero)
{
Console.WriteLine("Failed to open process. Error: " + GetLastError());
return false;
}
IntPtr allocMemAddress = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)dllPath.Length + 1, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (allocMemAddress == IntPtr.Zero)
{
Console.WriteLine("Failed to allocate memory in target process. Error: " + GetLastError());
CloseHandle(hProcess);
return false;
}
byte[] dllBytes = Encoding.ASCII.GetBytes(dllPath);
if (!WriteProcessMemory(hProcess, allocMemAddress, dllBytes, (uint)dllBytes.Length, out _))
{
Console.WriteLine("Failed to write to process memory. Error: " + GetLastError());
CloseHandle(hProcess);
return false;
}
IntPtr kernel32Handle = GetModuleHandle("kernel32.dll");
IntPtr loadLibraryAddr = GetProcAddress(kernel32Handle, "LoadLibraryA");
if (loadLibraryAddr == IntPtr.Zero)
{
Console.WriteLine("Failed to get address of LoadLibraryA. Error: " + GetLastError());
CloseHandle(hProcess);
return false;
}
IntPtr remoteThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLibraryAddr, allocMemAddress, 0, out _);
if (remoteThread == IntPtr.Zero)
{
Console.WriteLine("Failed to create remote thread. Error: " + GetLastError());
CloseHandle(hProcess);
return false;
}
Console.WriteLine("DLL injected successfully.");
CloseHandle(remoteThread);
CloseHandle(hProcess);
return true;
}
}