Java DNS 数据包创建

标签 java sockets dns

完全披露:这是我大学网络安全类(class)的一个项目,我不是在寻找讲义或有人为我编写代码,但我确实需要在正确的方向上插入/插入。感谢您提前抽出时间。

设置:该项目是使用我们提供的一些 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 数据报 header 开始,然后是 DNS 消息。 http://www.tcpipguide.com/free/t_DNSMessageHeaderandQuestionSectionFormat.htm

关于Java DNS 数据包创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337038/

相关文章:

python - 向服务器 pyzmq 发送数据时出现问题

linux - 在 Linux 中允许或阻止访问端口

ruby-on-rails - Rails : Proper setup with DNS, Heroku 的机架重写等

java - 复选框状态下的 TreeView 传输

Java HttpServletRequest 在浏览器 URL 栏中获取 URL

java - 使用 hibernate 更新数据库中的大量行

sockets - 为什么 cookie 继续工作

laravel - OS X Yosemite 上 Homestead Laravel 5.1 中的通配符动态子域

heroku - 将 .tk 域指向域名

java - 了解议程组的锁定 Activity