1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220:
| using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading;
namespace injector {
public partial class Form1 : Form { Button button1; TextBox Path; TextBox ProcessName;
String strDLLName; String strProcessName;
public Form1() { InitializeComponent(); }
public void InitializeComponent() { this.Size = new Size(400, 200); this.StartPosition = FormStartPosition.CenterScreen;
button1 = new Button(); button1.Size = new Size(50, 30); button1.Location = new Point(20, 20); button1.Text = "Inject";
Path = new TextBox(); Path.Size = new Size(200, 20); Path.Location = new Point(20, 50); Path.Text = "Path";
ProcessName = new TextBox(); ProcessName.Size = new Size(200, 20); ProcessName.Location = new Point(Path.Location.X, Path.Location.Y + 20); ProcessName.Text = "Process Name";
button1.Click += new EventHandler(button1_Click);
this.Controls.Add(button1); this.Controls.Add(Path); this.Controls.Add(ProcessName); }
[DllImport("kernel32")] public static extern IntPtr CreateRemoteThread( IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, UIntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId );
[DllImport("kernel32.dll")] public static extern IntPtr OpenProcess( UInt32 dwDesiredAccess, Int32 bInheritHandle, Int32 dwProcessId );
[DllImport("kernel32.dll")] public static extern Int32 CloseHandle( IntPtr hObject );
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] static extern bool VirtualFreeEx( IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType );
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)] public static extern UIntPtr GetProcAddress( IntPtr hModule, string procName );
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] static extern IntPtr VirtualAllocEx( IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect );
[DllImport("kernel32.dll")] static extern bool WriteProcessMemory( IntPtr hProcess, IntPtr lpBaseAddress, string lpBuffer, UIntPtr nSize, out IntPtr lpNumberOfBytesWritten );
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern IntPtr GetModuleHandle( string lpModuleName );
[DllImport("kernel32", SetLastError = true, ExactSpelling = true)] internal static extern Int32 WaitForSingleObject( IntPtr handle, Int32 milliseconds );
public Int32 GetProcessId(String proc) { try { Process[] ProcList; ProcList = Process.GetProcessesByName(proc); return ProcList[0].Id; } catch { MessageBox.Show("Process not found!"); return 0; } }
public void InjectDLL(IntPtr hProcess, String strDLLName) { IntPtr bytesout;
Int32 LenWrite = strDLLName.Length + 1; IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40); WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout); UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
if (bytesout == null) { MessageBox.Show("Error writing in Process Memory!"); return; } else if (Injector == null) { MessageBox.Show(" Injector Error! \n "); return; }
IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout); if (hThread == null) { MessageBox.Show(" hThread [ 1 ] Error! \n "); return; } int Result = WaitForSingleObject(hThread, 10 * 1000); if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF) { MessageBox.Show(" hThread [ 2 ] Error! \n "); if (hThread != null) { CloseHandle(hThread); } return; } Thread.Sleep(1000); VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000); if (hThread != null) { CloseHandle(hThread); } return; }
private void button1_Click(object sender, EventArgs e) { strDLLName = Path.Text; strProcessName = ProcessName.Text;
Int32 ProcID = GetProcessId(strProcessName); if (ProcID >= 0) { IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID); if (hProcess == null) { MessageBox.Show("Opening Process failed!"); return; } else InjectDLL(hProcess, strDLLName); } } } } |