DNS的虚假UDP响应

时间:2016-06-06 19:44:04

标签: java networking

作为学术项目的一部分,我要编写一段可以生成虚假UDP响应的代码。我是使用jnetpcap进行的,这是我的代码:

public static void SendResponse() throws IOException {
        System.out.println("Generating fake response!");
        try {
            String host = "fakeme.example.com";
            int port = XXXX; //This is the port where the DNS lookup service runs.
            byte[] message = "Hello Boyz !".getBytes();
            // Get the internet address of the specified host
            InetAddress address;
            address = InetAddress.getByName("77.**.**.**");

            // Initialize a datagram packet with data and address
            DatagramPacket packet = new DatagramPacket(message, message.length, address, port);
            // Create a datagram socket, send the packet through it, close it.
            DatagramSocket dsocket = new DatagramSocket();
            dsocket.send(packet);
            dsocket.close();
        } catch (SocketException ex) {
            Logger.getLogger(Sniffer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

碰巧我们有一个服务,当这段代码运行时,服务应返回一个值。在我的情况下,它总是失败。

我搞砸了哪里?

感谢。

其他信息:调用此代码的代码是:

public static PcapPacketHandler<String> packet_handler(Pcap pcap) {
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
            Udp udp = new Udp();

            @Override
            public void nextPacket(PcapPacket packet, String user) {

                if (!packet.hasHeader(udp)) {
                    return; // not a UDP package, skip
                }
                    try {
                        System.out.println("Fake response ");
                        //Send repsonse
                        SendResponse();
                    } catch (IOException ex) {
                        Logger.getLogger(Sniffer.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
        };
        return jpacketHandler;
    }

请注意:我首先运行此服务,然后在2个终端标签中运行服务(为给定的网址执行dns lookup以检查我的欺骗程序是否正常工作)。我也被要求使用某个端口,我把它放在我的代码的端口地址中。这是正确的方法吗?

再次感谢。

0 个答案:

没有答案