using System; //This is used to actually inject the specific DLL into the selected process namespace Injector { public static class Inject { #region DllImport private static class WINAPI { [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr OpenProcess( UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId); [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] public static extern Int32 CloseHandle( IntPtr hObject); [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetProcAddress( IntPtr hModule, string lpProcName); [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetModuleHandle( string lpModuleName); [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr VirtualAllocEx( IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, uint flAllocationType, uint flProtect); [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] public static extern Int32 WriteProcessMemory( IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, out IntPtr lpNumberOfBytesWritten); [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr CreateRemoteThread( IntPtr hProcess, IntPtr lpThreadAttribute, IntPtr dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId); public static class VAE_Enums { public enum AllocationType { MEM_COMMIT = 0x1000, MEM_RESERVE = 0x2000, MEM_RESET = 0x80000, } public enum ProtectionConstants { PAGE_EXECUTE = 0X10, PAGE_EXECUTE_READ = 0X20, PAGE_EXECUTE_READWRITE = 0X40, PAGE_EXECUTE_WRITECOPY = 0X80, PAGE_NOACCESS = 0X01 } } } #endregion ---DllImport--- #region DoInject public static bool DoInject(string nameOfAppIN, string nameOfDllIN, bool StartThread) { int PID = -1; foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { if (p.ProcessName == nameOfAppIN) { // found target PID = p.Id; break; } } if (PID == -1) { System.Windows.Forms.MessageBox.Show("Process Not Found #908hfg9dfh"); return false; } else if (PID == 0) { System.Windows.Forms.MessageBox.Show("don't inject PID-0 #908hf456467"); return false; } System.Diagnostics.Process targetProcess = System.Diagnostics.Process.GetProcessById(PID); try { // inject IntPtr hwnd = LoadPayload(targetProcess, nameOfDllIN, StartThread); if (hwnd == IntPtr.Zero) { return false; } } catch { //WINAPI.CloseHandle(hwnd); return false; } return true; } #endregion ---DoInject--- #region LoadPayload private static IntPtr LoadPayload(System.Diagnostics.Process targetProcessIN, string DllPathIN, bool StartThread) { // grab process and take the ability to do... // 0x2=create thread, 0x8=query info, 0x10=operation, 0x20=write, 0x400=read IntPtr hndProc = WINAPI.OpenProcess((0x2 | 0x8 | 0x10 | 0x20 | 0x400), 1, (uint)targetProcessIN.Id); if (hndProc == (IntPtr)0) { System.Windows.Forms.MessageBox.Show("Fail on - attatch to process With (admin nedded??) code#f43f34-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); throw new System.Exception("Fail on - attatch to process With (admin nedded??) code#f43f34-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } // call into kernel32 for address for LoadLibraryA IntPtr lpLLAddress = WINAPI.GetProcAddress(WINAPI.GetModuleHandle("kernel32.dll"), "LoadLibraryA"); if (lpLLAddress == (IntPtr)0) { System.Windows.Forms.MessageBox.Show("Fail on - get address of (LoadLibraryA) code#he5634-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); throw new System.Exception("Fail on - get address of (LoadLibraryA) code#he5634-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } // VirtualAlloc in target IntPtr lpAddress = WINAPI.VirtualAllocEx(hndProc, (IntPtr)null, (IntPtr)DllPathIN.Length, //520 bytes should be enough (uint)WINAPI.VAE_Enums.AllocationType.MEM_COMMIT | (uint)WINAPI.VAE_Enums.AllocationType.MEM_RESERVE, (uint)WINAPI.VAE_Enums.ProtectionConstants.PAGE_EXECUTE_READWRITE); if (lpAddress == (IntPtr)0) { if (lpAddress == (IntPtr)0) { System.Windows.Forms.MessageBox.Show("Fail on - allocate memory in target process code#64356yhrt-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); throw new System.Exception("Fail on - allocate memory in target process code#64356yhrt-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } } // copy path to Bootstrap loader (this gets injected and ran) byte[] bytes = System.Text.Encoding.ASCII.GetBytes(DllPathIN); IntPtr ipTmp = IntPtr.Zero; // write memory inside target WINAPI.WriteProcessMemory(hndProc, lpAddress, bytes, (uint)bytes.Length, out ipTmp); if (System.Runtime.InteropServices.Marshal.GetLastWin32Error() != 0) { System.Windows.Forms.MessageBox.Show("Fail on - write memory in target process code#dfsg566y-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); throw new System.Exception("Fail on - write memory in target process code#dfsg566y-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } if (StartThread) { // get a thread and point it around the Bootstarp (will auto run Bootstrap) IntPtr ipThread = WINAPI.CreateRemoteThread(hndProc, (IntPtr)null, (IntPtr)0, lpLLAddress, lpAddress, 0, (IntPtr)null); if (ipThread == (IntPtr)0) { System.Windows.Forms.MessageBox.Show("Fail on - load dll in target process code#g54wh56-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); throw new System.Exception("Fail on - load dll in target process code#g54wh56-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } } // return handel to process return hndProc; } #endregion ---LoadPayload--- } }