Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Diagnostics.Contracts;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7.  
  8. /// <summary>
  9. /// Dll Injector
  10. /// Made by BahNahNah
  11. /// uid=2388291
  12. /// </summary>
  13. public class DllInjector
  14. {
  15. /// <summary>
  16. /// Gets the last error
  17. /// </summary>
  18. /// <returns></returns>
  19. public static string GetLastError()
  20. {
  21. return new Win32Exception(Marshal.GetLastWin32Error()).Message;
  22. }
  23.  
  24. /// <summary>
  25. /// Injects a dll into the target process
  26. /// </summary>
  27. /// <param name="TargetProcess">Process to inject into</param>
  28. /// <param name="dll">Path to dll</param>
  29. /// <returns>Success of injection</returns>
  30. public static bool Inject(Process TargetProcess, string dll)
  31. {
  32. if (TargetProcess == null) throw new ArgumentNullException("TargetProcess");
  33. if (TargetProcess.HasExited) throw new ArgumentException("Process must be running", "TargetProcess");
  34. return Inject(TargetProcess.Id, dll);
  35. }
  36.  
  37. /// <summary>
  38. /// Injects a dll into the target process
  39. /// </summary>
  40. /// <param name="pID">Process ID of target process</param>
  41. /// <param name="DllPath">Path to dll</param>
  42. /// <returns>Success of injection</returns>
  43. public static bool Inject(int pID, string DllPath)
  44. {
  45. IntPtr Handle = OpenProcess( 0x8 | 0x2 | 0x400 | 0x10 | 0x20, false, pID);
  46. if (Handle == IntPtr.Zero) throw new ArgumentException("Invalid process id", "pID");
  47. return Inject(Handle, DllPath) != IntPtr.Zero;
  48. }
  49.  
  50.  
  51. /// <summary>
  52. /// Injects a dll into the target process
  53. /// </summary>
  54. /// <param name="Handle">Handle of target process</param>
  55. /// <param name="DllPath">Path to dll</param>
  56. /// <returns>Handle of DLL thread or IntPtr.Zero on fail</returns>
  57. public static IntPtr Inject(IntPtr Handle, string DllPath)
  58. {
  59. if (Handle == IntPtr.Zero) throw new ArgumentNullException("Handle");
  60. if (!File.Exists(DllPath)) throw new ArgumentException("Must point to a valid file", "DllPath");
  61. string FullDllPath = Path.GetFullPath(DllPath);
  62.  
  63. IntPtr vAlloc = VirtualAllocEx(Handle, 0, FullDllPath.Length + 1, 0x1000, 0x40);
  64. if(vAlloc == IntPtr.Zero)
  65. {
  66. CloseHandle(Handle);
  67. return IntPtr.Zero;
  68. }
  69.  
  70. if(WriteProcessMemory(Handle, vAlloc, FullDllPath, FullDllPath.Length, 0) == 0)
  71. {
  72. CloseHandle(Handle);
  73. return IntPtr.Zero;
  74. }
  75.  
  76. IntPtr hKernel32 = GetModuleHandle("kernel32.dll");
  77. IntPtr hLoadLibrary = GetProcAddress(hKernel32, "LoadLibraryA");
  78. if(hLoadLibrary == IntPtr.Zero)
  79. {
  80. CloseHandle(Handle);
  81. return IntPtr.Zero;
  82. }
  83.  
  84. IntPtr hThread = CreateRemoteThread(Handle, 0, 0, hLoadLibrary, vAlloc, 0, 0);
  85.  
  86. CloseHandle(Handle);
  87. return hThread;
  88. }
  89.  
  90. #region " WinApi "
  91.  
  92. [DllImport("kernel32.dll", SetLastError=true)]
  93. private static extern IntPtr VirtualAllocEx(IntPtr hProcess, int lpAddress, int dwSize, uint flAllocationType, uint flProtect);
  94.  
  95. [DllImport("kernel32.dll", SetLastError = true)]
  96. private static extern uint WaitForSingleObject(IntPtr handle, int timeout);
  97.  
  98. [DllImport("kernel32.dll", SetLastError = true)]
  99. private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  100.  
  101. [DllImport("kernel32.dll", SetLastError = true)]
  102. private static extern bool CloseHandle(IntPtr handle);
  103.  
  104. [DllImport("kernel32.dll", SetLastError = true)]
  105. private static extern int WriteProcessMemory(IntPtr handle, IntPtr address, string buffer, int blength, int readwrite);
  106.  
  107. [DllImport("kernel32.dll", SetLastError=true)]
  108. private static extern IntPtr GetModuleHandle(string name);
  109.  
  110. [DllImport("kernel32.dll", SetLastError = true)]
  111. private static extern IntPtr GetProcAddress(IntPtr mHandle, string fname);
  112.  
  113. [DllImport("kernel32.dll", SetLastError = true)]
  114. private static extern IntPtr CreateRemoteThread(IntPtr pHandle, int att_0, int stacksize_0, IntPtr callingFunction, IntPtr paramiters, uint createFlags_0, int tID);
  115.  
  116. #endregion
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement