【C#】远控程序の初体验

发布于 2022-05-17  308 次阅读



使用的软件:

  • Visual Studio

使用的编程语言:

  • C#

Github:https://github.com/Mangofang/MonitorServer

Github:https://github.com/Mangofang/WindowsSystemMessage


示意图


1.远控端


这里新建了一个控制台应用程序来编写远控端


设置监听,后续客户端(被控端)只需要连接远控端监听端口即可

Console.WriteLine("键入本地IP地址");
IPAddress ip = IPAddress.Parse(Console.ReadLine());
Console.WriteLine("键入监听端口");
int port = int.Parse(Console.ReadLine());
TcpListener myList = new TcpListener(ip, port);
myList.Start();
Console.WriteLine("开启监听:" + myList.LocalEndpoint);

Socket s = myList.AcceptSocket();
Console.WriteLine("连接来自 " + s.RemoteEndPoint);

建立连接

Socket s = myList.AcceptSocket();
Console.WriteLine("连接来自 " + s.RemoteEndPoint);

建立流传输数据

            while (s.Connected)
            {
                try
                {
                    ASCIIEncoding asen = new ASCIIEncoding();
                    Console.WriteLine("键入指令");
                    s.Send(asen.GetBytes(Console.ReadLine()));
                    byte[] b = new byte[30000];
                    int k = s.Receive(b);
                    string a = "";
                    for (int i = 0; i < k; i++)
                    {
                        a += Convert.ToChar(b[i]);
                    }
                    Console.WriteLine(a);
                }
                catch (SocketException)
                {
                    Console.WriteLine("远程主机强迫关闭了连接");
                    s = myList.AcceptSocket();
                    Console.WriteLine("连接来自 " + s.RemoteEndPoint);
                }

            }

2.客户端(被控端)

建立与远控端连接,

注:这里IP地址需要填写远控机的公网IP地址,远控机需要提前在防火墙放行监听端口

            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect("IP地址", 端口);
            Console.WriteLine("已与远控端建立连接!");

            Stream stm = tcpClient.GetStream();

接收指令,将接收的指令写入switch中匹配对应的指令,以下代码添加了“notepad”指令,使用远控端键入“notepad”在被控机中打开记事本,当然还能继续在switch中添加其他指令

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] remessage = null;

            while (tcpClient.Connected)
            {
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                string a = "";
                for (int i = 0; i < k; i++)
                {
                    a += Convert.ToChar(bb[i]);
                }
                switch (a)
                {
                    case "notepad":
                        Process process = new Process();
                        process.StartInfo.FileName = "cmd.exe";
                        process.StartInfo.Arguments = "/c" + "start notepad";
                        process.StartInfo.UseShellExecute = false;
                        process.StartInfo.CreateNoWindow = true;
                        process.Start();
                        process.WaitForExit();
                        process.Close();

                        remessage = asen.GetBytes("Complete!");
                        break;
                    default:
                        remessage = asen.GetBytes("Can't find this command");
                        break;
                }
                stm.Write(remessage, 0, remessage.Length);