using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class ProcessInjector
{
// Windows API fonksiyon tanımları (P/Invoke)
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr GetModuleHandle(string lpModuleName);
// ... Diğer gerekli Windows API tanımları ...
public static void InjectDLL(int processId, string dllPath)
{
IntPtr processHandle = OpenProcess(/* ... Gerekli Erişim Hakları ... */, false, processId);
if (processHandle == IntPtr.Zero)
{
Console.WriteLine("İşlem handılı alınamadı!");
return;
}
IntPtr loadLibraryAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryW"); // LoadLibraryW fonksiyonunun adresi
if (loadLibraryAddress == IntPtr.Zero)
{
Console.WriteLine("LoadLibraryW adresi alınamadı!");
return;
}
IntPtr dllPathAddress = VirtualAllocEx(processHandle, IntPtr.Zero, (uint)((dllPath.Length + 1) * Marshal.SizeOf(typeof(char))), /* ... Bellek Ayırma Tipleri ... */, /* ... Bellek Koruma Ayarları ... */);
if (dllPathAddress == IntPtr.Zero)
{
Console.WriteLine("Bellek ayrılamadı!");
return;
}
byte[] dllPathBytes = System.Text.Encoding.Unicode.GetBytes(dllPath + "\0"); // DLL yolunu byte dizisine dönüştür
UIntPtr bytesWritten;
if (!WriteProcessMemory(processHandle, dllPathAddress, dllPathBytes, (uint)dllPathBytes.Length, out bytesWritten))
{
Console.WriteLine("Belleğe yazılamadı!");
return;
}
IntPtr remoteThreadHandle = CreateRemoteThread(processHandle, IntPtr.Zero, 0, loadLibraryAddress, dllPathAddress, 0, IntPtr.Zero);
if (remoteThreadHandle == IntPtr.Zero)
{
Console.WriteLine("Uzak iş parçacığı oluşturulamadı!");
return;
}
// ... Handle'ları kapatma, hata kontrolü vb. ...
Console.WriteLine("DLL enjekte edildi!");
}
public static void Main(string[] args)
{
int targetProcessId = /* ... Hedef İşlem PID'si ... */;
string dllFilePath = /* ... Enjekte Edilecek DLL Dosyası Yolu ... */;
InjectDLL(targetProcessId, dllFilePath);
}
}