Java DNS数据包创建

时间:2015-10-26 01:15:31

标签: java sockets dns

完全披露:这是我大学网络安全课程的一个项目,我不是在寻找讲义或有人为我编写代码,但我确实需要朝着正确的方向轻推/推动。我提前感谢你的时间。

设置:该项目是构建一个DNS中毒系统,其中包含一些已经提供给我们的Java代码,这是在包含所有虚拟机的虚拟网络上完成的,因此不应该影响合法网络或机器。我的问题是我不明白如何创建发送到DNS服务器的数据包。我已经在我的书,讲座和在线搜索了一段时间,我找不到如何将它放入代码中。我已经看过各种有关它如何工作的象形图,我理解这一点,但我很难为它编写代码。

以下是该计划的代码:

    public class Main {

    /*
     * This method calls the various other functions to accomplish the poisoning
     * after handling the command line arguments.
     */
    public static void main(String[] args) {
        System.out.println("DNS Poisoner");
        if (args.length != 3)
        {
            System.out.println("Invalid quantity of arguments.");
            System.out.println
            ("dnsServer: IP address of the DNS server to poison\n"
                    + "hostname: URL to hijack\n"
                    + "poisonIP: IP address to inject as the poisoning attempt.\n");
            System.exit(-1);
        }

        String dnsAddressString = args[0];
        String hostname = args[1];
        String poisonIPstring = args[2];

        //Get the byte representation of the IP addresses.
        byte[] dnsAddress = ip4StringToByte(dnsAddressString);
        byte[] poisonIP = ip4StringToByte(poisonIPstring);

        //Spam the poisoned DNS replies until reply.
        while (true)
        {
            //Set port and ID distribution here.
            int destPort = 0;
            int transactionID = 0;
            System.out.println("STUBBED PORT AND ID - IMPLEMENT!");
            //Otherwise, your code is essentially doing this: http://xkcd.com/221/

            launchPoisonPacket(dnsAddress, poisonIP, hostname, destPort,
                    transactionID);
        }
    }

    /*
     * This method converts an IPv4 address from a string representation
     * to a byte array.
     * ipAddress: The string representation of an IPv4 address.
     */
    public static byte[] ip4StringToByte(String ipAddress)
    {       
        //Parse IP address.
        InetAddress ip = null;
        try {
            ip = InetAddress.getByName(ipAddress);
        } catch (UnknownHostException e) {
            System.out.println("Unknown Host Error: " + e.getMessage());
            e.printStackTrace();
            System.exit(-1);
        }

        byte[] ipByte = ip.getAddress();

        return ipByte;
    }

    public static void launchPoisonPacket(byte[] dnsAddress, 
            byte[] poisonIP, String hostname, 
            int destinationPort, int transactionID)
    {
        //Get a record to add to the packet.
        byte[] packet = null;

        System.out.println("STUBBED POISON PACKET GENERATION - IMPLEMENT!");

        //Open a socket to send it on.
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            System.out.println("Failed to grab socket for port.");
            System.out.println(e.getMessage());
            return;
        } catch (IllegalArgumentException e) {
            System.out.println("Port out of range");
            System.out.println(e.getMessage());
        }

        //Craft a datagram to send.
        DatagramPacket dPacket = new DatagramPacket(packet, packet.length);
        try {
            dPacket.setAddress(InetAddress.getByAddress(dnsAddress));
            dPacket.setPort(destinationPort);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            socket.close();
            return;
        }

        //Send it.
        try {
            socket.send(dPacket);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            socket.close();
            return;
        }
        socket.close();
    }
}

我相信我们的工作是指定端口号,事务ID和数据包。我认为端口应该是53,事务ID应该是0-65535(含)的随机int,但是数据包让我不知所措。它是UDP数据报的有效负载,但我该如何指定它?它是字节数组的类型,但我不知道应该指定哪些部分以及如何将它们放入数组中。如果我问得太多或张贴太多请告诉我,我会补偿。再次感谢您的时间。

1 个答案:

答案 0 :(得分:1)

UDP有效负载是DNS数据报。 DNS数据报格式遍布各处。 UDP段封装在IP包中。应用程序层数据报在技术上不是数据包。从DNS数据报头开始,然后是DNS消息。 http://www.tcpipguide.com/free/t_DNSMessageHeaderandQuestionSectionFormat.htm