Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace PlebSkylinesLauncher.Helpers
  11. {
  12.     public class DllInjector
  13.     {
  14.         private static readonly IntPtr IntptrZero = (IntPtr)0;
  15.  
  16.         [DllImport("kernel32.dll", SetLastError = true)]
  17.         private static extern IntPtr OpenProcess(uint dwDesiredAccess, int bInheritHandle, uint dwProcessId);
  18.  
  19.         [DllImport("kernel32.dll", SetLastError = true)]
  20.         private static extern int CloseHandle(IntPtr hObject);
  21.  
  22.         [DllImport("kernel32.dll", SetLastError = true)]
  23.         private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
  24.  
  25.         [DllImport("kernel32.dll", SetLastError = true)]
  26.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  27.  
  28.         [DllImport("kernel32.dll", SetLastError = true)]
  29.         private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, uint flAllocationType, uint flProtect);
  30.  
  31.         [DllImport("kernel32.dll", SetLastError = true)]
  32.         private static extern int WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, int lpNumberOfBytesWritten);
  33.  
  34.         [DllImport("kernel32.dll", SetLastError = true)]
  35.         private static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttribute, IntPtr dwStackSize, IntPtr lpStartAddress,
  36.             IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
  37.  
  38.         public static DllInjectionResult Inject(Process process, string dllPath)
  39.         {
  40.             if (!File.Exists(dllPath))
  41.             {
  42.                 return DllInjectionResult.DllNotFound;
  43.             }
  44.  
  45.             if (!PerformInjection((uint)process.Id, dllPath))
  46.             {
  47.                 return DllInjectionResult.InjectionFailed;
  48.             }
  49.  
  50.             return DllInjectionResult.Success;
  51.         }
  52.  
  53.         private static bool PerformInjection(uint targetProcess, string dllPath)
  54.         {
  55.             var hndProc = OpenProcess(0x2 | 0x8 | 0x10 | 0x20 | 0x400, 1, targetProcess);
  56.  
  57.             if (hndProc == IntptrZero)
  58.             {
  59.                 return false;
  60.             }
  61.  
  62.             var lpLlAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  63.  
  64.             if (lpLlAddress == IntptrZero)
  65.             {
  66.                 return false;
  67.             }
  68.  
  69.             var lpAddress = VirtualAllocEx(hndProc, (IntPtr)null, (IntPtr)dllPath.Length, (0x1000 | 0x2000), 0X40);
  70.  
  71.             if (lpAddress == IntptrZero)
  72.             {
  73.                 return false;
  74.             }
  75.  
  76.             var bytes = Encoding.ASCII.GetBytes(dllPath);
  77.  
  78.             if (WriteProcessMemory(hndProc, lpAddress, bytes, (uint)bytes.Length, 0) == 0)
  79.             {
  80.                 return false;
  81.             }
  82.  
  83.             if (CreateRemoteThread(hndProc, (IntPtr)null, IntptrZero, lpLlAddress, lpAddress, 0, (IntPtr)null) == IntptrZero)
  84.             {
  85.                 return false;
  86.             }
  87.  
  88.             CloseHandle(hndProc);
  89.  
  90.             return true;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement