端口53上的DNS中继UDP

时间:2010-10-12 11:46:58

标签: c# sockets dns udp

我注意到BT Home正在从他们的DNS服务器发回假DNS结果,这使得网站可以绕过我在防火墙中阻止的IP地址,所以我想创建自己的DNS中继/服务器。

到目前为止,我可以在UDP端口53上接收请求并将它们发送到DNS服务器并获得有效的byte []流结果,然后我使用远程客户端端口发送回请求,但是浏览器只是再次发回请求。

我已经测试了套接字中的代码并且结果正常但是由于某种原因,IE / FF除了结果之外根本不会。

    public void Listen()
    {
        receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
        receiveEndPoint = new IPEndPoint(IPAddress.Any, receivePort); receiveSocket.Bind(receiveEndPoint); 
        receivePort = (receiveSocket.LocalEndPoint as IPEndPoint).Port; 
        receiveBuffer = new byte[BufferSize]; 
        receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
    }


    public void NetworkMessageReceivedCallback(IAsyncResult asyncResult)
    {
        EndPoint remoteEndPoint = null;            
        byte[] bytes = null;                        
        remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); //Will contain the clients port                
        int bytesRead = receiveSocket.EndReceiveFrom(asyncResult, ref remoteEndPoint);                                              
        bytes = new Byte[bytesRead];                
        Buffer.BlockCopy(receiveBuffer, 0, bytes, 0, bytesRead);
       //string ip = "208.67.222.222";
       string ip = "192.168.1.254";
       IPAddress dnsServer = IPAddress.Parse(ip);
       Response R = Resolver.Lookup(bytes, dnsServer);
       receiveSocket.SendTo(R.Message , remoteEndPoint);//127.0.0.1
       receiveSocket.Close();
       Listen();
    }

2 个答案:

答案 0 :(得分:1)

我从未处理过来自C#的原始DNS,但看起来您正在尝试解析从客户端收到的字节,而不仅仅是转发到DNS服务器

您从UDP套接字读取的消息包含 DNS查询,而不仅仅是主机名。看看RFC 2929了解那里的内容。

你可能会对这个小而精彩的DNS过滤器感兴趣 - adsuck - 作者:Marco Peereboom(虽然它适用于Unix,而不是Windows)。

答案 1 :(得分:0)

此外,您不应该尝试收听UDP和TCP。我认为UDP主要用于权威的DNS查询。