簡體   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