使用 C# 做一個假裝自己是 RDP Server

  • 12400
  • 0
  • 2025-11-18

看到一段有趣的  code ,就是有網路上的大大用 python 寫了一個模擬自己試 RDP Server

然後看了一下,程式碼不難,我就翻寫成 C# ,測了一下,蠻好玩的就分享一下..


 

原文程式碼: https://github.com/cheeseandcereal/fake-rdp/blob/master/fake_rdp.py

因為其實我就是無腦改寫,其中我把 port 改成 3391 

    
            var  serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 3391));
            serverSocket.Listen(5);

            while (true)
            {
                try
                {
                    var  clientSocket = serverSocket.Accept();
                    Console.WriteLine("Received connection from {0}", clientSocket.RemoteEndPoint +" @"+DateTime.Now.ToString());

                    byte[] buffer = new byte[256];
                    int bytesRead = clientSocket.Receive(buffer);

                    Console.WriteLine(System.Text.Encoding.ASCII.GetString(buffer) + " |" + DateTime.Now.ToString());
                
                     byte[] initPacket = { 0x03, 0x00, 0x00, 0x13, 0x0E, 0xD0, 0x00, 0x00, 0x12, 0x34, 0x00, 0x02, 0x09, 0x08, 0x00, 0x02, 0x00, 0x00, 0x00 };

                    clientSocket.Send(initPacket);

                    bytesRead = clientSocket.Receive(buffer);
                    Console.WriteLine(System.Text.Encoding.ASCII.GetString(buffer) + " ||" + DateTime.Now.ToString());
                    var  response = "Microsoft Terminal Services";

                    // Wait before sending a response (resulting in a client-side error)
                    Thread.Sleep(2000);
                    clientSocket.Send(Encoding.Default.GetBytes(response));

                    clientSocket.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ErrorMessage : {0}", ex.Message + " @" + DateTime.Now.ToString());
                }
            }
            
            
      

跑起來測試看看,竟然可以到輸入帳號密碼的步驟,之後才是跳出錯錯誤視窗..

-

本文原文首發於我的個人部落格:使用 C# 做一個假裝自己是 RDP Server

---

The bug existed in all possible states.
Until I ran the code.