无法在端口 23 上捕获 TCP 流量 - C#

Trouble catching TCP traffic on port 23 - C#

我有一个 'honeypot' 正在用 C# 开发,它侦听一系列端口(用户输入)。这是一个大型 project/windows 服务,几乎对任何输入的端口都按预期运行,并且不会监听当前已经在监听的端口。问题是,当我使用 telnet 或 netcat 测试服务时,打开端口 23 上的连接不会被我的服务捕获,因此会建立连接。

我通过执行以下操作在防火墙中打开端口:

for (int i = 0; i < ports.Length; i++)
        {
            string arg = "advfirewall firewall add rule name=\"PeepHole Open" + "\" dir=in action=allow protocol=TCP localport=" + ports[i];
            string arg1 = "advfirewall firewall add rule name=\"PeepHole Open" + "\" dir=in action=allow protocol=UDP localport=" + ports[i];

            ProcessStartInfo procStartInfo = new ProcessStartInfo("netsh", arg);
            ProcessStartInfo procStartInfo1 = new ProcessStartInfo("netsh", arg1);
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo1.RedirectStandardOutput = true;

            procStartInfo.UseShellExecute = false;
            procStartInfo1.UseShellExecute = false;

            procStartInfo.CreateNoWindow = true;
            procStartInfo1.CreateNoWindow = true;

            Process.Start(procStartInfo1);
            Process.Start(procStartInfo);

        }

然后我开始听众:

 IPEndPoint Ep = new IPEndPoint(IPAddress.Parse("0.0.0.0"), current_port);
 //TcpListener tempListener = new TcpListener(hostIP, current_port);
 TcpListener tempListener = new TcpListener(Ep);
 TCP_Listener listen = new TCP_Listener();   //my defined tcplistener struct
 listen.listener = tempListener;             //set the Listener's TcpListener field
 listen.port = current_port;                 //set the Listener's Port field                                 
 listen.listener.Start();                    //start this particular TcpListener
 tcp_listener_list.Add(listen);              //add the struct to the list of Listeners

并通过以下方式接受 TCP 连接:

 for (int i = 0; i < tcp_listener_list.Count - 1; i++)
 {
     if (tcp_listener_list[i].listener.Pending())
     {
         TcpClient client = tcp_listener_list[i].listener.AcceptTcpClient();
         int clientPort = tcp_listener_list[i].port;
         IPEndPoint ep = client.Client.RemoteEndPoint as IPEndPoint;
         ThreadPool.QueueUserWorkItem(LogTCP, new object[] { client, clientPort, ep });
      }
  }

在 LogTCP 中,我通过以下方式关闭连接(其中客户端是一个 TcpClient 对象):

 NetworkStream networkStream = client.GetStream(); 
 networkStream.Close();
 client.Close(); //close the connection, all the data is gleaned from the attacker already

现在的问题是,当我运行 telnet 或netcat 测试端口的关闭和日志记录时,我的代码永远不会执行并建立连接,因为端口是打开的; TCP 连接永远不会是 .Pending(),如果我删除它,问题仍然存在。此外,如果我将侦听器设置为使用 IPAddress.Any 并且将我的 accept 方法重新配置为带或不带 .Pending() if 语句的 AcceptSocket,我也会遇到同样的问题。 windows 是否在某些程序的低级别上以不同方式对待某些端口?

我正在 运行从 Windows 8.1 连接一个 windows 服务,并通过 putty 上的 telnet(在安装该服务的机器上)以及 telnet 和 netcat 发送 TCP 连接在 Linux 虚拟机上。 'host' 机器上的 Telnet 客户端和 Telnet 服务器都被禁用。

我在研究过程中尝试了多种关闭套接字和连接的方法。

client.Client.Close() 产生一个 ObjectDisposedException client.Client.Shutdown(SocketShutdown.Both) 使所有以前的 'working' 端口挂起与 CLOSE_WAIT

的连接

Pending 永远不需要。只是不断接受所有听众。为每个听众启动一项任务:

while (true) {
 ProcessConnectionAsync(await listener.AcceptAsync());
}

像这样。

这是一个差一错误:tcp_listener_list.Count - 1。但是当你按照上面的解释去做时,这个错误就会消失。

关于关闭,为什么不 client.Dispose();?这是在 .NET 中进行资源管理的常规方法。没有其他要求。

我认为这里的关键是,你顺便提到你已经创建了你自己的结构。结构非常棘手。您可能认为下面的代码将以 TCP_Listener 实例结束,该实例具有您的 tempListener 和 current_port。但是,如果您设置断点,您可能会注意到其他情况。您对结构所做的每项修改实际上 returns 都是一个全新的结构,因此您不能像使用 class.

那样一次分配一个字段
TCP_Listener listen = new TCP_Listener();   //my defined tcplistener struct
listen.listener = tempListener;             //set the Listener's TcpListener field
listen.port = current_port;                 //set the Listener's Port field                                 
listen.listener.Start();  

我建议您使用 class 而不是结构。或者您将结构初始化为新的 TCP_Listener(tempListener, current_port),然后启动它。

这至少是你问题的一部分。解决该问题后,如果它仍然无法正常工作,请告诉我们,我们可以查看下一个问题。