Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
- using System.IO;
- using System.Runtime.InteropServices;
- /// <summary>
- /// Dll Injector
- /// Made by BahNahNah
- /// uid=2388291
- /// </summary>
- public class DllInjector
- {
- /// <summary>
- /// Gets the last error
- /// </summary>
- /// <returns></returns>
- public static string GetLastError()
- {
- return new Win32Exception(Marshal.GetLastWin32Error()).Message;
- }
- /// <summary>
- /// Injects a dll into the target process
- /// </summary>
- /// <param name="TargetProcess">Process to inject into</param>
- /// <param name="dll">Path to dll</param>
- /// <returns>Success of injection</returns>
- public static bool Inject(Process TargetProcess, string dll)
- {
- if (TargetProcess == null) throw new ArgumentNullException("TargetProcess");
- if (TargetProcess.HasExited) throw new ArgumentException("Process must be running", "TargetProcess");
- return Inject(TargetProcess.Id, dll);
- }
- /// <summary>
- /// Injects a dll into the target process
- /// </summary>
- /// <param name="pID">Process ID of target process</param>
- /// <param name="DllPath">Path to dll</param>
- /// <returns>Success of injection</returns>
- public static bool Inject(int pID, string DllPath)
- {
- IntPtr Handle = OpenProcess( 0x8 | 0x2 | 0x400 | 0x10 | 0x20, false, pID);
- if (Handle == IntPtr.Zero) throw new ArgumentException("Invalid process id", "pID");
- return Inject(Handle, DllPath) != IntPtr.Zero;
- }
- /// <summary>
- /// Injects a dll into the target process
- /// </summary>
- /// <param name="Handle">Handle of target process</param>
- /// <param name="DllPath">Path to dll</param>
- /// <returns>Handle of DLL thread or IntPtr.Zero on fail</returns>
- public static IntPtr Inject(IntPtr Handle, string DllPath)
- {
- if (Handle == IntPtr.Zero) throw new ArgumentNullException("Handle");
- if (!File.Exists(DllPath)) throw new ArgumentException("Must point to a valid file", "DllPath");
- string FullDllPath = Path.GetFullPath(DllPath);
- IntPtr vAlloc = VirtualAllocEx(Handle, 0, FullDllPath.Length + 1, 0x1000, 0x40);
- if(vAlloc == IntPtr.Zero)
- {
- CloseHandle(Handle);
- return IntPtr.Zero;
- }
- if(WriteProcessMemory(Handle, vAlloc, FullDllPath, FullDllPath.Length, 0) == 0)
- {
- CloseHandle(Handle);
- return IntPtr.Zero;
- }
- IntPtr hKernel32 = GetModuleHandle("kernel32.dll");
- IntPtr hLoadLibrary = GetProcAddress(hKernel32, "LoadLibraryA");
- if(hLoadLibrary == IntPtr.Zero)
- {
- CloseHandle(Handle);
- return IntPtr.Zero;
- }
- IntPtr hThread = CreateRemoteThread(Handle, 0, 0, hLoadLibrary, vAlloc, 0, 0);
- CloseHandle(Handle);
- return hThread;
- }
- #region " WinApi "
- [DllImport("kernel32.dll", SetLastError=true)]
- private static extern IntPtr VirtualAllocEx(IntPtr hProcess, int lpAddress, int dwSize, uint flAllocationType, uint flProtect);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern uint WaitForSingleObject(IntPtr handle, int timeout);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern bool CloseHandle(IntPtr handle);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern int WriteProcessMemory(IntPtr handle, IntPtr address, string buffer, int blength, int readwrite);
- [DllImport("kernel32.dll", SetLastError=true)]
- private static extern IntPtr GetModuleHandle(string name);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern IntPtr GetProcAddress(IntPtr mHandle, string fname);
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern IntPtr CreateRemoteThread(IntPtr pHandle, int att_0, int stacksize_0, IntPtr callingFunction, IntPtr paramiters, uint createFlags_0, int tID);
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement