繁体   English   中英

Java DNS数据包创建

[英]Java DNS Packet creation

全面披露:这是我大学网络安全课程的一个项目,我不是在寻找讲义或有人为我编写代码,但我确实需要在正确的方向上轻推一下。 谢谢您的宝贵时间。

设置:该项目是使用已经提供给我们的一些Java代码构建DNS中毒系统,这是在具有所有虚拟机的虚拟网络上完成的,因此不会影响合法网络或计算机。 我的问题是我不了解如何创建发送到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(含)之间的随机整数,但数据包让我感到茫然。 它是UDP数据报的有效负载,但是如何指定呢? 它是字节数组类型,但是我对应该指定哪些部分以及如何将它们放入数组一无所知。 如果我问得太多或张贴得太多,请让我知道,我会进行修改。 感谢你的宝贵时间。

UDP有效载荷是DNS数据报。 DNS数据报格式在各处都有详细说明。 UDP段封装在IP数据包中。 应用层数据报在技术上不是数据包。 从DNS数据报标头开始,然后是DNS消息。 http://www.tcpipguide.com/free/t_DNSMessageHeaderandQuestionSectionFormat.htm

暂无
暂无

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

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