简体   繁体   English

C#-登录到防火墙的套接字

[英]C# - Socket to log on to Firewall

I wrote an app to automatically connect to our different Firewalls. 我写了一个应用程序来自动连接到我们的不同防火墙。 All of them work with the same frontend. 他们都使用相同的前端。 We telnet to the IP and they give the message LOGIN or LOGOUT and ask for a username or password. 我们通过telnet到IP,他们给消息LOGIN或LOGOUT并询问用户名或密码。

I used this code: 我使用以下代码:

    public static void ConnectToFirewall(string strUsername, string strPassword, string strFirewallIp)
    {
        IPAddress[] ipaIpAddressCollection = Dns.GetHostAddresses(strFirewallIp);
        IPEndPoint ipeIpEndPoint = new IPEndPoint(ipaIpAddressCollection[0], intPort);
        Socket sckSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sckSocket.Connect(ipeIpEndPoint);
        string strData = strUsername + "\r\n"+ strPassword + "\r\n";
        byte[] bytData = new byte[1024];
        bytData = Encoding.ASCII.GetBytes(strData);
        sckSocket.Send(bytData);
        byte[] bytDataReceived = new byte[1024];
        int intData = sckSocket.Receive(bytDataReceived);
        sckSocket.Close();
    }

If I am not logged in, when I telnet to it, I receive the message: LOGIN, username: / Password:. 如果我未登录,则通过telnet登录时会收到以下消息:登录,用户名:/密码:。 If I am logged on, I receive LOGOUT, username: / Password. 如果我登录,则收到LOGOUT,用户名:/密码。

This works perfectly well with the above method for half of my firewalls, it does not seem to work (I keep getting login as if I had not tried to pass credentials). 对于上面一半的防火墙,上述方法都可以很好地工作,但似乎不起作用(我一直在登录,好像没有尝试通过凭据一样)。 I also tried it with 我也尝试过

    TcpClient tcpConnection = new TcpClient(myip,myport);

but this gives the same result. 但这给出了相同的结果。 It works for certain firewall ip's. 它适用于某些防火墙IP。 fails for others. 为别人失败。 They are all in the same domain. 它们都在同一个域中。

Does anyone have idea how I could get past this or what steps I could undertake to troubleshoot this or what may be the cause of some server not accepting this method, allthough it does accept if I telnet to it? 是否有人知道我将如何解决这个问题,或者我可以采取什么步骤来解决此问题,或者是某些服务器不接受此方法的原因(尽管如果我通过telnet接受了该方法也可以)是什么原因?

Any help or suggestions are appreciated. 任何帮助或建议,表示赞赏。

Many thanks in advance. 提前谢谢了。

When you call sckSocket.Send(bytData) , how does the socket know to send only the portion of the bytData that has been initialized with the username and password? 当您调用sckSocket.Send(bytData) ,套接字如何知道仅发送使用用户名和密码初始化的bytData部分? I have a feeling that Send() will send the entire 1024 bytes along, most of which will be 0x00 bytes. 我有一种感觉, Send()将发送整个1024个字节,其中大部分将是0x00字节。 I would not expect a router to handle this gracefully. 我不希望路由器能正常处理此问题。

I've seen systems that accepted the password only after the prompt for the password has been generated and sent. 我见过只有生成并发送密码提示后才接受密码的系统。 Try sending the username and password with two separate requests. 尝试通过两个单独的请求发送用户名和密码。 If your environment makes it feasible to set the TCP_NODELAY socket option to disable Nagle's algorithm , it might help to get the username string sent along more quickly. 如果您的环境允许设置TCP_NODELAY套接字选项以禁用Nagle的算法是可行 ,则可能有助于更快地发送用户名字符串。 (I wouldn't bother with this unless you also split apart sending the username from the password.) (除非您将发送用户名和密码分开,否则我不会为此而烦恼。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM